Evaluating a double integral using the trapezoidal rule

8 ビュー (過去 30 日間)
Robert
Robert 2011 年 2 月 22 日
回答済み: Mohammed 2023 年 12 月 12 日
I am trying to take the double integral of the function using the Trapezoidal rule for G=integral (3*x.^2*y+cos(2*x)*sin(y)+2+4*y.^-2*x+5*y)dxdy with x interval 0 to 2pi and y interval 1 to 10. I found a formula for it but don't know the proper syntax to enter it in. We must use the trapezoidal rule because we are comparing different techniques for evaluating integrals. Here is what I have so far:
x1=0;
x2=2*pi;
y1=1;
y2=10;
N=101
dx=(x2-x1)/(N-1);
dy=(y2-y1)/(N-1);
x=x1:dx:x2;
y=y1:dy:y2;
g(x,y)=3*x.^2*y+cos(2*x)*sin(y)+2+4*y.^-2*x+5*y;
N=101;
for i=1:N+1
for j=1:N+1
out(i,j)=((dx*dy)/4)*(g(i,j(1,1))+g(i,j(1,N))+g(i,j(N,1))+g(i,j(N,N))+2*(g(i,j(1,2:1:N-1))+g(i,j(N,2:1:N-1))+g(i,j(2:1:N-1,1))+g(i,j(2:1:N-1,N)))+4*(sum(g(i,j(2:1:N-1,2:1:N-1)))));
end
end
The really long formula is from this source: http://www.math.ohiou.edu/courses/math344/lecture24.pdf on page 2. I feel that I could streamline the code by using more summation but I do not know how to do that.

採用された回答

Andrew Newell
Andrew Newell 2011 年 2 月 22 日
How about this:
N = 101;
x = linspace(0,2,N)*pi;
y = linspace(1,10,N);
dx = diff(x(1:2));
dy = diff(y(1:2));
[x,y] = meshgrid(x,y);
mat = 3*x.^2.*y+cos(2*x).*sin(y)+2+4*y.^(-2).*x+5.*y;
mat(2:end-1,:) = mat(2:end-1,:)*2;
mat(:,2:end-1) = mat(:,2:end-1)*2;
out = sum(mat(:))*dx*dy/4;

その他の回答 (2 件)

Mohammed
Mohammed 2023 年 12 月 12 日
∫ 𝑠𝑖𝑛𝑥𝑑𝑥 𝜋 0 = 𝑐𝑜𝑠

Mohammed
Mohammed 2023 年 12 月 12 日
∫5 1 1/x 𝑑𝑥 = 𝑙𝑛x

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by