1
2
3
4
5
6
7
8
9
10
11 package nl.tudelft.simulation.jstats.ode.integrators;
12
13 import nl.tudelft.simulation.jstats.ode.DifferentialEquationInterface;
14
15 /***
16 * The RungeKuttaCashCarp.java numerical integrator.
17 * <p>
18 * (c) copyright 2004 <a href="http://www.simulation.tudelft.nl">Delft
19 * University of Technology </a>, the Netherlands. <br>
20 * See for project information <a
21 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
22 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
23 * License (GPL) </a>, no warranty <br>
24 *
25 * @author <a
26 * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
27 * Verbraeck </a>
28 * @version 1.0 May 12, 2004
29 * @since 1.4
30 */
31 public class RungeKuttaCashCarp extends NumericalIntegrator
32 {
33 /*** the parameters for a_i, in f(x_n + a_i h, .) */
34 protected static double[] a = new double[]{0d, 1d / 5d, 3d / 10d, 3d / 5d,
35 1d, 7d / 8d};
36
37 /*** the parameters for b_ij, in f(., y_n + b_p1 k1 + bp2 k2 + ...) */
38 protected static double[][] b = new double[][]{{0d, 0d, 0d, 0d, 0d},
39 {1d / 5d, 3d / 40d, 3d / 10d, -11d / 54d, 1631d / 55296d},
40 {0d, 9d / 40d, -9d / 10d, 5d / 2d, 175d / 212d},
41 {0d, 0d, 6d / 5d, -70d / 27d, 575d / 13824d},
42 {0d, 0d, 0d, 35 / 27d, 44275d / 110592d},
43 {0d, 0d, 0d, 0d, 253d / 4096d}};
44
45 /*** the parameters for c_i, in y_n+1 = y_n + c_1 k_1 + c_2 k_2 + ... */
46 protected static double[] c = new double[]{37d / 378d, 0d, 250d / 621d,
47 125d / 594d, 0d, 512d / 1771d};
48
49 /*** the parameters for c4_i, in y_n+1 = y_n + c4_1 k_1 + c4_2 k_2 + ... */
50 protected static double[] c4 = new double[]{2825d / 27648d, 0d,
51 18575d / 48384d, 13525d / 55296d, 277d / 14336d, 1d / 4d};
52
53 /*** the numer of k-s in the method */
54 protected static int nk = 6;
55
56 /***
57 * constructs a new RungeKuttaCashCarp
58 *
59 * @param timeStep the timeStep
60 * @param equation the differentialEquation
61 */
62 public RungeKuttaCashCarp(final double timeStep,
63 final DifferentialEquationInterface equation)
64 {
65 super(timeStep, equation);
66 }
67
68 /***
69 * @see nl.tudelft.simulation.jstats.ode.integrators.NumericalIntegrator
70 * #next(double,double[])
71 */
72 public double[] next(final double x, final double[] y)
73 {
74 double[][] k = new double[nk][];
75 for (int i = 0; i < nk; i++)
76 {
77 double[] ysum = (double[]) y.clone();
78 for (int j = 0; j < i; j++)
79 {
80 if (b[i][j] != 0.0)
81 {
82 ysum = this.add(ysum, this.multiply(b[i][j], k[j]));
83 }
84 }
85 k[i] = this.multiply(this.timeStep, this.equation.dy(x + a[i]
86 * this.timeStep, ysum));
87 }
88 double[] sum = (double[]) y.clone();
89 super.error = new double[y.length];
90 for (int i = 0; i < nk; i++)
91 {
92 sum = this.add(sum, this.multiply(c[i], k[i]));
93 super.error = this.add(super.error, this.multiply(c[i] - c4[i],
94 k[i]));
95 }
96 return sum;
97 }
98 }