本文介绍了RK4 Python解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将在网上找到的RK4的实现用于某些事情,但是我很难理解在网上找到的实现.例如:

I'd like to use an implementation of RK4 I found online for something, but I'm having a bit of difficulty wrapping my head around the implementations I have found online.For example:

def rk4(f, x0, y0, x1, n):
    vx = [0] * (n + 1)
    vy = [0] * (n + 1)
    h = (x1 - x0) / float(n)
    vx[0] = x = x0
    vy[0] = y = y0
    for i in range(1, n + 1):
        k1 = h * f(x, y)
        k2 = h * f(x + 0.5 * h, y + 0.5 * k1)
        k3 = h * f(x + 0.5 * h, y + 0.5 * k2)
        k4 = h * f(x + h, y + k3)
        vx[i] = x = x0 + i * h
        vy[i] = y = y + (k1 + k2 + k2 + k3 + k3 + k4) / 6
return vx, vy

有人可以帮我了解参数的确切含义吗?如果可能的话,我希望有一个更笼统的解释,但是,如果更加具体,则更易于解释,我将专门针对理想的弹簧系统使用它.

Could someone please help me understand what exactly the parameters are? If possible, I'd like a more general explanation, but, if being more specific makes it easier to explain, I'm going to be using it specifically for an ideal spring system.

推荐答案

您在此处询问参数:

def rk4(f, x0, y0, x1, n):
    ...
    return vx, vy

  • f 是ODE函数,对于微分方程 y'(x)= f(x,y)声明为 def f(x,y)(x))
  • (x0,y0)是初始点和值
  • x1 是积分间隔的结束时间 [x0,x1]
  • n 是子间隔或积分步骤的数量

    • f is the ODE function, declared as def f(x,y) for the differential equation y'(x)=f(x,y(x)),
    • (x0,y0) is the initial point and value,
    • x1 is the end of the integration interval [x0,x1]
    • n is the number of sub-intervals or integration steps

      vx,vx 是计算出的样本点, vy [k] 近似于 y(vx [k]).

      vx,vx are the computed sample points, vy[k] approximates y(vx[k]).

      不能将其用于spring系统,因为该代码仅适用于标量 v .您需要对其进行更改以与 numpy 一起进行矢量操作.

      You can not use this for the spring system, as that code only works for a scalar v. You would need to change it to work with numpy for vector operations.

      def rk4(func, x0, y0, x1, n):
          y0 = np.array(y0)
          f = lambda x,y: np.array(func(x,y))
          vx = [0] * (n + 1)
          vy = np.zeros( (n + 1,)+y0.shape)
          h = (x1 - x0) / float(n)
          vx[0] = x = x0
          vy[0] = y = y0[:]
          for i in range(1, n + 1):
              k1 = h * f(x, y)
              k2 = h * f(x + 0.5 * h, y + 0.5 * k1)
              k3 = h * f(x + 0.5 * h, y + 0.5 * k2)
              k4 = h * f(x + h, y + k3)
              vx[i] = x = x0 + i * h
              vy[i] = y = y + (k1 + 2*(k2 + k3) + k4) / 6
          return vx, vy
      

      这篇关于RK4 Python解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 15:26