Evaluating a double integral using the trapezoidal rule

8 vues (au cours des 30 derniers jours)
Robert
Robert le 22 Fév 2011
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.

Réponse acceptée

Andrew Newell
Andrew Newell le 22 Fév 2011
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;

Plus de réponses (2)

Mohammed
Mohammed le 12 Déc 2023
∫ 𝑠𝑖𝑛𝑥𝑑𝑥 𝜋 0 = 𝑐𝑜𝑠

Mohammed
Mohammed le 12 Déc 2023
∫5 1 1/x 𝑑𝑥 = 𝑙𝑛x

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by