I'm trying to solve the following one-dimensional ODE (2nd order) using the scipy odeint function:
z'' = 2*F*cos(vt-kz)*(z*cos(vt-k*z) - k*(1+z^2)*sin(vt-kz))/(1+z^2)^2
And my python script is
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def func(var,t,F,k,v):
z,Vz = var
tmp = v*t-k*z
cos_tmp = np.cos(tmp)
denom = 1+z**2
return [Vz,
2*F*cos_tmp*(z*cos_tmp - k*denom*np.sin(tmp))/(denom*denom)]
I understand that odeint uses an adaptive time step and it returns the solution evaluated at the time points given by the input t array. However, I observed that the solution diverges when the time array is not fine enough which doesn't make sense to me if odeint chooses the internal time step automatically.
k= 12184.
F = -0.000125597154176
v = 0.3
y0 = [-1.0,0.]
t1 = np.linspace(0,10,10000)
t2 = np.linspace(0,10,100)
t3 = np.linspace(0,10,10)
result1 = odeint(func,y0,t1,args=(F,k,v))
result2 = odeint(func,y0,t2,args=(F,k,v))
result3 = odeint(func,y0,t3,args=(F,k,v))
fig,ax = plt.subplots(1,2,figsize=(14,5))
ax[0].plot(t1,result1[:,0],label='t1')
ax[0].plot(t2,result2[:,0],'r.',label='t2')
ax[1].plot(t3,result3[:,0],'r.',label='t3')
ax[0].legend(frameon=False,loc='upper left',numpoints=1)
ax[1].legend(frameon=False,loc='upper left',numpoints=1)
plt.show()
An image of the solution is here: