I wrote a program to use odeint to solve a differential equation. But it had a problem. When I setted Cosmopara as np.array([70.0,0.3,0,-1.0,0]), it gave a warning that invalid value encountered in sqrt and invalid value encountered in double_scalars in h = np.sqrt(y1 ** 2 + Omega_M * t ** (-3) + Omega_DE*y2). But I checked that line and didn't find any error. If Cosmopara = np.array([70.0,0.3,0.0,-1.0,0.0]), Y shouldn't change but it changed. Besides, If I chose Cosmopara = np.array([70.0,0.3,0.1,-1.0,0.1]), this program could gives the right result.
What am I doing wrong?
import numpy as np import matplotlib.pyplot as plt from scipy.integrate import odeint global CosmoPara Cosmopara = np.array([70.0,0.3,0.0,-1.0,0.0]) def derivfun(Y,t): Omega_M = Cosmopara[1] Sigma_0 = Cosmopara[2] omega = Cosmopara[3] delta = Cosmopara[4] Omega_DE = 1-Omega_M-Sigma_0**2 y1 = Y[0] y2 = Y[1] h = np.sqrt(y1**2 + Omega_M*t**(-3) + Omega_DE*y2) dy1dt = -3.0*y1/t + (delta*Omega_DE*y2)/(t*h) dy2dt = -(3.0*(1+omega) + 2.0*delta*y1/h)*y2/t return np.array([dy1dt,dy2dt]) z = np.linspace(1,2.5,15001) time = 1.0/z Omega_M = Cosmopara[1] Sigma_0 = Cosmopara[2] omega = Cosmopara[3] delta = Cosmopara[4] Omega_DE = 1-Omega_M-Sigma_0**2 y1init = Sigma_0 y2init = 1.0 Yinit = np.array([y1init,y2init]) Y = odeint(derivfun,Yinit,time) y1 = Y[:,0] y2 = Y[:,1] h = np.sqrt(y1**2 + Omega_M*time**(-3) + Omega_DE*y2) plt.figure() plt.plot(z,h) plt.show() 

np.sqrt? Check with a debugger, or simply add a print statement. Does the value look correct? If not, look further at the terms in the argument tosqrt. Which ones are unexpected?global CosmoParais unnecessary (and it has a mistake: the variable that you use isCosmopara).