import numpy as np
from numpy import sin, cos
from scipy.integrate import odeint
from matplotlib import pyplot as plt
def equations(y0, t):
theta, x = y0
f = [x, -(g/l) * sin(theta)]
return f
def plot_results(time, theta1, theta2):
plt.plot(time, theta1[:,0])
plt.plot(time, theta2)
s = '(Initial Angle = ' + str(initial_angle) + ' degrees)'
plt.title('Metotalet: ' + s)
plt.xlabel('time (s)')
plt.ylabel('angle (rad)')
plt.grid(True)
plt.legend(['nonlinear', 'linear'])
plt.show()
g = 9.81
l = 1.0
E_lst=[]
E=0
m=10
time = np.arange(0, 12.0, 0.025)
initial_angle = input("choose an initial angle")
theta0 = np.radians(initial_angle)
x0 = np.radians(0.0)
theta1 = odeint(equations, [theta0, x0], time)
w = np.sqrt(g/l)
theta2 = [theta0 * cos(w*t) for t in time]
plot_results(time, theta1, theta2)