UNPKG

integrate-adaptive-simpson

Version:

Integrate a system of ODEs using the Second Order Runge-Kutta (Midpoint) method

91 lines (61 loc) 5.21 kB
# integrate-adaptive-simpson [![Build Status](https://travis-ci.org/scijs/integrate-adaptive-simpson.svg)](https://travis-ci.org/scijs/integrate-adaptive-simpson) [![npm version](https://badge.fury.io/js/integrate-adaptive-simpson.svg)](http://badge.fury.io/js/integrate-adaptive-simpson) [![Dependency Status](https://david-dm.org/scijs/integrate-adaptive-simpson.svg)](https://david-dm.org/scijs/integrate-adaptive-simpson) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square)](https://github.com/Flet/semistandard) > Compute a definite integral of one variable using [Simpson's Rule](https://en.wikipedia.org/wiki/Simpson%27s_rule) with adaptive quadrature ## Introduction This module computes the definite integral <p align="center"><img alt="&bsol;int&lowbar;a&Hat;b f&lpar;x&rpar; &bsol;&comma; dx" valign="middle" src="images/int_ab-fx-dx-49d001614b.png" width="99.5" height="56.5"></p> using [Romberg Integration](https://en.wikipedia.org/wiki/Romberg%27s_method) based on [Simpson's Rule](https://en.wikipedia.org/wiki/Simpson%27s_rule). That is, it uses [Richardson Extrapolation](https://en.wikipedia.org/wiki/Richardson_extrapolation) to estimate the error and recursively subdivide intervals until the error tolerance is met. The code is adapted from the pseudocode in [Romberg Integration and Adaptive Quadrature](http://www.math.utk.edu/~ccollins/refs/Handouts/rich.pdf). ## Install ```bash $ npm install integrate-adaptive-simpson ``` ## Example To compute the definite integral <p align="center"><img alt="&bsol;int&lowbar;&lcub;0&period;01&rcub;&Hat;&lcub;1&rcub; &bsol;frac&lcub;1&rcub;&lcub;x&rcub;&bsol;cos&bsol;left&lpar;&bsol;frac&lcub;1&rcub;&lcub;x&rcub;&bsol;right&rpar;&bsol;&comma;dx&comma;" valign="middle" src="images/int_0011-frac1xcosleftfrac1xrightdx-c5d6a6f216.png" width="177" height="56.5"></p> execute: ```javascript var integrate = require('integrate-adaptive-simpson'); function f (x) { return Math.cos(1 / x) / x); } intiegrate(f, 0.01, 1, 1e-8); // => -0.3425527480294604 ``` To integrate a vector function, you may import the vectorized version. To compute a contour integral of, say, <img alt="1 &sol; z" valign="middle" src="images/1-z-32ebeece91.png" width="34.5" height="33"> about <img alt="z&lowbar;0 &equals; 0" valign="middle" src="images/z_0-0-227c53dd15.png" width="59" height="31">, that is, <p align="center"><img alt="&bsol;oint &bsol;frac&lcub;dz&rcub;&lcub;z&rcub; &equals; 2&bsol;pi i&comma;" valign="middle" src="images/oint-fracdzz-2pi-i-3243136d9d.png" width="114" height="50.5"></p> ```javascript var integrate = require('integrate-adaptive-simpson/vector'); integrate(function (f, theta) { // z = unit circle: var c = Math.cos(theta); var s = Math.sin(theta); // dz: var dzr = -s; var dzi = c; // 1 / z at this point on the unit circle: var fr = c / (c * c + s * s); var fi = -s / (c * c + s * s); // Multiply f(z) * dz: f[0] = fr * dzr - fi * dzi; f[1] = fr * dzi + fi * dzr; }, 0, Math.PI * 2); // => [ 0, 6.283185307179586 ] ``` ## API #### `require('integrate-adaptive-simpson')( f, a, b [, tol, maxdepth]] )` Compute the definite integral of scalar function f from a to b. **Arguments:** - `f`: The function to be integrated. A function of one variable that returns a value. - `a`: The lower limit of integration, <img alt="a" valign="middle" src="images/a-2217a6870d.png" width="15" height="28">. - `b`: The upper limit of integration, <img alt="b" valign="middle" src="images/b-224c764dec.png" width="13" height="28">. - `tol`: The relative error required for an interval to be subdivided, based on Richardson extraplation. Default tolerance is `1e-8`. Be careful—the total accumulated error may be significantly less and result in more function evaluations than necessary. - `maxdepth`: The maximum recursion depth. Default depth is `20`. If reached, computation continues and a warning is output to the console. **Returns**: The computed value of the definite integral. #### `require('integrate-adaptive-simpson/vector')( f, a, b [, tol, maxdepth]] )` Compute the definite integral of vector function f from a to b. **Arguments:** - `f`: The function to be integrated. The first argument is an array of length `n` into which the output must be written. The second argument is the scalar value of the independent variable. - `a`: The lower limit of integration, <img alt="a" valign="middle" src="images/a-2217a6870d.png" width="15" height="28">. - `b`: The upper limit of integration, <img alt="b" valign="middle" src="images/b-224c764dec.png" width="13" height="28">. - `tol`: The relative error required for an interval to be subdivided, based on Richardson extraplation. Default tolerance is `1e-8`. - `maxdepth`: The maximum recursion depth. Default depth is `20`. If reached, computation continues and a warning is output to the console. **Returns**: An `Array` representing The computed value of the definite integral. ## References Colins, C., [Romberg Integration and Adaptive Quadrature Course Notes](http://www.math.utk.edu/~ccollins/refs/Handouts/rich.pdf). ## License (c) 2015 Scijs Authors. MIT License.