1@c Copyright (C) 1996-2019 John W. Eaton
2@c
3@c This file is part of Octave.
4@c
5@c Octave is free software: you can redistribute it and/or modify it
6@c under the terms of the GNU General Public License as published by
7@c the Free Software Foundation, either version 3 of the License, or
8@c (at your option) any later version.
9@c
10@c Octave is distributed in the hope that it will be useful, but
11@c WITHOUT ANY WARRANTY; without even the implied warranty of
12@c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13@c GNU General Public License for more details.
14@c
15@c You should have received a copy of the GNU General Public License
16@c along with Octave; see the file COPYING.  If not, see
17@c <https://www.gnu.org/licenses/>.
18
19@node Differential Equations
20@chapter Differential Equations
21
22Octave has built-in functions for solving ordinary differential equations
23(ODEs), and differential-algebraic equations (DAEs).
24
25@menu
26* Ordinary Differential Equations::
27* Differential-Algebraic Equations::
28* Matlab-compatible solvers::
29@end menu
30
31@cindex differential equations
32@cindex ODE
33@cindex DAE
34
35@node Ordinary Differential Equations
36@section Ordinary Differential Equations
37
38The function @code{lsode} can be used to solve ODEs of the form
39@tex
40$$
41 {dx\over dt} = f (x, t)
42$$
43@end tex
44@ifnottex
45
46@example
47@group
48dx
49-- = f (x, t)
50dt
51@end group
52@end example
53
54@end ifnottex
55
56@noindent
57using @nospell{Hindmarsh's} ODE solver @sc{lsode}.
58
59@DOCSTRING(lsode)
60
61@DOCSTRING(lsode_options)
62
63Here is an example of solving a set of three differential equations using
64@code{lsode}.  Given the function
65
66@cindex oregonator
67
68@example
69@group
70## oregonator differential equation
71function xdot = f (x, t)
72
73  xdot = zeros (3,1);
74
75  xdot(1) = 77.27 * (x(2) - x(1)*x(2) + x(1) ...
76            - 8.375e-06*x(1)^2);
77  xdot(2) = (x(3) - x(1)*x(2) - x(2)) / 77.27;
78  xdot(3) = 0.161*(x(1) - x(3));
79
80endfunction
81@end group
82@end example
83
84@noindent
85and the initial condition @code{x0 = [ 4; 1.1; 4 ]}, the set of
86equations can be integrated using the command
87
88@example
89@group
90t = linspace (0, 500, 1000);
91
92y = lsode ("f", x0, t);
93@end group
94@end example
95
96If you try this, you will see that the value of the result changes
97dramatically between @var{t} = 0 and 5, and again around @var{t} = 305.
98A more efficient set of output points might be
99
100@example
101@group
102t = [0, logspace(-1, log10(303), 150), ...
103        logspace(log10(304), log10(500), 150)];
104@end group
105@end example
106
107An m-file for the differential equation used above is included with the
108Octave distribution in the examples directory under the name
109@file{oregonator.m}.
110
111
112@node Differential-Algebraic Equations
113@section Differential-Algebraic Equations
114
115The function @code{daspk} can be used to solve DAEs of the form
116@tex
117$$
118 0 = f (\dot{x}, x, t), \qquad x(t=0) = x_0, \dot{x}(t=0) = \dot{x}_0
119$$
120@end tex
121@ifnottex
122
123@example
1240 = f (x-dot, x, t),    x(t=0) = x_0, x-dot(t=0) = x-dot_0
125@end example
126
127@end ifnottex
128
129@noindent
130where
131@tex
132$\dot{x} = {dx \over dt}$
133@end tex
134@ifnottex
135@math{x-dot}
136@end ifnottex
137is the derivative of @math{x}.  The equation is solved using
138@nospell{Petzold's} DAE solver @sc{daspk}.
139
140@DOCSTRING(daspk)
141
142@DOCSTRING(daspk_options)
143
144Octave also includes @sc{dassl}, an earlier version of @sc{daspk},
145and @sc{dasrt}, which can be used to solve DAEs with constraints
146(stopping conditions).
147
148@DOCSTRING(dassl)
149
150@DOCSTRING(dassl_options)
151
152@DOCSTRING(dasrt)
153
154@DOCSTRING(dasrt_options)
155
156See @nospell{K. E. Brenan}, et al., @cite{Numerical Solution of Initial-Value
157Problems in Differential-Algebraic Equations}, North-Holland (1989),
158DOI: @url{https://doi.org/10.1137/1.9781611971224},
159for more information about the implementation of @sc{dassl}.
160
161
162@node Matlab-compatible solvers
163@section Matlab-compatible solvers
164
165Octave also provides a set of solvers for initial value problems for ordinary
166differential equations (ODEs) that have a @sc{matlab}-compatible interface.
167The options for this class of methods are set using the functions.
168
169@itemize
170  @item @ref{XREFodeset,,odeset}
171
172  @item @ref{XREFodeget,,odeget}
173@end itemize
174
175Currently implemented solvers are:
176
177@itemize
178  @item @nospell{Runge-Kutta} methods
179
180  @itemize
181    @item @ref{XREFode45,,ode45} integrates a system of non-stiff ODEs or
182    index-1 differential-algebraic equations (DAEs) using the high-order,
183    variable-step @nospell{Dormand-Prince} method.  It requires six function
184    evaluations per integration step, but may take larger steps on smooth
185    problems than @code{ode23}: potentially offering improved efficiency at
186    smaller tolerances.
187
188    @item @ref{XREFode23,,ode23} integrates a system of non-stiff ODEs or (or
189    index-1 DAEs).  It uses the third-order @nospell{Bogacki-Shampine} method
190    and adapts the local step size in order to satisfy a user-specified
191    tolerance.  The solver requires three function evaluations per integration
192    step.
193
194    @item @ref{XREFode23s,,ode23s} integrates a system of stiff ODEs (or
195    index-1 DAEs) using a modified second-order @nospell{Rosenbrock} method.
196  @end itemize
197
198  @item Linear multistep methods
199
200  @itemize
201    @item @ref{XREFode15s,,ode15s} integrates a system of stiff ODEs (or
202    index-1 DAEs) using a variable step, variable order method based on
203    Backward Difference Formulas (BDF).
204
205    @item @ref{XREFode15i,,ode15i} integrates a system of fully-implicit ODEs
206    (or index-1 DAEs) using the same variable step, variable order method as
207    @code{ode15s}.  @ref{XREFdecic,,decic} can be used to compute consistent
208    initial conditions for @code{ode15i}.
209  @end itemize
210@end itemize
211
212Detailed information on the solvers are given in @nospell{L. F. Shampine} and
213@nospell{M. W. Reichelt}, @cite{The MATLAB ODE Suite}, SIAM Journal on
214Scientific Computing, Vol. 18, 1997, pp. 1–22,
215DOI: @url{https://doi.org/10.1137/S1064827594276424}.
216
217@DOCSTRING(ode45)
218
219@DOCSTRING(ode23)
220
221@DOCSTRING(ode23s)
222
223@DOCSTRING(ode15s)
224
225@DOCSTRING(ode15i)
226
227@DOCSTRING(decic)
228
229@DOCSTRING(odeset)
230
231@DOCSTRING(odeget)
232
233@DOCSTRING(odeplot)
234