How can I use MATLAB to solve double integrals?

  • Context: MATLAB 
  • Thread starter Thread starter Moly
  • Start date Start date
  • Tags Tags
    Integrals Matlab
Click For Summary

Discussion Overview

The discussion centers around using MATLAB to solve double integrals, specifically focusing on the integration of the function \(\int_0^1 \int_0^x e^{-x^2} dy dx\). Participants explore methods for implementing nested loops in MATLAB for numerical integration, particularly using the trapezoidal rule.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant seeks assistance in setting up nested for loops in MATLAB for a double integral where the upper limit of the inner integral is variable.
  • Another participant shares their MATLAB code but reports a significant discrepancy between their computed integral value (24.8427) and the expected result (approximately 0.316), questioning what might be wrong with their implementation.
  • A participant mentions using the trapezoidal rule for integration, indicating their approach to numerical methods.
  • One participant expresses uncertainty about whether their revised code, which yields the correct answer, is valid or if they have inadvertently "cheated" in the process.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correctness of the initial code or the validity of the revised approach. There is ongoing uncertainty regarding the implementation details and the appropriateness of the methods used.

Contextual Notes

Limitations include potential misunderstandings of the trapezoidal rule's application, the dependence on the choice of step sizes, and unresolved questions about the correctness of the code logic.

Moly
Messages
20
Reaction score
0
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
 
Physics news on Phys.org
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:
oh... forgot to say that I am using trapazoidal rule for the integration
 
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
 

Similar threads

Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
27
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
Replies
10
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K