-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.mdtex
77 lines (51 loc) · 2.79 KB
/
README.mdtex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# ode-rk4 [![Build Status](https://travis-ci.org/scijs/ode-rk4.svg)](https://travis-ci.org/scijs/ode-rk4) [![npm version](https://badge.fury.io/js/ode-rk4.svg)](http://badge.fury.io/js/ode-rk4) [![Dependency Status](https://david-dm.org/scijs/ode-rk4.svg)](https://david-dm.org/scijs/ode-rk4)
> Integrate a system of ODEs using the Fourth Order Runge-Kutta (RK-4) method
## Introduction
This module integrates a system of ordinary differential equations of the form
$$[plain=true] \begin{eqnarray*} y'(t) &=& f(t, y(t)), \\ y(t_0) &=& y_0 \end{eqnarray*} $$
where $y$ is a vector of length $n$. Given time step $\Delta t$, the Runge-Kutta 4 method integrates the ODE with update
$$[plain=true] \begin{eqnarray*} y_{n+1} &=& \frac{\Delta t}{6}\left(k_1 + 2k_2 + 2k_3 + k_4\right) \\ t_{n+1} &=& t_n + \Delta t \end{eqnarray*}$$
where $k_n$ are given by
$$[plain=true] \begin{eqnarray*} k_1 &=& f(t_n, y_n), \\ k_2 &=& f(t_n + \frac{\Delta t}{2}, y_n + \frac{\Delta t}{2} k_1), \\ k_3 &=& f(t_n + \frac{\Delta t}{2}, y_n + \frac{\Delta t}{2} k_2), \\ k_4 &=& f(t_n + \Delta t, y_n + \Delta tk_3). \end{eqnarray*}$$
For a similar adaptive method using the fifth order Cash-Karp Runge-Kutta method with fourth order embedded error estimator, see [ode45-cash-karp](https://github.com/scijs/ode45-cash-karp).
## Install
```bash
$ npm install ode-rk4
```
## Example
```javascript
var rk4 = require('ode-rk4')
var deriv = function(dydt, y, t) {
dydt[0] = -y[1]
dydt[1] = y[0]
}
var y0 = [1,0]
var n = 1000
var t0 = 0
var dt = 2.0 * Math.PI / n
var integrator = rk4( y0, deriv, t0, dt )
// Integrate 1000 steps:
integrator.steps(n)
// Integrate all the way around a circle:
// => integrator.y = [ 0.9999999999995743, -8.160481752145232e-11 ]
```
## API
### `require('ode-rk4')( y0, deriv, t0, dt )`
**Arguments:**
- `y0`: an array or typed array containing initial conditions. This vector is updated in-place with each integrator step.
- `deriv`: a function that calculates the derivative. Format is `function( dydt, y, t )`. Inputs are current state `y` and current time `t`, output is the calculated derivative `dydt`.
- `t0`: initial time $t$.
- `dt`: time step $\Delta t$.
**Returns**:
Initialized integrator object.
**Properties:**
- `n`: dimension of `y0`.
- `y`: current state. Initialized as a shallow copy of input `y0`.
- `deriv`: function that calculates the derivative. Initialized from input. May be changed.
- `t`: current time, incremented by `dt` with each time step.
- `dt`: time step $\Delta t$. Initialized from input `dt`. May be changed.
**Methods:**
- `.step()`: takes a single step of the RK-4 integrator and stores the result in-place in the `y` property.
- `.steps( n )`: takes `n` steps of the RK-4 integrator, storing the result in-place in the `y` property.
## Credits
(c) 2015 Ricky Reusser. MIT License