1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.jstats.ode.integrators;
11
12 import nl.tudelft.simulation.jstats.ode.DifferentialEquationInterface;
13
14 /***
15 * The RungeKutta4 numerical integrator.
16 * <p>
17 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
18 * University of Technology </a>, the Netherlands. <br>
19 * See for project information <a
20 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
21 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
22 * License (GPL) </a>, no warranty <br>
23 *
24 * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
25 * Jacobs </a>
26 * @version 1.2 Apr 20, 2004
27 * @since 1.4
28 */
29 public class RungeKutta4 extends NumericalIntegrator
30 {
31 /***
32 * constructs a new RungeKutta4
33 *
34 * @param timeStep the timeStep
35 * @param equation the differentialEquation
36 */
37 public RungeKutta4(final double timeStep,
38 final DifferentialEquationInterface equation)
39 {
40 super(timeStep, equation);
41 }
42
43 /***
44 * @see nl.tudelft.simulation.jstats.ode.integrators.NumericalIntegrator
45 * #next(double,double[])
46 */
47 public double[] next(final double x, final double[] y)
48 {
49 double[] k1 = this.equation.dy(x, y);
50 double[] k2 = this.equation.dy(x + 0.5 * this.timeStep, super.add(y,
51 super.multiply(0.5 * this.timeStep, k1)));
52 double[] k3 = this.equation.dy(x + 0.5 * this.timeStep, super.add(y,
53 super.multiply(0.5 * this.timeStep, k2)));
54 double[] k4 = this.equation.dy(x + this.timeStep, super.add(y, super
55 .multiply(this.timeStep, k3)));
56 double[] sum = super.add(k1, super.multiply(2.0, k2), super.multiply(
57 2.0, k3), k4);
58 return super.add(y, super.multiply(this.timeStep / 6.0, sum));
59 }
60 }