using QUADGK vs QUADL numerical integration algorithm
显示 更早的评论
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?
采纳的回答
更多回答(3 个)
the cyclist
2011-12-21
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
2011-12-21
2 个评论
the cyclist
2011-12-21
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
2012-1-3
Mike Hosea
2011-12-23
0 个投票
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!