ODE Solver

Copyright 2012, Lance Larsen
Licensed under the MIT license

Uses vector.js

Summary
ODE Solver
ODEThe ode class is used to solve a system of ordinary differential equations.
Functions
ODE
stepThe ODE step function adds one step to the ODE solution based on the given dt.
solveThe function calculate an ODE solution.
pushPtThis appends value at ‘stepn’.
setLimitAdds a limit function to the ODE.

ODE

The ode class is used to solve a system of ordinary differential equations.  The ODE is expected to be provided to the constuctor in vector field form.  Take the following ODE for example:

x''' - x'' + 2*x' - 3*x = sin(t)         (1)

This can be written in first order form, by assigning a new variable for all but the highest order derivative:

x' = y                                   (2)
y' = z  (i.e. x'' = z)

Note that the higest order derivative can then be replaced with a first order derivative (z’ = x’’’).  If we substitute (2) and z’ into (1) and solve for z’ (i.e. x’’’) we get:

z' = z - 2*y + 3*x + sin(t)              (3)

If we combine the equations from (2) and (3) we now have a system of equations in first order form.

x' = y
y' = z
z' = z - 2*y + 3*x + sin(t)

This can be represented as a vector or an array.

[x',y',z'] = [y, z, z-2*y+3*x+sin(t)]    (4)

To use the ODE class, a javascript function needs to be created that replicates a first order vector field such as equation (4) above.  The javascript function takes the independent derivative variable as a first value (if we assume x’ = dx/dt then t is our independent variable), and the array of dependent variables (in this case [x,y,z]) as the second term.  Here is an example function.

var V = function(t, pt) {
   var x = pt[0], y= pt[1], z = pt[2];
   return [y, z, z - 2*y + 3*x + sin(t)];
}

var ode_ = ODE(V);
Summary
Functions
ODE
stepThe ODE step function adds one step to the ODE solution based on the given dt.
solveThe function calculate an ODE solution.
pushPtThis appends value at ‘stepn’.
setLimitAdds a limit function to the ODE.

Functions

ODE

PARAMETERS

VThe ODE in vector field form V(t,pt) where t is the derivative variable, and pt is a current initial or solution point.  V should return the ODE vector feild value at the point ‘pt’

step

The ODE step function adds one step to the ODE solution based on the given dt.

PARAMETERS

dtThe time step size

RETURNS

true if a point was added, or false if it was not.  A point is not added if any of the vector values is NaN.  In addition, if the ODE has a function named ‘limit’, this will be called with the new point that is calculated.  If the ‘limit’ function returns false, the point is not added and false is returned from this function.

solve

The function calculate an ODE solution.

PARAMETERS

stepsThe number of ODE steps to take.
dtThe step size.
t(Optional) The initial time.  If ‘solve’ has already been called once, it can be called again to extend the solution, and this value is not needed.
pt(Optional) The initial point.  Only needed for initial call to solve.

RETURNS

An object literal containing ‘t’, which is an array of time values, and ‘pts’, which is the solution point associated with each time in ‘t’.

pushPt

This appends value at ‘stepn’.

PARAMETERS

stepnThe step to take values from
tThe t array to push t to.
ptsThe pts array to push a point to.

setLimit

Adds a limit function to the ODE.

PARAMETERS

limit_funcThe function will be called as limit_func(pt), where ‘pt’ is the next solution point.  The function should return true if the point is within the acceptable solution bounds, or false if it is outside the bounds.
Close