Evaluating a double integral using the trapezoidal rule
조회 수: 6 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!