Integral function gives NaN

23 次查看(过去 30 天)
Hi all,
I am attempting to model some equations using Matlab and I have everything working well until I get to the integration part. The function with the integral gives me NaN and the following error message:
Warning: Infinite or Not-a-Number value encountered.
> In funfun\private\integralCalc>iterateScalarValued at 349
In funfun\private\integralCalc>vadapt at 132
In funfun\private\integralCalc at 75
In integral at 88
In siev_avg at 8
In hydro_dynamic at 33
Function with integral:
function s_avg=siev_avg(rs,Mw,Jv)
rp=0;
fun1=@(rp)sieving(rs,rp,Mw,Jv).*log_normal(rp).*rp.^4;
fun2=@(rp)log_normal(rp).*rp.^4;
s_avg =integral(fun1,rs,inf)/integral(fun2,0,inf);
end
Does anyone have any idea on how to troubleshoot this or insight on how the integral function works which may explain why this is happening? I also tried writing my functions symbolically and running the integration with the "int" function, however this caused matlab to show the "Busy" message but never come up with a solution.
I would appreciate any help,
Thank you

回答(2 个)

Roger Stafford
Roger Stafford 2015-3-25
My guess is that your 'fun2' at infinity is what is giving 'integral' the trouble. log_normal(rp) is approaching zero and rp^4 is approaching infinity. Perhaps 'integral' is unaware that the limit of their product is zero, and it therefore gives a NaN.
  1 个评论
TheBestTreeInTheForest
It was actually the starting value of the fun1 integral that was screwing it up in the end, I still don't know why but it works when I start at a higher value. For now I'll work with what I got and try to perfect it later.
Thanks for the help.

请先登录,再进行评论。


John D'Errico
John D'Errico 2015-3-25
NaN results when an operation is indeterminate. That is, things like this:
NaN = inf/inf = inf - inf = 0/0 = sin(inf) = cos(inf) ...
There are lots of things that may produce a NaN. These are just the ones that come to mind.
As well, NaNs propagate. Almost any operation that is done with a NaN, will usually produce another NaN, including 0*NaN. So once you get one NaN, they breed like well, bunnies, or maybe clothes hangars in my closet.
As for what has produced the NAN in your code, learn to use the debugger.
help dbstop
Use it to interrupt the code when a NaN was first generated, then look at what caused it. So you might try this:
dbstop if infnan
Then think about what in that line caused the problem. You will surely see it is one of those indeterminate forms I mentioned (or another I did not think of.)
  3 个评论
John D'Errico
John D'Errico 2015-3-25
The nice thing about the dbstop call, is it lets you run until that happens.
Don't forget to clear the debugger afterwards, as it will cause your code to run slightly slower until you do so. Finding the problem point is more important for now though.
TheBestTreeInTheForest
I used the debugging tool and saw that for small rp values (about the first 17) I get NaN. The rp value effects values of other variables in sub-functions so I changed the range to start at higher rp value:
old: s_avg =integral(fun1,rs,inf)/integral(fun2,0,inf);
new: s_avg =integral(fun1,1.05*rs,inf)/integral(fun2,0,inf);
and I got pretty much the expected result even though technically I should be starting at rs. For now I'm happy with this and I will try to tune it later to make it work for the desired range.
Thanks for the help!

请先登录,再进行评论。

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by