Integration of an expression, with matlab
Im having trouble understanding what is wrong with my program, it is intended to integrate the following expression (contained on the link) using the trapezium rule:
http://integrals.wolfram.com/index.jsp?expr=2*x*sin[P*Pi*x]^2&random=false
Where P=2
and it is bounded by the values pi and 0.
My program obtains the results 6.1295 when using my trapezium rule, and 4.6979 when it is done analytically.
It works for simple expressions i.e. sin (x) but for some reason it doesnt like this more complicated expression. I believe the error to be because of the 'P' in my expression but im unsure how to solve this, if this is the problem. Here is my program thus far:
Quote:
n=100 %number of intervals
P=2 %constant
b=pi % upper bound
a=0 %lower bound
h=(b-a)/n;
f=inline('2*x*((sin(P*pi*x)).^2)')
i=1:1:n-1;
I=(h/2)*( f(a,P) + f(b,P) ) + h*sum(f(a+i*h,P))
%exact integration:
x=b;
Iexact01= - (cos(2*P*pi*x) + 2*P*pi*x*(sin(2*P*pi*x) - P*pi*x))/(4*(P^2)*(pi^2));
x=a;
Iexact02= - (cos(2*P*pi*x) + 2*P*pi*x*(sin(2*P*pi*x) - P*pi*x))/(4*(P^2)*(pi^2));
Iexact = Iexact01 - Iexact02
Any help would be greatly appreciated!!