I'm trying to solve a differential equation for a physical problem : the falling of a golf ball. I am using the Euler resolution method, and I have this code :
def F(Z, t):
res = [Z[1], (-K/m)*Z[1]*sqrt((Z[1]**2) + (Z[1]**2)), Z[3], (-K/m)*Z[3]*sqrt((Z[3]**2) + (Z[3]**2))]
return res
def reso_z_euler(liste_t, F, CI, K):
n = len(liste_t)
Z = CI
pas = (liste_t[-1] - liste_t[0])/(n-1)
listeZ = [Z]
for i in range(1, n):
Z = F(Z, temps[i-1])
listeZ.append(Z)
return listeZ
listeZ = reso_z_euler(temps, F, Z0, K)
When I run this code, the terminal throws an error : OverflowError: (34, 'Result too large') I have never seen this error and I don't know what to do.. Your help would be so nice.. Thanks
You are using an unknown or non-local array for the times, temps
instead of liste_t
You are not implementing the Euler method Z = Z + pas*F(Z,liste_t[i-1])
, you would have to employ some mechanism to implement the vector arithmetic, for instance
Z = [ zk+pas*fk for zk, fk in zip(Z, F(Z,liste_t[i-1])) ]
You forgot to include the gravity force in the derivatives computation.
In general, the Euler method is only good to learn the principles of numerical ODE integration, for any useful result use a higher order method.