Results 1 to 4 of 4

Thread: 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:

    $\displaystyle
    \int^4_2 x^2 dx
    $

    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:

    $\displaystyle
    \int^4_2 x^2 dx
    $

    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
    5
    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:

    $\displaystyle
    \int^4_2 x^2 dx
    $

    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?
    $\displaystyle E(X^2)$ when $\displaystyle X\sim U(2,4)$ is:

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

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

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

    or:

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

    and you have use $\displaystyle 4$ instead of $\displaystyle 2$.

    CB
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    5
    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: Sep 27th 2010, 07:18 PM
  2. how to use MATLAB to do this numerical double integral
    Posted in the Math Software Forum
    Replies: 0
    Last Post: Mar 2nd 2010, 08:25 PM
  3. Help with calculating integral in MATLAB
    Posted in the Math Software Forum
    Replies: 2
    Last Post: Nov 30th 2009, 04:15 AM
  4. Replies: 6
    Last Post: Aug 24th 2009, 11:29 AM
  5. Minimizing a nonlinear integral (in Matlab, I hope)
    Posted in the Math Software Forum
    Replies: 2
    Last Post: Apr 28th 2009, 09:17 PM

Search Tags


/mathhelpforum @mathhelpforum