annotate scripts/optimization/fsolve.m @ 8693:e5ffb52c9c61

improve fsolve and add ComplexEqn option
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 06 Feb 2009 13:45:48 +0100
parents 9c092b111b1d
children 5ce12bca4c51
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
1 ## Copyright (C) 2008, 2009 VZLU Prague, a.s.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
2 ##
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
3 ## This file is part of Octave.
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
4 ##
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 ## your option) any later version.
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 ##
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
13 ## General Public License for more details.
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ##
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18 ##
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 ## Author: Jaroslav Hajek <highegg@gmail.com>
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
21 ## -*- texinfo -*-
8513
352d3245d4c1 fsolve.m: doc fix
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
22 ## @deftypefn{Function File} {} fsolve (@var{fcn}, @var{x0}, @var{options})
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
23 ## @deftypefnx{Function File} {[@var{x}, @var{fvec}, @var{info}, @var{output}, @var{fjac}]} = fsolve (@var{fcn}, @dots{})
8513
352d3245d4c1 fsolve.m: doc fix
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
24 ## Solve a system of nonlinear equations defined by the function @var{fcn}.
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
25 ## @var{fcn} should accepts a vector (array) defining the unknown variables,
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
26 ## and return a vector of left-hand sides of the equations. Right-hand sides
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
27 ## are defined to be zeros.
8514
39867b4aca52 fsolve.m: additional doc fix
John W. Eaton <jwe@octave.org>
parents: 8513
diff changeset
28 ## In other words, this function attempts to determine a vector @var{x} such
39867b4aca52 fsolve.m: additional doc fix
John W. Eaton <jwe@octave.org>
parents: 8513
diff changeset
29 ## that @code{@var{fcn} (@var{x})} gives (approximately) all zeros.
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
30 ## @var{x0} determines a starting guess. The shape of @var{x0} is preserved
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
31 ## in all calls to @var{fcn}, but otherwise it is treated as a column vector.
8515
ec2715c76039 fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents: 8514
diff changeset
32 ## @var{options} is a structure specifying additional options.
ec2715c76039 fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents: 8514
diff changeset
33 ## Currently, @code{fsolve} recognizes these options:
ec2715c76039 fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents: 8514
diff changeset
34 ## @code{"FunValCheck"}, @code{"OutputFcn"}, @code{"TolX"},
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
35 ## @code{"TolFun"}, @code{"MaxIter"}, @code{"MaxFunEvals"},
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
36 ## @code{"Jacobian"}, @code{"Updating"} and @code{"ComplexEqn"}.
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
37 ##
8513
352d3245d4c1 fsolve.m: doc fix
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
38 ## If @code{"Jacobian"} is @code{"on"}, it specifies that @var{fcn},
352d3245d4c1 fsolve.m: doc fix
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
39 ## called with 2 output arguments, also returns the Jacobian matrix
352d3245d4c1 fsolve.m: doc fix
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
40 ## of right-hand sides at the requested point. @code{"TolX"} specifies
352d3245d4c1 fsolve.m: doc fix
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
41 ## the termination tolerance in the unknown variables, while
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
42 ## @code{"TolFun"} is a tolerance for equations. Default is @code{1e-7}
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
43 ## for both @code{"TolX"} and @code{"TolFun"}.
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
44 ## If @code{"Updating"} is "on", the function will attempt to use Broyden
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
45 ## updates to update the Jacobian, in order to reduce the amount of jacobian
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
46 ## calculations.
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
47 ## If your user function always calculates the Jacobian (regardless of number
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
48 ## of output arguments), this option provides no advantage and should be set to
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
49 ## false.
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
50 ##
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
51 ## @code{"ComplexEqn"} is @code{"on"}, @code{fsolve} will attempt to solve
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
52 ## complex equations in complex variables, assuming that the equations posess a
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
53 ## complex derivative (i.e. are holomorphic). If this is not what you want,
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
54 ## should unpack the real and imaginary parts of the system to get a real
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
55 ## system.
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
56 ##
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
57 ## For description of the other options, see @code{optimset}.
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
58 ##
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
59 ## On return, @var{fval} contains the value of the function @var{fcn}
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
60 ## evaluated at @var{x}, and @var{info} may be one of the following values:
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
61 ##
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
62 ## @table @asis
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
63 ## @item 1
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
64 ## Converged to a solution point. Relative residual error is less than specified
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
65 ## by TolFun.
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
66 ## @item 2
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
67 ## Last relative step size was less that TolX.
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
68 ## @item 3
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
69 ## Last relative decrease in residual was less than TolF.
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
70 ## @item 0
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
71 ## Iteration limit exceeded.
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
72 ## @item -3
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
73 ## The trust region radius became excessively small.
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
74 ## @end table
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
75 ##
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
76 ## Note: If you only have a single nonlinear equation of one variable, using
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
77 ## @code{fzero} is usually a much better idea.
8513
352d3245d4c1 fsolve.m: doc fix
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
78 ## @seealso{fzero, optimset}
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
79 ## @end deftypefn
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
80
8648
ff61b53eb294 optimization: use PKG_ADD: comments instead of PKG_ADD file
John W. Eaton <jwe@octave.org>
parents: 8647
diff changeset
81 ## PKG_ADD: __all_opts__ ("fsolve");
ff61b53eb294 optimization: use PKG_ADD: comments instead of PKG_ADD file
John W. Eaton <jwe@octave.org>
parents: 8647
diff changeset
82
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
83 function [x, fvec, info, output, fjac] = fsolve (fcn, x0, options = struct ())
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
84
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
85 ## Get default options if requested.
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
86 if (nargin == 1 && ischar (fcn) && strcmp (fcn, 'defaults'))
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
87 x = optimset ("MaxIter", 400, "MaxFunEvals", Inf, \
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8616
diff changeset
88 "Jacobian", "off", "TolX", 1.5e-8, "TolFun", 1.5e-8,
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
89 "OutputFcn", [], "Updating", "on", "FunValCheck", "off",
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
90 "ComplexEqn", "off");
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
91 return;
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
92 endif
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
93
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
94 if (nargin < 2 || nargin > 3 || ! ismatrix (x0))
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
95 print_usage ();
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
96 endif
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
97
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
98 if (ischar (fcn))
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
99 fcn = str2func (fcn);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
100 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
101
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
102 xsiz = size (x0);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
103 n = numel (x0);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
104
8467
77b8d4aa2743 fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents: 8466
diff changeset
105 has_jac = strcmpi (optimget (options, "Jacobian", "off"), "on");
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
106 maxiter = optimget (options, "MaxIter", 400);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
107 maxfev = optimget (options, "MaxFunEvals", Inf);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
108 outfcn = optimget (options, "OutputFcn");
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
109 updating = strcmpi (optimget (options, "Updating", "on"), "on");
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
110 complexeqn = strcmpi (optimget (options, "ComplexEqn", "off"), "on");
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
111
8467
77b8d4aa2743 fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents: 8466
diff changeset
112 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on");
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
113
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
114 if (funvalchk)
8604
43f831758d42 fix typo in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8596
diff changeset
115 ## Replace fcn with a guarded version.
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
116 fcn = @(x) guarded_eval (fcn, x, complexeqn);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
117 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
118
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
119 ## These defaults are rather stringent. I think that normally, user
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
120 ## prefers accuracy to performance.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
121
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
122 macheps = eps (class (x0));
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
123
8613
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
124 tolx = optimget (options, "TolX", sqrt (macheps));
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
125 tolf = optimget (options, "TolFun", sqrt (macheps));
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
126
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
127 factor = 100;
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
128 ## FIXME: TypicalX corresponds to user scaling (???)
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
129 autodg = true;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
130
8467
77b8d4aa2743 fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents: 8466
diff changeset
131 niter = 1;
77b8d4aa2743 fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents: 8466
diff changeset
132 nfev = 0;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
133
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
134 x = x0(:);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
135 info = 0;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
136
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
137 ## Outer loop.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
138 while (niter < maxiter && nfev < maxfev && ! info)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
139
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
140 ## Calculate function value and Jacobian (possibly via FD).
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
141 ## Handle arbitrary shapes of x and f and remember them.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
142 if (has_jac)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
143 [fvec, fjac] = fcn (reshape (x, xsiz));
8592
dacfd030633a handle sparse jacobians in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8590
diff changeset
144 ## If the jacobian is sparse, disable Broyden updating.
dacfd030633a handle sparse jacobians in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8590
diff changeset
145 if (issparse (fjac))
dacfd030633a handle sparse jacobians in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8590
diff changeset
146 updating = false;
dacfd030633a handle sparse jacobians in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8590
diff changeset
147 endif
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
148 nfev ++;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
149 else
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
150 [fvec, fjac] = __fdjac__ (fcn, reshape (x, xsiz));
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
151 nfev += 1 + length (x);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
152 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
153 fsiz = size (fvec);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
154 fvec = fvec(:);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
155 fn = norm (fvec);
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
156 m = length (fvec);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
157 n = length (x);
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
158
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
159 ## For square and overdetermined systems, we update a (pivoted) QR
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
160 ## factorization of the jacobian to avoid solving a full system in each
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
161 ## step. In this case, we pass a triangular matrix to __dogleg__.
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
162 ## Pivoted QR is used for slightly better robustness and invariance
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
163 ## w.r.t. permutations of variables.
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
164 useqr = updating && m >= n && n > 10;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
165
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
166 if (useqr)
8616
3d75d717cbe0 do not pivot by default in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8615
diff changeset
167 ## FIXME: Currently, pivoting is mostly useless because the \ operator
3d75d717cbe0 do not pivot by default in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8615
diff changeset
168 ## cannot exploit the resulting props of the triangular factor.
3d75d717cbe0 do not pivot by default in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8615
diff changeset
169 ## Unpivoted QR is significantly faster so it doesn't seem right to pivot
3d75d717cbe0 do not pivot by default in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8615
diff changeset
170 ## just to get invariance. Original MINPACK didn't pivot either, at least
3d75d717cbe0 do not pivot by default in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8615
diff changeset
171 ## when qr updating was used.
3d75d717cbe0 do not pivot by default in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8615
diff changeset
172 [q, r] = qr (fjac, 0);
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
173 endif
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
174
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
175 ## Get column norms, use them as scaling factors.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
176 jcn = norm (fjac, 'columns').';
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
177 if (niter == 1)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
178 if (autodg)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
179 dg = jcn;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
180 dg(dg == 0) = 1;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
181 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
182 xn = norm (dg .* x);
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
183 ## FIXME: something better?
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
184 delta = max (factor * xn, 1);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
185 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
186
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
187 ## Rescale if necessary.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
188 if (autodg)
8613
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
189 ## FIXME: the original minpack used the following rescaling strategy:
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
190 ## dg = max (dg, jcn);
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
191 ## but it seems not good if we start with a bad guess yielding jacobian
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
192 ## columns with large norms that later decrease, because the corresponding
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
193 ## variable will still be overscaled. So instead, we only give the old
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
194 ## scaling a small momentum, but do not honor it.
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
195
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
196 dg = max (0.1*dg, jcn);
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
197
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
198 ## It also seems that in the case of fast (and inhomogeneously) changing
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
199 ## jacobian, the Broyden updates are of little use, so maybe we could
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
200 ## skip them if a big disproportional change is expected. The question is,
38482007c834 relax scaling in fsolve and use class-dependent default tolerances
Jaroslav Hajek <highegg@gmail.com>
parents: 8604
diff changeset
201 ## of course, how to define the above terms :)
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
202 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
203
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
204 nfail = 0;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
205 nsuc = 0;
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
206 ## Inner loop.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
207 while (niter <= maxiter && nfev < maxfev && ! info)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
208
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
209 if (useqr)
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
210 tr_mat = r;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
211 tr_vec = q'*fvec;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
212 else
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
213 tr_mat = fjac;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
214 tr_vec = fvec;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
215 endif
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
216
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
217 ## Get trust-region model (dogleg) minimizer.
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
218 if (useqr)
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
219 qtf = q'*fvec;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
220 s = - __dogleg__ (r, qtf, dg, delta);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
221 w = qtf + r * s;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
222 else
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
223 s = - __dogleg__ (fjac, fvec, dg, delta);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
224 w = fvec + fjac * s;
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
225 endif
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
226
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
227 sn = norm (dg .* s);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
228 if (niter == 1)
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
229 delta = min (delta, sn);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
230 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
231
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
232 fvec1 = fcn (reshape (x + s, xsiz)) (:);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
233 fn1 = norm (fvec1);
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
234 nfev ++;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
235
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
236 if (fn1 < fn)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
237 ## Scaled actual reduction.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
238 actred = 1 - (fn1/fn)^2;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
239 else
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
240 actred = -1;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
241 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
242
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
243 ## Scaled predicted reduction, and ratio.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
244 t = norm (w);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
245 if (t < fn)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
246 prered = 1 - (t/fn)^2;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
247 ratio = actred / prered;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
248 else
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
249 prered = 0;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
250 ratio = 0;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
251 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
252
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
253 ## Update delta.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
254 if (ratio < 0.1)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
255 nsuc = 0;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
256 nfail ++;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
257 delta *= 0.5;
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
258 if (delta <= 1e1*macheps*xn)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
259 ## Trust region became uselessly small.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
260 info = -3;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
261 break;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
262 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
263 else
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
264 nfail = 0;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
265 nsuc ++;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
266 if (abs (1-ratio) <= 0.1)
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
267 delta = 2*sn;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
268 elseif (ratio >= 0.5 || nsuc > 1)
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
269 delta = max (delta, 2*sn);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
270 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
271 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
272
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
273 if (ratio >= 1e-4)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
274 ## Successful iteration.
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
275 x += s;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
276 xn = norm (dg .* x);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
277 fvec = fvec1;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
278 fn = fn1;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
279 niter ++;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
280 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
281
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
282 ## FIXME: should outputfcn be only called after a successful iteration?
8615
7baacb6a8a65 fix typo in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8613
diff changeset
283 if (! isempty (outfcn))
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
284 optimvalues.iter = niter;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
285 optimvalues.funccount = nfev;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
286 optimvalues.fval = fn;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
287 optimvalues.searchdirection = s;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
288 state = 'iter';
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
289 stop = outfcn (x, optimvalues, state);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
290 if (stop)
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
291 info = -1;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
292 break;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
293 endif
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
294 endif
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
295
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
296 ## Tests for termination conditions. A mysterious place, anything
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
297 ## can happen if you change something here...
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
298
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
299 ## The rule of thumb (which I'm not sure M*b is quite following)
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
300 ## is that for a tolerance that depends on scaling, only 0 makes
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
301 ## sense as a default value. But 0 usually means uselessly long
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
302 ## iterations, so we need scaling-independent tolerances wherever
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
303 ## possible.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
304
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
305 ## FIXME -- why tolf*n*xn? If abs (e) ~ abs(x) * eps is a vector
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
306 ## of perturbations of x, then norm (fjac*e) <= eps*n*xn, i.e. by
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
307 ## tolf ~ eps we demand as much accuracy as we can expect.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
308 if (fn <= tolf*n*xn)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
309 info = 1;
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
310 ## The following tests done only after successful step.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
311 elseif (actred > 0)
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
312 ## This one is classic. Note that we use scaled variables again,
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
313 ## but compare to scaled step, so nothing bad.
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
314 if (sn <= tolx*xn)
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
315 info = 2;
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
316 ## Again a classic one. It seems weird to use the same tolf
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
317 ## for two different tests, but that's what M*b manual appears
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
318 ## to say.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
319 elseif (actred < tolf)
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
320 info = 3;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
321 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
322 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
323
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
324 ## Criterion for recalculating jacobian.
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
325 if (! updating || nfail == 2)
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
326 break;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
327 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
328
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
329 ## Compute the scaled Broyden update.
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
330 if (useqr)
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
331 u = (fvec1 - q*w) / sn;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
332 v = dg .* ((dg .* s) / sn);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
333
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
334 ## Update the QR factorization.
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
335 [q, r] = qrupdate (q, r, u, v);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
336 else
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
337 u = (fvec1 - w);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
338 v = dg .* ((dg .* s) / sn);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
339
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
340 ## update the jacobian
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
341 fjac += u * v';
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
342 endif
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
343 endwhile
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
344 endwhile
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
345
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
346 ## Restore original shapes.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
347 x = reshape (x, xsiz);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
348 fvec = reshape (fvec, fsiz);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
349
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
350 output.iterations = niter;
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
351 output.funcCount = nfev;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
352
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
353 endfunction
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
354
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
355 ## An assistant function that evaluates a function handle and checks for
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
356 ## bad results.
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
357 function [fx, jx] = guarded_eval (fun, x, complexeqn)
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
358 if (nargout > 1)
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
359 [fx, jx] = fun (x);
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
360 else
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
361 fx = fun (x);
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
362 jx = []
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
363 endif
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
364
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
365 if (! complexeqn && ! (all (isreal (fx(:))) && all (isreal (jx(:)))))
8468
866492035ecf fsolve.m, fzero.m: undo part of previous change
John W. Eaton <jwe@octave.org>
parents: 8467
diff changeset
366 error ("fsolve:notreal", "fsolve: non-real value encountered");
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
367 elseif (complexeqn && ! (all (isnumeric (fx(:))) && all (isnumeric(jx(:)))))
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
368 error ("fsolve:notnum", "fsolve: non-numeric value encountered");
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
369 elseif (any (isnan (fx(:))))
8468
866492035ecf fsolve.m, fzero.m: undo part of previous change
John W. Eaton <jwe@octave.org>
parents: 8467
diff changeset
370 error ("fsolve:isnan", "fsolve: NaN value encountered");
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
371 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
372 endfunction
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
373
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
374 %!function retval = f (p)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
375 %! x = p(1);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
376 %! y = p(2);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
377 %! z = p(3);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
378 %! retval = zeros (3, 1);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
379 %! retval(1) = sin(x) + y**2 + log(z) - 7;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
380 %! retval(2) = 3*x + 2**y -z**3 + 1;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
381 %! retval(3) = x + y + z - 5;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
382 %!test
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
383 %! x_opt = [ 0.599054;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
384 %! 2.395931;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
385 %! 2.005014 ];
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
386 %! tol = 1.0e-5;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
387 %! [x, fval, info] = fsolve (@f, [ 0.5; 2.0; 2.5 ]);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
388 %! assert (info > 0);
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
389 %! assert (norm (x - x_opt, Inf) < tol);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
390 %! assert (norm (fval) < tol);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
391
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
392 %!function retval = f (p)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
393 %! x = p(1);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
394 %! y = p(2);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
395 %! z = p(3);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
396 %! w = p(4);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
397 %! retval = zeros (4, 1);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
398 %! retval(1) = 3*x + 4*y + exp (z + w) - 1.007;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
399 %! retval(2) = 6*x - 4*y + exp (3*z + w) - 11;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
400 %! retval(3) = x^4 - 4*y^2 + 6*z - 8*w - 20;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
401 %! retval(4) = x^2 + 2*y^3 + z - w - 4;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
402 %!test
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
403 %! x_opt = [ -0.767297326653401, 0.590671081117440, 1.47190018629642, -1.52719341133957 ];
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
404 %! tol = 1.0e-5;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
405 %! [x, fval, info] = fsolve (@f, [-1, 1, 2, -1]);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
406 %! assert (info > 0);
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
407 %! assert (norm (x - x_opt, Inf) < tol);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
408 %! assert (norm (fval) < tol);
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
409
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
410 %!function retval = f (p)
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
411 %! x = p(1);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
412 %! y = p(2);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
413 %! z = p(3);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
414 %! retval = zeros (3, 1);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
415 %! retval(1) = sin(x) + y**2 + log(z) - 7;
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
416 %! retval(2) = 3*x + 2**y -z**3 + 1;
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
417 %! retval(3) = x + y + z - 5;
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
418 %! retval(4) = x*x + y - z*log(z) - 1.36;
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
419 %!test
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
420 %! x_opt = [ 0.599054;
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
421 %! 2.395931;
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
422 %! 2.005014 ];
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
423 %! tol = 1.0e-5;
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
424 %! [x, fval, info] = fsolve (@f, [ 0.5; 2.0; 2.5 ]);
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
425 %! assert (info > 0);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
426 %! assert (norm (x - x_opt, Inf) < tol);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
427 %! assert (norm (fval) < tol);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
428
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
429 %!function retval = f (p)
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
430 %! x = p(1);
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
431 %! y = p(2);
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
432 %! z = p(3);
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
433 %! retval = zeros (3, 1);
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
434 %! retval(1) = sin(x) + y**2 + log(z) - 7;
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
435 %! retval(2) = 3*x + 2**y -z**3 + 1;
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
436 %! retval(3) = x + y + z - 5;
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
437 %!test
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
438 %! x_opt = [ 0.599054;
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
439 %! 2.395931;
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
440 %! 2.005014 ];
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
441 %! tol = 1.0e-5;
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
442 %! opt = optimset ("Updating", "qrp");
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
443 %! [x, fval, info] = fsolve (@f, [ 0.5; 2.0; 2.5 ], opt);
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
444 %! assert (info > 0);
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
445 %! assert (norm (x - x_opt, Inf) < tol);
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
446 %! assert (norm (fval) < tol);
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
447
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
448 %!test
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
449 %! b0 = 3;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
450 %! a0 = 0.2;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
451 %! x = 0:.5:5;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
452 %! noise = 1e-5 * sin (100*x);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
453 %! y = exp (-a0*x) + b0 + noise;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
454 %! c_opt = [a0, b0];
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
455 %! tol = 1e-5;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
456 %!
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
457 %! [c, fval, info, output] = fsolve (@(c) (exp(-c(1)*x) + c(2) - y), [0, 0]);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
458 %! assert (info > 0);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
459 %! assert (norm (c - c_opt, Inf) < tol);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
460 %! assert (norm (fval) < norm (noise));
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
461
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
462
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
463 %!function y = cfun (x)
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
464 %! y(1) = (1+i)*x(1)^2 - (1-i)*x(2) - 2;
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
465 %! y(2) = sqrt (x(1)*x(2)) - (1-2i)*x(3) + (3-4i);
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
466 %! y(3) = x(1) * x(2) - x(3)^2 + (3+2i);
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
467
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
468 %!test
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
469 %! x_opt = [-1+i, 1-i, 2+i];
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
470 %! x = [i, 1, 1+i];
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
471 %!
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
472 %! [x, f, info] = fsolve (@cfun, x, optimset ("ComplexEqn", "on"));
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
473 %! tol = 1e-5;
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
474 %! assert (norm (f) < tol);
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
475 %! assert (norm (x - x_opt, Inf) < tol);
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
476