Ordinary Differential Equations (a) Write a Python function implementing the 4\'
ID: 3817476 • Letter: O
Question
Ordinary Differential Equations (a) Write a Python function implementing the 4'th order Runge-Kutta method. (b) Solve the following amusing variation on a pendulum problem using your routine. A pendulum is suspended from a sliding collar as shown in the diagram below. The system is at rest when an oscillating motion y(t) = Y sin (omega t) is imposed on the collar, starting at t = 0. The differential equation that describes the pendulum motion is given by: d^2 theta/dt^2 = -g/L sin theta + omega^2/L Y cos theta sin omega t Plot theta versus t for t = 0 to 40 s and determine the largest value of 0 during this time. Use g = 9.80665 m/s^2, L = 1.0 m, y = 0.25 m, and omega = 2.5 rad/s.Explanation / Answer
a) answer
def RK4(f):
return lambda t, y, dt: (
lambda dy1: (
lambda dy2: (
lambda dy3: (
lambda dy4: (dy1 + 2*dy2 + 2*dy3 + dy4)/6
)( dt * f( t + dt , y + dy3 ) )
)( dt * f( t + dt/2, y + dy2/2 ) )
)( dt * f( t + dt/2, y + dy1/2 ) )
)( dt * f( t , y ) )
def theory(t): return (t**2 + 4)**2 /16
from math import sqrt
dy = RK4(lambda t, y: t*sqrt(y))
t, y, dt = 0., 1., .1
while t <= 10:
if abs(round(t) - t) < 1e-5:
print("y(%2.1f) = %4.6f error: %4.6g" % ( t, y, abs(y - theory(t))))
t, y = t + dt, y + dy( t, y, dt )
output:
y(9.0) = 451.562459 error: 4.07232e-05
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.