2

我们应该编写一个程序来使用 4 阶 Runge-Kutta 数值求解以下初始值问题。该算法不是问题,我可以在完成后发布我的解决方案。

问题是,将它干净地分离成我可以放入 Runge-Kutta 的东西。

e^(-x') = x' −x + exp(−t^3)
x(t=0) = 1

知道这叫什么类型的 ODE?或解决此问题的方法?与数学相比,我对 CS 技能和编程数值方法更有信心……所以对这个问题的任何见解都会有所帮助。

更新:如果有人对解决方案感兴趣,代码如下。我认为这是一个有趣的问题。

import numpy as np
import matplotlib.pyplot as plt

def Newton(fn, dfn, xp_guess, x, t, tolerance):
    iterations = 0
    value = 100.
    max_iter = 100
    xp = xp_guess
    value = fn(t, x, xp)
    while (abs(value) > tolerance and iterations < max_iter):
        xp = xp - (value / dfn(t,x,xp))
        value = fn(t,x,xp)
        iterations += 1
    root = xp
    return root

tolerance = 0.00001
x_init = 1.
tmin = 0.0
tmax = 4.0
t = tmin
n = 1
y = 0.0
xp_init = 0.5

def fn(t,x,xp):
    '''
    0 = x' - x + e^(-t^3) - e^(-x')
    '''
    return (xp - x + np.e**(-t**3.) - np.e**(-xp))

def dfn(t,x,xp):
    return 1 + np.e**(-xp)

i = 0
h = 0.0001
tarr = np.arange(tmin, tmax, h)
y = np.zeros((len(tarr)))
x = x_init
xp = xp_init
for t in tarr:
    # RK4 with values coming from Newton's method
    y[i] = x
    f1 = Newton(fn, dfn, xp, x, t, tolerance)
    K1 = h * f1
    f2 = Newton(fn, dfn, f1, x+0.5*K1, t+0.5*h, tolerance)
    K2 = h * f2
    f3 = Newton(fn, dfn, f2, x+0.5*K2, t+0.5*h, tolerance)
    K3 = h * f3
    f4 = Newton(fn, dfn, f3, x+K3, t+h, tolerance)
    K4 = h * f4
    x = x + (K1+2.*K2+2.*K3+K4)/6.
    xp = f4
    i += 1

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(tarr, y)
plt.show()
4

3 回答 3

4

对于 Runge-Kutta,您只需要数值解,而不需要解析解。

也就是说,您需要能够编写一段代码来接收(x, t)和返回yexp(-y) == y - x + exp(-t**3)以便在舍入误差范围内。该代码可以执行某种迭代逼近算法,Runge-Kutta 会非常高兴。

这有帮助吗?

于 2011-04-02T05:36:50.643 回答
2

Wolfram Alpha 说解决方案看起来像这样

我发现在开始之前了解答案是有帮助的。

了解像 Wolfram Alpha 这样的资源随时可供您使用也很有帮助。

PS - 在互联网、Wolfram Alpha、谷歌、维基百科等时代成为一名学生或教授意味着什么?

于 2011-04-02T17:57:53.803 回答
1

为 x - exp( -t^3) 写 K,我们要求解 exp(-y) = y - K;我得到 y = K + W(exp(-K)) 其中 W 是兰伯特的 W 函数,例如这里

于 2011-04-05T16:38:35.973 回答