# Integration of an expression, with matlab

• Mar 1st 2009, 12:00 PM
p123nky
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!!
• Mar 1st 2009, 01:43 PM
CaptainBlack
Quote:

Originally Posted by p123nky
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!!

Try specifying the arguments in the inline statement:

f=inline('2*x*((sin(P*pi*x)).^2)','x','P')

just as it tell you in the help system, otherwise you don't know which argument it will decide is which.

CB
• Mar 2nd 2009, 12:24 AM
p123nky
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:

Quote:

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);
using the m-file:

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)','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
Thankyou for having a look, it is really appreciated!!
• Mar 2nd 2009, 03:01 AM
p123nky
Thankyou for your time, ive managed to figure it out. I failed to realise that simply typing '*' to multiply has confused the program.

• Mar 4th 2009, 02:10 AM
jmparis
Similar Problem
Quote:

Originally Posted by p123nky
Thankyou for your time, ive managed to figure it out. I failed to realise that simply typing '*' to multiply has confused the program.