I have been asked to reduce the KdV ODE Equation
$d^3/dX^3=(c+6f)df/dX $
to a first order ODE and then solve this ODE by RK4 (Runge-Kutta 4) Numerical Integration.
Analytically, letting c=1 for simplicity, we integrate to get
$f''-3f^2-f=C$
Where $C$ is integration constant. Then Multiply by $f'$ and Integrate to get
$(f')^2/2 -f^3 -f^2/2=Cf +D$
Where $D$ is another integration constant. Now Solve for $f'$ we get
$df/dX=\sqrt(2f^3+f^2+2Cf+2D)$
I have been given initial conditions: $f(0)=-1/2$, $df/dX=0$, and $df^2/dX^2=1/4$
I have been told to:
Use RK4 to integrate numerically the first-order reduction of the ODE from $X = 0$ forward $X_n = n * h, n = 0, 1, . . .N$ and for sufficiently large $X$ (i.e. large $N$). You will need to be plotting $f$ vs $X$ to see if the solution is settling to determine when to stop the integration. Use your judgement as to how small a step size you need to solve this system accurately. If you cannot figure this out from pure thought, experiment with different step sizes and use $δC = |(C(t) − C(t = 0))/C(t = 0)|$ to determine the accuracy. If $δC$ is smaller than $10^{−3}$ for all integration times, then you have acceptable accuracy. Show in a plot your solution for $f(X)$, for $X \in [−20, 20]$.
Here is my Code:
ti = -\[Pi];
tf = \[Pi];
i = 256;
\[CapitalDelta]t = (tf - ti)/i;
X = Range[ti, tf, \[CapitalDelta]t] // N;
F = ConstantArray[0., i + 1];
D1 = ConstantArray[0., i + 1];
D2 = ConstantArray[0., i + 1];
(*Runge Kutta of Order 4*)
F[[1]] = -1/2;
D1[[1]] = 0.;
D2[[1]] = 1/4;
Do[
k1 = \[CapitalDelta]t*\[Sqrt]Abs[(2 F[[n]]^3 + F[[n]]^2 +
2*(D2[[n]] - 3 F[[n]]^2 - F[[n]])* F[[n]] +
2*(1/2 D1[[n]]^2 - F[[n]]^3 - 1/2 F[[n]]^2 - D2[[n]] -
3 F[[n]]^2 - F[[n]]* F[[n]]))];
k2 = \[Sqrt]Abs[(2 F[[n]]^3 + F[[n]]^2 +
2*(D2[[n]] - 3 F[[n]]^2 - F[[n]])* F[[n]] +
2*(1/2 D1[[n]]^2 - F[[n]]^3 - 1/2 F[[n]]^2 - D2[[n]] -
3 F[[n]]^2 - F[[n]]* F[[n]]))] + \[CapitalDelta]t*k1/2;
k3 = \[Sqrt]Abs[(2 F[[n]]^3 + F[[n]]^2 +
2*(D2[[n]] - 3 F[[n]]^2 - F[[n]])* F[[n]] +
2*(1/2 D1[[n]]^2 - F[[n]]^3 - 1/2 F[[n]]^2 - D2[[n]] -
3 F[[n]]^2 - F[[n]]* F[[n]]))] + \[CapitalDelta]t*k2/2;
k4 = \[Sqrt]Abs[(2 F[[n]]^3 + F[[n]]^2 +
2*(D2[[n]] - 3 F[[n]]^2 - F[[n]])* F[[n]] +
2*(1/2 D1[[n]]^2 - F[[n]]^3 - 1/2 F[[n]]^2 - D2[[n]] -
3 F[[n]]^2 - F[[n]]* F[[n]]))] + \[CapitalDelta]t*k3/2;
D1[[n + 1]] = D1[[n]] + 1/6*\:f000k1 + 2 k2 + 2 k3 + k4\:f006,
D2[[n + 1]] = D2[[n]] + 1/6*\:f000k1 + 2 k2 + 2 k3 + k4\:f006,
F[[n + 1]] = F[[n]] + 1/6*\:f000k1 + 2 k2 + 2 k3 + k4\:f006,
{n, 1, i}]
So I tried to use 3 vectors with 3 initial values given, and iterate until all the values are filled. but I don't know how to compute the next values of D1 and D2, based on the initial values.
On the other hand, apparently there must be a function with two variable inputs, where it returns a only one F back. I found this online:
Y, T, h = initialize_all(y0, t0, t1, n)
for i in xrange(1, n):
K1 = f(T[i-1], Y[i-1])
tplus = (T[i] + T[i-1]) * .5
K2 = f(tplus, Y[i-1] + .5 * h * K1)
K3 = f(tplus, Y[i-1] + .5 * h * K2)
K4 = f(T[i], Y[i-1] + h * K3)
Y[i] = Y[i-1] + (h / 6.) * (K1 + 2 * K2 + 2 * K3 + K4)
return T, Y
Which is in another language. but clearly my code should follow this pattern. I have tried hard to solve it, but couldn't figure it out. If anyone can help me, I really appreciate their help.
Thanks