UNPKG

4.42 kBMarkdownView Raw
1# RungeKuttaJS
2
3> JavaScript implementation of the classic Runge-Kutta method (RK4)
4
5[![npm][npm-badge]][npm-url] [![build][build-badge]][build-url] [![coverage][coverage-badge]][coverage-url] [![Known Vulnerabilities][vulnerabilities-badge]][vulnerabilities-url] [![dependencies][dependencies-badge]][dependencies-url] [![size][size-badge]][size-url] [![types][types-badge]][types-url] [![unicorn][unicorn-badge]][unicorn-url] [![xo][xo-badge]][xo-url] [![license][license-badge]][license-url]
6
7## Key notes / highlights
8
9- Solves ordinary diffenrential equations numerically
10- Less error prone than a Euler integration
11- Works with single equations and systems of equations
12- Minimal implementation
13
14## Install
15
16```console
17$ npm install runge-kutta
18```
19
20## Usage
21
22Suppose you want to predict the spread of a viral disease. The SIR model could be implemented and solved with the following four lines of code.
23
24```js
25import rungeKutta from 'runge-kutta';
26
27// Setup parameters for the transmission speed (T)
28// and the recovery rate R (R).
29// This corresponds to a basic reproduction number equal to 3
30// ~ T/R.
31const T = .2143, R = 1/14;
32// Define the set of ordinary differential equations.
33const dSIR = (t, y) => [-T * y[0] * y[1], (T * y[0] - R) * y[1], R * y[1]];
34
35// Solve the system and log the result (reduced to the infection count).
36console.log(rungeKutta(dSIR, [1, .1, 0], [0, 14], .2).map(x => x[1]));
37```
38
39## API
40
41### Methods
42
43#### rungeKutta
44
45► **rungeKutta**(`ODE`: initialValueFunction, `initialCondition`: number | number[], `range`: readonly [number, number], `stepSize` = 1): number[] | number[][]
46
47Solves the the initial value problem given through `ODE`. It is specified through a function taking in a number for the undependent variable and a number or an array of numbers for the dependent variable(s):
48
49```js
50type initialValueFunction = (x: number, y: any) => any;
51type initialValueFunctionSingle = (x: number, y: number) => number;
52type initialValueFunctionMulti = (x: number, y: readonly number[]) => number[];
53```
54
55If the `initialCondition` is a number (or an array containg one number) the `rungeKutta` assumes a single variable problem, otherwise a multi variable problem.
56
57##### Example (Single variable problem: y' = y, y(0) = 1)
58
59```js
60const dy = (t, y) => y;
61const approxExp = rungeKutta(dy, 1, [0, 8], .1);
62console.log(approxExp);
63```
64
65A multi variable problem is given in the introduction.
66
67## Keywords
68
69- math
70- mathematics
71- numerical
72- numeric
73- methods
74- solve
75- differential
76- equation
77- system
78- equations
79- calculus
80
81## Dependencies
82
83- [type-insurance](https://www.npmjs.com/package/type-insurance)
84
85## Related
86
87- [nsolvejs](https://www.npmjs.com/package/nsolvejs) - Solve equations numerically and regression analysis
88
89## Maintainer
90
91- [Ruben Giannotti](http://rubengiannotti.com) - ruben.giannotti@gmx.net - github.com/giannotr
92
93[npm-badge]: https://img.shields.io/npm/v/runge-kutta.svg
94[npm-url]: https://www.npmjs.com/package/runge-kutta
95[build-badge]: https://travis-ci.org/giannotr/runge-kutta-js.svg?branch=master
96[build-url]: https://travis-ci.org/giannotr/runge-kutta-js
97[coverage-badge]: https://coveralls.io/repos/github/giannotr/runge-kutta-js/badge.svg?branch=master
98[coverage-url]: https://coveralls.io/github/giannotr/runge-kutta-js?branch=master
99[vulnerabilities-badge]: https://snyk.io/test/github/giannotr/runge-kutta-js/badge.svg?targetFile=package.json
100[vulnerabilities-url]: https://snyk.io/test/github/giannotr/runge-kutta-js?targetFile=package.json
101[dependencies-badge]: https://david-dm.org/giannotr/runge-kutta-js.svg
102[dependencies-url]: https://david-dm.org/giannotr/runge-kutta-js
103[size-badge]: https://badgen.net/packagephobia/publish/runge-kutta
104[size-url]: https://packagephobia.now.sh/result?p=runge-kutta
105[types-badge]: https://badgen.net/npm/types/runge-kutta
106[types-url]: https://github.com/giannotr/runge-kutta-js/tree/master/src
107[unicorn-badge]: https://img.shields.io/badge/unicorn-approved-ff69b4.svg
108[unicorn-url]: https://www.youtube.com/watch?v=9auOCbH5Ns4
109[xo-badge]: https://img.shields.io/badge/code_style-XO-5ed9c7.svg
110[xo-url]: https://github.com/xojs/xo
111[license-badge]: https://img.shields.io/github/license/giannotr/runge-kutta-js.svg
112[license-url]: https://github.com/giannotr/runge-kutta-js/blob/master/LICENSE