@OrionPaxxx
2016-11-15T12:48:56.000000Z
字数 3083
阅读 720
computationalphysics
1.考虑风阻和高度随海拔的变化
2.找到精确打击某一空中目标所需的最小速度,并且输出相应的发射角
3.未考虑各种误差
参见[上一次作业的背景介绍](https://github.com/OrionPaxxx/computational_physics_N2014301020039/blob/master/exercise_05/exercise_05.md)
代码如下
import pylab as plimport mathclass cannon_strike:def __init__(self,mass=10,altitude=0,initial_velocity=700,angle_of_departure=45,step_time=0.01,target_location_x=7000,target_location_y=5000):self.v=[initial_velocity]self.angle=angle_of_departure/180*math.piself.vx=[initial_velocity*math.cos(self.angle)]self.vy=[initial_velocity*math.sin(self.angle)]self.x=[0]self.y=[0]self.t=[0]self.dt=step_timeself.m=massself.target_x=target_location_xself.target_y=target_location_yself.strike_point_y=0def trajectory(self):while (self.y[-1]>=0):temp_vx=self.vx[-1]-0.003233*math.exp(-self.y[-1]/10000)*(self.v[-1]**2)/self.m*self.vx[-1]/self.v[-1]*self.dttemp_vy=self.vy[-1]-0.003233*math.exp(-self.y[-1]/10000)*(self.v[-1]**2)/self.m*self.vy[-1]/self.v[-1]*self.dt-9.8*self.dttemp_v=math.sqrt(temp_vx**2+temp_vy**2)self.vx.append(temp_vx)self.vy.append(temp_vy)self.v.append(temp_v)temp_x=self.x[-1]+temp_vx*self.dttemp_y=self.y[-1]+temp_vy*self.dtself.x.append(temp_x)self.y.append(temp_y)temp_t=self.t[-1]+self.dtself.t.append(temp_t)if self.x[-1]>self.target_x:self.strike_point_y=(self.y[-2]-self.y[-1])/(self.x[-2]-self.x[-1])*(self.target_x-self.x[-1])+self.y[-1]breakdef show_result(self):pl.plot(self.x,self.y)pl.xlabel("x(m)")pl.ylabel("y(m)")pl.title("trajectory of the canon")class precise_strike(cannon_strike):def pre_stk(self):#input the target location_input=input("please input the strike location like this:'x,y'\n")target_location=_input.split(",")temp_target_x=float(target_location[0])temp_target_y=float(target_location[1])#find the best angletemp_angle=1temp_velocity=10a=cannon_strike(angle_of_departure=temp_angle,initial_velocity=temp_velocity,target_location_x=temp_target_x,target_location_y=temp_target_y)a.trajectory()while a.strike_point_y<temp_target_y:if temp_angle<89:temp_angle=temp_angle+1a=cannon_strike(angle_of_departure=temp_angle,initial_velocity=temp_velocity,target_location_x=temp_target_x,target_location_y=temp_target_y)a.trajectory()else:temp_angle=1temp_velocity=temp_velocity+1a=cannon_strike(angle_of_departure=temp_angle,initial_velocity=temp_velocity,target_location_x=temp_target_x,target_location_y=temp_target_y)a.trajectory()#output the resultprint("stike point:","x=",a.target_x,"y=",a.strike_point_y)print("velocity=",temp_velocity,"angle=",temp_angle)tra,=pl.plot(a.x,a.y)loc,=pl.plot([a.target_x],[a.target_y],"*")pl.xlabel("x(m)")pl.ylabel("y(m)")pl.title("trajectory of the canon")pl.legend([tra,loc],["trajectory","target location"],loc="upper left")b=precise_strike()b.pre_stk()
以输入的打击电坐标为(1000,1000)为例,其结果如下
| 发射速度 | 发射角 | 落点位置 |
|---|---|---|
| 193 | 64 | (1000,1002.177) |
| 192.60 | 65 | (1000,1000.197) |
可视化输出:
如果图裂请点击链接(https://github.com/OrionPaxxx/computational_physics_N2014301020039/blob/master/exercise_06/output.png)
在不考虑误差的情况下,对于短程打击,此程序十分有效