ODESolverRK4


package ode1d;

/**
 * solver using classical 4th order Runge-Kutta method
 */
public class ODESolverRK4 extends ODESolver {
  
  /** sets ode and initial values */
  public ODESolverRK4(ODE ode) {
    super(ode);
  }

  /** computes solution at next time step<br>
   * using the classical 4th order Runke-Kutta scheme
   */
  public void nextStep(double h) {
    double k1 = ode.f(x, t);
    
    double t1 = t + h / 2.0;
    double x1 = x + h * k1 / 2.0;
    double k2 = ode.f(x1, t1);
    
    double t2 = t1;
    double x2 = x + h * k2 / 2.0;
    double k3 = ode.f(x2, t2);
    
    double t3 = t + h;
    double x3 = x + k3 * h;
    double k4 = ode.f(x3, t3);
    
    x += (k1 + 2.0 * k2 + 2.0 * k3 + k4) * h / 6.0;
    t += h;
  }
}

previous    contents     next

Peter Junglas 20.12.1999