UNPKG

@tubular/math

Version:
41 lines 1.33 kB
export class ZeroFinder { constructor(zeroSeekingFunction, tolerance, maxIterations, x1, y1, x2, y2) { this.zeroSeekingFunction = zeroSeekingFunction; this.tolerance = tolerance; this.maxIterations = maxIterations; this.x1 = x1; this.y1 = y1; this._iterationCount = 0; if (x2 == null) { this.x2 = y1; this.y1 = zeroSeekingFunction(x1); this.y2 = zeroSeekingFunction(y1); } else { this.x2 = x2; this.y2 = y2; } } getXAtZero() { let x; this._iterationCount = 0; while (++this._iterationCount <= this.maxIterations) { x = this.x1 - this.y1 / (this.y2 - this.y1) * (this.x2 - this.x1); this.y = this.zeroSeekingFunction(x); if (Math.abs(this.y) <= this.tolerance) break; if ((this.y1 < this.y2 && this.y < 0.0) || (this.y1 > this.y2 && this.y > 0.0)) { this.x1 = x; this.y1 = this.y; } else { this.x2 = x; this.y2 = this.y; } } return x; } get lastY() { return this.y; } get iterationCount() { return this._iterationCount; } } //# sourceMappingURL=zero-finder.js.map