Copyright 2012, Lance Larsen Licensed under the MIT license
Uses vector.js
ODE Solver | |
ODE | The ode class is used to solve a system of ordinary differential equations. |
Functions | |
ODE | |
step | The ODE step function adds one step to the ODE solution based on the given dt. |
solve | The function calculate an ODE solution. |
pushPt | This appends value at ‘stepn’. |
setLimit | Adds a limit function to the 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);
The ODE step function adds one step to the ODE solution based on the given dt.
dt | The time step size |
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.
The function calculate an ODE solution.
steps | The number of ODE steps to take. |
dt | The 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. |
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’.