@2014301020054
2016-10-16T16:55:18.000000Z
字数 2270
阅读 1312
physics python
Use the Euler method to calculate cannon shell trajectories ignoring both air drag and the effect of air density(actually, ignoring the former automatically rules out the latter). Compare your results with those in Figure 2.4, and with the exact solution.
The Euler method we used to treat the bicycle problem can easily be generalized to deal with motion in two spatial dimensions. To be specific, we consider a projectile such as a shell shot by a cannon. We have a very large cannon in mind, and the large size will determine some of the important physics...
Use the Euler method to calculate cannon shell trajectories ignoring both air drag and the effect of air density(actually, ignoring the former automatically rules out the latter). Compare your results with those in Figure 2.4, and with the exact solution.
Store position as and velocity as .
For each time step calculate position and velocity at step :
- Stop when or
while.
import pylab as plimport math as mtclass cannon_shells:def __init__(self, initial_velocity = 0.7, g = 0.0098, range = 0, height = 0,\time_step = 0.01, initial_angle = 30.0, da = 5.0):self.v = [initial_velocity]self.a = [initial_angle]self.g = gself.dt = time_stepself.da = daself.x = [range]self.y = [height]def run(self):_a = self.a[0]while(_a <= 60):_a += self.daself.a.append(self.a[-1] + self.da)t = 2 * mt.sin(_a / 180.0 * mt.pi) * self.v[0]/self.gvx = mt.cos(_a / 180.0 * mt.pi) * self.v[0]vy = mt.sin(_a / 180.0 * mt.pi) * self.v[0]_time = 0xi = [0]yi = [0]while(_time < t):xi.append(xi[-1] + self.dt * vx)yi.append(yi[-1] + self.dt * vy)vx = vxvy = vy - self.g * self.dt_time += self.dtself.x = self.x + xiself.y = self.y + yidef show_results(self):font = {'family': 'serif','color': 'darkred','weight': 'normal','size': 16,}pl.title('The trajectory of a cannon shell')pl.text(0.95 * self.x[-1], 0.95 * max(self.y),\'No drag', fontdict=font)pl.plot(self.x,self.y)pl.xlabel('x($km$)')pl.ylabel('y($km$)')pl.show()a = cannon_shells()a.run()a.show_results()


The results are the same as those in Figure 2.4.
This is a exact solution.