Python 钟摆
Python pendulum
我正在尝试从 Verlet 算法中推导出摆方程。这就是我对 Python 所做的,使用 r
作为钟摆的角度,w
作为速度,a
作为 angular 加速度。 h
是时间的增加,我的主要问题是,如果我引入一个小于 2 的 h
,程序只会一次又一次地向我显示相同的数字。
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()
对于任何物理上合理的钟摆,您的 h
应该小于 1(秒)才能正确建模。但是你正在投射到 int
向下舍入,所以你得到 h=0
(没有时间步长)。相反,使用 float
:
h=float(input("h: "))
q=float(input("Angle: "))
l=float(input("Pendulum length: "))
我对物理学一无所知 -- xnx 可能有您的答案。
但是,我可以建议您采用函数式方法进行编程吗?
让我们从封装a的公式开始:
from math import sin, cos, pi
from pylab import plot,show
def getA(pLen, r):
g = 9.81
return (-g / pLen) * sin(r)
然后我们需要把你程序的其余部分变成一个函数,这样我们马上就能做一些非常有用的事情。
def main():
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
a=getA(l, 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=getA(l, r)
w= w+((h/2)*a)
xx.append(x)
yy.append(y)
plot (xx,yy)
show()
# call your main function only when running directly
if __name__ == '__main__':
main()
现在,假设您的文件名为 pendulum.py
,跳转到您的控制台并打开 python shell:
>python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pendulum
>>> pendulum.getA(5, 50)
0.5147794629671083
>>>
您现在可以检查您的 getA() 函数以确保它针对给定的输入返回正确的输出。
重复程序中的所有其他操作,您将能够进一步隔离问题,这是解决问题的重要一步。
我正在尝试从 Verlet 算法中推导出摆方程。这就是我对 Python 所做的,使用 r
作为钟摆的角度,w
作为速度,a
作为 angular 加速度。 h
是时间的增加,我的主要问题是,如果我引入一个小于 2 的 h
,程序只会一次又一次地向我显示相同的数字。
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()
对于任何物理上合理的钟摆,您的 h
应该小于 1(秒)才能正确建模。但是你正在投射到 int
向下舍入,所以你得到 h=0
(没有时间步长)。相反,使用 float
:
h=float(input("h: "))
q=float(input("Angle: "))
l=float(input("Pendulum length: "))
我对物理学一无所知 -- xnx 可能有您的答案。
但是,我可以建议您采用函数式方法进行编程吗?
让我们从封装a的公式开始:
from math import sin, cos, pi
from pylab import plot,show
def getA(pLen, r):
g = 9.81
return (-g / pLen) * sin(r)
然后我们需要把你程序的其余部分变成一个函数,这样我们马上就能做一些非常有用的事情。
def main():
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
a=getA(l, 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=getA(l, r)
w= w+((h/2)*a)
xx.append(x)
yy.append(y)
plot (xx,yy)
show()
# call your main function only when running directly
if __name__ == '__main__':
main()
现在,假设您的文件名为 pendulum.py
,跳转到您的控制台并打开 python shell:
>python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pendulum
>>> pendulum.getA(5, 50)
0.5147794629671083
>>>
您现在可以检查您的 getA() 函数以确保它针对给定的输入返回正确的输出。
重复程序中的所有其他操作,您将能够进一步隔离问题,这是解决问题的重要一步。