annotate scripts/optimization/fsolve.m @ 20165:f1d0f506ee78 stable

doc: Update more docstrings to have one sentence summary as first line. Reviewed optimization, polynomial, signal script directories. * scripts/optimization/fminbnd.m, scripts/optimization/fminsearch.m, scripts/optimization/fminunc.m, scripts/optimization/fsolve.m, scripts/optimization/fzero.m, scripts/optimization/glpk.m, scripts/optimization/lsqnonneg.m, scripts/optimization/pqpnonneg.m, scripts/optimization/qp.m, scripts/optimization/sqp.m, scripts/polynomial/compan.m, scripts/polynomial/mkpp.m, scripts/polynomial/mpoles.m, scripts/polynomial/pchip.m, scripts/polynomial/poly.m, scripts/polynomial/polyaffine.m, scripts/polynomial/polyder.m, scripts/polynomial/polyeig.m, scripts/polynomial/polyfit.m, scripts/polynomial/polygcd.m, scripts/polynomial/polyint.m, scripts/polynomial/polyout.m, scripts/polynomial/polyval.m, scripts/polynomial/ppder.m, scripts/polynomial/ppint.m, scripts/polynomial/ppjumps.m, scripts/polynomial/ppval.m, scripts/polynomial/residue.m, scripts/polynomial/roots.m, scripts/polynomial/spline.m, scripts/polynomial/splinefit.m, scripts/polynomial/unmkpp.m, scripts/signal/arch_fit.m, scripts/signal/arch_rnd.m, scripts/signal/arma_rnd.m, scripts/signal/autoreg_matrix.m, scripts/signal/bartlett.m, scripts/signal/blackman.m, scripts/signal/detrend.m, scripts/signal/diffpara.m, scripts/signal/durbinlevinson.m, scripts/signal/fftconv.m, scripts/signal/fftfilt.m, scripts/signal/fftshift.m, scripts/signal/filter2.m, scripts/signal/freqz.m, scripts/signal/hamming.m, scripts/signal/hanning.m, scripts/signal/hurst.m, scripts/signal/ifftshift.m, scripts/signal/periodogram.m, scripts/signal/sinc.m, scripts/signal/sinetone.m, scripts/signal/sinewave.m, scripts/signal/spectral_adf.m, scripts/signal/spectral_xdf.m, scripts/signal/spencer.m, scripts/signal/stft.m, scripts/signal/synthesis.m, scripts/signal/unwrap.m, scripts/signal/yulewalker.m: Update more docstrings to have one sentence summary as first line.
author Rik <rik@octave.org>
date Mon, 04 May 2015 21:50:57 -0700
parents 9fc020886ae9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 18857
diff changeset
1 ## Copyright (C) 2008-2015 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 -*-
9143
74d5c1a4ca96 Eliminate 'unbalanced parentheses in @def...' error during texi2pdf.
Rik <rdrider0-list@yahoo.com>
parents: 9075
diff changeset
22 ## @deftypefn {Function File} {} fsolve (@var{fcn}, @var{x0}, @var{options})
9724
f22bbc5d56e9 Fix various incorrect usages of TeXinfo deffn and deftypefn macros
Rik <rdrider0-list@yahoo.com>
parents: 9628
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}.
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
25 ##
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
26 ## @var{fcn} should accept a vector (array) defining the unknown variables,
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8997
diff changeset
27 ## and return a vector of left-hand sides of the equations. Right-hand sides
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
28 ## are defined to be zeros. In other words, this function attempts to
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
29 ## determine a vector @var{x} such that @code{@var{fcn} (@var{x})} gives
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
30 ## (approximately) all zeros.
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ##
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8997
diff changeset
32 ## @var{x0} determines a starting guess. The shape of @var{x0} is preserved
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
33 ## in all calls to @var{fcn}, but otherwise it is treated as a column vector.
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ##
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
35 ## @var{options} is a structure specifying additional options. Currently,
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
36 ## @code{fsolve} recognizes these options:
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17245
diff changeset
37 ## @qcode{"FunValCheck"}, @qcode{"OutputFcn"}, @qcode{"TolX"},
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17245
diff changeset
38 ## @qcode{"TolFun"}, @qcode{"MaxIter"}, @qcode{"MaxFunEvals"},
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17245
diff changeset
39 ## @qcode{"Jacobian"}, @qcode{"Updating"}, @qcode{"ComplexEqn"}
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17245
diff changeset
40 ## @qcode{"TypicalX"}, @qcode{"AutoScaling"} and @qcode{"FinDiffType"}.
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
41 ##
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
42 ## If @qcode{"Jacobian"} is @qcode{"on"}, it specifies that @var{fcn}, called
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
43 ## with 2 output arguments also returns the Jacobian matrix of right-hand sides
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
44 ## at the requested point. @qcode{"TolX"} specifies the termination tolerance
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
45 ## in the unknown variables, while @qcode{"TolFun"} is a tolerance for
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
46 ## equations. Default is @code{1e-7} for both @qcode{"TolX"} and
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
47 ## @qcode{"TolFun"}.
10201
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
48 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17245
diff changeset
49 ## If @qcode{"AutoScaling"} is on, the variables will be automatically scaled
10711
fbd7843974fa Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
50 ## according to the column norms of the (estimated) Jacobian. As a result,
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
51 ## TolF becomes scaling-independent. By default, this option is off because
10201
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
52 ## it may sometimes deliver unexpected (though mathematically correct) results.
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
53 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17245
diff changeset
54 ## If @qcode{"Updating"} is @qcode{"on"}, the function will attempt to use
17290
17be601bc783 doc: Periodic spellchecking of documentation.
Rik <rik@octave.org>
parents: 17281
diff changeset
55 ## @nospell{Broyden} updates to update the Jacobian, in order to reduce the
17be601bc783 doc: Periodic spellchecking of documentation.
Rik <rik@octave.org>
parents: 17281
diff changeset
56 ## amount of Jacobian calculations. If your user function always calculates the
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
57 ## Jacobian (regardless of number of output arguments) then this option provides
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17245
diff changeset
58 ## no advantage and should be set to false.
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
59 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17245
diff changeset
60 ## @qcode{"ComplexEqn"} is @qcode{"on"}, @code{fsolve} will attempt to solve
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
61 ## complex equations in complex variables, assuming that the equations possess
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
62 ## a complex derivative (i.e., are holomorphic). If this is not what you want,
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
63 ## you should unpack the real and imaginary parts of the system to get a real
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
64 ## system.
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
65 ##
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
66 ## For description of the other options, see @code{optimset}.
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
67 ##
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
68 ## On return, @var{fval} contains the value of the function @var{fcn}
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
69 ## evaluated at @var{x}.
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
70 ##
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
71 ## @var{info} may be one of the following values:
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
72 ##
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
73 ## @table @asis
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
74 ## @item 1
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
75 ## Converged to a solution point. Relative residual error is less than
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
76 ## specified by TolFun.
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
77 ##
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
78 ## @item 2
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
79 ## Last relative step size was less that TolX.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
80 ##
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
81 ## @item 3
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
82 ## Last relative decrease in residual was less than TolF.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
83 ##
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
84 ## @item 0
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
85 ## Iteration limit exceeded.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
86 ##
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
87 ## @item -3
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
88 ## The trust region radius became excessively small.
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
89 ## @end table
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
90 ##
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
91 ## Note: If you only have a single nonlinear equation of one variable, using
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
92 ## @code{fzero} is usually a much better idea.
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
93 ##
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
94 ## Note about user-supplied Jacobians:
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
95 ## As an inherent property of the algorithm, a Jacobian is always requested for
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
96 ## a solution vector whose residual vector is already known, and it is the last
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8997
diff changeset
97 ## accepted successful step. Often this will be one of the last two calls, but
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8997
diff changeset
98 ## not always. If the savings by reusing intermediate results from residual
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
99 ## calculation in Jacobian calculation are significant, the best strategy is to
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
100 ## employ OutputFcn: After a vector is evaluated for residuals, if OutputFcn is
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
101 ## called with that vector, then the intermediate results should be saved for
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
102 ## future Jacobian evaluation, and should be kept until a Jacobian evaluation
16013
4efc0c1537df Fix typo in the documentation of function fsolve
Rafael Laboissiere <rafael@laboissiere.net>
parents: 14868
diff changeset
103 ## is requested or until OutputFcn is called with a different vector, in which
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8997
diff changeset
104 ## case they should be dropped in favor of this most recent vector. A short
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
105 ## example how this can be achieved follows:
8988
315828058e0d minor doc fixes in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8986
diff changeset
106 ##
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
107 ## @example
9153
5247e89688e1 Eliminate most overfull errors when running texi2pdf for generating pdf documentation
Rik <rdrider0-list@yahoo.com>
parents: 9143
diff changeset
108 ## function [fvec, fjac] = user_func (x, optimvalues, state)
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
109 ## persistent sav = [], sav0 = [];
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
110 ## if (nargin == 1)
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
111 ## ## evaluation call
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
112 ## if (nargout == 1)
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
113 ## sav0.x = x; # mark saved vector
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
114 ## ## calculate fvec, save results to sav0.
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
115 ## elseif (nargout == 2)
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
116 ## ## calculate fjac using sav.
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
117 ## endif
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
118 ## else
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
119 ## ## outputfcn call.
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
120 ## if (all (x == sav0.x))
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
121 ## sav = sav0;
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
122 ## endif
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9207
diff changeset
123 ## ## maybe output iteration status, etc.
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
124 ## endif
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
125 ## endfunction
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
126 ##
14143
04dcbb8fb880 fsolve.m: Move @seealso to bottom in docstring to silence warning.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
127 ## ## @dots{}
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
128 ##
9153
5247e89688e1 Eliminate most overfull errors when running texi2pdf for generating pdf documentation
Rik <rdrider0-list@yahoo.com>
parents: 9143
diff changeset
129 ## fsolve (@@user_func, x0, optimset ("OutputFcn", @@user_func, @dots{}))
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
130 ## @end example
14143
04dcbb8fb880 fsolve.m: Move @seealso to bottom in docstring to silence warning.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
131 ## @seealso{fzero, optimset}
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
132 ## @end deftypefn
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
133
13027
b9a89ca0fb75 prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
134 ## PKG_ADD: ## Discard result to avoid polluting workspace with ans at startup.
b9a89ca0fb75 prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
135 ## PKG_ADD: [~] = __all_opts__ ("fsolve");
8648
ff61b53eb294 optimization: use PKG_ADD: comments instead of PKG_ADD file
John W. Eaton <jwe@octave.org>
parents: 8647
diff changeset
136
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
137 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
138
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
139 ## Get default options if requested.
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
140 if (nargin == 1 && ischar (fcn) && strcmp (fcn, 'defaults'))
17245
7babcdb9bc13 Use ... instead of \ for line continuation marker.
Stefan Mahr <dac922@gmx.de>
parents: 16013
diff changeset
141 x = optimset ("MaxIter", 400, "MaxFunEvals", Inf, ...
10201
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
142 "Jacobian", "off", "TolX", 1e-7, "TolFun", 1e-7,
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
143 "OutputFcn", [], "Updating", "on", "FunValCheck", "off",
10201
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
144 "ComplexEqn", "off", "FinDiffType", "central",
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
145 "TypicalX", [], "AutoScaling", "off");
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
146 return;
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
147 endif
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
148
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
149 if (nargin < 2 || nargin > 3 || ! ismatrix (x0))
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
150 print_usage ();
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
151 endif
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
152
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
153 if (ischar (fcn))
9464
e598248a060d safer str2func use in optim functions
Jaroslav Hajek <highegg@gmail.com>
parents: 9212
diff changeset
154 fcn = str2func (fcn, "global");
10050
dc88a0b6472c support old style jacobian for fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9899
diff changeset
155 elseif (iscell (fcn))
dc88a0b6472c support old style jacobian for fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9899
diff changeset
156 fcn = @(x) make_fcn_jac (x, fcn{1}, fcn{2});
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
157 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
158
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
159 xsiz = size (x0);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
160 n = numel (x0);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
161
8467
77b8d4aa2743 fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents: 8466
diff changeset
162 has_jac = strcmpi (optimget (options, "Jacobian", "off"), "on");
9212
6feb27c38da1 support central differences in fminunc and fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
163 cdif = strcmpi (optimget (options, "FinDiffType", "central"), "central");
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
164 maxiter = optimget (options, "MaxIter", 400);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
165 maxfev = optimget (options, "MaxFunEvals", Inf);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
166 outfcn = optimget (options, "OutputFcn");
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8592
diff changeset
167 updating = strcmpi (optimget (options, "Updating", "on"), "on");
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
168 complexeqn = strcmpi (optimget (options, "ComplexEqn", "off"), "on");
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
169
10201
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
170 ## Get scaling matrix using the TypicalX option. If set to "auto", the
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
171 ## scaling matrix is estimated using the Jacobian.
10201
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
172 typicalx = optimget (options, "TypicalX");
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
173 if (isempty (typicalx))
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
174 typicalx = ones (n, 1);
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
175 endif
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
176 autoscale = strcmpi (optimget (options, "AutoScaling", "off"), "on");
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
177 if (! autoscale)
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
178 dg = 1 ./ typicalx;
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
179 endif
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
180
8467
77b8d4aa2743 fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents: 8466
diff changeset
181 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on");
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
182
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
183 if (funvalchk)
8604
43f831758d42 fix typo in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8596
diff changeset
184 ## Replace fcn with a guarded version.
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
185 fcn = @(x) guarded_eval (fcn, x, complexeqn);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
186 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
187
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
188 ## 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
189 ## prefers accuracy to performance.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
190
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
191 macheps = eps (class (x0));
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
192
10201
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
193 tolx = optimget (options, "TolX", 1e-7);
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
194 tolf = optimget (options, "TolFun", 1e-7);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
195
9623
bc0739d02724 update initial TR step for fsolve and fminunc
Jaroslav Hajek <highegg@gmail.com>
parents: 9464
diff changeset
196 factor = 1;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
197
8467
77b8d4aa2743 fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents: 8466
diff changeset
198 niter = 1;
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
199 nfev = 1;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
200
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
201 x = x0(:);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
202 info = 0;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
203
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
204 ## Initial evaluation.
8990
349d75161672 more cosmetic adjustments to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8988
diff changeset
205 ## Handle arbitrary shapes of x and f and remember them.
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
206 fvec = fcn (reshape (x, xsiz));
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
207 fsiz = size (fvec);
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
208 fvec = fvec(:);
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
209 fn = norm (fvec);
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
210 m = length (fvec);
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
211 n = length (x);
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
212
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
213 if (! isempty (outfcn))
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
214 optimvalues.iter = niter;
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
215 optimvalues.funccount = nfev;
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
216 optimvalues.fval = fn;
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
217 optimvalues.searchdirection = zeros (n, 1);
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
218 state = 'init';
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
219 stop = outfcn (x, optimvalues, state);
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
220 if (stop)
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
221 info = -1;
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
222 break;
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
223 endif
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
224 endif
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
225
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
226 nsuciter = 0;
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
227
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
228 ## Outer loop.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
229 while (niter < maxiter && nfev < maxfev && ! info)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
230
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
231 ## Calculate function value and Jacobian (possibly via FD).
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
232 if (has_jac)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
233 [fvec, fjac] = fcn (reshape (x, xsiz));
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
234 ## If the Jacobian is sparse, disable Broyden updating.
8592
dacfd030633a handle sparse jacobians in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8590
diff changeset
235 if (issparse (fjac))
dacfd030633a handle sparse jacobians in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8590
diff changeset
236 updating = false;
dacfd030633a handle sparse jacobians in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8590
diff changeset
237 endif
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
238 fvec = fvec(:);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
239 nfev ++;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
240 else
10201
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
241 fjac = __fdjac__ (fcn, reshape (x, xsiz), fvec, typicalx, cdif);
9212
6feb27c38da1 support central differences in fminunc and fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
242 nfev += (1 + cdif) * length (x);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
243 endif
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
244
8763
5ce12bca4c51 update comments in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8693
diff changeset
245 ## For square and overdetermined systems, we update a QR
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
246 ## factorization of the Jacobian to avoid solving a full system in each
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
247 ## step. In this case, we pass a triangular matrix to __dogleg__.
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
248 useqr = updating && m >= n && n > 10;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
249
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
250 if (useqr)
8616
3d75d717cbe0 do not pivot by default in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8615
diff changeset
251 ## 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
252 ## 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
253 ## 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
254 ## 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
255 ## when qr updating was used.
3d75d717cbe0 do not pivot by default in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8615
diff changeset
256 [q, r] = qr (fjac, 0);
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
257 endif
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
258
10201
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
259 if (autoscale)
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
260 ## Get column norms, use them as scaling factors.
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
261 jcn = norm (fjac, 'columns').';
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
262 if (niter == 1)
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
263 dg = jcn;
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
264 dg(dg == 0) = 1;
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
265 else
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
266 ## Rescale adaptively.
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
267 ## FIXME: the original minpack used the following rescaling strategy:
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
268 ## dg = max (dg, jcn);
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
269 ## but it seems not good if we start with a bad guess yielding Jacobian
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
270 ## columns with large norms that later decrease, because the
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
271 ## corresponding variable will still be overscaled. So instead, we only
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
272 ## give the old scaling a small momentum, but do not honor it.
10201
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
273
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
274 dg = max (0.1*dg, jcn);
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
275 endif
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
276 endif
5c66978f3fdf support TypicalX and AutoScaling in fsolve/fminunc, don't autoscale by default
Jaroslav Hajek <highegg@gmail.com>
parents: 10050
diff changeset
277
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
278 if (niter == 1)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
279 xn = norm (dg .* x);
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
280 ## FIXME: something better?
9628
73e6ad869f08 further correct initial TR step strategy
Jaroslav Hajek <highegg@gmail.com>
parents: 9623
diff changeset
281 delta = factor * max (xn, 1);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
282 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
283
8990
349d75161672 more cosmetic adjustments to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8988
diff changeset
284 ## It also seems that in the case of fast (and inhomogeneously) changing
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
285 ## Jacobian, the Broyden updates are of little use, so maybe we could
8990
349d75161672 more cosmetic adjustments to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8988
diff changeset
286 ## skip them if a big disproportional change is expected. The question is,
349d75161672 more cosmetic adjustments to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8988
diff changeset
287 ## of course, how to define the above terms :)
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
288
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
289 lastratio = 0;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
290 nfail = 0;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
291 nsuc = 0;
9207
25f50d2d76b3 improve TR updating strategy for fminunc and fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
292 decfac = 0.5;
25f50d2d76b3 improve TR updating strategy for fminunc and fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
293
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
294 ## Inner loop.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
295 while (niter <= maxiter && nfev < maxfev && ! info)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
296
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
297 ## Get trust-region model (dogleg) minimizer.
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
298 if (useqr)
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
299 qtf = q'*fvec;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
300 s = - __dogleg__ (r, qtf, dg, delta);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
301 w = qtf + r * s;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
302 else
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
303 s = - __dogleg__ (fjac, fvec, dg, delta);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
304 w = fvec + fjac * s;
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
305 endif
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
306
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
307 sn = norm (dg .* s);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
308 if (niter == 1)
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
309 delta = min (delta, sn);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
310 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
311
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
312 fvec1 = fcn (reshape (x + s, xsiz)) (:);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
313 fn1 = norm (fvec1);
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
314 nfev ++;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
315
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
316 if (fn1 < fn)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
317 ## Scaled actual reduction.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
318 actred = 1 - (fn1/fn)^2;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
319 else
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
320 actred = -1;
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
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
323 ## Scaled predicted reduction, and ratio.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
324 t = norm (w);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
325 if (t < fn)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
326 prered = 1 - (t/fn)^2;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
327 ratio = actred / prered;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
328 else
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
329 prered = 0;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
330 ratio = 0;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
331 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
332
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
333 ## Update delta.
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14386
diff changeset
334 if (ratio < min (max (0.1, 0.8*lastratio), 0.9))
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
335 nsuc = 0;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
336 nfail ++;
9207
25f50d2d76b3 improve TR updating strategy for fminunc and fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
337 delta *= decfac;
25f50d2d76b3 improve TR updating strategy for fminunc and fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
338 decfac ^= 1.4142;
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
339 if (delta <= 1e1*macheps*xn)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
340 ## Trust region became uselessly small.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
341 info = -3;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
342 break;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
343 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
344 else
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
345 lastratio = ratio;
9207
25f50d2d76b3 improve TR updating strategy for fminunc and fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
346 decfac = 0.5;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
347 nfail = 0;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
348 nsuc ++;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
349 if (abs (1-ratio) <= 0.1)
9207
25f50d2d76b3 improve TR updating strategy for fminunc and fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
350 delta = 1.4142*sn;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
351 elseif (ratio >= 0.5 || nsuc > 1)
9207
25f50d2d76b3 improve TR updating strategy for fminunc and fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
352 delta = max (delta, 1.4142*sn);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
353 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
354 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
355
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
356 if (ratio >= 1e-4)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
357 ## Successful iteration.
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
358 x += s;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
359 xn = norm (dg .* x);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
360 fvec = fvec1;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
361 fn = fn1;
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
362 nsuciter ++;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
363 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
364
8857
20589a8f1a33 fix fsolve bug
Jaroslav Hajek <highegg@gmail.com>
parents: 8763
diff changeset
365 niter ++;
20589a8f1a33 fix fsolve bug
Jaroslav Hajek <highegg@gmail.com>
parents: 8763
diff changeset
366
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
367 ## 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
368 if (! isempty (outfcn))
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
369 optimvalues.iter = niter;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
370 optimvalues.funccount = nfev;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
371 optimvalues.fval = fn;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
372 optimvalues.searchdirection = s;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
373 state = 'iter';
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
374 stop = outfcn (x, optimvalues, state);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
375 if (stop)
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
376 info = -1;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
377 break;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
378 endif
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
379 endif
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
380
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
381 ## Tests for termination conditions. A mysterious place, anything
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
382 ## can happen if you change something here...
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
383
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
384 ## 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
385 ## 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
386 ## 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
387 ## iterations, so we need scaling-independent tolerances wherever
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
388 ## possible.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
389
18857
7bbe3658c5ef maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents: 17744
diff changeset
390 ## FIXME: Why tolf*n*xn? If abs (e) ~ abs(x) * eps is a vector
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
391 ## 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
392 ## 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
393 if (fn <= tolf*n*xn)
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
394 info = 1;
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10201
diff changeset
395 ## The following tests done only after successful step.
9075
7b35d3b21c1c fix test in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9065
diff changeset
396 elseif (ratio >= 1e-4)
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
397 ## This one is classic. Note that we use scaled variables again,
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10201
diff changeset
398 ## but compare to scaled step, so nothing bad.
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
399 if (sn <= tolx*xn)
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
400 info = 2;
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
401 ## Again a classic one. It seems weird to use the same tolf
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10201
diff changeset
402 ## for two different tests, but that's what M*b manual appears
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10201
diff changeset
403 ## to say.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
404 elseif (actred < tolf)
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
405 info = 3;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
406 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
407 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
408
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
409 ## Criterion for recalculating Jacobian.
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
410 if (! updating || nfail == 2 || nsuciter < 2)
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
411 break;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
412 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
413
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
414 ## Compute the scaled Broyden update.
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
415 if (useqr)
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
416 u = (fvec1 - q*w) / sn;
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
417 v = dg .* ((dg .* s) / sn);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
418
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
419 ## Update the QR factorization.
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
420 [q, r] = qrupdate (q, r, u, v);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
421 else
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
422 u = (fvec1 - w);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
423 v = dg .* ((dg .* s) / sn);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
424
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
425 ## update the Jacobian
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
426 fjac += u * v';
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
427 endif
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
428 endwhile
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
429 endwhile
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
430
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
431 ## Restore original shapes.
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
432 x = reshape (x, xsiz);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
433 fvec = reshape (fvec, fsiz);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
434
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
435 output.iterations = niter;
8986
22c8272af34b improvements to fsolve & family
Jaroslav Hajek <highegg@gmail.com>
parents: 8857
diff changeset
436 output.successful = nsuciter;
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
437 output.funcCount = nfev;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
438
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
439 endfunction
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
440
8466
4d008d9f0ccf fsolve.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8395
diff changeset
441 ## 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
442 ## bad results.
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
443 function [fx, jx] = guarded_eval (fun, x, complexeqn)
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
444 if (nargout > 1)
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
445 [fx, jx] = fun (x);
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
446 else
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
447 fx = fun (x);
8997
187a9d9c2f04 simplifications to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8991
diff changeset
448 jx = [];
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
449 endif
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
450
8997
187a9d9c2f04 simplifications to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8991
diff changeset
451 if (! complexeqn && ! (isreal (fx) && isreal (jx)))
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
452 error ("fsolve:notreal", "fsolve: non-real value encountered");
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14386
diff changeset
453 elseif (complexeqn && ! (isnumeric (fx) && isnumeric (jx)))
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
454 error ("fsolve:notnum", "fsolve: non-numeric value encountered");
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
455 elseif (any (isnan (fx(:))))
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
456 error ("fsolve:isnan", "fsolve: NaN value encountered");
14386
59aab666f2bf Extend "FunValCheck" option to optimization routines to detect Inf values.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
457 elseif (any (isinf (fx(:))))
59aab666f2bf Extend "FunValCheck" option to optimization routines to detect Inf values.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
458 error ("fsolve:isinf", "fsolve: Inf value encountered");
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
459 endif
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
460 endfunction
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
461
10050
dc88a0b6472c support old style jacobian for fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9899
diff changeset
462 function [fx, jx] = make_fcn_jac (x, fcn, fjac)
dc88a0b6472c support old style jacobian for fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9899
diff changeset
463 fx = fcn (x);
dc88a0b6472c support old style jacobian for fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9899
diff changeset
464 if (nargout == 2)
dc88a0b6472c support old style jacobian for fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9899
diff changeset
465 jx = fjac (x);
dc88a0b6472c support old style jacobian for fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9899
diff changeset
466 endif
dc88a0b6472c support old style jacobian for fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9899
diff changeset
467 endfunction
dc88a0b6472c support old style jacobian for fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 9899
diff changeset
468
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17290
diff changeset
469
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
470 %!function retval = __f (p)
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
471 %! x = p(1);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
472 %! y = p(2);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
473 %! z = p(3);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
474 %! retval = zeros (3, 1);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
475 %! retval(1) = sin (x) + y^2 + log (z) - 7;
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
476 %! retval(2) = 3*x + 2^y -z^3 + 1;
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
477 %! retval(3) = x + y + z - 5;
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
478 %!endfunction
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
479 %!test
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
480 %! x_opt = [ 0.599054;
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
481 %! 2.395931;
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
482 %! 2.005014 ];
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
483 %! tol = 1.0e-5;
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
484 %! [x, fval, info] = fsolve (@__f, [ 0.5; 2.0; 2.5 ]);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
485 %! assert (info > 0);
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
486 %! assert (norm (x - x_opt, Inf) < tol);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
487 %! assert (norm (fval) < tol);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
488
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
489 %!function retval = __f (p)
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
490 %! x = p(1);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
491 %! y = p(2);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
492 %! z = p(3);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
493 %! w = p(4);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
494 %! retval = zeros (4, 1);
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
495 %! retval(1) = 3*x + 4*y + exp (z + w) - 1.007;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
496 %! retval(2) = 6*x - 4*y + exp (3*z + w) - 11;
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
497 %! 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
498 %! retval(4) = x^2 + 2*y^3 + z - w - 4;
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
499 %!endfunction
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
500 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
501 %! x_opt = [ -0.767297326653401, 0.590671081117440, ...
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
502 %! 1.47190018629642, -1.52719341133957 ];
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
503 %! tol = 1.0e-5;
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
504 %! [x, fval, info] = fsolve (@__f, [-1, 1, 2, -1]);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
505 %! assert (info > 0);
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
506 %! assert (norm (x - x_opt, Inf) < tol);
8306
43795cf108d0 initial implementation of fsolve
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
507 %! assert (norm (fval) < tol);
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
508
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
509 %!function retval = __f (p)
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
510 %! x = p(1);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
511 %! y = p(2);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
512 %! z = p(3);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
513 %! retval = zeros (3, 1);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
514 %! retval(1) = sin (x) + y^2 + log (z) - 7;
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
515 %! retval(2) = 3*x + 2^y -z^3 + 1;
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
516 %! retval(3) = x + y + z - 5;
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
517 %! retval(4) = x*x + y - z*log (z) - 1.36;
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
518 %!endfunction
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
519 %!test
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
520 %! x_opt = [ 0.599054;
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
521 %! 2.395931;
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
522 %! 2.005014 ];
8383
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
523 %! tol = 1.0e-5;
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
524 %! [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
525 %! assert (info > 0);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
526 %! assert (norm (x - x_opt, Inf) < tol);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
527 %! assert (norm (fval) < tol);
a762d9daa700 allow overdetermined systems in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8306
diff changeset
528
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
529 %!function retval = __f (p)
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
530 %! x = p(1);
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
531 %! y = p(2);
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
532 %! z = p(3);
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
533 %! retval = zeros (3, 1);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
534 %! retval(1) = sin (x) + y^2 + log (z) - 7;
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
535 %! retval(2) = 3*x + 2^y -z^3 + 1;
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
536 %! retval(3) = x + y + z - 5;
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
537 %!endfunction
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
538 %!test
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
539 %! x_opt = [ 0.599054;
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
540 %! 2.395931;
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14143
diff changeset
541 %! 2.005014 ];
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
542 %! tol = 1.0e-5;
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
543 %! opt = optimset ("Updating", "qrp");
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
544 %! [x, fval, info] = fsolve (@__f, [ 0.5; 2.0; 2.5 ], opt);
8395
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
545 %! assert (info > 0);
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
546 %! assert (norm (x - x_opt, Inf) < tol);
88fd356b0d95 optionally allow pivoting in fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8383
diff changeset
547 %! assert (norm (fval) < tol);
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
548
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
549 %!test
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
550 %! b0 = 3;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
551 %! a0 = 0.2;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
552 %! x = 0:.5:5;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
553 %! noise = 1e-5 * sin (100*x);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
554 %! y = exp (-a0*x) + b0 + noise;
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
555 %! c_opt = [a0, b0];
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
556 %! tol = 1e-5;
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
557 %!
8590
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
558 %! [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
559 %! assert (info > 0);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
560 %! assert (norm (c - c_opt, Inf) < tol);
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
561 %! assert (norm (fval) < norm (noise));
c136d313206a improvements to fsolve
Jaroslav Hajek <highegg@gmail.com>
parents: 8562
diff changeset
562
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
563 %!function y = cfun (x)
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
564 %! 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
565 %! 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
566 %! y(3) = x(1) * x(2) - x(3)^2 + (3+2i);
13305
63463570d9fe Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents: 13027
diff changeset
567 %!endfunction
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
568
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
569 %!test
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
570 %! x_opt = [-1+i, 1-i, 2+i];
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
571 %! x = [i, 1, 1+i];
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
572 %!
8693
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
573 %! [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
574 %! tol = 1e-5;
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
575 %! assert (norm (f) < tol);
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
576 %! assert (norm (x - x_opt, Inf) < tol);
e5ffb52c9c61 improve fsolve and add ComplexEqn option
Jaroslav Hajek <highegg@gmail.com>
parents: 8661
diff changeset
577
9899
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
578 ## Solve the double dogleg trust-region least-squares problem:
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
579 ## Minimize norm(r*x-b) subject to the constraint norm(d.*x) <= delta,
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
580 ## x being a convex combination of the gauss-newton and scaled gradient.
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
581
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
582 ## TODO: error checks
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
583 ## TODO: handle singularity, or leave it up to mldivide?
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
584
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
585 function x = __dogleg__ (r, b, d, delta)
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
586 ## Get Gauss-Newton direction.
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
587 x = r \ b;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
588 xn = norm (d .* x);
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
589 if (xn > delta)
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
590 ## GN is too big, get scaled gradient.
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
591 s = (r' * b) ./ d;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
592 sn = norm (s);
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
593 if (sn > 0)
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
594 ## Normalize and rescale.
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
595 s = (s / sn) ./ d;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
596 ## Get the line minimizer in s direction.
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
597 tn = norm (r*s);
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
598 snm = (sn / tn) / tn;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
599 if (snm < delta)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10201
diff changeset
600 ## Get the dogleg path minimizer.
9899
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
601 bn = norm (b);
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
602 dxn = delta/xn; snmd = snm/delta;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
603 t = (bn/sn) * (bn/xn) * snmd;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
604 t -= dxn * snmd^2 - sqrt ((t-dxn)^2 + (1-dxn^2)*(1-snmd^2));
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
605 alpha = dxn*(1-snmd^2) / t;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
606 else
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
607 alpha = 0;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
608 endif
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
609 else
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
610 alpha = delta / xn;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
611 snm = 0;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
612 endif
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
613 ## Form the appropriate convex combination.
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
614 x = alpha * x + ((1-alpha) * min (snm, delta)) * s;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
615 endif
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
616 endfunction
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
617