Mercurial > octave
annotate doc/interpreter/nonlin.txi @ 31706:597f3ee61a48 stable
update Octave Project Developers copyright for the new year
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 06 Jan 2023 13:11:27 -0500 |
parents | 796f54d4ddbf |
children | 2e484f9f1f18 |
rev | line source |
---|---|
31706
597f3ee61a48
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
1 @c Copyright (C) 1996-2023 The Octave Project Developers |
7018 | 2 @c |
3 @c This file is part of Octave. | |
4 @c | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23885
diff
changeset
|
5 @c Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
6 @c under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23885
diff
changeset
|
7 @c the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
8 @c (at your option) any later version. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
9 @c |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
10 @c Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
11 @c WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 @c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 @c GNU General Public License for more details. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
14 @c |
7018 | 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 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23885
diff
changeset
|
17 @c <https://www.gnu.org/licenses/>. |
3294 | 18 |
4167 | 19 @node Nonlinear Equations |
3294 | 20 @chapter Nonlinear Equations |
21 @cindex nonlinear equations | |
22 @cindex equations, nonlinear | |
23 | |
12521
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
24 @menu |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
25 * Solvers:: |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
14895
diff
changeset
|
26 * Minimizers:: |
12521
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
27 @end menu |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
28 |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
29 @node Solvers |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
30 @section Solvers |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
31 |
3294 | 32 Octave can solve sets of nonlinear equations of the form |
33 @tex | |
34 $$ | |
35 f (x) = 0 | |
36 $$ | |
37 @end tex | |
6850 | 38 @ifnottex |
3294 | 39 |
40 @example | |
41 F (x) = 0 | |
42 @end example | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10668
diff
changeset
|
43 |
6850 | 44 @end ifnottex |
3294 | 45 |
46 @noindent | |
12521
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
47 using the function @code{fsolve}, which is based on the @sc{minpack} |
6850 | 48 subroutine @code{hybrd}. This is an iterative technique so a starting |
12521
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
49 point must be provided. This also has the consequence that |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7359
diff
changeset
|
50 convergence is not guaranteed even if a solution exists. |
3294 | 51 |
3368 | 52 @DOCSTRING(fsolve) |
3294 | 53 |
12521
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
54 The following is a complete example. To solve the set of equations |
3294 | 55 @tex |
56 $$ | |
57 \eqalign{-2x^2 + 3xy + 4\sin(y) - 6 &= 0\cr | |
58 3x^2 - 2xy^2 + 3\cos(x) + 4 &= 0} | |
59 $$ | |
60 @end tex | |
10668
72585f1ca7a2
Replace @ifinfo with @ifnottex.
Rik <octave@nomad.inbox5.com>
parents:
9209
diff
changeset
|
61 @ifnottex |
3294 | 62 |
63 @example | |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
64 @group |
3294 | 65 -2x^2 + 3xy + 4 sin(y) = 6 |
66 3x^2 - 2xy^2 + 3 cos(x) = -4 | |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
67 @end group |
3294 | 68 @end example |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10668
diff
changeset
|
69 |
10668
72585f1ca7a2
Replace @ifinfo with @ifnottex.
Rik <octave@nomad.inbox5.com>
parents:
9209
diff
changeset
|
70 @end ifnottex |
3294 | 71 |
72 @noindent | |
73 you first need to write a function to compute the value of the given | |
74 function. For example: | |
75 | |
76 @example | |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
77 @group |
3294 | 78 function y = f (x) |
11406
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
79 y = zeros (2, 1); |
3294 | 80 y(1) = -2*x(1)^2 + 3*x(1)*x(2) + 4*sin(x(2)) - 6; |
81 y(2) = 3*x(1)^2 - 2*x(1)*x(2)^2 + 3*cos(x(1)) + 4; | |
82 endfunction | |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
83 @end group |
3294 | 84 @end example |
85 | |
86 Then, call @code{fsolve} with a specified initial condition to find the | |
87 roots of the system of equations. For example, given the function | |
88 @code{f} defined above, | |
89 | |
90 @example | |
7359 | 91 [x, fval, info] = fsolve (@@f, [1; 2]) |
3294 | 92 @end example |
93 | |
94 @noindent | |
95 results in the solution | |
96 | |
97 @example | |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
98 @group |
3294 | 99 x = |
100 | |
101 0.57983 | |
102 2.54621 | |
103 | |
7359 | 104 fval = |
105 | |
106 -5.7184e-10 | |
107 5.5460e-10 | |
108 | |
3294 | 109 info = 1 |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
110 @end group |
3294 | 111 @end example |
112 | |
6850 | 113 @noindent |
3294 | 114 A value of @code{info = 1} indicates that the solution has converged. |
115 | |
6850 | 116 When no Jacobian is supplied (as in the example above) it is approximated |
117 numerically. This requires more function evaluations, and hence is | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
118 less efficient. In the example above we could compute the Jacobian |
6850 | 119 analytically as |
3294 | 120 |
6850 | 121 @iftex |
122 @tex | |
123 $$ | |
124 \left[\matrix{ {\partial f_1 \over \partial x_1} & | |
125 {\partial f_1 \over \partial x_2} \cr | |
126 {\partial f_2 \over \partial x_1} & | |
127 {\partial f_2 \over \partial x_2} \cr}\right] = | |
128 \left[\matrix{ 3 x_2 - 4 x_1 & | |
129 4 \cos(x_2) + 3 x_1 \cr | |
130 -2 x_2^2 - 3 \sin(x_1) + 6 x_1 & | |
131 -4 x_1 x_2 \cr }\right] | |
132 $$ | |
133 @end tex | |
8828 | 134 and compute it with the following Octave function |
6850 | 135 @end iftex |
136 | |
137 @example | |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
138 @group |
11406
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
139 function [y, jac] = f (x) |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
140 y = zeros (2, 1); |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
141 y(1) = -2*x(1)^2 + 3*x(1)*x(2) + 4*sin(x(2)) - 6; |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
142 y(2) = 3*x(1)^2 - 2*x(1)*x(2)^2 + 3*cos(x(1)) + 4; |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
143 if (nargout == 2) |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
144 jac = zeros (2, 2); |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
145 jac(1,1) = 3*x(2) - 4*x(1); |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
146 jac(1,2) = 4*cos(x(2)) + 3*x(1); |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
147 jac(2,1) = -2*x(2)^2 - 3*sin(x(1)) + 6*x(1); |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
148 jac(2,2) = -4*x(1)*x(2); |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
149 endif |
6850 | 150 endfunction |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
151 @end group |
6850 | 152 @end example |
153 | |
154 @noindent | |
8828 | 155 The Jacobian can then be used with the following call to @code{fsolve}: |
6850 | 156 |
157 @example | |
11406
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
158 [x, fval, info] = fsolve (@@f, [1; 2], optimset ("jacobian", "on")); |
6850 | 159 @end example |
160 | |
161 @noindent | |
162 which gives the same solution as before. | |
163 | |
8515
ec2715c76039
fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
164 @DOCSTRING(fzero) |
12521
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
165 |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
166 @node Minimizers |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
167 @section Minimizers |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
168 @cindex local minimum |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
169 @cindex finding minimums |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
170 |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
171 Often it is useful to find the minimum value of a function rather than just |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
172 the zeroes where it crosses the x-axis. @code{fminbnd} is designed for the |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
173 simpler, but very common, case of a univariate function where the interval |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
174 to search is bounded. For unbounded minimization of a function with |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14138
diff
changeset
|
175 potentially many variables use @code{fminunc} or @code{fminsearch}. The two |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14138
diff
changeset
|
176 functions use different internal algorithms and some knowledge of the objective |
22299
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
177 function is required. For functions which can be differentiated, |
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
178 @code{fminunc} is appropriate. For functions with discontinuities, or for |
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
179 which a gradient search would fail, use @code{fminsearch}. |
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
180 @xref{Optimization}, for minimization with the presence of constraint |
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
181 functions. Note that searches can be made for maxima by simply inverting the |
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
182 objective function |
12521
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
183 @tex |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
184 ($F_{max} = -F_{min}$). |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
185 @end tex |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
186 @ifnottex |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
187 (@code{Fto_max = -Fto_min}). |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
188 @end ifnottex |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
189 |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
190 @DOCSTRING(fminbnd) |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
191 |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
192 @DOCSTRING(fminunc) |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
193 |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14138
diff
changeset
|
194 @DOCSTRING(fminsearch) |
23885
86a49caa5100
new function humps (bug #33935)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
23220
diff
changeset
|
195 |
86a49caa5100
new function humps (bug #33935)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
23220
diff
changeset
|
196 The function @code{humps} is a useful function for testing zero and |
86a49caa5100
new function humps (bug #33935)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
23220
diff
changeset
|
197 extrema finding functions. |
86a49caa5100
new function humps (bug #33935)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
23220
diff
changeset
|
198 |
86a49caa5100
new function humps (bug #33935)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
23220
diff
changeset
|
199 @DOCSTRING(humps) |