Integral function gives NaN
23 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
TheBestTreeInTheForest
el 25 de Mzo. de 2015
Comentada: TheBestTreeInTheForest
el 26 de Mzo. de 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
0 comentarios
Respuestas (2)
Roger Stafford
el 25 de Mzo. de 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.
John D'Errico
el 25 de Mzo. de 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 comentarios
John D'Errico
el 25 de Mzo. de 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.
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!