Callback function in DifferentialEquations

I am trying to solve the Lane-Emden equations using DifferentialEquations. This is a system of two first order ODEs which are something like dtheta/dxi = psi, dpsi/dxi = -2 psi/xi - theta^n. I want to stop the integration when theta hits zero. However, as you can see if n is a fraction negative theta would cause problem. In principle, since the integration should stop as it hits zero, there should not be any problem. But the error that the solver gives is exactly due to the negative theta. How to circumvent this problem?

Use abs doesn’t error and let the callback “rewind”. It’s the same in all languages/cases.

1 Like