@rfhongyi
2016-11-09T03:36:23.000000Z
字数 5911
阅读 673
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 pylab as plimport mathclass trajectories:def __init__(self,mass=10, time_step=0.1,total_time=13,\initial_velocity_x=50,initial_velocity_y=50,initial_x=0,initial_y=0,B2=0):self.v_x = [initial_velocity_x]self.v_y = [initial_velocity_y]self.v=[math.sqrt(pow(initial_velocity_x,2)+pow(initial_velocity_y,2))]self.x=[initial_x]self.y=[initial_y]self.t = [0]self.m = massself.dt = time_stepself.time = total_timeself.B2=B2self.initial_velocity_x=initial_velocity_xself.initial_velocity_y=initial_velocity_ydef run(self):_time = 0while(_time < self.time):self.v.append(math.sqrt(pow(self.v_x[-1],2)+pow(self.v_y[-1],2)))self.v_x.append(self.v_x[-1]-(self.B2/self.m)\*self.v[-1]*self.dt*self.v_x[-1])self.v_y.append(self.v_y[-1]-10*self.dt-\(self.B2/self.m)*self.v[-1]*self.dt*self.v_y[-1])self.x.append(self.v_x[-1]*self.dt+self.x[-1])self.y.append(self.v_y[-1]*self.dt+self.y[-1])self.t.append(_time)_time += self.dtdef show_results(self):font = {'family': 'serif','color': 'darkred','weight': 'normal','size': 16,}pl.plot(self.x, self.y,label='angle is %f'%(math.atan(self.\initial_velocity_y/self.initial_velocity_x)*180/math.pi))pl.title('Cannon shell trajectories', fontdict = font)pl.xlabel('x ($km$)')pl.ylabel('y ($km$)')pl.legend()pl.show()a = trajectories()a.run()a.show_results()
import pylab as plimport mathclass trajectories:def __init__(self,mass=10, time_step=0.1,total_time=13,\initial_velocity_x=50,initial_velocity_y=50,initial_x=0,initial_y=0,B2=0.01):self.v_x = [initial_velocity_x]self.v_y = [initial_velocity_y]self.v=[math.sqrt(pow(initial_velocity_x,2)+pow(initial_velocity_y,2))]self.x=[initial_x]self.y=[initial_y]self.t = [0]self.m = massself.dt = time_stepself.time = total_timeself.B2=B2self.initial_velocity_x=initial_velocity_xself.initial_velocity_y=initial_velocity_ydef run(self):_time = 0while(_time < self.time):self.v.append(math.sqrt(pow(self.v_x[-1],2)+pow(self.v_y[-1],2)))self.v_x.append(self.v_x[-1]-(self.B2/self.m)\*self.v[-1]*self.dt*self.v_x[-1])self.v_y.append(self.v_y[-1]-10*self.dt-\(self.B2/self.m)*self.v[-1]*self.dt*self.v_y[-1])self.x.append(self.v_x[-1]*self.dt+self.x[-1])self.y.append(self.v_y[-1]*self.dt+self.y[-1])self.t.append(_time)_time += self.dtdef show_results(self):font = {'family': 'serif','color': 'darkred','weight': 'normal','size': 16,}pl.plot(self.x, self.y,label='angle is %f'%(math.atan(self.\initial_velocity_y/self.initial_velocity_x)*180/math.pi))pl.title('Cannon shell trajectories', fontdict = font)pl.xlabel('x ($km$)')pl.ylabel('y ($km$)')pl.legend()pl.show()a = trajectories()a.run()a.show_results()
import pylab as plimport mathclass trajectories:def __init__(self,mass=10, time_step=0.1,total_time=20,\initial_velocity_x=150,initial_velocity_y=150,initial_x=0,initial_y=0,B2=0.01):self.v_x = [initial_velocity_x]self.v_y = [initial_velocity_y]self.v=[math.sqrt(pow(initial_velocity_x,2)+pow(initial_velocity_y,2))]self.x=[initial_x]self.y=[initial_y]self.t = [0]self.m = massself.dt = time_stepself.time = total_timeself.B2=B2self.initial_velocity_x=initial_velocity_xself.initial_velocity_y=initial_velocity_ydef run(self):_time = 0while(_time < self.time):self.v.append(math.sqrt(pow(self.v_x[-1],2)+pow(self.v_y[-1],2)))self.v_x.append(self.v_x[-1]-(self.B2/self.m)\*self.v[-1]*self.dt*self.v_x[-1])self.v_y.append(self.v_y[-1]-(4.03*pow(10,14)\/pow((self.y[-1]+6.371*pow(10,6)),2))*self.dt-\(self.B2/self.m)*self.v[-1]*self.dt*self.v_y[-1])#use '(4.03*pow(10,14)\pow((self.y[-1]+6.371*pow(10,6)),2))' to change '10'self.x.append(self.v_x[-1]*self.dt+self.x[-1])self.y.append(self.v_y[-1]*self.dt+self.y[-1])self.t.append(_time)_time += self.dtdef show_results(self):font = {'family': 'serif','color': 'darkred','weight': 'normal','size': 16,}pl.plot(self.x, self.y,label='angle is %f'%(math.atan(self.\initial_velocity_y/self.initial_velocity_x)*180/math.pi))pl.title('Cannon shell trajectories', fontdict = font)pl.xlabel('x ($km$)')pl.ylabel('y ($km$)')pl.legend()pl.show()a = trajectories()a.run()a.show_results()


