forked from Apress/numerical-methods-using-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter7.java
More file actions
378 lines (325 loc) · 13.7 KB
/
Chapter7.java
File metadata and controls
378 lines (325 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Copyright (c) NM LTD.
* https://nm.dev/
*
* THIS SOFTWARE IS LICENSED, NOT SOLD.
*
* YOU MAY USE THIS SOFTWARE ONLY AS DESCRIBED IN THE LICENSE.
* IF YOU ARE NOT AWARE OF AND/OR DO NOT AGREE TO THE TERMS OF THE LICENSE,
* DO NOT USE THIS SOFTWARE.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITH NO WARRANTY WHATSOEVER,
* EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
* ANY WARRANTIES OF ACCURACY, ACCESSIBILITY, COMPLETENESS,
* FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, NON-INFRINGEMENT,
* TITLE AND USEFULNESS.
*
* IN NO EVENT AND UNDER NO LEGAL THEORY,
* WHETHER IN ACTION, CONTRACT, NEGLIGENCE, TORT, OR OTHERWISE,
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIMS, DAMAGES OR OTHER LIABILITIES,
* ARISING AS A RESULT OF USING OR OTHER DEALINGS IN THE SOFTWARE.
*/
package dev.nm.nmj;
import dev.nm.algebra.linear.vector.doubles.Vector;
import dev.nm.algebra.linear.vector.doubles.dense.DenseVector;
import dev.nm.analysis.differentialequation.ode.ivp.problem.DerivativeFunction;
import dev.nm.analysis.differentialequation.ode.ivp.problem.ODE1stOrder;
import dev.nm.analysis.differentialequation.ode.ivp.solver.EulerMethod;
import dev.nm.analysis.differentialequation.ode.ivp.solver.ODESolution;
import dev.nm.analysis.differentialequation.ode.ivp.solver.ODESolver;
import dev.nm.analysis.differentialequation.ode.ivp.solver.multistep.adamsbashforthmoulton.ABMPredictorCorrector1;
import dev.nm.analysis.differentialequation.ode.ivp.solver.multistep.adamsbashforthmoulton.ABMPredictorCorrector2;
import dev.nm.analysis.differentialequation.ode.ivp.solver.multistep.adamsbashforthmoulton.ABMPredictorCorrector3;
import dev.nm.analysis.differentialequation.ode.ivp.solver.multistep.adamsbashforthmoulton.ABMPredictorCorrector4;
import dev.nm.analysis.differentialequation.ode.ivp.solver.multistep.adamsbashforthmoulton.ABMPredictorCorrector5;
import dev.nm.analysis.differentialequation.ode.ivp.solver.multistep.adamsbashforthmoulton.AdamsBashforthMoulton;
import dev.nm.analysis.differentialequation.ode.ivp.solver.rungekutta.RungeKutta;
import dev.nm.analysis.differentialequation.ode.ivp.solver.rungekutta.RungeKutta1;
import dev.nm.analysis.differentialequation.ode.ivp.solver.rungekutta.RungeKutta2;
import dev.nm.analysis.differentialequation.ode.ivp.solver.rungekutta.RungeKutta3;
import dev.nm.analysis.differentialequation.ode.ivp.solver.rungekutta.RungeKuttaStepper;
import dev.nm.analysis.function.rn2r1.univariate.AbstractUnivariateRealFunction;
import dev.nm.analysis.function.rn2r1.univariate.UnivariateRealFunction;
import dev.nm.analysis.function.rn2rm.AbstractRealVectorFunction;
import dev.nm.analysis.function.rn2rm.RealVectorFunction;
import java.io.IOException;
import static java.lang.Math.exp;
/**
* Numerical Methods Using Java: For Data Science, Analysis, and Engineering
*
* @author haksunli
* @see
* https://www.amazon.com/Numerical-Methods-Using-Java-Engineering/dp/1484267966
* https://nm.dev/
*/
public class Chapter7 {
public static void main(String[] args) throws IOException {
System.out.println("Chapter 7 demos");
Chapter7 chapter7 = new Chapter7();
chapter7.EulerMethod();
chapter7.RungeKutta();
chapter7.ABM();
chapter7.system_of_ODEs();
chapter7.higher_order_ODEs();
}
private void higher_order_ODEs() {
System.out.println("solve a higher-order ODE using Euler's method");
// define the equivalent system of ODEs to solve
DerivativeFunction dY = new DerivativeFunction() {
@Override
public Vector evaluate(double t, Vector v) {
double y_1 = v.get(1);
double y_2 = v.get(2);
double dy_1 = y_2;
double dy_2 = y_2 + 6. * y_1;
return new DenseVector(new double[]{dy_1, dy_2});
}
@Override
public int dimension() {
return 2;
}
};
// initial condition, y1(0) = 1, y2(0) = 2
Vector Y0 = new DenseVector(1., 2.);
double t0 = 0, t1 = 1.; // solution domain
double h = 0.1; // step size
// the analytical solution
UnivariateRealFunction f
= new AbstractUnivariateRealFunction() {
@Override
public double evaluate(double t) {
double f = 0.8 * exp(3. * t);
f += 0.2 * exp(-2. * t);
return f;
}
};
// define an IVP
ODE1stOrder ivp = new ODE1stOrder(dY, Y0, t0, t1);
// construt an ODE solver using Euler's method
// ODESolver solver = new EulerMethod(h);
// construt an ODE solver using the third order Runge-Kutta formula
RungeKuttaStepper stepper3 = new RungeKutta3();
ODESolver solver = new RungeKutta(stepper3, h);
// solve the ODE
ODESolution soln = solver.solve(ivp);
// print out the solution function, y, at discrete points
double[] t = soln.x();
Vector[] v = soln.y();
for (int i = 0; i < t.length; ++i) {
double y1 = v[i].get(1); // the numerical solution
System.out.println(String.format(
"y(%f) = %f vs %f",
t[i],
y1,
f.evaluate(t[i])
));
}
}
private void system_of_ODEs() {
System.out.println("solve a system of ODEs using Euler's method");
// define the system of ODEs to solve
DerivativeFunction dY = new DerivativeFunction() {
@Override
public Vector evaluate(double t, Vector v) {
double x = v.get(1);
double y = v.get(2);
double dx = 3. * x - 4. * y;
double dy = 4. * x - 7. * y;
return new DenseVector(new double[]{dx, dy});
}
@Override
public int dimension() {
return 2;
}
};
// initial condition, x0=y0=1
Vector Y0 = new DenseVector(1., 1.);
double x0 = 0, x1 = 1.; // solution domain
double h = 0.1; // step size
// the analytical solution
RealVectorFunction F
= new AbstractRealVectorFunction(1, 2) {
@Override
public Vector evaluate(Vector v) {
double t = v.get(1);
double x = 2. / 3 * exp(t) + 1. / 3 * exp(-5. * t);
double y = 1. / 3 * exp(t) + 2. / 3 * exp(-5. * t);
return new DenseVector(x, y);
}
};
// define an IVP
ODE1stOrder ivp = new ODE1stOrder(dY, Y0, x0, x1);
// construt an ODE solver using Euler's method
ODESolver solver = new EulerMethod(h);
// solve the ODE
ODESolution soln = solver.solve(ivp);
// print out the solution function, y, at discrete points
double[] t = soln.x();
Vector[] v = soln.y();
for (int i = 0; i < t.length; ++i) {
System.out.println(String.format(
"y(%f) = %s vs %s",
t[i],
v[i],
F.evaluate(new DenseVector(t[i]))
));
}
}
private void ABM() {
System.out.println("solve an ODE using Adams-Bashforth methods");
// define the ODE to solve
DerivativeFunction dy = new DerivativeFunction() {
@Override
public Vector evaluate(double x, Vector v) {
double y = v.get(1);
double dy = y - x + 1;
return new DenseVector(dy);
}
@Override
public int dimension() {
return 1;
}
};
// initial condition, y0=1
Vector y0 = new DenseVector(1.);
double x0 = 0, x1 = 1.; // solution domain
double h = 0.1; // step size
// the analytical solution
UnivariateRealFunction y = new AbstractUnivariateRealFunction() {
@Override
public double evaluate(double x) {
double y = exp(x) + x;
return y;
}
};
// define an IVP
ODE1stOrder ivp = new ODE1stOrder(dy, y0, x0, x1);
// using first order Adams-Bashforth formula
ODESolver solver1 = new AdamsBashforthMoulton(new ABMPredictorCorrector1(), h);
ODESolution soln1 = solver1.solve(ivp);
// using second order Adams-Bashforth formula
ODESolver solver2 = new AdamsBashforthMoulton(new ABMPredictorCorrector2(), h);
ODESolution soln2 = solver2.solve(ivp);
// using third order Adams-Bashforth formula
ODESolver solver3 = new AdamsBashforthMoulton(new ABMPredictorCorrector3(), h);
ODESolution soln3 = solver3.solve(ivp);
// using forth order Adams-Bashforth formula
ODESolver solver4 = new AdamsBashforthMoulton(new ABMPredictorCorrector4(), h);
ODESolution soln4 = solver4.solve(ivp);
// using fifth order Adams-Bashforth formula
ODESolver solver5 = new AdamsBashforthMoulton(new ABMPredictorCorrector5(), h);
ODESolution soln5 = solver5.solve(ivp);
double[] x = soln1.x();
Vector[] y1 = soln1.y();
Vector[] y2 = soln2.y();
Vector[] y3 = soln3.y();
Vector[] y4 = soln4.y();
Vector[] y5 = soln5.y();
for (int i = 0; i < x.length; ++i) {
double yx = y.evaluate(x[i]); // the analytical solution
double diff1 = yx - y1[i].get(1); // the first order error
double diff2 = yx - y2[i].get(1); // the second order error
double diff3 = yx - y3[i].get(1); // the third order error
double diff4 = yx - y4[i].get(1); // the forth order error
double diff5 = yx - y5[i].get(1); // the fifth order error
System.out.println(
String.format("y(%f) = %s (%.16f); = %s (%.16f); = %s (%.16f); = %s (%.16f); = %s (%.16f)",
x[i], y1[i], diff1,
y2[i], diff2,
y3[i], diff3,
y4[i], diff4,
y5[i], diff5
));
}
}
private void RungeKutta() {
System.out.println("solve an ODE using Runge-Kutta methods");
// define the ODE to solve
DerivativeFunction dy = new DerivativeFunction() {
@Override
public Vector evaluate(double x, Vector v) {
double y = v.get(1);
double dy = y - x + 1;
return new DenseVector(dy);
}
@Override
public int dimension() {
return 1;
}
};
// initial condition, y0=1
Vector y0 = new DenseVector(1.);
double x0 = 0, x1 = 1.; // solution domain
double h = 0.1; // step size
// the analytical solution
UnivariateRealFunction y = new AbstractUnivariateRealFunction() {
@Override
public double evaluate(double x) {
double y = exp(x) + x;
return y;
}
};
// define an IVP
ODE1stOrder ivp = new ODE1stOrder(dy, y0, x0, x1);
// using first order Runge-Kutta formula
RungeKuttaStepper stepper1 = new RungeKutta1();
ODESolver solver1 = new RungeKutta(stepper1, h);
ODESolution soln1 = solver1.solve(ivp);
// using second order Runge-Kutta formula
RungeKuttaStepper stepper2 = new RungeKutta2();
ODESolver solver2 = new RungeKutta(stepper2, h);
ODESolution soln2 = solver2.solve(ivp);
// using third order Runge-Kutta formula
RungeKuttaStepper stepper3 = new RungeKutta3();
ODESolver solver3 = new RungeKutta(stepper3, h);
ODESolution soln3 = solver3.solve(ivp);
double[] x = soln1.x();
Vector[] y1 = soln1.y();
Vector[] y2 = soln2.y();
Vector[] y3 = soln3.y();
for (int i = 0; i < x.length; ++i) {
double yx = y.evaluate(x[i]); // the analytical solution
double diff1 = yx - y1[i].get(1); // the first order error
double diff2 = yx - y2[i].get(1); // the second order error
double diff3 = yx - y3[i].get(1); // the third order error
System.out.println(
String.format("y(%f) = %s (%.16f); = %s (%.16f); = %s (%.16f)",
x[i], y1[i], diff1,
y2[i], diff2,
y3[i], diff3
));
}
}
private void EulerMethod() {
System.out.println("solve an ODE using Euler's method");
// define the ODE to solve
DerivativeFunction dy = new DerivativeFunction() {
@Override
public Vector evaluate(double x, Vector y) {
Vector dy = y.scaled(-2. * x);
return dy.add(1); // y' = 1 - 2xy
}
@Override
public int dimension() {
return 1;
}
};
// initial condition, y0=0
Vector y0 = new DenseVector(0.);
double x0 = 0, x1 = 1.; // solution domain
double h = 0.1; // step size
// define an IVP
ODE1stOrder ivp = new ODE1stOrder(dy, y0, x0, x1);
// construt an ODE solver using Euler's method
ODESolver solver = new EulerMethod(h);
// solve the ODE
ODESolution soln = solver.solve(ivp);
// print out the solution function, y, at discrete points
double[] x = soln.x();
Vector[] y = soln.y();
for (int i = 0; i < x.length; ++i) {
System.out.println(String.format("y(%f) = %s", x[i], y[i]));
}
}
}