In: Physics
In Group Project C of Chapter 4, it was shown that the simple pendulum equation:
y''(t) + siny(t) = 0
has periodic solutions when the initial displacement and velocity are small. Show that the period of the solution may depend on the initial conditions by using the vectorized Runge–Kutta algorithm with h 0.02 to approximate the solutions to the simple pendulum problem on for the initial conditions:
a) y(t) = 0.1 y'(t) = 0
b) y(t) = 0.5 y'(t) = 0
c) y(t) = 1.0 y'(t) = 0
Code to solve and plot the ode using python
Here, we have plotted the solution for t from 0 to 50. By counting the number of peaks in the plot, we can compare the time period of the pendulum.
#!usr/bin/env/python
#Runge-Kutta Method
import numpy as np
import matplotlib.pyplot as plt
"""defining the fucntion to be sin(y(t))"""
def func(t,x):
return -np.sin(x)
def frange(start, stop, step):
x = start
while x < stop:
yield x
x += step
"""solving and plotting the solution for y"(t) = sin(y(t)) by
rung-kutta 4 point algorithm"""
def rksolve(init1,init2):
h = 0.02
t = map(float,frange(0,50,h))
x = [init1]
y = [init2]
"""calculating y(t) derivatives"""
for i in range(len(t)-1):
s1 =
func(t[i],x[i])
s2 = func(t[i] +
h/2,x[i] + h/2 * s1)
s3 = func(t[i] +
h/2,x[i] + h/2 * s2)
s4 = func(t[i] + h,x[i]
+ h * s3)
y.append(y[i] + h/6*(s1
+ s4 + 2 * ( s2 + s3 )))
s1 = y[i]
s2 = y[i] + h/2 *
s1
s3 = y[i] + h/2 *
s2
s4 = y[i] + h * s3
x.append(x[i] + h/6*(s1
+ s4 + 2 * ( s2 +s3)))
"""calculating y values"""
plt.plot(t,x,'r')
plt.plot(t,[0 for i in t],'b')
plt.show()
return(0)
rksolve(0.1,0)
rksolve(0.5,0)
rksolve(1.0,0)
The output plots are as follows:(shown in the order of execution)
It is clear from the figure that initial condition A had more peaks (number of peak = 8) than condition B (number of peak =7.5) and c(number of peak =7) had. So, time period had a trend of Tc > Tb > Ta. Timeperiod depends on initial condition.