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