-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrhcalc.js
231 lines (195 loc) · 5.84 KB
/
rhcalc.js
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
// Event handler for the routh hurwitz button
jQuery(document).ready(function ($) {
$('#compute-rh').on('click', function() {
RH();
});
// Hide result elements
$("#rh-matrix-card").hide();
$("#result-card").hide();
});
// When we change the order of the system
function systemOrderChangeEvent() {
var $sysOrderInput = $("#sys-order");
var $sysOrderText = $("#sys-order-text")
var order = $sysOrderInput.val();
if (order > 0 && order <= 11) {
switch (order % 10) {
case 1:
$sysOrderText.html('-st');
break;
case 2:
$sysOrderText.html('-nd');
break;
case 3:
$sysOrderText.html('-rd');
break;
default:
$sysOrderText.html('-th');
break;
}
$("#denom_input").html(regenDenomInputHTML(order));
}
}
// Regenerate the input boxes based on system order entered
function regenDenomInputHTML(order) {
htmlstr = '';
for (var i = order; i > 0; i--) {
htmlstr += '<input id="denom_coeff_' + i + '" class="coeff-input" type="number" value="0">';
htmlstr += '<span>s</span>';
if (order > 1) {
htmlstr += '<sup class="sup-dense">' + i + '</sup>';
}
htmlstr += '+';
}
// Constant term
htmlstr += '<input id="denom_coeff_0" class="coeff-input" type="number" value="1">';
return htmlstr;
}
// Run the main routh hurwitz function
function RH() {
// Get order
var order = $("#sys-order").val();
// Check that the error is correct
if (order < 1 || order > 11) {
console.error("Error: wrong order");
return;
}
// Obtain all coefficients and put into an array
var coeff = getCoefficients(order);
if (coeff == undefined) return;
// Check if all coefficient are non-zero
if (checkAllZero(coeff)) {
console.warn("All denominators have 0 as coefficients; system is naturally unstable");
return;
}
// do some more RH stuff
var matrix = computeRH(coeff);
outputStability(checkStability(matrix));
outputMatrix(matrix);
}
// Get coefficients from HTML and return as an array
function getCoefficients(order) {
var coeff = [];
for (var i = order; i >= 0; i--) {
var ci = parseFloat($('#denom_coeff_' + i).val());
// check that every entry is valid
if (ci != undefined) {
coeff.push(ci);
} else {
console.error("Error: coefficient undefined");
return undefined;
}
}
return coeff;
}
// Returns true if all of the coefficients are 0
function checkAllZero(coeff) {
for (var i = 0, l = coeff.length; i < l; i++) {
if (coeff[i] != 0) {
return false;
}
}
return true;
}
// returns a lower bound and upper bound for k
function computeRH(coeff) {
var matrix = makeRHMatrix(coeff);
var rows = matrix.length;
var cols = matrix[0].length;
// for each of the remaining rows
for (var i = 2; i < rows; i++) {
var divider = matrix[i - 1][0];
// if the 'b' value is 0, then set it to a small number
if (divider == 0) {
divider = 1e-10;
}
var head1 = matrix[i - 2][0];
var head2 = matrix[i - 1][0];
// TODO: don't need to calculate some 0 cases
// TODO: if possible, use corner tricks
// we don't need to do the first two rows
// for the row 3-4, we do -1 of the total columns
for (var j = 0; j < cols; j++) {
var tail1 = matrix[i - 2][j + 1];
var tail2 = matrix[i - 1][j + 1];
if (tail1 === undefined) {
tail1 = 0;
}
if (tail2 === undefined) {
tail2 = 0;
}
// console.log([divider, head1, tail1, head2, tail2]);
var m = (-1 / divider) * determinant([head1, tail1, head2, tail2]);
// console.log(m);
matrix[i][j] = m;
}
}
console.log(matrix);
return matrix;
}
// Generates a matrix with an appropriate size, then put the coeffcients inside
function makeRHMatrix(coeff) {
var cols = Math.ceil(coeff.length / 2);
var rows = coeff.length;
// Create 2d array for the matrix full of 0s
var mat = new Array(rows);
for (var r = 0; r < rows; r++) {
mat[r] = new Array(cols);
for (var c = 0; c < cols; c++) {
mat[r][c] = 0;
}
}
// Fill in the coefficients
for (var i = 0; i < coeff.length; i++) {
var row = i % 2;
var col = Math.floor(i / 2);
mat[row][col] = coeff[i];
}
return mat;
}
// Compute the determinant of matrix [[a, b], [c, d]] represented in 1D as [a, b, c, d]
function determinant(X) {
if (X.length < 4) {
console.error("Error: determinant takes 2x2 matrix or array of 4");
return;
}
var det = X[0] * X[3] - X[1] * X[2];
if (isNaN(det)) {
det = 0;
}
return(det);
}
function checkStability(matrix) {
for (var i = 0; i < matrix.length; i++) {
if (matrix[i][0] < 0) {
return false;
}
}
return true;
}
function outputStability(stable) {
var txt;
if (stable) {
txt = 'System is stable!';
} else {
txt = 'System is unstable!';
}
$("#stable_result").html(txt);
$("#result-card").show();
}
// Generate HTML for the matrix
function outputMatrix(matrix) {
var mat;
for (var row = 0; row < matrix.length; row++) {
thisRow = matrix[row];
mat += '<tr>';
for (var col = 0; col < thisRow.length; col++) {
mat += '<td>';
mat += thisRow[col];
mat += '</td>';
}
mat += '</tr>';
}
$("#rh-matrix").html(mat);
$("#rh-matrix-card").show();
}