import math # import packages import pylab as pl import numpy as np class PROJECTILE(object): def __init__(self,_x0,_y0,_v0,_theta0,_B_m,_a,_alpha,_T0,_g,_dt): self.x= [float(_x0)] self.y= [float(_y0)] self.vx= _v0*math.cos(_theta0) self.vy= _v0*math.sin(_theta0) self.v0, self.theta0= _v0, _theta0 self.v= _v0 self.B= float(_B_m) self.a, self.alpha, self.T0= float(_a), float(_alpha), float(_T0) self.g=_g self.dt= float(_dt) def calculate(self): while True : if self.y[-1]<-1E-4 : self.yi= self.y[-1]+self.vy*self.dt self.xi= self.x[-1]+self.vx*self.dt self.gama= -self.y[-1]/self.yi self.x.append((self.x[-1]+self.gama*self.xi)/(self.gama+1.)) self.y.append(0.) break self.x.append(self.vx*self.dt+self.x[-1]) self.y.append(self.vy*self.dt+self.y[-1]) self.vx= -self.B*((1-self.a*self.y[-2]/self.T0)**self.alpha)\ *self.vx*self.v*self.dt+self.vx self.vy= -self.B*((1-self.a*self.y[-2]/self.T0)**self.alpha)\ *self.vy*self.v*self.dt+self.vy-self.g*self.dt self.v= math.sqrt(self.vx**2+self.vy**2) def store_max(self,x,v,theta): # store the max range and theta and v0 x.append(self.x[-1]) v.append(self.v0) theta.append(self.theta0) def plot(self): # plot the trace of the projectile pl.plot(self.x,self.y,'--') pl.annotate(r'$\theta$'+'= '+'%.2f'%(self.theta0*180./np.pi)+r'$^{o}$', xy=(7000,18000),fontsize=18) pl.annotate(r'$v_{0}$'+'= '+'%.2f'%self.v0+r'm/s',xy=(7000,16000),fontsize=18) pl.annotate('start y= %.2f m'%self.y[0],xy=(7000,14000),fontsize=18) # class TARGET give the minimun seperation of a given orbit # where : # x_target, y_target : the target's coordinates class TARGET(PROJECTILE): def reinit(self,_x_target,_y_target): self.x_target= _x_target self.y_target= _y_target def deviation(self): self.devi= np.sqrt((np.array(self.x)-self.x_target)**2+(np.array(self.y)-self.y_target)**2) self.devi= [min(self.devi),self.v0,self.theta0,self.x[0],self.y[0],\ self.x_target,self.y_target] return self.devi class TARGET_PLUS(PROJECTILE): def reinit(self,_x_target,_y_target,_len): self.x_target= _x_target self.y_target= _y_target self.len= _len def calculate(self): while True : if math.sqrt((self.x[-1]-self.x_target)**2+(self.y[-1]-self.y_target)**2)<2*self.len \ or self.y[-1]<-1E-4 : break self.x.append(self.vx*self.dt+self.x[-1]) self.y.append(self.vy*self.dt+self.y[-1]) self.vx= -self.B*((1-self.a*self.y[-2]/self.T0)**self.alpha)\ *self.vx*self.v*self.dt+self.vx self.vy= -self.B*((1-self.a*self.y[-2]/self.T0)**self.alpha)\ *self.vy*self.v*self.dt+self.vy-self.g*self.dt self.v= math.sqrt(self.vx**2+self.vy**2) def findextreme(to_find) : # to find the maximum and minimum of a table maxvalue, maxposition= 0, 0 minvalue, minposition= 10**9, 0 for i in range(len(to_find)): if to_find[i]>maxvalue: maxvalue= to_find[i] maxposition= i if to_find[i]