Skip to content

Instantly share code, notes, and snippets.

@iscadar
Created July 7, 2012 10:45
Show Gist options
  • Save iscadar/3065817 to your computer and use it in GitHub Desktop.
Save iscadar/3065817 to your computer and use it in GitHub Desktop.
A pendulum simulation using Euler integration
from scipy.integrate import *
from scipy import *
from matplotlib.pyplot import *
##A pendulum simulation
##v0.2
##Alexandros Kourkoulas Chondrorizos
th=pi/4#((rand()*2)-1)*pi #initial angle
om=0 #initial angular velocity
u=0 #torque
y0 = [om, th] #initial values
t = linspace(0, 40, 4000) #
def f(y, t):
mu=0.1 #friction factor
m=1 #mass
g=9.81 #grav. acceleration
l=1 #length
return (y[1],
((-mu*y[1]) + (m*g*l*sin(y[0])) + u)/(m*(l**2)))
r = odeint(f, y0, t)
#print(r)
#plot(t,r)
subplot(211),plot(t,r[:,1]),xlabel('Angle'),ylabel('')
subplot(212),plot(t,r[:,0],'r'),xlabel('Angular velocity'),ylabel('')
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment