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:
Any help would be greatly appreciated!!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
Thankyou for the reply, that seems to have done something. Ive tried it again though but it is telling me that my inner matrix dimensions don't agree, I'm rather confused by what this means. The error message is as follows:
using the m-file:f =
Inline function:
f(x,P) = 2*x*((sin(P*pi*x)).^2)
??? Error using ==> inlineeval at 15
Error in inline expression ==> 2*x*((sin(P*pi*x)).^2)
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> inline.subsref at 27
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Thankyou for having a look, it is really appreciated!!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)','x','P')
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