Mercurial > hg > octave-nkf
annotate doc/interpreter/nonlin.txi @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 8463d1a2e544 |
children | 8207b833557f |
rev | line source |
---|---|
8920 | 1 @c Copyright (C) 1996, 1997, 2007, 2008, 2009 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 @iftex | |
26 @tex | |
27 $$ | |
28 f (x) = 0 | |
29 $$ | |
30 @end tex | |
31 @end iftex | |
6850 | 32 @ifnottex |
3294 | 33 |
34 @example | |
35 F (x) = 0 | |
36 @end example | |
6850 | 37 @end ifnottex |
3294 | 38 |
39 @noindent | |
40 using the function @code{fsolve}, which is based on the @sc{Minpack} | |
6850 | 41 subroutine @code{hybrd}. This is an iterative technique so a starting |
42 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
|
43 convergence is not guaranteed even if a solution exists. |
3294 | 44 |
3368 | 45 @DOCSTRING(fsolve) |
3294 | 46 |
47 Here is a complete example. To solve the set of equations | |
48 @iftex | |
49 @tex | |
50 $$ | |
51 \eqalign{-2x^2 + 3xy + 4\sin(y) - 6 &= 0\cr | |
52 3x^2 - 2xy^2 + 3\cos(x) + 4 &= 0} | |
53 $$ | |
54 @end tex | |
55 @end iftex | |
56 @ifinfo | |
57 | |
58 @example | |
59 -2x^2 + 3xy + 4 sin(y) = 6 | |
60 3x^2 - 2xy^2 + 3 cos(x) = -4 | |
61 @end example | |
62 @end ifinfo | |
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 | |
69 function y = f (x) | |
70 y(1) = -2*x(1)^2 + 3*x(1)*x(2) + 4*sin(x(2)) - 6; | |
71 y(2) = 3*x(1)^2 - 2*x(1)*x(2)^2 + 3*cos(x(1)) + 4; | |
72 endfunction | |
73 @end example | |
74 | |
75 Then, call @code{fsolve} with a specified initial condition to find the | |
76 roots of the system of equations. For example, given the function | |
77 @code{f} defined above, | |
78 | |
79 @example | |
7359 | 80 [x, fval, info] = fsolve (@@f, [1; 2]) |
3294 | 81 @end example |
82 | |
83 @noindent | |
84 results in the solution | |
85 | |
86 @example | |
87 x = | |
88 | |
89 0.57983 | |
90 2.54621 | |
91 | |
7359 | 92 fval = |
93 | |
94 -5.7184e-10 | |
95 5.5460e-10 | |
96 | |
3294 | 97 info = 1 |
98 @end example | |
99 | |
6850 | 100 @noindent |
3294 | 101 A value of @code{info = 1} indicates that the solution has converged. |
102 | |
103 The function @code{perror} may be used to print English messages | |
104 corresponding to the numeric error codes. For example, | |
105 | |
106 @example | |
107 @group | |
108 perror ("fsolve", 1) | |
109 @print{} solution converged to requested tolerance | |
110 @end group | |
111 @end example | |
112 | |
6850 | 113 When no Jacobian is supplied (as in the example above) it is approximated |
114 numerically. This requires more function evaluations, and hence is | |
115 less efficient. In the example above we could compute the Jacobian | |
116 analytically as | |
3294 | 117 |
6850 | 118 @iftex |
119 @tex | |
120 $$ | |
121 \left[\matrix{ {\partial f_1 \over \partial x_1} & | |
122 {\partial f_1 \over \partial x_2} \cr | |
123 {\partial f_2 \over \partial x_1} & | |
124 {\partial f_2 \over \partial x_2} \cr}\right] = | |
125 \left[\matrix{ 3 x_2 - 4 x_1 & | |
126 4 \cos(x_2) + 3 x_1 \cr | |
127 -2 x_2^2 - 3 \sin(x_1) + 6 x_1 & | |
128 -4 x_1 x_2 \cr }\right] | |
129 $$ | |
130 @end tex | |
8828 | 131 and compute it with the following Octave function |
6850 | 132 @end iftex |
133 | |
134 @example | |
135 function J = jacobian(x) | |
136 J(1,1) = 3*x(2) - 4*x(1); | |
137 J(1,2) = 4*cos(x(2)) + 3*x(1); | |
138 J(2,1) = -2*x(2)^2 - 3*sin(x(1)) + 6*x(1); | |
139 J(2,2) = -4*x(1)*x(2); | |
140 endfunction | |
141 @end example | |
142 | |
143 @noindent | |
8828 | 144 The Jacobian can then be used with the following call to @code{fsolve}: |
6850 | 145 |
146 @example | |
7359 | 147 [x, fval, info] = fsolve (@{@@f, @@jacobian@}, [1; 2]); |
6850 | 148 @end example |
149 | |
150 @noindent | |
151 which gives the same solution as before. | |
152 | |
8515
ec2715c76039
fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
153 @DOCSTRING(fzero) |