Evaluating a double integral using the trapezoidal rule

조회 수: 6 (최근 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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by