Mercurial > hg > octave-nkf
annotate doc/interpreter/nonlin.txi @ 14953:711e64a11d36
Fix some segfaults
author | Max Brister <max@2bass.com> |
---|---|
date | Fri, 15 Jun 2012 13:15:48 -0500 |
parents | 72c96de7a403 |
children | e0525ecf156e |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12576
diff
changeset
|
1 @c Copyright (C) 1996-2012 John W. Eaton |
7018 | 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 the | |
7 @c Free Software Foundation; either version 3 of the License, or (at | |
8 @c your option) any later version. | |
9 @c | |
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT | |
11 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 @c 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 <http://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:: |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
26 * Minimizers:: |
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 | |
118 less efficient. In the example above we could compute the Jacobian | |
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 |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
175 potentially many variables use @code{fminunc}. @xref{Optimization}, for |
12576
a1e386b9ef4b
Spellcheck documentation for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
12574
diff
changeset
|
176 minimization with the presence of constraint functions. Note that searches |
12521
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
177 can be made for maxima by simply inverting the objective function |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
178 @tex |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
179 ($F_{max} = -F_{min}$). |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
180 @end tex |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
181 @ifnottex |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
182 (@code{Fto_max = -Fto_min}). |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
183 @end ifnottex |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
184 |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
185 @DOCSTRING(fminbnd) |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
186 |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
187 @DOCSTRING(fminunc) |
02e48856e486
Add fminbnd, fminunc functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
188 |