3294
|
1 @c Copyright (C) 1996, 1997 John W. Eaton |
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
|
5 @node Differential Equations, Optimization, Quadrature, Top |
|
6 @chapter Differential Equations |
|
7 |
|
8 Octave has two built-in functions for solving differential equations. |
|
9 Both are based on reliable ODE solvers written in Fortran. |
|
10 |
|
11 @menu |
|
12 * Ordinary Differential Equations:: |
|
13 * Differential-Algebraic Equations:: |
|
14 @end menu |
|
15 |
|
16 @cindex Differential Equations |
|
17 @cindex ODE |
|
18 @cindex DAE |
|
19 |
|
20 @node Ordinary Differential Equations, Differential-Algebraic Equations, Differential Equations, Differential Equations |
|
21 @section Ordinary Differential Equations |
|
22 |
|
23 The function @code{lsode} can be used to solve ODEs of the form |
|
24 @iftex |
|
25 @tex |
|
26 $$ |
|
27 {dx\over dt} = f (x, t) |
|
28 $$ |
|
29 @end tex |
|
30 @end iftex |
|
31 @ifinfo |
|
32 |
|
33 @example |
|
34 dx |
|
35 -- = f (x, t) |
|
36 dt |
|
37 @end example |
|
38 @end ifinfo |
|
39 |
|
40 @noindent |
|
41 using Hindmarsh's ODE solver @sc{Lsode}. |
|
42 |
|
43 @deftypefn {Loadable Function} {} lsode (@var{fcn}, @var{x0}, @var{t}, @var{t_crit}) |
|
44 Return a matrix of @var{x} as a function of @var{t}, given the initial |
|
45 state of the system @var{x0}. Each row in the result matrix corresponds |
|
46 to one of the elements in the vector @var{t}. The first element of |
|
47 @var{t} corresponds to the initial state @var{x0}, so that the first row |
|
48 of the output is @var{x0}. |
|
49 |
|
50 The first argument, @var{fcn}, is a string that names the function to |
|
51 call to compute the vector of right hand sides for the set of equations. |
|
52 It must have the form |
|
53 |
|
54 @example |
|
55 @var{xdot} = f (@var{x}, @var{t}) |
|
56 @end example |
|
57 |
|
58 @noindent |
|
59 where @var{xdot} and @var{x} are vectors and @var{t} is a scalar. |
|
60 |
|
61 The fourth argument is optional, and may be used to specify a set of |
|
62 times that the ODE solver should not integrate past. It is useful for |
|
63 avoiding difficulties with singularities and points where there is a |
|
64 discontinuity in the derivative. |
|
65 @end deftypefn |
|
66 |
|
67 Here is an example of solving a set of three differential equations using |
|
68 @code{lsode}. Given the function |
|
69 |
|
70 @cindex oregonator |
|
71 |
|
72 @example |
|
73 @group |
|
74 function xdot = f (x, t) |
|
75 |
|
76 xdot = zeros (3,1); |
|
77 |
|
78 xdot(1) = 77.27 * (x(2) - x(1)*x(2) + x(1) \ |
|
79 - 8.375e-06*x(1)^2); |
|
80 xdot(2) = (x(3) - x(1)*x(2) - x(2)) / 77.27; |
|
81 xdot(3) = 0.161*(x(1) - x(3)); |
|
82 |
|
83 endfunction |
|
84 @end group |
|
85 @end example |
|
86 |
|
87 @noindent |
|
88 and the initial condition @code{x0 = [ 4; 1.1; 4 ]}, the set of |
|
89 equations can be integrated using the command |
|
90 |
|
91 @example |
|
92 @group |
|
93 t = linspace (0, 500, 1000); |
|
94 |
|
95 y = lsode ("f", x0, t); |
|
96 @end group |
|
97 @end example |
|
98 |
|
99 If you try this, you will see that the value of the result changes |
|
100 dramatically between @var{t} = 0 and 5, and again around @var{t} = 305. |
|
101 A more efficient set of output points might be |
|
102 |
|
103 @example |
|
104 @group |
|
105 t = [0, logspace (-1, log10(303), 150), \ |
|
106 logspace (log10(304), log10(500), 150)]; |
|
107 @end group |
|
108 @end example |
|
109 |
|
110 @deftypefn {Loadable Function} {} lsode_options (@var{opt}, @var{val}) |
|
111 When called with two arguments, this function allows you set options |
|
112 parameters for the function @code{lsode}. Given one argument, |
|
113 @code{lsode_options} returns the value of the corresponding option. If |
|
114 no arguments are supplied, the names of all the available options and |
|
115 their current values are displayed. |
|
116 @end deftypefn |
|
117 |
|
118 See Alan C. Hindmarsh, @cite{ODEPACK, A Systematized Collection of ODE |
|
119 Solvers}, in Scientific Computing, R. S. Stepleman, editor, (1983) for |
|
120 more information about the inner workings of @code{lsode}. |
|
121 |
|
122 @node Differential-Algebraic Equations, , Ordinary Differential Equations, Differential Equations |
|
123 @section Differential-Algebraic Equations |
|
124 |
|
125 The function @code{dassl} can be used to solve DAEs of the form |
|
126 @iftex |
|
127 @tex |
|
128 $$ |
|
129 0 = f (\dot{x}, x, t), \qquad x(t=0) = x_0, \dot{x}(t=0) = \dot{x}_0 |
|
130 $$ |
|
131 @end tex |
|
132 @end iftex |
|
133 @ifinfo |
|
134 |
|
135 @example |
|
136 0 = f (x-dot, x, t), x(t=0) = x_0, x-dot(t=0) = x-dot_0 |
|
137 @end example |
|
138 @end ifinfo |
|
139 |
|
140 @noindent |
|
141 using Petzold's DAE solver @sc{Dassl}. |
|
142 |
|
143 @deftypefn {Loadable Function} {[@var{x}, @var{xdot}] =} dassl (@var{fcn}, @var{x0}, @var{xdot0}, @var{t}, @var{t_crit}) |
|
144 Return a matrix of states and their first derivatives with respect to |
|
145 @var{t}. Each row in the result matrices correspond to one of the |
|
146 elements in the vector @var{t}. The first element of @var{t} |
|
147 corresponds to the initial state @var{x0} and derivative @var{xdot0}, so |
|
148 that the first row of the output @var{x} is @var{x0} and the first row |
|
149 of the output @var{xdot} is @var{xdot0}. |
|
150 |
|
151 The first argument, @var{fcn}, is a string that names the function to |
|
152 call to compute the vector of residuals for the set of equations. |
|
153 It must have the form |
|
154 |
|
155 @example |
|
156 @var{res} = f (@var{x}, @var{xdot}, @var{t}) |
|
157 @end example |
|
158 |
|
159 @noindent |
|
160 where @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a |
|
161 scalar. |
|
162 |
|
163 The second and third arguments to @code{dassl} specify the initial |
|
164 condition of the states and their derivatives, and the fourth argument |
|
165 specifies a vector of output times at which the solution is desired, |
|
166 including the time corresponding to the initial condition. |
|
167 |
|
168 The set of initial states and derivatives are not strictly required to |
|
169 be consistent. In practice, however, @sc{Dassl} is not very good at |
|
170 determining a consistent set for you, so it is best if you ensure that |
|
171 the initial values result in the function evaluating to zero. |
|
172 |
|
173 The fifth argument is optional, and may be used to specify a set of |
|
174 times that the DAE solver should not integrate past. It is useful for |
|
175 avoiding difficulties with singularities and points where there is a |
|
176 discontinuity in the derivative. |
|
177 @end deftypefn |
|
178 |
|
179 @deftypefn {Loadable Function} {} dassl_options (@var{opt}, @var{val}) |
|
180 When called with two arguments, this function allows you set options |
|
181 parameters for the function @code{lsode}. Given one argument, |
|
182 @code{dassl_options} returns the value of the corresponding option. If |
|
183 no arguments are supplied, the names of all the available options and |
|
184 their current values are displayed. |
|
185 @end deftypefn |
|
186 |
|
187 See K. E. Brenan, et al., @cite{Numerical Solution of Initial-Value |
|
188 Problems in Differential-Algebraic Equations}, North-Holland (1989) for |
|
189 more information about the implementation of @sc{Dassl}. |