ํ•„ํ„ฐ ์ง€์šฐ๊ธฐ
ํ•„ํ„ฐ ์ง€์šฐ๊ธฐ

double integral using trapz

์กฐํšŒ ์ˆ˜: 43 (์ตœ๊ทผ 30์ผ)
woonghee Cho
woonghee Cho 2022๋…„ 5์›” 21์ผ
๋Œ“๊ธ€: Parsa 2022๋…„ 5์›” 22์ผ
I'm trying to approximate a double integral using traps. I'm not sure how to coding it.
-A given question
๐‘“(๐‘ฅ, ๐‘ฆ) = (500*x*exp(y)) / (1 + 2*x^2)
0<x<4, -2<y<0
Using Trapezoidal rule with โ–ณx, โ–ณy=0.5, 0.25, 0.125, 0.0625, 0.03125
-The code that I tired :
clc, clear all;
format long
x = 0:0.5:4;
y = 0:0.5:4;
[X,Y] = meshgrid(x,y);
F = (500.*X.*exp(Y))/(1.+2.*X.^2);
I = trapz(y,trapz(x,F,2))

๋‹ต๋ณ€ (1๊ฐœ)

Parsa
Parsa 2022๋…„ 5์›” 21์ผ
Hi Woonghee
In definition of F, you forgot a dot '.' , before '/ ' . Please try it and run your code again.
Also please take a look at the exapmle " Multiple Numerical Integrations " @doc:Trapezoidal numerical integration - MATLAB trapz (mathworks.com)
  ๋Œ“๊ธ€ ์ˆ˜: 2
woonghee Cho
woonghee Cho 2022๋…„ 5์›” 22์ผ
Thank you very much for your help!
Is there any way to generalize โ–ณx and โ–ณy to code?
Parsa
Parsa 2022๋…„ 5์›” 22์ผ
If, by the term " generalize ", you mean that you want to calculate the integral for different integration intervals (steps), it could be done in a double 'for' loop, and a double-indexed I, so that you'll have a matrix for I, that the rows represent the integral values for fixed deltax and different deltay s, and the columns corrrespond to integral values for fixed delta-y and varying delta-x s. Alsoe you could show the integral convergence graphically. Please try some lines such as below :
xi=0; xf=4;
dx = [0.5, 0.25, 0.125, 0.0625, 0.03125];
yi=0; yf=4;
dy = dx;
for m=1:numel(dx)
x=xi:dx(m):xf;
for n=1:numel(dy)
y=yi:dy(n):yf;
[X,Y] = meshgrid(x,y);
F = (500.*X.*exp(Y))./(1.+2.*X.^2);
I(m,n) = trapz(y,trapz(x,F,2));
end
end
% convergence graph
figure, surf(I),xlabel('dy'),ylabel('dx')

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Numerical Integration and Differentiation์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

ํƒœ๊ทธ

์ œํ’ˆ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by