1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.ode;
19
20 import org.apache.commons.math.ode.SwitchingFunction;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 class TestProblem4
38 extends TestProblemAbstract {
39
40
41 private double a;
42
43
44 private double[] y;
45
46
47 public TestProblem4() {
48 super();
49 a = 1.2;
50 double[] y0 = { Math.sin(a), Math.cos(a) };
51 setInitialConditions(0.0, y0);
52 setFinalConditions(15);
53 double[] errorScale = { 1.0, 0.0 };
54 setErrorScale(errorScale);
55 y = new double[y0.length];
56 }
57
58
59
60
61
62 public TestProblem4(TestProblem4 problem) {
63 super(problem);
64 a = problem.a;
65 y = (double[]) problem.y.clone();
66 }
67
68
69
70
71
72 public Object clone() {
73 return new TestProblem4(this);
74 }
75
76 public SwitchingFunction[] getSwitchingFunctions() {
77 return new SwitchingFunction[] { new Bounce(), new Stop() };
78 }
79
80 public void doComputeDerivatives(double t, double[] y, double[] yDot) {
81 yDot[0] = y[1];
82 yDot[1] = -y[0];
83 }
84
85 public double[] computeTheoreticalState(double t) {
86 double sin = Math.sin(t + a);
87 double cos = Math.cos(t + a);
88 y[0] = Math.abs(sin);
89 y[1] = (sin >= 0) ? cos : -cos;
90 return y;
91 }
92
93 private static class Bounce implements SwitchingFunction {
94
95 private static final long serialVersionUID = 1356097180027801200L;
96 private int sign;
97
98 public Bounce() {
99 sign = +1;
100 }
101
102 public double g(double t, double[] y) {
103 return sign * y[0];
104 }
105
106 public int eventOccurred(double t, double[] y) {
107
108 sign = -sign;
109 return SwitchingFunction.RESET_STATE;
110 }
111
112 public void resetState(double t, double[] y) {
113 y[1] = -y[1];
114 }
115
116 }
117
118 private static class Stop implements SwitchingFunction {
119
120 private static final long serialVersionUID = 6975050568227951931L;
121
122 public Stop() {
123 }
124
125 public double g(double t, double[] y) {
126 return t - 12.0;
127 }
128
129 public int eventOccurred(double t, double[] y) {
130 return SwitchingFunction.STOP;
131 }
132
133 public void resetState(double t, double[] y) {
134 }
135
136 }
137
138 }