how to evaluate a double integral using the trapezoidal rule equation?

34 ビュー (過去 30 日間)
Susan Santiago
Susan Santiago 2018 年 4 月 15 日
回答済み: Apoorv Rajput 2021 年 10 月 7 日
Here's what I have so far
function [ I ] = myTrapz2D( f, x0, xn, y0, yn, nx, ny )
dx = (xn - x0)/nx;
dy = (yn - y0)/ny;
i = 1;
sumx = zeros(nx,1);
sumy =zeros(ny,1);
while i < nx
xi = x0 + i*dx;
sumx(i) = f(xi);
i = i+1;
end
sumx = sum(sumx);
Ix = ((dx)/2)*(f(x0)+f(xn)+(2*sumx));
fd = Ix(y);
while i < ny
yi = y0 + i*dy;
sumy(i) = fd(yi);
i = i+1;
end
sumy = sum(sumy);
I =((dy)/2)*(fd(y0)+fd(yn)+(2*sumy));
end
not sure if it's correct at all but it has to be solved using some variation of the equation for I that I used. I keep getting an error that there aren't enough input arguments. There are my input arguments: f = @(x,y) x.^2 - (2*y.^2) + (x.*y.^3); x0 = 0; xn = 2; y0 = -1; yn = 1; nx = 8; ny = 8;

採用された回答

Torsten
Torsten 2018 年 4 月 16 日
You don't need to program the trapezoidal rule in two dimensions.
Just call the trapezoidal rule in one dimension twice. In the section "Multiple Numeical Integrations" under
https://de.mathworks.com/help/matlab/ref/trapz.html
is an example with the MATALB implementation of the trapezoidal rule "trapz".
Best wishes
Torsten.
  1 件のコメント
Susan Santiago
Susan Santiago 2018 年 4 月 17 日
yes, I'm away that this can be done using trapz, however my assignment involved creating a function like the one I have shown you so, in this case, trapz does not help

サインインしてコメントする。

その他の回答 (1 件)

Apoorv Rajput
Apoorv Rajput 2021 年 10 月 7 日
function [ I ] = myTrapz2D( x0, xn, y0, yn, nx, ny )
syms f(x,y);
syms x;
syms y;
f(x,y)=exp(y-x);
dx = (xn - x0)/nx;
dy = (yn - y0)/ny;
i = 1;
sumx=0*x*y;
while i < nx
xi = x0 + i*dx;
sumx=sumx+ f(xi,y);
i = i+1;
end
Ix = ((dx)/2)*(f(x0,y)+f(xn,y)+(2*sumx));
syms fd(y);
fd(y) = Ix;
sumy=0*y;
i=1;
while i < ny
yi = y0 + i*dy;
sumy= sumy+fd(yi);
i = i+1;
end
I =((dy)/2)*(fd(y0)+fd(yn)+(2*sumy));
end

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by