-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·86 lines (79 loc) · 2.54 KB
/
main.js
File metadata and controls
executable file
·86 lines (79 loc) · 2.54 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
/**
* Right-hand side of our second-order ODE written as a simple of first-order
* ODEs.
*
* @param objectOfInputs Problem parameter.
* @param t Time (seconds).
* @param x x coordinate.
* @param dx dx/dt
* @return [dx/dt, d2x/dt2]
*/
function f(objectOfInputs, t, vars, dt) {
var {alpha, beta, gamma, delta, omega} = objectOfInputs;
var [x, dx] = vars;
return [dt*dx, dt*(- delta*dx - alpha*x - beta*x**3 + gamma * Math.cos(omega*t))];
}
/**
* Generate phase plot of x dot against x
*
* @param solution An object containing solution data.
* @return Nothing. Just generates the relevant plot.
*/
function generatePhasePlot(solution) {
// Extract solution data from solution object
var {vars} = solution;
var [x, dx] = vars;
// Generate plot
gen2DPlot(x, dx, "phasePlot", "Phase plot of dx/dt against x", "x", "dx/dt");
}
/**
* Generate plot of x and x dot against time
*
* @param solution An object containing solution data.
* @return Nothing. Just generates the relevant plot.
*/
function generateTimePlot(solution) {
// Plot
genMultPlot(solution, ["x", "dx/dt"], "timePlot", "Plot of dx/dt and x against time");
}
/**
* Generate two plots:
* - one of dx and x against t; and
* - a phase plot of dx against x.
*
* @param objectOfInputs An object containing all the problem parameters.
* @return Nothing. Just generates the plots.
*/
function generatePlots(objectOfInputs=undefined, solution=undefined) {
if (objectOfInputs==undefined) {
var objectOfInputs = readInputs();
}
if (solution==undefined) {
var solution = solveProblem(RKF45, objectOfInputs);
}
// Generate plots
generateTimePlot(solution);
generatePhasePlot(solution);
}
/**
* Generate animation
* @return nothing
*/
function generateAnimation(objectOfInputs=undefined, solution=undefined) {
if (objectOfInputs==undefined) {
var objectOfInputs = readInputs();
}
if (solution==undefined) {
var solution = solveProblem(RKF45, objectOfInputs);
}
animate2D(solution, {varnames: ["x", "dx/dt"], nos: [0, 1], title: "Duffing system: phase plot of dx/dt against x."});
}
function generateTable(objectOfInputs=undefined, solution=undefined) {
if (objectOfInputs==undefined) {
var objectOfInputs = readInputs();
}
if (solution==undefined) {
var solution = solveProblem(RKF45, objectOfInputs);
}
fillTable(objectOfInputs, ['x', 'dx/dt'], solution)
}