-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sums.js
65 lines (58 loc) · 1.69 KB
/
Sums.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
'use strict'
class Sums {
constructor(n, points, domain) {
this.n = n;
this.points = points;
this.domain = domain;
this.dx = this.domain / n;
this.pixelInterval = points.length / n;
}
leftHand() {
if (this.n === 0) return 0;
var sum = 0;
for (var i = 0; i < this.n; i++) {
sum += this.points[Math.round(i * this.pixelInterval)];
}
return sum * this.dx;
}
midpoint() {
if (this.n === 0) return 0;
var sum = 0;
for (var i = 0; i < this.n; i++) {
sum += this.points[Math.round(i * this.pixelInterval + this.pixelInterval / 2)];
}
return sum * this.dx;
}
rightHand() {
if (this.n === 0) return 0;
var sum = 0;
for (var i = 1; i <= this.n; i++) {
sum += this.points[Math.round(i * this.pixelInterval) - 1];
}
return sum * this.dx;
}
trapezoid() {
if (this.n === 0) return 0;
var sum = 0;
sum += this.points[0];
sum += this.points[this.points.length - 1];
for (var i = 1; i < this.n; i++) {
sum += 2 * this.points[Math.round(i * this.pixelInterval)];
}
sum *= this.dx;
sum *= .5;
return sum;
}
actual() {
var initValue = this.points[0] + this.points[this.points.length - 1];
var dx = this.domain / this.points.length;
return (dx/ 2) * this.points.reduce(function (sum, point) {
return sum + 2 * point;
}, initValue);
}
setN(n) {
this.n = n;
this.dx = this.domain / n
this.pixelInterval = this.points.length / n;
}
}