@XF
2016-10-16T15:29:19.000000Z
字数 1553
阅读 110
Use the Euler method to calculate cannon shell trajectories ignoring both air drag and 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.
用Euler法解二元二阶常微分方程组。计算一个斜抛运动。
忽略空气阻力和空气密度的影响,计算精确解并与图2.4比较。
图2.4:
对于二阶的牛顿方程:
import pylabfrom math import sqrtfrom math import cosfrom math import sinfrom math import piclass Euler2Dsimulation:def __init__(self,speed,theta,timestep=0.01):self._timestep=timestepself._x=[0]self._y=[0]self._vx=[speed*cos(theta)]self._vy=[speed*sin(theta)]self.g=9.8self.calculate()self.plot()def _accelerate(self,x,y,vx,vy):ansax=0ansay=-self.greturn (ansax,ansay)def calculate(self):while self._y[-1]>=0:self._x.append(self._x[-1]+self._vx[-1]*self._timestep)self._y.append(self._y[-1]+self._vy[-1]*self._timestep)(ax,ay)=self._accelerate(self._x[-1],self._y[-1],self._vx[-1],self._vy[-1])self._vx.append(self._vx[-1]+self._vx[-1]*self._timestep*ax)self._vy.append(self._vy[-1]+self._timestep*ay)def plot(self):pylab.plot(self._x, self._y)pylab.xlabel('x ($m$)')pylab.ylabel('y ($m$)')Euler2Dsimulation(700,pi/4)Euler2Dsimulation(700,pi/6)Euler2Dsimulation(700,pi/3)pylab.show()
如图
感谢杜威同学在写ptyhon程序方面的帮助。感谢蔡浩老师。