# Runge Kutta 4 ODE

4 views (last 30 days)
valeria herrera on 28 May 2021
Answered: Jesús Zambrano on 28 May 2021
So im tryign to solve this sistem of ODE, but my code is giving the same error and i dont know how to fix it. Could you please help me
t = 0;
h = 1;
x = 4.97;
y = -0.0069;
z = 0;
a = 0.583;
b = -52;
c = -0.308;
d = -0.0505;
e = 0.0009;
f = -1;
E1 = (a*x)-(b*(x.*y))-(c*z);
E2 = -d*y + e*(x.*y);
E3 = f*z;
for n =1:(length(x)-1)
m1 = (E1);
k1 = (E2);
j1 = (E3);
m2 = h*(t(n)+h/2, x(n)+m1/2, y(n)+k1/2, z(n)+j1/2);
k2 = h*(t(n)+h/2, x(n)+m1/2, y(n)+k1/2, z(n)+j1/2);
j2 = h*(t(n)+h/2, x(n)+m1/2, y(n)+k1/2, z(n)+j1/2);
m3 = h*(t(n)+h/2, x(n)+m2/2, y(n)+k2/2, z(n)+j2/2);
k3 = h*(t(n)+h/2, x(n)+m2/2, y(n)+k2/2, z(n)+j2/2);
j3 = h*(t(n)+h/2, x(n)+m2/2, y(n)+k2/2, z(n)+j2/2);
m4 = h*(t(n)+h, x(n)+m3, y(n)+k3, z(n)+j3);
k4 = h*(t(n)+h, x(n)+m3, y(n)+k3, z(n)+j3);
j4 = h*(t(n)+h, x(n)+m3, y(n)+k3, z(n)+j3);
t(n+1) = t(n) + h;
x(n+1) = x(n) + (1/6) * (m1 + (2*m2) + (2*m3) + m4);
y(n+1) = y(n) + (1/6) * (k1 + (2*k2) + (2*k3) + k4);
z(n+1) = z(n) + (1/6) * (j1 + (2*j2) + (2*j3) + j4);
end
plot(t(n+1),x(n+1))
plot(t(n+1),y(n+1))
plot(t(n+1),z(n+1))

Jesús Zambrano on 28 May 2021
Hi Valeria,
I don't see any system of ODEs (ordinary differntial equations) in equations 4 to 6.
For solving ODEs you can follow an example detailed in this link to the documentation:
Hope it helps!