Inconsistency using integral versus quad?
显示 更早的评论
I am encountering a strange problem using the built-in "integral" function in MatLab R2013b, where it is not giving me the proper answer. I am wondering if it is a usage difference between integral and quad, which seem at a glance to be very similar (other than the suggestion to use "integral" instead, which is where the bug resides).
I have a function which is a normalized Gaussian in theta-space, so that if I integrate it over all angular coordinates in a hemisphere, it should integrate to 1...that is, 2 * pi * integral(f(t)*cos(t)*sin(t), t=0 to pi/2) = 1 since it is azimuthally symmetric. (The cos(t) is due to power projection, sin(t) comes from spherical coordinate integration. The 2*pi is the integration over phi due to symmetry.)
I declare an inline function: intfunc = @(t) f(t) .* cos(t) .* sin(t)
Then I tried the following two snippets of code: intval = 2*pi*integral(intfunc, 0, pi/2)
The result that I get varies depending upon the choice of Gaussian width, but it does not integrate to 1.
If instead I do the following: intval = 2*pi*quad(intfunc, 0, pi/2)
Then the result integrates to 1. So, it appears one of the functions isn't working the way I expect.
To resolve this dilemma, I went to Mathematica, integrated the function symbolically, and obtained a value of 1 for any value of the Gaussian width (as long as it is positive, of course). This suggests to me that quad works properly and integral does not.
Am I somehow misusing integral(), or is it known to be buggy?
采纳的回答
更多回答(1 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!