利用差分方法,模拟计算抛物运动:
二阶的差分计算较为繁琐,为了简化计算,通常可以拆分为两个一阶微分方程再转化为差分运算:
利用Euler Method:
#this program is to solve the projectile motion problem
#inital
import math as M
def initialx(array):#便于初始化
i=0
while i<1000:
array.append(0)
i=i+1
return array
xpath=[0]
initialx(xpath)#初始化x方向位移
vx=[0]
initialx(vx)#初始化x方向速度
ypath=[0]
initialx(ypath)#初始化y方向位移
vy=[0]
initialx(vy)#初始化y方向速度
Nmax=1000
dt=0.01
g=9.8
def cannon(v,angle):#不同初始速度与角度
degree=3.14159*angle/180
vx_initial=v*M.cos(degree)
vy_initial=v*M.sin(degree)
v_initial=[vx_initial,vy_initial]
return v_initial
#caculate
v=input('please enter the initial velocity:')
v=int(v)
angle=input('please enter the initial angle(degree):')
angle=int(angle)
#gain the input of user
re=cannon(v,angle)
vx[0]=re[0]
vy[0]=re[1]
xpath[0]=0.0
ypath[0]=0.0
i=0
while i<Nmax:
xpath[i+1]=xpath[i]+vx[i]*dt
vx[i+1]=vx[i]
ypath[i+1]=ypath[i]+vy[i]*dt
vy[i+1]=vy[i]-9.8*dt
i=i+1
if ypath[i]<0:
imax=i
break
#store
i=0
filename='E:/计算物理/exercise/lecture1/drag.txt'
with open(filename, 'w')as datafile:
while i<imax:
datafile.write(str(xpath[i])+' ')
datafile.write(str(ypath[i])+'\n')
i=i+1
#display
#we use originpro to show the result instead becasuse i'm so weak QWQ
print('finsh!')
设置出射速度为30m/s,变换出射角度: