[关闭]
@c-xy 2016-10-16T15:55:27.000000Z 字数 1125 阅读 134

第五次作业

习题2.6


摘要
对于习题2.6,本文的主要工作是用Euler法求解二元二阶常微分方程组.
背景知识
本题的数值模拟源于以下微分方程组




则可用以下代码进行模拟,此处选取了30,60,90三个角度进行模拟
数值模拟代码

  1. import pylab
  2. from math import sqrt
  3. from math import cos
  4. from math import sin
  5. from math import pi
  6. class Euler2Dsimulation:
  7. def __init__(self,speed,theta,timestep=0.001):
  8. self._timestep=timestep
  9. self._x=[0]
  10. self._y=[0]
  11. self._vx=[speed*cos(theta)]
  12. self._vy=[speed*sin(theta)]
  13. self.g=9.8
  14. self.calculate()
  15. self.plot()
  16. def _accelerate(self,x,y,vx,vy):
  17. ansax=0
  18. ansay=-self.g
  19. return (ansax,ansay)
  20. def calculate(self):
  21. while self._y[-1]>=0:
  22. self._x.append(self._x[-1]+self._vx[-1]*self._timestep)
  23. self._y.append(self._y[-1]+self._vy[-1]*self._timestep)
  24. (ax,ay)=self._accelerate(self._x[-1],self._y[-1],self._vx[-1],self._vy[-1])
  25. self._vx.append(self._vx[-1]+self._vx[-1]*self._timestep*ax)
  26. self._vy.append(self._vy[-1]+self._timestep*ay)
  27. def plot(self):
  28. pylab.plot(self._x, self._y)
  29. pylab.xlabel('x ($m$)')
  30. pylab.ylabel('y ($m$)')
  31. Euler2Dsimulation(700,pi/4)
  32. Euler2Dsimulation(700,pi/6)
  33. Euler2Dsimulation(700,pi/3)
  34. pylab.show()

结果展示
结果展示

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注