-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (61 loc) · 1.77 KB
/
index.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
const asciichart = require ('asciichart');
let solution = [];
let beta = 0.35;
let gamma = 0.1;
let n = 1;
let i = 0;
const RungeKutta4 = require('runge-kutta-4');
const SIRModel = function (t, y) {
//the differential equation
let dydt = [];
let S = y[0];
let I = y[1];
let R = y[2];
let dS_dt = -(beta * S * I) / n;
let dI_dt = (beta * S * I) / n - (gamma * I);
let dR_dt = gamma * I;
solution.push({S: S, I: I, R: R});
dydt[0] = dS_dt;
dydt[1] = dI_dt;
dydt[2] = dR_dt;
i++;
return dydt;
};
const solve = function (options) {
let dt = options.t; // time step
let t0 = 0;
n = options.I0 + options.S0 + options.R0; //n is the total population so it is the sum of the I0,S0,R0
beta = options.beta;
gamma = options.gamma;
let y0 = [options.S0, options.I0, options.R0]; //initial values of the differential equation
let integrator = new RungeKutta4(SIRModel,t0,y0,dt); //solve the differential equation using the 4th order Runge-Kutta numerical method
integrator.steps(options.N); //solve for N steps
return solution;
};
const printChart = function (sol) {
let config = {
offset: 5,
padding: ' ',
height: 6
};
let S = [];
let I = [];
let R = [];
for (let i = 0; i < sol.length; i++) {
S[i] = sol[i].S;
I[i] = sol[i].I;
R[i] = sol[i].R;
}
console.log("=========== S ===========\n");
console.log (asciichart.plot(S, config));
console.log("\n");
console.log("=========== I ===========\n");
console.log (asciichart.plot(I, config));
console.log("\n");
console.log("=========== R ===========\n");
console.log (asciichart.plot(R, config));
};
module.exports = {
solve: solve,
printChart: printChart
};