ODESolverRK4
package odeadapt;
import Jama.*;
/**
* ODESolverRK4.java
* solves a given ODE by giving the "next" value for x
* using Runge-Kutta method of order 4
*/
public class ODESolverRK4 extends ODESingleStepSolver {
/**
* construct solver for a given ODE
*/
public ODESolverRK4(ODE ode) {
super(ode);
order = 4;
}
/*
* integrate until t + h
* using a fourth order Runge-Kutta scheme
*/
public int nextStep(double h) {
Matrix k1 = ode.f(x, t);
double t1 = t + h / 2.0;
// x1 = x + h*k1/2.0;
Matrix x1 = x.plus(k1.times(h / 2.0));
Matrix k2 = ode.f(x1, t1);
double t2 = t1;
// x2 = x + h*k2/2.0;
Matrix x2 = x.plus(k2.times(h / 2.0));
Matrix k3 = ode.f(x2, t2);
double t3 = t + h;
// x3 = x + k3*h;
Matrix x3 = x.plus(k3.times(h));
Matrix k4 = ode.f(x3, t3);
// x += (k1 + 2.0*k2 + 2.0*k3 + k4)*h/6.0;
x.plusEquals(k1.times(h / 6.0));
x.plusEquals(k2.times(h / 3.0));
x.plusEquals(k3.times(h / 3.0));
x.plusEquals(k4.times(h / 6.0));
t += h;
return 1;
}
}

Peter Junglas 20.12.1999