钟摆积分。溢流python
我编写了一个使用四阶龙格-库塔积分计算摆锤速度和力的代码,不幸的是,我无法运行它,因为我得到了以下错误:钟摆积分。溢流python,python,Python,我编写了一个使用四阶龙格-库塔积分计算摆锤速度和力的代码,不幸的是,我无法运行它,因为我得到了以下错误: 30: RuntimeWarning: overflow encountered in double_scalars th2 = th[j+1] + (ts/2)*om[j+1] 33: RuntimeWarning: overflow encountered in double_scalars om2 = om[j+1] + (ts/2)*f2 39: RuntimeWa
30: RuntimeWarning: overflow encountered in double_scalars
th2 = th[j+1] + (ts/2)*om[j+1]
33: RuntimeWarning: overflow encountered in double_scalars
om2 = om[j+1] + (ts/2)*f2
39: RuntimeWarning: invalid value encountered in double_scalars
th3 = th2 + (ts/2)*om2
40: RuntimeWarning: invalid value encountered in sin
f3 = (-q*om2 - sin(th2) + b*cos(od)*ts)
我不确定我错过了什么或者我犯了什么错。任何想法都会有帮助。谢谢
这是我的代码:
from scipy import *
from matplotlib.pyplot import *
##A pendulum simulation using fourth order
##Runge-Kutta differentiation
ts=.05 #time step size, dt_a
td=20 #trial duration, dt_b
te=int(td/ts) #no of timesteps
q=0.5 #scaled damping coefficient or friction factor
m=1 #mass
g=9.81 #grav. acceleration
l=9.81 #length of pendulum
th=[((rand()*2)-1)*pi] #random initial angle, [-pi,pi]
om=[0] #initial angular velocity
b=0.9 #scaled initial (force)/ml or driving coef.
od=0.66 #driving force freq
for j in range(te):
#Euler approximation
th.append(th[j] + ts*om[j]) #storing theta values
f1 = (-q*om[j] - sin(th[j]) + b*cos(od)*ts)
#ts += ts
om.append(om[j] + ts*f1)
#ts=.05
#1 at mid-interval
th2 = th[j+1] + (ts/2)*om[j+1]
f2 = (-q*om[j+1] - sin(th[j+1]) + b*cos(od)*ts)
om2 = om[j+1] + (ts/2)*f2
#ts += ts
#ts=.05
#2 at mid-interval
th3 = th2 + (ts/2)*om2
f3 = (-q*om2 - sin(th2) + b*cos(od)*ts)
om3 = om2 + (ts/2)*f3
#ts += ts
#next time step
th4 = th3 + (ts)*om3
f4 = (-q*om3 - sin(th3) + b*cos(od)*ts)
om4 = om3 + (ts)*f4
ts += ts
dth=(om[j] + 2*om[j+1] + 2*om2 + om3)/6
dom=(f1 + 2*f2 + 2*f3 + f4)/6
th[j+1] = th[j] + ts*dth #integral of angle
om[j+1] = om[j] + ts*dom #integral of velocity
plot(th,om),xlabel('Angle'),ylabel('')
show()
第一次拷贝粘贴对我来说似乎很好。。。小提示:当变量名少于一个字母时,调试起来就容易了大约两百万倍。当你在一个物理方程中写100次的时候,M是很有意义的。当你试图记住它代表1000行的内容时,我想你可以在这里找到问题的答案:我最近将python更新为3.5.1,我不知道这是否与RuntimeWarnings的源代码有关