forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler.js
More file actions
31 lines (27 loc) · 704 Bytes
/
euler.js
File metadata and controls
31 lines (27 loc) · 704 Bytes
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
function forwardEuler(timeStep, n) {
const arr = [1];
for (let i = 1; i <= n; i++) {
arr[i] = arr[i - 1] - 3 * arr[i - 1] * timeStep;
}
return arr;
}
function checkEuler(arr, timeStep, threshold) {
let isApprox = true;
arr.forEach((_value, i) => {
const solution = Math.exp(-3 * timeStep * i);
if (Math.abs(arr[i] - solution) > threshold) {
console.log(arr[i], solution);
isApprox = false;
}
});
return isApprox;
}
function main() {
const timeStep = 0.01;
const threshold = 0.01;
const n = 100;
const eulerResult = forwardEuler(timeStep, n);
const checkResult = checkEuler(eulerResult, timeStep, threshold);
console.log(checkResult);
}
main();