260
8 Solving Ordinary Differential Equations
The file osc_EC_general.py has a function EulerCromer that implements this
method:
def EulerCromer(f, s, F, m, T, U_0, V_0, dt):
import numpy as np
N_t = int(round(T/dt))
print(’N_t:’, N_t)
t = np.linspace(0, N_t*dt, N_t+1)
u = np.zeros(N_t+1)
v = np.zeros(N_t+1)
# Initial condition
u[0] = U_0
v[0] = V_0
# Step equations forward in time
for n in range(N_t):
v[n+1] = v[n] + dt*(1./m)*(F(t[n]) - f(v[n]) - s(u[n]))
u[n+1] = u[n] + dt*v[n+1]
return u, v, t
The Fourth Order Runge-Kutta Method The RK4 method just evaluates the
right-hand side of the ODE system,
(
1
m
(F (t) − s(u) − f (v)) , v)
for known values of u, v, and t, so the method is very simple to use regardless of
how the functions s(u) and f (v) are chosen.
8.4.9 Illustration of Linear Damping
We consider an engineering system with a linear spring, s(u) = kx, and a viscous
damper, where the damping force is proportional to u , f (u ) = bu , for some
constant b > 0. This choice may model the vertical spring system in a car (but
engineers often like to illustrate such a system by a horizontally moving mass, like
the one depicted in Fig. 8.28). We may choose simple values for the constants to
illustrate basic effects of damping (and later excitations). Choosing the oscillations
to be the simple u(t) = cos t function in the undamped case, we may set m = 1,
k = 1, b = 0.3, U 0 = 1, V 0 = 0. The following function implements this
case:
def linear_damping():
import numpy as np
b = 0.3
f = lambda v: b*v
s = lambda u: k*u
F = lambda t: 0
m = 1
k = 1
8 Solving Ordinary Differential Equations
The file osc_EC_general.py has a function EulerCromer that implements this
method:
def EulerCromer(f, s, F, m, T, U_0, V_0, dt):
import numpy as np
N_t = int(round(T/dt))
print(’N_t:’, N_t)
t = np.linspace(0, N_t*dt, N_t+1)
u = np.zeros(N_t+1)
v = np.zeros(N_t+1)
# Initial condition
u[0] = U_0
v[0] = V_0
# Step equations forward in time
for n in range(N_t):
v[n+1] = v[n] + dt*(1./m)*(F(t[n]) - f(v[n]) - s(u[n]))
u[n+1] = u[n] + dt*v[n+1]
return u, v, t
The Fourth Order Runge-Kutta Method The RK4 method just evaluates the
right-hand side of the ODE system,
(
1
m
(F (t) − s(u) − f (v)) , v)
for known values of u, v, and t, so the method is very simple to use regardless of
how the functions s(u) and f (v) are chosen.
8.4.9 Illustration of Linear Damping
We consider an engineering system with a linear spring, s(u) = kx, and a viscous
damper, where the damping force is proportional to u , f (u ) = bu , for some
constant b > 0. This choice may model the vertical spring system in a car (but
engineers often like to illustrate such a system by a horizontally moving mass, like
the one depicted in Fig. 8.28). We may choose simple values for the constants to
illustrate basic effects of damping (and later excitations). Choosing the oscillations
to be the simple u(t) = cos t function in the undamped case, we may set m = 1,
k = 1, b = 0.3, U 0 = 1, V 0 = 0. The following function implements this
case:
def linear_damping():
import numpy as np
b = 0.3
f = lambda v: b*v
s = lambda u: k*u
F = lambda t: 0
m = 1
k = 1
