I´m trying to run a fourth order Runge Kutta in Mathematica but the thing is that I´m so so new in Mathematica that I am not even sure what I´m doing. I have these two coupled equations:
$$\frac{dy}{dx} = z$$ $$\frac{dz}{dx} = 6y - z$$
with these initial conditions: $$y(0) = 3$$ $$z(0) = 1$$
So, I was looking for the code and I found this one and I tried to modify it for this problem:
yinit=List[0, 3, 1]
y=List[x, y, z]
func=List[y'[x]/dx = z, z'[x]/dx = 6 y - z]
step = 0.1
t = 1
RungeKutta[func_List, yinit_List, y_List, step_] :=
Module[{k1, k2, k3, k4},
k1 = step N[func /. MapThread[Rule, {y, yinit}]];
k2 = step N[func /. MapThread[Rule, {y, k1/2 + yinit}]];
k3 = step N[func /. MapThread[Rule, {y, k2/2 + yinit}]];
k4 = step N[func /. MapThread[Rule, {y, k3 + yinit}]];
yinit + Total[{k1, 2 k2, 2 k3, k4}]/6]
NestList[RungeKutta[func, #, y, step] &, N[yinit], Round[t/step]]
but I get the next errors:
"Recursion depth of 1024 exceeded during evaluation of {x,y,z}."
Tag Hold in Hold $\frac{[y'[x]]}{dx}$ is protected
"Recursion depth of 1024 exceeded during evaluation of {x,y,z}" Further output of RecursionLimit::reclim2 will be suppressed during this calculation
Maybe I am doing many wrong thing but this noob would really appreciate your help.
yinit_List[0, 3, 1]
is supposed to do. Maybe it should beyinit=List[0, 3, 1]
. You can edit and correct your question. $\endgroup$List[y'[x]/dx = z, z'[x]/dx = 6 y - z]
does not make any sense. You might want to look up the difference between=
and==
$\endgroup$NDSolve
. $\endgroup$