using QUADGK vs QUADL numerical integration algorithm
Show older comments
Hi all, I have some issues while using quadgk with handle functions. Sometimes it gives me some warnings, where quadl function does not give me any warning at all! I can not understand why. My handle function is like: y(x) = x The integration is from -0.5 to 0.5
Why does it happen that?
Accepted Answer
More Answers (3)
the cyclist
on 21 Dec 2011
Neither of these bits of code cause a problem for me. Did you do something differently? Maybe you could post the code you ran and the warnings you received.
% quadgk
f1 = @(x) x;
Q1 = quadgk(f1,-0.5,0.5)
% quadl
f2 = @(x) x;
Q2 = quadl(f2,-0.5,0.5)
EDIT IN RESPONSE TO ADDITIONAL INFO:
Here is a drastically simplified version of your code that exhibits the warning you saw.
% Function to integrate
funz = @(z) 1.e11 * z;
% Plot variable over range that is going to be integrated
z = -0.05:0.01:0.05;
plot(z,funz(z),'.-')
% Integrate
q = quadgk(@(z) funz(z),min(z),max(z))
I think the essence is that you are taking an integral of a function that is of order 10^11, but has an integral that is zero. In the course of MATLAB estimating the error in that integral (in order to determine whether it should stop making smaller intervals, and return a value), you are getting values that are tiny in comparison to the function itself.
That might not be exactly correct, but it is definitely the gist of the warning.
I have not gone back and looked at the ramifications for your actual function. However, my guess is that you could pull out some large scaling factor while doing the integral, and put it back in after the integration.
Nicholas
on 21 Dec 2011
2 Comments
the cyclist
on 21 Dec 2011
I'll try to take a look, but it might not be soon. In the meantime, a couple suggestions:
One is that if you have not done so, trying plotting what your functions look like over the interval that you are trying to integrate. Sometimes that exposes some unexpected weirdness. The other, related to the use of this forum, is that you might want to delete this "answer" (which is not really an answer) and incorporate the code into your question itself.
Nicholas
on 3 Jan 2012
Mike Hosea
on 23 Dec 2011
0 votes
I'm not sure what notifications the poster gets. Since I added an "I'll take a look" answer first and later edited it, maybe I should add an "I took a look" answer so that the OP will get notified.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!