-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-use.ts
More file actions
44 lines (36 loc) · 1.24 KB
/
basic-use.ts
File metadata and controls
44 lines (36 loc) · 1.24 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
/** Example. Solve the following initial value problem
dx / dt = x + y - t
dy / dt = x * y + t
x(0) = 1
y(0) = -1
on [0, 2] with the step 0.01.
*/
import {ODEs, mrt} from '../../index';
// Declare the problem
const task: ODEs = {
name: 'Example', // name of your model
arg: {
name: 't', // name of the argument
start: 0, // initial value of the argument
finish: 2, // final value of the argument
step: 0.01, // solution grid step
},
initial: [1, -1], // initial values
func: (t: number, y: Float64Array, output: Float64Array) => { // right-hand side of the system
output[0] = y[0] + y[1] - t; // 1-st equation
output[1] = y[0] * y[1] + t; // 2-nd equation
},
tolerance: 1e-7, // tolerance
solutionColNames: ['x', 'y'], // names of solution functions
};
try {
// Solve the problem
const solution = mrt(task);
// Output results
console.log(task.arg.name, ' ', task.solutionColNames[0], ' ', task.solutionColNames[1]);
const length = solution[0].length;
for (let i = 0; i < length; ++i)
console.log(solution[0][i], ' ', solution[1][i], ' ', solution[2][i]);
} catch (err) {
console.log('Solver failed: ', err instanceof Error ? err.message : 'Unknown problem!');
}