Octave Programming Tutorial/Differential equations
With Octave there are two functions available, that can be used for solving differential equations. The implementation is intergated in the core functionality of Octave. The used algorithms are based on Solvers for Ordinary Differential Equations written in Fortran.
Ordinary Differential Equations
[edit | edit source]The following function lsode can be used for Ordinary Differential Equations (ODE) of the form using Hindmarsh's ODE solver[1] LSODE.
Function: lsode (fcn, x0, t_out, t_crit)
- The first argument is the name of the function to call to compute the vector of right hand sides. It must have the form
x_dot = f (x, t)
- in this context x_dot is the return value of the function and is a vector (in the example below it has 2 components) and is a scalar. The solver lsode gets a vector of scalars as input.
- The second argument x0 defines the initial condition.
- By the third argument the output times t_out are defined by a vector. At these points in time the solution is requested for calculation. Points in time include as first element the corresponding time for the initial state.
- The last argument is optional parameter, that may be used to determine a set of points in times that the ODE solver should not integrate for. Especially when the solution contain singularities, these parameter might be used for successful run of the solver. Other use-case might be a discontinuity of the derivative.
Example
[edit | edit source]The following example can be used for solving a set of two differential equations using lsode. The function is defined by:
function x_dot = f (x, t) r = 0.15; k = 1.6; a = 1.25; b = 0.12; c = 0.89; d = 0.58; x_dot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1)); x_dot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2); endfunction
the function f is integrated with the command
x = lsode ("f", [1; 2], (t = linspace (0, 50, 200)'));
The linspace command is producing a set of 200 scalar values stored in the variable t.
plot (t, x)
Options for lsode
[edit | edit source]Partial Differential Equations
[edit | edit source]For Partial Differential Equations (PDE) see the following PDE example[2].
John Weatherwax (2006) provided the octave code defined the derivatives, boundary conditions with m-files:
- DBDU_BNDRY.m,
- DBDUX_BNDRY.m,
- DZDT_BNDRY.m and
- UINIT.m with the initial system for the PDE.
Display the libraries with the links above and adapt the code to your PDE problem.
- pdecol_Script.m call the m-files with the boundary conditions. Defining the files in that way encapsules the definition of functions in separate m-files for the solver script pdecol_Script.m. Apply the script to a new problem requires the adaptation of problem specific m-files above and the solver script remains untouched.
References
[edit | edit source]- ↑ Alan C. Hindmarsh (1983), ODEPACK, A Systematized Collection of ODE Solvers, in Scientific Computing, R. S. Stepleman, editor,
- ↑ John Weatherwax (2006) Solving Partial Differential Equations in Octave URL: https://waxworksmath.com/Software/PDECOL/Web/pdecol_example1.html (accessed 2020/07/14)