Results 1 to 4 of 4

Math Help - Estimating an integral using Matlab

  1. #1
    Member
    Joined
    Apr 2008
    Posts
    191

    Estimating an integral using Matlab

    I want to write a matlab script that uses simulation to evaluate the following integral using 1000 iterations:

     <br />
\int^4_2 x^2 dx<br />

    This is my code:

    Code:
     
    n=1000;
    total=0;
    for i=1:n
    x= rand()*2+2;
    y=x^2;
    total=total+y;
    end
    expectation=total/n;
    integral=expectation*4
    The output is:

    integral =
    36.7483

    But this answer is not even close!! Why is that? Can anyone show the problem with my code?
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Super Member malaygoel's Avatar
    Joined
    May 2006
    From
    India
    Posts
    648
    Quote Originally Posted by Roam View Post
    I want to write a matlab script that uses simulation to evaluate the following integral using 1000 iterations:

     <br />
\int^4_2 x^2 dx<br />

    This is my code:

    Code:
     
    n=1000;
    total=0;
    for i=1:n
    x= rand()*2+2;
    y=x^2;
    total=total+y;
    end
    expectation=total/n;
    integral=expectation*4
    The output is:

    integral =
    36.7483

    But this answer is not even close!! Why is that? Can anyone show the problem with my code?
    The eroor is:
    Code:
     
    
    x= rand()*2+2;
    Initialize x=2
    and then for each ieration...x=x+.002

    Hence, code will be
    ]
    Code:
     
    n=1000;
    total=0;
    x=2;
    for i=1:n
    x= x+.002;
    y=x^2;
    total=total+y;
    end
    expectation=total/n;
    integral=expectation*4
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Roam View Post
    I want to write a matlab script that uses simulation to evaluate the following integral using 1000 iterations:

     <br />
\int^4_2 x^2 dx<br />

    This is my code:

    Code:
     
    n=1000;
    total=0;
    for i=1:n
    x= rand()*2+2;
    y=x^2;
    total=total+y;
    end
    expectation=total/n;
    integral=expectation*4
    The output is:

    integral =
    36.7483

    But this answer is not even close!! Why is that? Can anyone show the problem with my code?
    E(X^2) when X\sim U(2,4) is:

    \int_{x=2}^4 x^2 p(x)\;dx

    but p(x)=1/2 for x \in [2,4], so:

    E(X^2)=\frac{1}{2}\int_{x=2}^4 x^2\;dx

    or:

    \int_{x=2}^4 x^2\;dx=2E(X^2)

    and you have use 4 instead of 2.

    CB
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by malaygoel View Post
    The eroor is:
    Code:
     
    
    x= rand()*2+2;
    Initialize x=2
    and then for each ieration...x=x+.002

    Hence, code will be
    ]
    Code:
     
    n=1000;
    total=0;
    x=2;
    for i=1:n
    x= x+.002;
    y=x^2;
    total=total+y;
    end
    expectation=total/n;
    integral=expectation*4
    Numerical integration does not qualify as "simulation" in the context of this problem, and you still have the same error as the OP, which is the *4 in the last line.

    The OP's Monte-Carlo code was as correct as yours (it has the same error).

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. MATLAB: Plot instantaneous integral numerically
    Posted in the Math Software Forum
    Replies: 3
    Last Post: September 27th 2010, 08:18 PM
  2. how to use MATLAB to do this numerical double integral
    Posted in the Math Software Forum
    Replies: 0
    Last Post: March 2nd 2010, 09:25 PM
  3. Help with calculating integral in MATLAB
    Posted in the Math Software Forum
    Replies: 2
    Last Post: November 30th 2009, 05:15 AM
  4. Replies: 6
    Last Post: August 24th 2009, 12:29 PM
  5. Minimizing a nonlinear integral (in Matlab, I hope)
    Posted in the Math Software Forum
    Replies: 2
    Last Post: April 28th 2009, 10:17 PM

Search Tags


/mathhelpforum @mathhelpforum