@Guibeen
2016-10-16T16:51:01.000000Z
字数 2809
阅读 613
坐标
import math
import pylab as pl
class cannon_shell():
def __init__(self, velocity=700, fixing_angle=9*math.pi/36, position_x=0,
position_y=0,drag_constant=0.00004, #drag_constant is B2/m
acceleration_of_gravity=9.8, decay_index=10000, time_step=0.1):
self.theta=fixing_angle*180/math.pi
self.x = [position_x]
self.y = [position_y]
self.v = [velocity]
self.v_x = [velocity*math.cos(fixing_angle)]
self.v_y = [velocity*math.sin(fixing_angle)]
self.B2_m = [drag_constant]
self.g = acceleration_of_gravity
self.y_0 = decay_index
self.dt = time_step
self.t = [0]
def run(self):
while(self.y[-1]>=0):
self.x.append(self.x[-1]+self.v_x[-1]*self.dt/1000)
self.y.append(self.y[-1]+self.v_y[-1]*self.dt/1000)
self.v_x.append(self.v_x[-1]-self.B2_m[-1]*self.v[-1]*self.v_x[-1]*self.dt)
self.v_y.append(self.v_y[-1]-self.g*self.dt-self.B2_m[-1]*self.v_y[-1]*self.v[-1]*self.dt)
self.B2_m.append(self.B2_m[-1]*math.exp(-self.y[-1]/self.y_0))
self.v.append(math.sqrt(pow(self.v_x[-1],2)+pow(self.v_y[-1],2)))
self.t.append(self.t[-1]+self.dt)
def show_results(self):
plot1=pl.plot(self.x,self.y)
pl.title('cannon shell trajectory')
pl.xlabel('x($km$)')
pl.ylabel('y($km$)')
pl.xlim(0,25)
pl.ylim(0,10)
pl.show
a = cannon_shell()
a.run()
a.show_results()
若不考虑空气密度随高度的变化,则程序将简化:
import math
import pylab as pl
class cannon_shell():
def __init__(self, velocity=700, fixing_angle=9*math.pi/36, position_x=0,
position_y=0,drag_constant=0.00004, #drag_constant is B2/m
acceleration_of_gravity=9.8, time_step=0.1):
self.theta=fixing_angle*180/math.pi
self.x = [position_x]
self.y = [position_y]
self.v = [velocity]
self.v_x = [velocity*math.cos(fixing_angle)]
self.v_y = [velocity*math.sin(fixing_angle)]
self.B2_m = drag_constant
self.g = acceleration_of_gravity
self.dt = time_step
self.t = [0]
def run(self):
while(self.y[-1]>=0):
self.x.append(self.x[-1]+self.v_x[-1]*self.dt/1000)
self.y.append(self.y[-1]+self.v_y[-1]*self.dt/1000)
self.v_x.append(self.v_x[-1]-self.B2_m*self.v[-1]*self.v_x[-1]*self.dt)
self.v_y.append(self.v_y[-1]-self.g*self.dt-self.B2_m*self.v_y[-1]*self.v[-1]*self.dt)
self.v.append(math.sqrt(pow(self.v_x[-1],2)+pow(self.v_y[-1],2)))
self.t.append(self.t[-1]+self.dt)
def show_results(self):
plot1=pl.plot(self.x,self.y,'--')
pl.title('cannon shell trajectory')
pl.xlabel('x($km$)')
pl.ylabel('y($km$)')
pl.xlim(0,25)
pl.ylim(0,10)
pl.show
a = cannon_shell()
a.run()
a.show_results()