龙格-库塔。解决不易分离的初值问题
我们应该编写一个程序来使用四阶龙格库塔数值求解以下初始值问题。该算法不是问题,完成后我可以发布我的解决方案。
问题是,将其干净地分离成可以放入龙格库塔的东西。
e^(-x') = x' −x + exp(−t^3)
x(t=0) = 1
你知道这叫什么类型的 ODE 吗?或者解决这个问题的方法?我对计算机科学技能和编程数值方法比对数学更有信心……所以任何对这个问题的见解都会有所帮助。
更新:如果有人对解决方案感兴趣,代码如下。我认为这是一个有趣的问题。
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()
We are supposed to write a program to solve the following initial value problem numerically using 4th order Runge-Kutta. That algorithm isn't a problem and I can post my solution when I finish.
The problem is, separating it out cleanly into something I can put into Runge-Kutta.
e^(-x') = x' −x + exp(−t^3)
x(t=0) = 1
Any ideas what type of ODE this is called? or methods to solve this? I feel more confident with CS skills and programming numerical methods than I do in math... so any insights into this problem would be helpful.
Update: If anyone is interested in the solution the code is below. I thought it was an interesting problem.
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于龙格库塔,您只需要一个数值解,而不是解析解。
也就是说,您需要能够编写一段接受
(x, t)
并返回y
的代码,使得exp(-y) = = y - x + exp(-t**3)
到舍入误差以内。该代码可以执行某种迭代近似算法,龙格库塔会非常高兴。这有帮助吗?
For Runge-Kutta you only need a numerical solution, not an analytical one.
That is, you need to be able to write a piece of code that takes
(x, t)
and gives backy
such thatexp(-y) == y - x + exp(-t**3)
to within round-off error. That code can do some sort of iterative approximation algorithm, and Runge-Kutta will be perfectly happy.Does that help?
Wolfram Alpha 表示解决方案将类似于 这个。
我发现在开始之前了解答案会有所帮助。
了解像 Wolfram Alpha 这样的资源随时可供您使用也很有帮助。
PS - 在互联网、Wolfram Alpha、谷歌、维基百科等时代,成为一名学生或教授意味着什么?
Wolfram Alpha says the solution will look like this.
I find that it helps to have an idea of what the answer is before I start.
It also helps to know that a resource like Wolfram Alpha is available to you at all times.
PS - What does it mean to be a student or professor in a time of Internet, Wolfram Alpha, Google, Wikipedia, etc.?
将 K 写成 x - exp(-t^3),我们要求解 exp(-y) = y - K;我得到 y = K + W(exp(-K)) 其中 W 是兰伯特的 W 函数,例如此处
Writing K for x - exp( -t^3), we want to solve exp(-y) = y - K; I get y = K + W(exp(-K)) where W is Lambert's W function, eg here