Using NDSolve for a set of three ordinary differential equations, I ran into the problem that intermediate results quickly develop negligible imaginary parts. I get the error

For the method IDA, only machine real code is available. Unable to continue with complex values or beyond floating-point exceptions.

Unfortunately, the equations themselves are very long and I haven't yet been able to reproduce the issue in a simpler example, so I can't offer an MWE at this point. However, adding the option StepMonitor :> Print[{t, var1[t], var2[t], var3[t]}] to NDSolve gives

{-0.0002,0.0200019 +0. I,0.499997 +0. I,-0.0000663555+0. I}

So the iteration fails already in the second step. The imaginary part appears to be due to rounding errors. My question is, is it possible to apply Chop to all variables after each step? Or alternatively, is there a method other than IDA that is equipped to deal with complex numbers?


closed as off-topic by Michael E2, Henrik Schumacher, José Antonio Díaz Navas, chuy, user21 Apr 5 '18 at 14:36

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Michael E2, Henrik Schumacher, José Antonio Díaz Navas, chuy, user21
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    $\begingroup$ NDSolve can work with any numerical black box, not just with symbolic equations. You can use f'[x] == fun[f[x], x] inside NDSolve and have a separate definition fun[fx_?NumericQ, x_?NumericQ] := Chop[...]. This may not be this straightfoward if you have a differential algebraic equation though. $\endgroup$ – Szabolcs Feb 1 '17 at 17:11
  • 2
    $\begingroup$ Would inserting Re[] in the appropriate places be feasible? $\endgroup$ – J. M. will be back soon Feb 1 '17 at 19:16
  • 1
    $\begingroup$ Without a specific example it's hard to give effective suggestions, but IDA method is for DAEs, so your system is a DAE system. (At least NDSolve thinks so.) A possible work-around is to modify the DAE system to an ODE system, here is an example. $\endgroup$ – xzczd Feb 2 '17 at 1:50
  • $\begingroup$ @J.M.That's the strange thing. I already did. All three ODE's are of the form var'[t] == Re[func[var[t]]], yet the problem persists. $\endgroup$ – Casimir Feb 2 '17 at 20:37