Mercurial > hg > octave-nkf
annotate doc/interpreter/nonlin.txi @ 12385:c468c5b902b3 release-3-4-x ss-3-3-92
version is now 3.3.92
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sun, 06 Feb 2011 06:27:19 -0500 |
parents | fd0a3ac60b0e |
children | 02e48856e486 |
rev | line source |
---|---|
11523 | 1 @c Copyright (C) 1996-2011 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 | |
24 Octave can solve sets of nonlinear equations of the form | |
25 @tex | |
26 $$ | |
27 f (x) = 0 | |
28 $$ | |
29 @end tex | |
6850 | 30 @ifnottex |
3294 | 31 |
32 @example | |
33 F (x) = 0 | |
34 @end example | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10668
diff
changeset
|
35 |
6850 | 36 @end ifnottex |
3294 | 37 |
38 @noindent | |
39 using the function @code{fsolve}, which is based on the @sc{Minpack} | |
6850 | 40 subroutine @code{hybrd}. This is an iterative technique so a starting |
41 point will have to be provided. This also has the consequence that | |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7359
diff
changeset
|
42 convergence is not guaranteed even if a solution exists. |
3294 | 43 |
3368 | 44 @DOCSTRING(fsolve) |
3294 | 45 |
46 Here is a complete example. To solve the set of equations | |
47 @tex | |
48 $$ | |
49 \eqalign{-2x^2 + 3xy + 4\sin(y) - 6 &= 0\cr | |
50 3x^2 - 2xy^2 + 3\cos(x) + 4 &= 0} | |
51 $$ | |
52 @end tex | |
10668
72585f1ca7a2
Replace @ifinfo with @ifnottex.
Rik <octave@nomad.inbox5.com>
parents:
9209
diff
changeset
|
53 @ifnottex |
3294 | 54 |
55 @example | |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
56 @group |
3294 | 57 -2x^2 + 3xy + 4 sin(y) = 6 |
58 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
|
59 @end group |
3294 | 60 @end example |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10668
diff
changeset
|
61 |
10668
72585f1ca7a2
Replace @ifinfo with @ifnottex.
Rik <octave@nomad.inbox5.com>
parents:
9209
diff
changeset
|
62 @end ifnottex |
3294 | 63 |
64 @noindent | |
65 you first need to write a function to compute the value of the given | |
66 function. For example: | |
67 | |
68 @example | |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
69 @group |
3294 | 70 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
|
71 y = zeros (2, 1); |
3294 | 72 y(1) = -2*x(1)^2 + 3*x(1)*x(2) + 4*sin(x(2)) - 6; |
73 y(2) = 3*x(1)^2 - 2*x(1)*x(2)^2 + 3*cos(x(1)) + 4; | |
74 endfunction | |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
75 @end group |
3294 | 76 @end example |
77 | |
78 Then, call @code{fsolve} with a specified initial condition to find the | |
79 roots of the system of equations. For example, given the function | |
80 @code{f} defined above, | |
81 | |
82 @example | |
7359 | 83 [x, fval, info] = fsolve (@@f, [1; 2]) |
3294 | 84 @end example |
85 | |
86 @noindent | |
87 results in the solution | |
88 | |
89 @example | |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
90 @group |
3294 | 91 x = |
92 | |
93 0.57983 | |
94 2.54621 | |
95 | |
7359 | 96 fval = |
97 | |
98 -5.7184e-10 | |
99 5.5460e-10 | |
100 | |
3294 | 101 info = 1 |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
102 @end group |
3294 | 103 @end example |
104 | |
6850 | 105 @noindent |
3294 | 106 A value of @code{info = 1} indicates that the solution has converged. |
107 | |
108 The function @code{perror} may be used to print English messages | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10668
diff
changeset
|
109 corresponding to the numeric error codes. For example: |
3294 | 110 |
111 @example | |
112 @group | |
113 perror ("fsolve", 1) | |
114 @print{} solution converged to requested tolerance | |
115 @end group | |
116 @end example | |
117 | |
6850 | 118 When no Jacobian is supplied (as in the example above) it is approximated |
119 numerically. This requires more function evaluations, and hence is | |
120 less efficient. In the example above we could compute the Jacobian | |
121 analytically as | |
3294 | 122 |
6850 | 123 @iftex |
124 @tex | |
125 $$ | |
126 \left[\matrix{ {\partial f_1 \over \partial x_1} & | |
127 {\partial f_1 \over \partial x_2} \cr | |
128 {\partial f_2 \over \partial x_1} & | |
129 {\partial f_2 \over \partial x_2} \cr}\right] = | |
130 \left[\matrix{ 3 x_2 - 4 x_1 & | |
131 4 \cos(x_2) + 3 x_1 \cr | |
132 -2 x_2^2 - 3 \sin(x_1) + 6 x_1 & | |
133 -4 x_1 x_2 \cr }\right] | |
134 $$ | |
135 @end tex | |
8828 | 136 and compute it with the following Octave function |
6850 | 137 @end iftex |
138 | |
139 @example | |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
140 @group |
11406
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
141 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
|
142 y = zeros (2, 1); |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
143 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
|
144 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
|
145 if (nargout == 2) |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
146 jac = zeros (2, 2); |
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 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
|
151 endif |
6850 | 152 endfunction |
9065
8207b833557f
Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
153 @end group |
6850 | 154 @end example |
155 | |
156 @noindent | |
8828 | 157 The Jacobian can then be used with the following call to @code{fsolve}: |
6850 | 158 |
159 @example | |
11406
a7e73f903ea6
nonlin.txi: correct docs for using Jacobian with fsolve
Tatsuro Matsuoka <tmacchant@yahoo.co.jp>
parents:
10828
diff
changeset
|
160 [x, fval, info] = fsolve (@@f, [1; 2], optimset ("jacobian", "on")); |
6850 | 161 @end example |
162 | |
163 @noindent | |
164 which gives the same solution as before. | |
165 | |
8515
ec2715c76039
fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
166 @DOCSTRING(fzero) |