Inconsistency using integral versus quad?
Show older comments
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?
Accepted Answer
More Answers (1)
Sam Butler
on 3 Sep 2014
0 votes
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!