Integral function gives NaN

32 views (last 30 days)
TheBestTreeInTheForest
TheBestTreeInTheForest on 25 Mar 2015
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

Answers (2)

Roger Stafford
Roger Stafford on 25 Mar 2015
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 Comment
TheBestTreeInTheForest
TheBestTreeInTheForest on 26 Mar 2015
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.

Sign in to comment.


John D'Errico
John D'Errico on 25 Mar 2015
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 Comments
John D'Errico
John D'Errico on 25 Mar 2015
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
TheBestTreeInTheForest on 26 Mar 2015
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!

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!