gpt4 book ai didi

python - 了解自适应龙格库塔积分器的局部截断误差

转载 作者:行者123 更新时间:2023-12-02 19:29:57 26 4
gpt4 key购买 nike

我正在实现一个 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)

因此,这段代码可以工作,只要它返回我给它的任何函数的积分的近似值。然而,局部截断误差似乎太大了。运行上面的代码将输出:

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 值是四阶方法和五阶方法之间的差异。我的印象是,n^th 阶方法的局部截断错误为 O(dt^(n+1)),这意味着上面的集成应该有 1e-9 左右的错误,而不是 1e-6

那么到底是我的代码错误还是我的理解错误?谢谢!

最佳答案

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

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

Delta 因子应该稍微缓和一些,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

或者稍微长一点的时间间隔来查看步长 Controller 的实际工作情况

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}")

关于python - 了解自适应龙格库塔积分器的局部截断误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61978904/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com