@oliver1995
2017-05-07T05:17:40.000000Z
字数 1414
阅读 907
- use python to do homework 2.6
- Use the Euler method to calculate cannon shell trajectories,ignoring both air drag and the effect of air density.
import pylab as plfrom math import *class cannon:def __init__(self,init_v=10,theta =60,init_x=0,init_y=0,time_step=0.01 ):self.vx = [init_v*cos(theta*2*pi/360)]self.vy = [init_v*sin(theta*2*pi/360)]self.x = [init_x]self.y = [init_y]self.t = [0]self.B_m = B_mself.dt = time_stepdef run(self):while self.y[-1]>=0:self.x.append(self.x[-1] + self.dt * self.vx[-1])self.y.append(self.y[-1] + self.dt * self.vy[-1])self.vy.append(self.vy[-1] - self.dt * 9.8 )def show_results(self):pl.plot(self.x, self.y,color="black",linestyle="-",label="60")pl.legend(loc='upper right')pl.xlabel('x(km)')pl.ylabel('y(km)')pl.ylim(0,4)pl.show()a = cannon()a.run()a.show_results()
And this is a picture, which shows three different launch angle.

Ignoring the air drag, we can easy see that when launch angle 30 degree and 60 degree reach the same distance , and when launch angle are 45 degree, the distance are the furthest. The theoretical result is identical with the picture.
After two exercise, I understand how to use python and Euler method to caltulate some appoxiation.
Thanks for teacher cai's PPT.