# Runge Kutta fourth order [closed]

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.

• Would either of these and examples therein help? ExplicitRungeKutta and [ImplicitRungeKutta] (reference.wolfram.com/language/tutorial/…) – dearN Sep 18 '18 at 22:17
• It seems you are confused about very basic syntax here. At least I do not understand, what yinit_List[0, 3, 1] is supposed to do. Maybe it should be yinit=List[0, 3, 1]. You can edit and correct your question. – Johu Sep 18 '18 at 22:24
• Sorry @Johu, is supposed that yinit are the initial conditions, func is the list of the functions and y are the variables. – Mohammet Sep 18 '18 at 22:30
• Also, I suspect 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 == – Johu Sep 18 '18 at 22:39
• If you just want to solve the differential equations numerically rather than implement your own Runge-Kutta, try NDSolve. – Chris K Sep 19 '18 at 13:41

system = {y'[x] == z[x], z'[x] == 6 y[x] - z[x], y[0] == 3, z[0] == 1}

1. In the second line of the code y=List[x, y, z] you use the variable y in its own definition, and this is what creates the recursion error.
2. In the third line func=List[y'[x]/dx = z, z'[x]/dx = 6 y - z], what are y'[x]/dx and z'[x]/dx exactly? To denote the derivatives you either type y'[x] and z'[x], or type D[y[x],x] and D[z[x],x]. And if funcis a list of equations (and not definitions), then you need to use the double equal sign == instead of a single equal =.