
B.1 ODE SOLVERS
w[0] = x0;
t[0] = t0;
for(n=0;n<=N;n++){
t[n+1] = t[n]+h;
k1 = h*f(t[n],w[n]);
k2 = h*f(t[n]+h/2,w[n]+k1/2);
k3 = h*f(t[n]+h/2,w[n]+k2/2);
k4 = h*f(t[n+1],w[n]+k3);
w[n+1] = w[n]+(k1 + 2*k2 + 2*k3 + k4)/6;
}
To run a simulation of a three-dimensional system such as the Lorenz at-
tractor, it is necessary to implement Runge-Kutta for a system of three differential
equations. This involves using the above formulas for each of the three variables
in parallel. A possible implementation:
wx[0] = x0;
wy[0] = y0;
wz[0] = z0;
t[0] = t0;
for(n=0;n<=N;n++){
t[n+1] = t[n]+h;
kx1 = h*fx(t[n],wx[n],wy[n],wz[n]);
ky1 = h*fy(t[n],wx[n],wy[n],wz[n]);
kz1 = h*fz(t[n],wx[n],wy[n],wz[n]);
kx2 = h*fx(t[n]+h/2,wx[n]+kx1/2,wy[n]+ky1/2,wz[n]+kz1/2);
ky2 = h*fy(t[n]+h/2,wx[n]+kx1/2,wy[n]+ky1/2,wz[n]+kz1/2);
kz2 = h*fz(t[n]+h/2,wx[n]+kx1/2,wy[n]+ky1/2,wz[n]+kz1/2);
kx3 = h*fx(t[n]+h/2,wx[n]+kx2/2,wy[n]+ky2/2,wz[n]+kz2/2);
ky3 = h*fy(t[n]+h/2,wx[n]+kx2/2,wy[n]+ky2/2,wz[n]+kz2/2);
kz3 = h*fz(t[n]+h/2,wx[n]+kx2/2,wy[n]+ky2/2,wz[n]+kz2/2);
kx4 = h*fx(t[n+1],wx[n]+kx3,wy[n]+ky3,wz[n]+kz3);
ky4 = h*fy(t[n+1],wx[n]+kx3,wy[n]+ky3,wz[n]+kz3);
kz4 = h*fz(t[n+1],wx[n]+kx3,wy[n]+ky3,wz[n]+kz3);
wx[n+1] = wx[n]+(kx1 + 2*kx2 + 2*kx3 + kx4)/6;
wy[n+1] = wy[n]+(ky1 + 2*ky2 + 2*ky3 + ky4)/6;
wz[n+1] = wz[n]+(kz1 + 2*kz2 + 2*kz3 + kz4)/6;
}
Here it is necessary to define one function for each of the differential equations:
fx(t,x,y,z) = 10.0*(y-x);
fy(t,x,y,z) = -x*z + 28*x - y;
fz(t,x,y,z) = x*y - (8/3)*z;
569