Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Double integrals with matlab

  1. Jan 24, 2009 #1
    Hi Everyone.

    I would like to integrate the following using matlab:
    [tex] \int_0^1 ~\int_0^x e^{-x^2} ~dy ~dx [/tex]
    looks pretty simple but don't know how to set up the embedded for loops for the integral with in the integral especially that the upper limit of the inner integral is x and not a number.
    My actual assignment is much more complicated than this but i thought trying to work it out for a simpler function might help me with the more complex one.
    Thanks for your help
     
  2. jcsd
  3. Jan 24, 2009 #2
    the program that i put together is giving me the integral=24.8427 and the answer is actually 0.316... my program is copied below, what am i doing wrong:

    x0=0; xt=1; xstep=11; dx=1/(xstep-1);
    y0=0; yt=1; ystep=11; dy=1/(ystep-1);

    sum2=0;
    for i1=1:xstep
    sum1=0;
    y=(i1-1)*dy;
    x2step=floor(y*xstep);
    for i=1:xstep
    if i>=x2step
    sum1=sum1+dx*exp(-y^2)/2
    %pause
    end
    x=(i-1)*dx;
    end
    sum2=sum2+sum1
    end
    sum3=sum2*dx
     
    Last edited: Jan 24, 2009
  4. Jan 24, 2009 #3
    oh... forgot to say that I am using trapazoidal rule for the integration
     
  5. Jan 24, 2009 #4
    Now, this is giving me the corrrect answer but not sure if i cheated in a way or not... what do you think:
    close all
    clear all

    x0=0; xt=1; xstep=110; dx=1/(xstep-1);
    y0=0; yt=1; ystep=110; dy=1/(ystep-1);

    sum2=0;
    for i1=1:ystep+1
    sum1=0;
    y=(i1-1)*dy;
    x2step=floor(y/dx);
    for i=1:xstep+1
    if i<=x2step
    sum1=sum1+dx*exp(-y^2);
    end
    x=(i-1)*dx;
    end
    sum2=sum2+sum1;
    end
    sum3=sum2*dy
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Double integrals with matlab
  1. Double Integrals (Replies: 9)

Loading...