-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·222 lines (200 loc) · 8.32 KB
/
main.js
File metadata and controls
executable file
·222 lines (200 loc) · 8.32 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
/**
* Right-hand side of our ODE written as a system of first-order ODEs.
*
* @param objectOfInputs An object containing problem parameters.
* @param t Time (seconds).
* @param vars An array of [theta1, p1, theta2, p2]
* @return [dtheta1/dt, dp1/dt, dtheta2/dt, dp2/dt]
*/
function f(objectOfInputs, t, vars, dt) {
var {g, l1, l2, m1b, m1r, m2b, m2r, b1b, b1r, c1b, c1r, b2b, b2r, c2b, c2r} = objectOfInputs;
var [theta1, dtheta1, theta2, dtheta2] = vars;
outercoef = 1/((m2r/12+m2b)*l2 - (m2b**2*l2*Math.cos(theta1-theta2)**2)/(m1r/12 + m1b+m2b));
mass = m1r/2 + m2r + m1b + m2b;
v1b = l1*dtheta1;
v1r = v1b/2;
v2b = Math.sqrt(l1**2*dtheta1**2+l2**2*dtheta2**2+2*l1*l2*dtheta1*dtheta2*Math.cos(theta1-theta2));
v2r = Math.sqrt(l1**2*dtheta1**2+(l2**2*dtheta2**2)/4 + l2*dtheta1*dtheta2*Math.cos(theta1-theta2));
drag1b = (b1b + c1b*v1b)*v1b;
drag1r = (b1r + c1r*v1r)*v1r/2;
drag2b = (b2b + c2b*v2b)*(l1*dtheta1 + l2*dtheta2*Math.cos(theta1-theta2));
drag2b2 = (b2b + c2b*v2b)*(l1*dtheta1*Math.cos(theta1-theta2) + l2*dtheta2);
drag2r = (b2r + c2r*v2r)*(l1*dtheta1 + l2*dtheta2*Math.cos(theta1-theta2)/2);
drag2l2 = 1/4*(b2r + c2r*v2r)*(2*l1*dtheta1*Math.cos(theta1-theta2) + l2*dtheta2);
innercoef = -m2b*Math.cos(theta1-theta2)/(m1r/12+m1b+m2b);
inner = innercoef*(-m2b*l2*dtheta2**2*Math.sin(theta1-theta2) - g*Math.cos(theta1)*(m1r/2+m2r+m1b+m2b)-drag1b-drag2b-drag1r-drag2r);
extra = m2b*(l1*dtheta1**2*Math.sin(theta1-theta2)-g*Math.cos(theta2))-drag2l2-drag2b2;
d2theta2 = outercoef*(inner+extra);
outercoef1 = 1/((m1r/12+m1b+m2b)*l1);
innel11 = -m2b*l2*(d2theta2*Math.cos(theta1-theta2)+dtheta2**2*Math.sin(theta1-theta2));
innel12 = -g*Math.cos(theta1)*mass - drag1b - drag2b - drag1r - drag2r;
d2theta1 = outercoef1*(innel11 + innel12);
// Return statement
return [dt*dtheta1, dt*d2theta1, dt*dtheta2, dt*d2theta2];
}
/**
* Generates a 2D phase plot of theta2 against theta1
*
* @param solution An object containing solution data.
* @return Nothing.
*/
function generateTheta1Theta2PhasePlot(solution) {
// Extract solution data from solution object
var {vars} = solution;
var theta1 = vars[0];
var theta2 = vars[2];
// Generate 2D plot
gen2DPlot(theta1, theta2, "phasePlotTheta1Theta2", "Phase plot of θ<sub>2</sub> against θ<sub>1</sub>", "θ<sub>1</sub>", "θ<sub>2</sub>");
}
/**
* Generates a dtheta2 against dtheta1 phase plot
*
* @param solution An object containing solution data.
* @return Nothing.
*/
function generateDtheta1Dtheta2PhasePlot(solution) {
// Extract solution data from solution object
var {vars} = solution;
var dtheta1 = vars[1];
var dtheta2 = vars[3];
// Generate 2D plot
gen2DPlot(dtheta1, dtheta2, "phasePlotDtheta2Dtheta1", "Phase plot of dθ<sub>2</sub>/dt against dθ<sub>1</sub>/dt", "dθ<sub>1</sub>/dt", "dθ<sub>2</sub>/dt");
}
/**
* Generates a dtheta1 against theta1 phase plot
*
* @param solution An object containing solution data.
* @return Nothing.
*/
function generateDtheta1Theta1PhasePlot(solution) {
// Extract solution data from solution object
var {vars} = solution;
var theta1 = vars[0];
var dtheta1 = vars[1];
// Generate 2D plot
gen2DPlot(theta1, dtheta1, "phasePlotDtheta1Theta1", "Phase plot of dθ<sub>1</sub>/dt against θ<sub>1</sub>", "θ<sub>1</sub>", "dθ<sub>1</sub>/dt");
}
/**
* Generates a dtheta2 against theta1 phase plot
*
* @param solution An object containing solution data.
* @return Nothing.
*/
function generateTheta1Dtheta2PhasePlot(solution) {
// Extract solution data from solution object
var {vars} = solution;
var theta1 = vars[0];
var dtheta2 = vars[2];
// Generate 2D plot
gen2DPlot(theta1, dtheta2, "phasePlotDtheta1Theta2", "Phase plot of dθ<sub>2</sub>/dt against θ<sub>1</sub>", "θ<sub>1</sub>", "θ<sub>2</sub>/dt");
}
/**
* Generates a dtheta1 against theta2 phase plot
*
* @param solution An object containing solution data.
* @return Nothing.
*/
function generateTheta2Dtheta1PhasePlot(solution) {
// Extract solution data from solution object
var {vars} = solution;
var theta2 = vars[2];
var dtheta1 = vars[1];
// Generate 2D plot
gen2DPlot(theta2, dtheta1, "phasePlotDtheta2Theta1", "Phase plot of dθ<sub>1</sub>/dt against θ<sub>2</sub>", "θ<sub>2</sub>", "dθ<sub>1</sub>/dt");
}
/**
* Generates a dtheta2 against theta2 phase plot
*
* @param solution An object containing solution data.
* @return Nothing.
*/
function generateDtheta2Theta2PhasePlot(solution) {
// Extract solution data from solution object
var {vars} = solution;
var theta2 = vars[2];
var dtheta2 = vars[3];
// Generate 2D plot
gen2DPlot(theta2, dtheta2, "phasePlotDtheta2Theta2", "Phase plot of dθ<sub>2</sub>/dt against θ<sub>2</sub>", "θ<sub>2</sub>", "dθ<sub>2</sub>/dt");
}
/**
* Generates a time plot
*
* @param solution An object containing solution data.
* @return Nothing.
*/
function generateTimePlot(solution) {
// Generate time plot
genMultPlot(solution, ["θ<sub>1</sub>", "dθ<sub>1</sub>/dt", "θ<sub>2</sub>", "dθ<sub>2</sub>/dt"], "timePlot", "Plot of θ<sub>1</sub>, dθ<sub>1</sub>/dt, θ<sub>2</sub> and dθ<sub>2</sub>/dt against time");
}
/**
* Generate all plots
*
* @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) {
// Solve problem
var solution = solveProblem(RKF45, objectOfInputs);
}
// Generate plots
generatePendulumPlots(objectOfInputs, solution);
generateTheta1Theta2PhasePlot(solution);
generateDtheta1Theta1PhasePlot(solution);
generateTheta1Dtheta2PhasePlot(solution);
generateTheta2Dtheta1PhasePlot(solution);
generateDtheta2Theta2PhasePlot(solution);
generateDtheta1Dtheta2PhasePlot(solution);
generatePendulumPlots(objectOfInputs, solution)
generateTimePlot(solution);
}
function generateAnimation(objectOfInputs=undefined, solution=undefined) {
if (objectOfInputs==undefined) {
var objectOfInputs = readInputs();
}
if (solution==undefined) {
var solution = solveProblem(RKF45, objectOfInputs);
}
animatePendulum(objectOfInputs, solution, "Double pendulum");
}
function generateTheta1PhaseAnimation(objectOfInputs=undefined, solution=undefined) {
if (objectOfInputs == undefined) {
var objectOfInputs = readInputs();
}
if (solution == undefined) {
var solution = solveProblem(RKF45, objectOfInputs);
}
animate2D(solution, {varnames: ["θ<sub>1</sub>", "dθ<sub>1</sub>/dt"], IdSuffix: "Theta1Phase", title: "Phase plot of dθ<sub>1</sub>/dt against θ<sub>1</sub>."});
}
function generateTheta2PhaseAnimation(objectOfInputs=undefined, solution=undefined) {
if (objectOfInputs == undefined) {
var objectOfInputs = readInputs();
}
if (solution == undefined) {
var solution = solveProblem(RKF45, objectOfInputs);
}
animate2D(solution, {timer: [0, 1.0], varnames: ["θ<sub>2</sub>", "dθ<sub>2</sub>/dt"], IdSuffix: "Theta2Phase", nos: [2, 3], title: "Phase plot of dθ<sub>2</sub>/dt against θ<sub>2</sub>."});
}
function generateTable(objectOfInputs=undefined, solution=undefined) {
if (objectOfInputs==undefined) {
var objectOfInputs = readInputs();
}
if (solution==undefined) {
var solution = solveProblem(RKF45, objectOfInputs);
}
fillTable(objectOfInputs, ['θ<sub>1</sub>', 'dθ<sub>1</sub>/dt', 'θ<sub>2</sub>', 'dθ<sub>2</sub>/dt'], solution)
}
function generateAnimations(objectOfInputs=undefined, solution=undefined) {
if (objectOfInputs == undefined) {
var objectOfInputs = readInputs();
}
if (solution == undefined) {
var solution = solveProblem(RKF45, objectOfInputs);
}
generateAnimation(objectOfInputs, solution)
generateTheta1PhaseAnimation(objectOfInputs, solution);
generateTheta2PhaseAnimation(objectOfInputs, solution);
}