How to use the trapezium rule

5 views (last 30 days)
Mike
Mike on 10 May 2013
Hi,
I've been trying to figure out how to use the trapezium rule and completely failing, since I'm a bit of a simpleton at Matlab.
I'm integrating the product of two functions and trying to find the value of the definite integral using the trapezium rule.
This is my code:
syms x f=(((x.^2)-1)^n);
Pn=(1/((2.^n).*factorial(n))).*diff(f,x,n); Pm=(1/((2.^m).*factorial(m))).*diff(f,x,m);
P=Pn.*Pm
x = -1 :0.1: 1;
y=P;
trapz(y,x)
I keep getting these errors:
??? Error using ==> trapz at 59 LENGTH(X) must equal the length of the first non-singleton dimension of Y.
Error in ==> Question7b at 11 trapz(y,x)
Thanks.

Answers (2)

Roger Stafford
Roger Stafford on 10 May 2013
Pm would be the m-th Legendre Polynomial except that, as you have defined it, it appears to involve the m-th derivative of the n-th power of x^2-1, not its m-th power. I see no way for the m value to automatically be substituted for n in that formula. You may have to define another function, g, which uses m for this purpose.
Also I see no place where you have given n and m specific values before calling on 'trapz'. It does not know how to do its computation without being given specific numerical values for all its variables. As far as I know, it is incapable of producing a symbolic answer.
In fact n and m need to be defined before matlab can produce a function into which x values could be substituted. The form of the functions depends on the values of n and m.
I don't have an updated symbolic toolbox to check this with, but your error message indicates that the vector length of y is incorrect. You had better chose a small n and m and manually check out the forms of the Pn and Pm functions. Also check out that the proper substitution has been made of the x numerical values in getting y. With your x you should get a vector length for y of 21. If not, you need to find out why not.
Finally I notice that your call on 'trapz' is erroneous. It should be
trapz(x,y)
not
trapz(y,x).
(Note that you can also use 0.1*trapz(y) with your x sub-intervals all that same length.)
  1 Comment
Mike
Mike on 10 May 2013
Hi Roger,
I was asked to create a function which computes the integral of P (which is equivalent to Pm*Pn) for any given value of n and m, using the trapezium rule. So far, as you can see I haven't figure out how to make it a function, and have neither figured out how to solve my errors. My code is relatively unchanged:
syms x
n=1;
m=1;
f=(((x.^2)-1)^n);
Pn=(1/((2.^n).*factorial(n))).*diff(f,x,n);
Pm=(1/((2.^m).*factorial(m))).*diff(f,x,m);
P=Pn.*Pm
x = -1 :0.1: 1;
y=P;
trapz(x,y)
end
I get the following errors:
??? Undefined function or method 'max' for input arguments of type 'sym'.
Error in ==> trapz at 43 perm = [dim:max(ndims(y),dim) 1:dim-1];
Error in ==> Question7b at 12 trapz(x,y)
Thanks for your help, and sorry if I wasn't clear.

Sign in to comment.


Mike
Mike on 10 May 2013
Ok so I edited the code to try and include the function legendrepolynomial:
function y=legendrepolynomial(x)
n=1;
m=1;
f=(((x.^2)-1)^n);
g=(((x.^2)-1)^m);
Pn=(1/((2.^n).*factorial(n))).*diff(f,x,n);
Pm=(1/((2.^m).*factorial(m))).*diff(g,x,m);
P=Pn.*Pm
y=P;
trapz(x,y)
end
In the command window I input
x=-1:0.1:1
legendrepolynomial(x)
and the following errors show up:
??? Error using ==> mpower Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead.
Error in ==> legendrepolynomial at 6 f=(((x.^2)-1)^n);
Thanks again.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!