No Problem Man

# npm

## integrate

0.0.1 • Public • Published

# Integrate

Some numerical integrators for ordinary differential equations:

Want one that's not there? Open an issue, or better yet, a pull request adding it.

## Fast

All of the methods are hand-written in asm.js, so they should be fast. Not that your integration method would ever be your bottleneck anyway, but hey... why not.

## Example

There are two steps: constructing your integrator, and then calling it.

Should log this:

`````````
t = 0        1
t = 0.25     1.25
t = 0.5      1.5625
t = 0.75     1.953125
t = 1        2.44140625
t = 1.25     3.0517578125
t = 1.5      3.814697265625
t = 1.7      4.76837158203125
t = 2        5.9604644775390625
```
``````

Well for starters, that `5.960...` at the end of the example logs should actually be `7.389...`.

How far do we have to shrink our step size to get within `0.0001` of the exact solution? How much better is Runge-Kutta? The fourth-order Runge-Kutta integrator will evaulate your ODE four times for each step, so it has to converge at least 4x faster to be worthwhile...

Here's a quick and dirty test, computing how far we have to shrink the step size for the Euler method and for fourth-order Runge-Kutta, to get within `0.0001` of the exact solution for `t = 2`.

Our ODE, `y' = y` at `y(0) = 1` is actually just `e^x`, so we should be converging to `e^2`.

Should log

`````````
euler step with error < 0.0001 at t=2:  0.00000762939453125
rk4 step with error < 0.0001 at t=2:  0.125
```
``````

So, for a cost of 4x more evaluations per step, we get to run with a step size about 16,000x bigger with Runge-Kutta than with the Euler Method for similar accuracy. After our 4x evaluations per step penalty, we are still winning by about 4,000x the number of evaluations required in this example.

So, use rk4.

## API

### Create an integrator from an ODE function

The nice way:

Shave off one wrapping function call:

Both forms will return an identical object.

`myODEFunction` should accept two parameters: `t`, and `y`.

### Integrate with one of the numerical integration methods

``````var yNext = integrator.euler(yLast, tNow, tStepSize);

var yNext = integrator.rk4(yLast, tNow, tStepSize);

var yNext = integrator.rk4general(yLast, tNow, 3);
``````

## Keywords

### Install

`npm i integrate`

### Repository

github.com/uniphil/integrate

### Homepage

github.com/uniphil/integrate