Results 1 to 6 of 6

Math Help - Integration of an expression, with matlab

  1. #1
    Newbie
    Joined
    Mar 2009
    Posts
    11
    Awards
    1

    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:

    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!!
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by p123nky View Post
    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
    Last edited by CaptainBlack; March 1st 2009 at 03:13 PM.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Mar 2009
    Posts
    11
    Awards
    1
    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:

    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:

    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!!
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Mar 2009
    Posts
    11
    Awards
    1
    Thankyou for your time, ive managed to figure it out. I failed to realise that simply typing '*' to multiply has confused the program.

    Again, thankyou for your help!!
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Feb 2009
    From
    Liverpool
    Posts
    3

    Question Similar Problem

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

    Again, thankyou for your help!!
    I am trying to write a similar program but getting the same error message as the previous post. Please can you tell me what command you used instead of '*' to multiply? Thank you.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by jmparis View Post
    I am trying to write a similar program but getting the same error message as the previous post. Please can you tell me what command you used instead of '*' to multiply? Thank you.
    Elementwise multiplication ".*"

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Double Integration Matlab
    Posted in the Math Software Forum
    Replies: 0
    Last Post: January 17th 2011, 03:40 AM
  2. Matlab: Integration Approximation
    Posted in the Math Software Forum
    Replies: 1
    Last Post: November 11th 2010, 11:10 PM
  3. integration matlab qn
    Posted in the Math Software Forum
    Replies: 4
    Last Post: May 9th 2010, 12:00 AM
  4. matlab numeric integration
    Posted in the Math Software Forum
    Replies: 0
    Last Post: November 6th 2009, 02:29 AM
  5. Numerical integration with MATLAB
    Posted in the Calculus Forum
    Replies: 0
    Last Post: September 2nd 2008, 04:47 AM

Search Tags


/mathhelpforum @mathhelpforum