forked from imiRemy/RK45js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
105 lines (83 loc) · 2.96 KB
/
index.html
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
<meta charset="utf-8">
<head>
<script src="./dist/rk45-min.js"></script>
</head>
<body>
<script>
// ---------------------------------------------------------------------
// Sample use of the RK45js code. In this case solving an ODE (Ordinary
// Differential Equation) of the form:
//
// dy/dt = y; y(0) = 1
//
// Find the value of y(t).
//
// Note: in the code you will not see a reference to a variable 'y'.
// Rather, you will see a reference to X0. The solver uses
// generalized coordinates (x0, x1, x2, ..., xN) rather than
// referring to 'x', 'y', 'z', etc.
//
// ------------------------------------------
// Set up equation(s) and initial conditions.
// ------------------------------------------
var diffEqX0 = function( time, x ) { return x[0]; }
var initCoord = [ 1 ];
// -------------------------
// Create a new RK45 solver.
// -------------------------
var foo = new rk45();
foo.setStart( 0.0 ); // Initial start time, t=0.
foo.setStop( 1.0 ); // Time at which we want a solution, t=1.
foo.setInitX( initCoord.slice() ); // y(0) -- value of y when t=0.
foo.setFn( [diffEqX0] ); // Differential equation we're solving.
// ---------------------------------------------------------
// Call the solver function: foo.solve().
// Bracket with calls hrtime calls to see how long it takes.
// ---------------------------------------------------------
var hrstart = performance.now();
foo.solve();
var hrend = performance.now();
// ---------------------------------------
// Check status of result. Should be o.k.
// ---------------------------------------
var status = foo.getStatus();
console.log("status: \n\tsuccess: %s\n\tstate: %s\n\tmessage: %s", status.success, status.state, status.message);
// -------------
// Print result.
// -------------
console.log("result: " + foo.newX);
console.log("Computed in %d iterations taking %d ms", foo.count, hrend - hrstart);
// ---------------------------------------
// Find result for an array of time points
// ---------------------------------------
var bar = new rk45();
bar.setInitX( initCoord.slice() );
bar.setFn( [diffEqX0] ); // Differential equation we're solving.
var times = [0,1,2,3,4];
var y = bar.solveTimes(times);
for(let i=0; i<times.length; ++i)
console.log("t =", times[i], "computed = "+ y[i], "exact =", Math.exp(times[i]));
// ---------------------
// Integrate iteratively
// ---------------------
var foobar = new rk45();
foobar.setInitX( initCoord.slice() );
foobar.setFn( [diffEqX0] ); // Differential equation we're solving.
foobar.setStart( 0.0 ); // Initial start time, t=0.
var t = 0;
var tmax = 4;
var dt = 1;
while (t < tmax)
{
// advance time
t = t+dt;
foobar.setStop(t);
// solve up to this time
foobar.solve();
// return result
console.log("t =", t, "y =", foobar.newX.slice());
// tell the integrator to adopt the current state as new initial state
foobar.adoptCurrentState();
}
</script>
</body>