Is it possible that ode23 or ode45 could result in complex numbers instead of real ones? If Yes, what are the possible reasons for getting complex numbers once you would expect real ones?

39 views (last 30 days)
Sim on 3 Jul 2020
Commented: Sim on 10 Jul 2020
Hi, I perfromed ode23 for solving a differential equation, but after some timesteps I started to get complex numbers (i.e. the imaginary part) as result, instead of real numbers. Based on my equations/calculations, I should always get real numbers, and I do not understand if/why those solvers could give imaginary numbers.
Therefore, Is it possible that ode23 or ode45 could result in complex numbers instead of real ones?
If Yes, what are the possible reasons for getting complex numbers once you would expect real ones?
Thanks a lot!
Sim on 10 Jul 2020
Thanks a lot to you all :)
@Steven Lord, thanks for your suggestion :) However, I do not see a so much difference by using the NonNegative option...
@John D'Errico, Yes you all gave me the right advices, thanks a lot :)
I think I partially fixed the issue of complex numbers by putting an abs() function here (please see my previous comment to @dpb, about the Qcell{i})
% c1, c2, c3 are constant numbers and h is the unknown quantity of the differential equation
A = abs((c1 * h)) / (c2 + h);
B = sqrt(c3 * A);
Qcell{i} = B * h;
However, once I run the code - and by using ode15s instead of the previous ode23t, since a bit faster than ode23t - I get this plot, which represents my unknown quantity "h" versus time "t", for different parts of my network: At the beginnning, up to about t=13, everything looks correct to me, i.e. positive smooth curves (curve = equation's solutions).. However, after that time, some curves go negative and some get a spike..... Why?! :)