Runge-Kutta. Solving initial value problem that isn't easily seperated
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.ara开发者_C百科nge(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()
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 back y
such that exp(-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 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.?
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
精彩评论