I am trying to numerically solve the Lane-Emden equation in Python using the scipy.integrate.ode class.
For some reason, my code works for integer values of n (the polytropic index) such as 3, but not for non-integer values such as 2.9 and 3.1.
I am getting a Runtime Warning, "7: RuntimeWarning: invalid value encountered in double_scalars" and the print statement "print f0, f1" shows that several values are nan's when n is not an integer.
from scipy.integrate import ode
import numpy as np
def rhs(zet, u, n):
x = u[0] # theta
y = u[1] # phi
f0 = -u[1]/(zet**2)
f1 = (u[0]**(n))*(zet**2)
print f0, f1
return np.array([f0,f1])
r = ode(rhs).set_integrator("vode", method="adams", atol=1.e-13, rtol=1.e-13, nsteps=15000, order=12)
y0 = np.array([1.,0.])
zeta0 = 0.000001
r.set_initial_value(y0, zeta0)
n=3.1
r.set_f_params(n)
dzeta = 0.000001
zeta = [zeta0]
theta = [y0[0]]
while r.successful() and r.y[0] > 0.0:
r.integrate(r.t + dzeta)
zeta.append(r.t)
theta.append(r.y[0])
What could be causing this issue?