【问题标题】:Python 2.7 Runge Kutta Orbit GUIPython 2.7 龙格库塔轨道 GUI
【发布时间】:2016-12-01 20:42:37
【问题描述】:

我正在尝试使用 Tkinter 在 Python 中创建一个 GUI,该 GUI 使用 Runge-Kutta 方法绘制质量的轨道路径。我的 GUI 工作正常,但我的问题是,无论我向 GUI 输入什么值,它都只会绘制一条直线。 我希望有人能告诉我我在 GUI 中的功能有什么问题,以便它能够正确地绘制轨道。

def calcPath(self):
    M = float(self.entM.get())
    m = float(self.entm.get())
    G = 6.67e-11
    c = 3e8



    velocity = np.array([float(self.entvx.get()),float(self.entvy.get()),float(self.entvz.get())])
    pos = np.array([float(self.entx.get()), float(self.enty.get()), float(self.entz.get())])

    Force = lambda pos: (G*m*M/(pos**2))

    #assigning variables empty lists to append x, y and z values to
    a = [] 
    b = []
    c = []
    t = 0.0
    tf = float(self.enttf.get())
    dt = float(self.entdt.get())

    while t < tf: # creating a while loop to trace the path for a set period of time
        #using the Runge-kutta formula from the problem sheet to assign variables at different steps and half steps 
        k1v=(dt*Force(pos))/m
        k2v=(dt*Force(pos+(k1v/2.0)))/m 
        k3v=(dt*Force(pos+(k2v/2.0)))/m 
        k4v=(dt*Force(pos+k3v))/m
        velocity=velocity+(k1v/6.0)+(k2v/3.0)+(k3v/3.0)+(k4v/6.0) #calaculating the weighted average of the k values
        pos=pos+velocity*dt #velocity is not a function of space or time so it will be identical at all 4 k values

        a.append(pos[0]) # appending the lists for each vaiable to produce a plot
        b.append(pos[1])
        c.append(pos[2])
        t = t+dt # setting the time steps

    #generating the path plot figure and formatting it    
    ax = Axes3D(self.fig) #creating a 3D figure 
    ax.set_title("Path of planetary mass") #produces title on the figure
    ax.plot3D(a,b,c, color='darkviolet', label='Runge-kutta path method') 
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')
    ax.legend(loc='lower left') #selecting the location of legend
    self.canvas.show()

【问题讨论】:

  • 不确定,但pos[0](和其他人)是否有可能对所有迭代使用相同的引用,从而导致abc 中的值相同?
  • 我已将 force 函数更改为在位置数组上使用 np.square 但不幸的是,当我运行它时它仍然只是绘制一条直线
  • 你能在每次迭代时 print(id(pos[0]) 看看它是否产生相同的数字吗?

标签: python-2.7 runge-kutta orbital-mechanics


【解决方案1】:

你的物理是错误的。而且你对运动方程的处理是错误的。

力计算为

Force = lambda pos: -G*m*M/(norm2(pos)**3)*pos

运动方程为

m·pos''(t) = 力(pos(t))

在哪里

pos''(t) = d²/dt² pos(t)

你必须转换成一阶系统

[ pos' , vel' ] = [ vel, Force(pos)/m ]

您现在可以将 RK4 方法应用于此一阶系统。

    k1p = dt *       vel
    k1v = dt * Force(pos        ) / m
    k2p = dt *      (vel+0.5*k1v) 
    k2v = dt * Force(pos+0.5*k1p) / m 
    k3p = dt *      (vel+0.5*k2v) 
    k3v = dt * Force(pos+0.5*k2p) / m 
    k4p = dt *      (vel+    k3v)
    k4v = dt * Force(pos+    k3p) / m
    pos = pos + (k1p+2*k2p+2*k3p+k4p)/6.0 
    vel = vel + (k1v+2*k2v+2*k3v+k4v)/6.0 

注意k 向量如何在位置和速度之间交错。


或者您可以使用自动执行正确的 RK4 实现

System = lambda pos, vel : vel, Force(pos)/m

然后在时间循环内

    k1p, k1v = System( pos           , vel           )
    k2p, k2v = System( pos+0.5*k1p*dt, vel+0.5*k1v*dt )
    k3p, k3v = System( pos+0.5*k2p*dt, vel+0.5*k2v*dt )
    k3p, k3v = System( pos+    k3p*dt, vel+    k3v*dt )
    pos = pos + (k1p+2*k2p+2*k3p+k4p)*dt/6.0 
    vel = vel + (k1v+2*k2v+2*k3v+k4v)*dt/6.0 

如果你只想要数值解,即不必证明你可以实现 RK4,那么使用scipy.integrate.odescipy.integrate.odeint

【讨论】:

  • 你导入了哪些模块?它提出了无效的语法 m*pos'' = Force(pos)
  • 那不是python,而是微分方程的ASCII渲染。 m·d²/dt² pos(t) = m·pos''(t) = F(pos(t)).
  • norm2numpy.linalg.norm。可以将norm2(pos)**3 替换为np.dot(pos,pos)**1.5。这避免了numpy.linalg.norm中的规范类型选择逻辑。
  • 我已经更正了 k1p 和 k1v 等。公式,但是我看不出新的 vel 是如何进入情节的
  • 因为你没有绘制速度,你不需要vel resp。 velocity 在情节中。它对位置的影响是在 RK4 步骤中的状态更新期间施加的。
猜你喜欢
  • 2016-02-29
  • 1970-01-01
  • 2017-10-21
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
相关资源
最近更新 更多