UNPKG

@nexys/math-ts

Version:

[![npm version](https://badge.fury.io/js/%40nexys%2Fmath-ts.svg)](https://www.npmjs.com/package/@nexys/math-ts) [![TavisCI](https://travis-ci.com/Nexysweb/tableau-wdc-react.svg?branch=master)](https://travis-ci.com/github/Nexysweb/math-ts) [![Deployment](

28 lines (27 loc) 821 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generic = (f, a, n, method = "simpson") => { const dx = (a[1] - a[0]) / n; const evalFunc = (f, x, b, dx, g) => { if (x > b) { return 0; } else { return g(f, x, dx) + evalFunc(f, x + dx, b, dx, g); } }; return evalFunc(f, a[0], a[1], dx, getMethod(method)); }; const getMethod = (m) => { switch (m) { case 'simpson': return simpson; case 'trapezoidal': return trapezoidal; default: return rectangle; } }; const rectangle = (f, x, dx) => f(x) * dx; const trapezoidal = (f, x, dx) => (f(x) + f(x + dx)) / 2 * dx; const simpson = (f, x, dx) => (f(x) + 4 * f((2 * x + dx) / 2) + f(x + dx)) * dx / 6;