[英]double integral by Riemann Sums in matlab
clear
n=45; // widht
m=23; // length
// total 990 blocks m*n
a=-2; b=1; // x-limits
c=2; d=4; // y-limits
f=@(x,y) 4.0*x.^3.*y+0.7.*x.^2.*y+2.5.*x+0.2.*y; //function
x=linspace(a,b,n);
y=linspace(c,d,m);
h1=(b-a)/n
h2=(d-c)/m
dA=h1*h2
[X,Y]=meshgrid(x,y); //did a meshgrid cause q wouldnt accept different index bounds without meshgriding.
q=sum((sum(dA*f(X,Y))))
Ive been using a formula for double riemanns at this link.我一直在此链接上使用双黎曼公式。 https://activecalculus.org/multi/S-11-1-Double-Integrals-Rectangles.html https://activecalculus.org/multi/S-11-1-Double-Integrals-Rectangles.html
these are the answers这些是答案
1.I=81.3000. 1.I=81.3000。
2.I-left=-87.4287 //-84.5523 my result 2.I-left=-87.4287 //-84.5523 我的结果
3.I-Right=-75.1072 3.I-右=-75.1072
I can't see what im doing wrong.我看不出我做错了什么。 I need input from somebody.我需要某人的意见。
I would debug the integration scheme with a dummy constant function我将使用虚拟常量 function 调试集成方案
f = @(x,y) 1.0*ones(size(x))
The result should be the exact total area (ba)*(dc) = 6
, but your code gives 6.415
.结果应该是确切的总面积(ba)*(dc) = 6
,但您的代码给出6.415
。
The problem is that you are integrating outside the domain with those limits.问题是您正在使用这些限制在域外进行集成。 You should stop the domain discretization one step before in each dimension:您应该在每个维度前一步停止域离散化:
h1 = (b-a)/n
h2 = (d-c)/m
x = linspace(a, b - h1, n);
y = linspace(c, d - h2, m);
This will give you the expected area for the dummy function:这将为您提供虚拟 function 的预期区域:
q = 6.0000
and for the real function, evaluating at the top-left corner, you get:对于真正的 function,在左上角进行评估,您会得到:
q = -87.482
You didn't do anything wrong with your code.您的代码没有做错任何事情。 The difference comes from the resolution of x
and y
used in you code, since they are not sufficiently high.差异来自代码中使用的x
和y
的分辨率,因为它们不够高。
For example, when you have n = 5000
and m = 5000
例如,当您有n = 5000
且m = 5000
q = sum((sum(dA*f(X,Y)))); % your Riemann sum approach
l = integral2(f,a,b,c,d); % using built-in function for double integral to verify the value of `q`
you will see that the results are very close now你会看到结果现在非常接近
q = -81.329
l = -81.300
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.