when use ode45,there nan output,why?and how to solve it.

6 views (last 30 days)
when use ode45,there nan output,why?and how to solve it.the code is in the LCS-Tool-master.zip.please run the elliptic_hyperbolic_lcs_details.m in ...\LCS-Tool-master\demo\ocean_dataset
  2 Comments
Torsten
Torsten on 18 Mar 2016
Nobody can answer this question without seeing the code you are using.
Best wishes
Torsten.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 18 Mar 2016
Your code has one or more situations in which it encounters one of:
0/0
0*inf
0*(-inf)
inf-inf
-inf+inf
inf/inf
-inf/inf
-inf/(-inf)
inf/(-inf)
Some of these might not be direct: for example they can be encountered while doing an algebraic matrix multiply, or while doing a matrix inversion, or while using the "\" or "/" operators, or possibly even while using cov(). For that matter, even mean, since mean([inf -inf]) would trigger it.
Note the "/" in that list. One of the ways to trigger the situation is if you have accidentally used "/" instead of "./" when you intended an element-by-element division of values.
But more typically for ode45 would be that you encountered an infinity (possibly due to a singularity) as infinity tend to spread and then you tend to end up with an accidental infinity minus infinity or infinity divided by infinity.
You can debug the situation by commanding
dbstop if naninf
and then running your ode45 call. It will stop when the nan or infinity shows up and you can then examine the values of variables to see how it happened.
  2 Comments
Mohammad Farhat
Mohammad Farhat on 7 Apr 2020
Walter, can NaNs be generated by other than these conditions? If you can check my code here:
At some point my data turns into nans and I cannot see any of these singularities applicable in my case.
Walter Roberson
Walter Roberson on 7 Apr 2020
Your code has sin() and cos() of some complex quantities with coefficients on the order of 1e20. sin() and cos() of those are giving you complex inf. The first on shows up in B. They get into a number of other variables for the same reason. Then you get to
dedt=15/8*e*J * sin(II)^2 * sin(2*w)...
-(15*J*eoct/512)*( cos(O)*( (4+3*e^2)*(3+5*cos(2*II))...
*sin(w)+210*e^2*sin(II)^2*sin(3*w)) +2*cos(II)...
*cos(w)*sin(O)*(15*(2+5*e^2)*cos(2*II) ...
+7*(30*e^2 *cos(2*w)*sin(II)^2 -2 -9*e^2)));
The sin(II)^2 part is complex infinity, so the first term 15/8*e*J * sin(II)^2 * sin(2*w) leads to complex infinity. Then the second term that is being subtracted from it has cos(2*II) which is going to get you another complex infinity. Without going through a lot of detailed analysis as to exactly what happens, you have a situation involving the sum or difference of complex infinities, and that is going to give you nan.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!