@rfhongyi
2016-10-24T04:14:47.000000Z
字数 5500
阅读 736
From the Computational Physics we know the method of Euler.Now I want to take the Euler method into the projectile movement calculation:
Newton’s second law in two spatial dimensions :
import mathxx = 1000 #x position of the target,unit:myy = 100 #y position of the target,unit:mg = 9.8a = (6.5)*(math.pow(10,-3))Bm = 4*math.pow(10,-5) #B2/m,unit:m^(-1)T0 = 300vw = -20 #velocity of wind, unit:m/sva = 1000 #initial_velocity,unit:m/ssv = [va]sa = [0]s = xxfor i in range(21):v = vav0 = 0angle = 25 + i*0.001 #initial_angle=25°,add 2° each timefor j in range(20):x = [0] #initial_x,unit:my = [0] # initial_y,unit:mdt = 0.1 #time_step,unit:svx = [v*math.cos(math.pi*angle/180)]vy = [v*math.sin(math.pi*angle/180)]while (x[-1]<=xx):if y[-1] >100000000:c = 0else:c = math.pow((1-(a*y[-1]/T0)),2.5)Fx = -Bm*math.sqrt((vx[-1]-vw)*(vx[-1]-vw)+vy[-1]*vy[-1])*(vx[-1]-vw)*cFy = -Bm*math.sqrt((vx[-1]-vw)*(vx[-1]-vw)+vy[-1]*vy[-1])*vy[-1]*cvx.append(vx[-1]+Fx*dt)vy.append(vy[-1]+(-g+Fy)*dt)x.append(x[-1]+vx[-2]*dt)y.append(y[-1]+vy[-2]*dt)r = (xx-x[-2])/(x[-1]-xx)y[-1] = (y[-2]+r*y[-1])/(r+1)x[-1] = xxif y[-1]>yy:vt = vv = v0+(vt-v0)/2if y[-1]<yy:v0 = vv = v0+(vt-v0)/2sv.append(v)sa.append(angle)vmin = min(sv)l = sv.index(vmin)amin = sa[l]print('The x position of the target',xx)print('The y position of the target',yy)print('velocity of wind',vw)print ('The minimum initial velocity:'+str(vmin)+'m/s')print('angle:'+str(amin)+'°')
The x position of the target 1000
The y position of the target 100
velocity of wind -20
The minimum initial velocity:129.1036605834961m/s
angle:25.02°
The x position of the target 1000
The y position of the target -100
velocity of wind -20
The minimum initial velocity:104.19178009033203m/s
angle:25.019°
The x position of the target 100
The y position of the target -100
velocity of wind -20
The minimum initial velocity:20.12920379638672m/s
angle:25.0°
The x position of the target 100
The y position of the target 100
velocity of wind -20
The minimum initial velocity:20.131091947405366m/s
angle:25.0°
import mathimport pylab as plimport mathimport timeclass cannon:def __init__(self, vel_0=1000, time_of_duration=1, vel_0_step=0.01, time_step=0.01, m=100, B=0.01, vel_wind=2, theta=0, step=0.01, value_of_x=0, value_of_y=0, g=10, T0=300):self.v0 = [vel_0]self.theta = [theta]self.vx = [self.v0[0]]self.vy = [self.v0[0]]self.x = [value_of_x]self.y = [value_of_y]self.t = [0]self.g = gself.dt = time_stepself.time = time_of_durationself.nsteps = int(time_of_duration // time_step + 1)self.dtheta = stepself.nsteps2 = int(90//step + 1)self.B = Bself.m = mself.dv = vel_0_stepself.vw = vel_windself.T0 = T0self.n = 0def calculate(self):for k in range(100):tmpv0 = self.v0[k] + self.dvself.v0.append(tmpv0)self.theta = [0]self.vx = [0]self.vy = [0]for j in range(self.nsteps2):tmptheta = self.theta[j] + self.dthetaself.theta.append(tmptheta)self.x = [0]self.y = [0]self.vx[0] = self.v0[k] * math.cos(self.theta[j] * math.pi / 180)self.vy[0] = self.v0[k] * math.sin(self.theta[j] * math.pi / 180)for i in range(self.nsteps):tmpvx = self.vx[i] - self.dt * pow((1-0.0065 * self.y[i]/self.T0),2.5) * self.B * math.sqrt(float(self.vx[i]-self.vw) * float(self.vx[i]-self.vw) + self.vy[i] * self.vy[i]) * (self.vx[i] - self.vw)/self.mtmpvy = self.vy[i] - self.g * self.dt + self.dt * pow((1-0.0065 * self.y[i]/self.T0),2.5) * self.B * \math.sqrt(float(self.vx[i]-self.vw) * float(self.vx[i]-self.vw)+ self.vy[i] * self.vy[i]) * self.vy[i]/self.mtmpx=self.x[i] + self.vx[i] * self.dttmpy=self.y[i] + self.vy[i] * self.dtself.vx.append(tmpvx)self.vy.append(tmpvy)self.x.append(tmpx)self.y.append(tmpy)self.t.append(self.t[i] + self.dt)if int(self.x[i]) == 10:if self.y[i] > 10:self.n = 1breakif self.n == 1:breakif self.n == 1:self.show_results()print("The minimum of velocity is:%f", self.v0[k])breakdef show_results(self):pl.plot(self.x, self.y)pl.xlabel('x ($m$)')pl.ylabel('y ($m$)')pl.show()a = cannon()a.calculate()
The x position of the target 10000
The y position of the target 10000
velocity of wind 2
The minimum initial velocity:1000.10999m/s
But the code have a problem which is too slowly .