# Chop intermediate results of NDSolve after each step [closed]

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.0001,0.020001,0.499998,-0.0000663528}
{-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?

• 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. – Szabolcs Feb 1 '17 at 17:11
• Would inserting Re[] in the appropriate places be feasible? – J. M.'s ennui Feb 1 '17 at 19:16
• 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. – xzczd Feb 2 '17 at 1:50
• @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. – Casimir Feb 2 '17 at 20:37