使用梯形法则评估双积分(Matlab)

时间:2015-04-15 14:48:32

标签: matlab integration

我有一个使用梯形法则来评估双积分的任务。第一部分是使用带限制的梯形法则来评估双积分 0 <= x <= 2,0 <= y <= 1

我有一个工作脚本:

N = 100;
xh= 1.25;
x = linspace(0,2,N);
y = linspace(0,1,0.5*N);
dx = diff(x(1:2));
dy = diff(y(1:2));
[x,y] = meshgrid(x,y);
funk = exp(-10.*((x-xh).^2+y.^2)).*cos(y.*(x-xh));
funk(2:end-1,:) = funk(2:end-1,:)*2;
funk(:,2:end-1) = funk(:,2:end-1)*2;
out = sum(funk(:))*dx*dy/4;

disp(out)

现在对于第二部分,限制是0&lt; = x&lt; = 2,0 <= y&lt; =((pi * x)/ 2)

如何获得y(代码中的第4行)从x矩阵中获取相应的x值以创建y矩阵?如果我开始工作,我不应该改变代码中的任何其他内容,或者我错过了什么?

1 个答案:

答案 0 :(得分:2)

有趣的问题。想象一个网格,x坐标跨越0 <= x <= 2y坐标跨越0 <= y <= pi。这是因为当x = 2时,y = pi*2/2 = pi

现在想象一下从原点到pi/2绘制一条正斜率为(x,y) = (2,pi)的直线。您要关注的(x,y)值是网格的右下角

要做到这一点,只需创建meshgrid x [0,2]跨越y以及[0,pi]跨越%// Generate grid of points N = 100; xx = linspace(0,2,N); yy = linspace(0,pi,N); [x,y] = meshgrid(xx,yy); %// Obtain valid region ind = y <= (pi/2)*x; %// Show valid region in black and white imagesc(ind); axis xy; colormap gray; set(gca,'XTick',10:10:100); set(gca,'YTick',10:10:100); set(gca,'XTickLabel',xx(10:10:end)); set(gca,'YTickLabel',yy(10:10:end)); ,然后选择网格的右下角。假设网格为100 x 100点:

ind

这是我们得到的数字:

enter image description here

白色区域是我们追求的区域。 logical包含meshgrid矩阵,可让我们选择我们需要选择的%// Generate grid of points N = 100; xh = 1.25; xx = linspace(0,2,N); yy = linspace(0,pi,N); [x,y] = meshgrid(xx,yy); %// Obtain valid region ind = y <= (pi/2)*x; %// Perform calculations with normal grid dx = diff(xx(1:2)); dy = diff(yy(1:2)); funk = exp(-10.*((x-xh).^2+y.^2)).*cos(y.*(x-xh)); funk(2:end-1,:) = funk(2:end-1,:)*2; funk(:,2:end-1) = funk(:,2:end-1)*2; %// Select out valid region coordinates funk = funk(ind); %// Now sum out = sum(funk(:))*dx*dy/4; 中的哪些值。因此,您的代码现在就是这样:

out

对于>> out out = 0.156821355105871 ,我得到:

{{1}}