-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.html
137 lines (106 loc) · 3.61 KB
/
model.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Model for dinamyc systems</title>
</head>
<body onload="load()">
<canvas id="rateCanvas" width="100" height="100" style="border: 1px dashed #00c3c3;"></canvas>
<canvas id="levelCanvas" width="100" height="100" style="border: 1px dashed #00c3c3;"></canvas>
<table border="1" id="dataTable">
<th>Time</th><th>Level</th><th>Rate</th>
</table>
<script src="Graph.js" charset="utf-8"></script>
<script type="text/javascript">
"use strict";
var rateGraph;
var levelGraph;
var dt = 1;
var time = 0;
var stopTime = 10;
var levelArray = [];
var rateArray = [];
var auxiliaryArray = [];
class Level {
constructor(value) {
this.value = value;
levelArray.push(this);
}
updateFn() {};
update() {
this.value = this.value + dt * this.updateFn();
}
}
class Rate {
constructor(value) {
this.value = value;
rateArray.push(this);
}
updateFn() {};
update() {
this.value = this.updateFn();
}
}
class Auxiliary {
constructor(value) {
this.value = value;
auxiliaryArray.push(this);
}
updateFn() {};
update() {
this.value = this.updateFn();
}
}
//Model Definition
var state = new Level(10);
var rate = new Rate();
var a = 0.5;
//Rate
rate.updateFn = () => a * state.value;
// Level
state.updateFn = () => rate.value;
//End of model definition
function init() {
var tableData = [];
tableData.push(time, state.value, 0);
auxiliaryArray.forEach(item => item.update);
rateArray.forEach(item => {
item.update();
});
levelGraph.plot([state.value]);
rateGraph.plot([rate.value]);
fillTableRow("dataTable", tableData);
}
function loop() {
var tableData = [];
time++;
levelArray.forEach(item => item.update());
auxiliaryArray.forEach(item => item.update());
rateArray.forEach(item => item.update());
var timer = setTimeout(loop, 300);
tableData.push(time, levelArray[0].value, rateArray[0].value)
fillTableRow("dataTable", tableData);
levelGraph.plot([state.value]);
rateGraph.plot([rate.value]);
if (time >= stopTime) {
clearTimeout(timer)
};
}
function load() {
rateGraph = new Graph("rateCanvas", 0, 10, 0, 300, ["red"], ["rate"], ["0", "10", "0", "300"]); // arguments: Arg1: canvasId, Arg2: maxX, Arg3: maxY, Arg4: [vector of colors]; this determines the size
levelGraph = new Graph("levelCanvas", 0, 10, 0, 600, ["green"], ["level"], ["0", "10", "0", "600"]);
init();
loop();
}
function fillTableRow(tableId, data) {
var table = document.getElementById(tableId);
var newRow = table.insertRow(table.rows.length);
data.forEach((item, index) => {
var newCell = newRow.insertCell(index);
var newText = document.createTextNode(item);
newCell.appendChild(newText);
})
}
</script>
</body>
</html>