@nexys/math-ts
Version:
[](https://www.npmjs.com/package/@nexys/math-ts) [](https://travis-ci.com/github/Nexysweb/math-ts) [ • 1.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.bissection = (f, x, y, precision = .0001, niterations = 1000) => {
const z = (x + y) / 2;
const fz = f(z);
if ((fz * (1 - precision) <= 0 && fz * (1 + precision) >= 0) || niterations === 0) {
return z;
}
if (fz * f(x) < 0) {
return exports.bissection(f, x, z, precision, niterations - 1);
}
return exports.bissection(f, z, y, precision, niterations - 1);
};
exports.newton = (x, f, df, precision = .0000000001, niterations = 1000) => {
const fx = f(x);
if (Math.abs(fx) <= precision || niterations === 0) {
return x;
}
return exports.newton(x - fx / df(x), f, df, precision, niterations - 1);
};
exports.secant = (x, f, precision = .0001, niterations = 1000) => {
const y = x[0] - f(x[0]) * (x[0] - x[1]) / (f(x[0]) - f(x[1]));
if (Math.abs(y - x[0]) <= precision || niterations === 0) {
return y;
}
return exports.secant([y, x[0]], f, precision, niterations - 1);
};