Why Matlab cannot calculate simple double integrals with good accuracy?
显示 更早的评论
Let's say that I want to calculate the area of a circle of radius 1 which is π. I use the following code:
f=@(x,y) double(1-x.^2-y.^2>0);
integral2(f,-2,2,-2,2)
What I get using the long format is: 3.141530523062315.
I put more precision:
integral2(f,-2,2,-2,2,'Method','iterated','AbsTol',1e-10)
Now I get:
3.141535921819190
What is happening??? Why the answer is not with the precision required? And it lasts a long time to compute it. In Mathematica the code:
N[Integrate[HeavisideTheta[1 - x^2 - y^2], {x, -Infinity, Infinity}, {y, -Infinity, Infinity}], 100]
returns the correct result with tons of digits in an instant:
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
How can I make my Matlab program to get to the same precision?
2 个评论
Paul
2021-4-4
I thought it would be too. But:
>> f(x,y) = heaviside(1 - x^2 - y^2)
f(x, y) =
heaviside(- x^2 - y^2 + 1)
>> int(int(f(x,y),x,-2,2,'IgnoreAnalyticConstraints',true),y,-2,2,'IgnoreAnalyticConstraints',true)
ans =
pi/2
Did I do something wrong?
回答(1 个)
I'm guessing that numerical accuracy might suffer because the integrand is discontinuous, and the curved boundary of the discontinuity is hard to sample accurately with the small rectangular elements used in a Cartesian coordinate integral. In polar coordinates, it seems to work quite well:
format long
f=@(r,theta) r.*(r<=1);
integral2(f,0,2,0,2*pi,'AbsTol',1e-10)
类别
在 帮助中心 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!