I am trying to solve a simple mechanical problem, which is the non linear simple pendulum issue.
For this, I have to use odeint from scipy.
For a reminder, I have to solve the non linar ODE which is: theta'' + w²*sin(theta) = 0
Here is what I have for now, I have to define a function that returns the position of the pendulum, that is to say theta(t)
:
def ODE(omega,theta0,tf):
"""solution au bout tf avec odeint"""
def F(Y,t,omega):
"""second membre de l'EDO"""
return np.array([Y[1],-omega**2*np.sin(Y[0])])
Y0 = np.array([theta0,0.])
Y = odeint(F, Y0, tf, args=(omega,))
return Y
But when I try a verification with this, to compare to the linear solution:
# test
theta0 = 0.1
omega = 2.58
N = 15
T = np.linspace(0,2*np.pi/omega,N)
# solution with odeint
ThetaODE = ODE(omega,theta0,T)
# linear solution
ThetaL = theta0*np.cos(omega*T)
# plot
plt.figure(figsize=(15,6))
plt.subplot(1,2,1)
plt.plot(T,ThetaODE,label="ODEint")
plt.plot(T,ThetaL,label="Lineaire")
plt.xlabel('t')
plt.legend()
plt.subplot(1,2,2)
plt.plot(T,ThetaL-ThetaODE)
plt.title('Ecart entre ODEint et la sol Lineaire')
plt.xlabel('t')
I have this error:
ValueError Traceback (most recent call last)
<ipython-input-128-d8b2832c3f6c> in <module>
22
23 plt.subplot(1,2,2)
---> 24 plt.plot(T,ThetaL-ThetaODE)
25 plt.title('Ecart entre ODEint et la sol Lineaire')
26 plt.xlabel('t')
ValueError: operands could not be broadcast together with shapes (15,) (15,2)
There must be something I understand the wrong way concerning odeint, any idea what I do wrong ? Thank you !