I'm trying to deduce the pendulum equation out of the Verlet algorithm. This is what I've done with Python using r
as the angle of the pendulum, w
as the speed and a
as the angular acceleration. h
is the increase of time, and my main problem is that if I introduce an h
lower than 2 the program just show me the same number again and again.
from math import sin, cos, pi
from pylab import plot,show
h=int(input("h: "))
q=int(input("Angle: "))
l=int(input("Pendulum lenght: "))
r=q*pi/180 #Change the angle from degrees to rad
w=0
g=9.81
a=(-g/l)*sin(r)
y=0
xx=[]
yy=[]
while y>=0:
r= r+h*w+ (h**2)/2*a
x= cos(r) #Cartesians
y= sin(r)
w= w+ ((h/2)*a)
a=(-g/l)*sin(r)
w= w+((h/2)*a)
xx.append(x)
yy.append(y)
plot (xx,yy)
show()
For any physically-reasonable pendulum, your
h
should be less than 1 (seconds) to model this properly. But you're casting toint
which rounds down, so you geth=0
(no time step). Instead, usefloat
: