@Guoguo0605
2016-06-21T16:28:41.000000Z
字数 2503
阅读 1218
code trajectory_of_cannon_shell
# -*- coding: utf-8 -*-from pylab import *from math import *g = 9.8b2m = 4e-5class flight_state:def __init__(self, _x = 0, _y = 0, _vx = 0, _vy = 0, _t = 0):self.x = _xself.y = _yself.vx = _vxself.vy = _vyself.t = _tclass cannon:def __init__(self, _fs = flight_state(0, 0, 0, 0, 0), _dt = 0.1):self.cannon_flight_state = []self.cannon_flight_state.append(_fs)self.dt = _dtdef next_state(self, current_state):global gnext_x = current_state.x + current_state.vx * self.dtnext_vx = current_state.vxnext_y = current_state.y + current_state.vy * self.dtnext_vy = current_state.vy - g * self.dt#print next_x, next_yreturn flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)def shoot(self):while not(self.cannon_flight_state[-1].y < 0):self.cannon_flight_state.append(self.next_state(self.cannon_flight_state[-1]))r = - self.cannon_flight_state[-2].y / self.cannon_flight_state[-1].yself.cannon_flight_state[-1].x = (self.cannon_flight_state[-2].x + r * self.cannon_flight_state[-1].x) / (r + 1)self.cannon_flight_state[-1].y = 0def show_trajectory(self, labe):x = []y = []for fs in self.cannon_flight_state:x.append(fs.x)y.append(fs.y)plot(x,y, label = labe)#show()class drag_cannon(cannon):def next_state(self, current_state):global g, b2mv = sqrt(current_state.vx * current_state.vx + current_state.vy * current_state.vy)next_x = current_state.x + current_state.vx * self.dtnext_vx = current_state.vx - b2m * v * current_state.vx * self.dtnext_y = current_state.y + current_state.vy * self.dtnext_vy = current_state.vy - g * self.dt - b2m * v * current_state.vy * self.dt#print next_x, next_yreturn flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)class adiabatic_drag_cannon(cannon):def next_state(self, current_state):global g, b2mfactor = (1 - 6.5e-3 * current_state.y / 288.15) ** 2.5v = sqrt(current_state.vx * current_state.vx + current_state.vy * current_state.vy)next_x = current_state.x + current_state.vx * self.dtnext_vx = current_state.vx - factor * b2m * v * current_state.vx * self.dtnext_y = current_state.y + current_state.vy * self.dtnext_vy = current_state.vy - g * self.dt - factor * b2m * v * current_state.vy * self.dt#print next_x, next_yreturn flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)speed = 700theta = range(30,56,5)v_x = [speed * cos(i * pi / 180) for i in theta]v_y = [speed * sin(i * pi / 180) for i in theta]def nodrag():a = []for i in range(len(theta)):a.append(cannon(flight_state(0, 0, v_x[i], v_y[i], 0), _dt = 0.1))labe = str(theta[i]) + r'$^{\circ}$'a[i].shoot()a[i].show_trajectory(labe)legend(loc='upper left', frameon=False)nodrag()xlabel(r'$x(m)$', fontsize=16)ylabel(r'$y(m)$', fontsize=16)text(45367, 15000, 'No Drag')title('Trajectory of cannon shell')show()