了解自适应Runge Kutta积分器的局部截断误差

问题描述 投票:0回答:1

我正在实现RKF4(5)集成器,但是我无法弄清楚我的代码是否有效,并且我不了解本地截断错误,或者我的代码是否无效。

我为代码块的大小表示歉意,但在这种情况下,最小的可重现示例相当大。

import numpy as np

def RKF45(state, derivative_function, h):
    """
    Calculate the next state with the 4th-order calculation, along with a 5th-order error
    check.

    Inputs:
    state: the current value of the function, float
    derivative_function: A function which takes a state (given as a float)
                         and returns the derivative of a function at that point
    h: step size, float
    """
    k1 = h * derivative_function(state)
    k2 = h * derivative_function(state + (k1 / 4))
    k3 = h * derivative_function(state + (k1 * (3/32)) + (k2 * (9/32)))
    k4 = h * derivative_function(state + (k1 * (1932/2197)) + (k2 * (-7200/2197)) + (k3 * (7296/2197)))
    k5 = h * derivative_function(state + (k1 * (439/216)) + (k2 * (-8)) + (k3 * (3680/513)) + (k4 * (-845/4104)))
    k6 = h * derivative_function(state + (k1 * (-8/27)) + (k2 * (2)) + (k3 * (-3544/2565)) + (k4 * (1859/4104)) + (k5 * (-11/40)))
    y1 = state + ((25/216) * k1) + ((1408/2565) * k3) + ((2197/4101) * k4) - ((1/5)*k5)
    y2 = state + ((16/135) * k1) + ((6656/12825) * k3) + ((28561/56430) * k4) - ((9/50) * k5) + ((2/55) * k6)
    return(y1, y2)


def integrate_RKF45(t0, tmax, tol, h_init, x_0, df, verbose = False):
    """
    integrate a function whose derivative is df from t0 to tmax
    t0: starting time
    tmax: end time
    h_init: initial timestep
    x_0: starting position
    df: a function which takes x and returns the derivative of a function at x
    """
    h = h_init
    x_i = x_0
    t = t0
    while t < tmax:
        h = min(h, tmax - t)
        y1, y2 = RKF45(x_i, df, h)
        err_i = np.abs(y1 - y2)
        R = 2 * err_i / h
        delta = (tol/R)**(1/4)
        if verbose:
            print(f"t: {t:0.2e}, dt: {h:0.2e}, x: {x_i:0.2e}, err: {err_i:0.2e}")
        if err_i < tol:
            t += h
            x_i = y1
        elif err_i > tol:        
            h *= delta
    return(x_i)


def exponential(x_0, k=1):
    """
    A simple test function, this returns the input, so it'll integrate to e^x.
    """
    return(k * x_0)

if __name__ == "__main__":
    integrate_RKF45(t0 = 0., 
                    tmax = 0.15,
                    tol = 1e-4, 
                    h_init = 1e-2, 
                    x_0 = 1.,
                    df = exponential,
                    verbose=True)

因此,此代码works在一定程度上返回了我提供的任何函数的积分的近似值。但是,本地截断错误似乎太大。运行上面的代码将输出:

t: 0.00e+00, dt: 1.00e-02, x: 1.00e+00, err: 3.95e-06
t: 1.00e-02, dt: 1.00e-02, x: 1.01e+00, err: 3.99e-06
t: 2.00e-02, dt: 1.00e-02, x: 1.02e+00, err: 4.03e-06
t: 3.00e-02, dt: 1.00e-02, x: 1.03e+00, err: 4.07e-06
t: 4.00e-02, dt: 1.00e-02, x: 1.04e+00, err: 4.11e-06
t: 5.00e-02, dt: 1.00e-02, x: 1.05e+00, err: 4.16e-06
t: 6.00e-02, dt: 1.00e-02, x: 1.06e+00, err: 4.20e-06
t: 7.00e-02, dt: 1.00e-02, x: 1.07e+00, err: 4.24e-06
t: 8.00e-02, dt: 1.00e-02, x: 1.08e+00, err: 4.28e-06
t: 9.00e-02, dt: 1.00e-02, x: 1.09e+00, err: 4.32e-06
t: 1.00e-01, dt: 1.00e-02, x: 1.11e+00, err: 4.37e-06
t: 1.10e-01, dt: 1.00e-02, x: 1.12e+00, err: 4.41e-06
t: 1.20e-01, dt: 1.00e-02, x: 1.13e+00, err: 4.46e-06
t: 1.30e-01, dt: 1.00e-02, x: 1.14e+00, err: 4.50e-06
t: 1.40e-01, dt: 1.00e-02, x: 1.15e+00, err: 4.55e-06

err值是4阶和5阶方法之间的差。我的印象是,n^th阶方法的局部截断误差为O(dt^(n+1))阶,这意味着上述积分的误差应该在1e-9左右,而不是1e-6

所以我的代码错误还是我的理解错误?谢谢!

python physics numerical-methods numerical-integration runge-kutta
1个回答
1
投票

请参见https://math.stackexchange.com/questions/2701385/adaptive-step-size-in-rk45-for-second-order-ode/2701678#2701678,您似乎对方法系数使用了相同的损坏源。

y1中的分母4101是错误的,必须为4104。

增量因子应该稍微减轻一点,delta = (tol/R)**(1/5)delta = (tol/R)**(1/6),并在每个步骤中都应用,也要在成功的步骤中应用。

本地错误err_i的参考错误为tol*h,这就是为什么在R中用h除以​​的原因。

这将导致迭代步骤

t: 0.000000e+00, dt: 1.00e-02, x: 1.000000e+00, err: 1.28e-13
t: 1.000000e-02, dt: 1.40e-01, x: 1.010050e+00, err: 6.60e-08
t: 1.500000e-01, dt: 3.88e-01, x: 1.161834e+00

或间隔较长时间

t: 0.000000e+00, dt: 1.00e-02, x: 1.000000e+00, err: 1.28e-13
t: 1.000000e-02, dt: 2.27e-01, x: 1.010050e+00, err: 7.18e-07
t: 2.372490e-01, dt: 4.31e-01, x: 1.267757e+00, err: 2.02e-05
t: 6.680839e-01, dt: 4.76e-01, x: 1.950509e+00, err: 5.03e-05
t: 6.680839e-01, dt: 4.47e-01, x: 1.950509e+00, err: 3.73e-05
t: 1.115525e+00, dt: 3.84e-01, x: 3.051213e+00, err: 2.81e-05
t: 1.500000e+00, dt: 3.89e-01, x: 4.481769e+00

所有提到的更正均在RKF45中给出了新的循环

    while t < tmax:
        h = min(h, tmax - t)
        y1, y2 = RKF45(x_i, df, h)
        err_i = np.abs(y1 - y2)
        R = err_i / h
        delta = 0.95*(tol/R)**(1/5)
        if verbose:
            print(f"t: {t:0.6e}, dt: {h:0.2e}, x: {x_i:0.6e}, err: {err_i:0.2e}")
        if R < tol:
            t += h
            x_i = y1
        h *= delta
    if verbose:
        print(f"t: {t:0.6e}, dt: {h:0.2e}, x: {x_i:0.6e}")
© www.soinside.com 2019 - 2024. All rights reserved.