]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/nonlin_curvefit.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / nonlin_curvefit.m
1 ## Copyright (C) 2010, 2011 Olaf Till <olaf.till@uni-jena.de>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {[@var{p}, @var{fy}, @var{cvg}, @var{outp}] =} nonlin_curvefit (@var{f}, @var{pin}, @var{x}, @var{y})
18 ## @deftypefnx {Function File} {[@var{p}, @var{fy}, @var{cvg}, @var{outp}] =} nonlin_curvefit (@var{f}, @var{pin}, @var{x}, @var{y}, @var{settings})
19 ## Frontend for nonlinear fitting of values, computed by a model
20 ## function, to observed values.
21 ##
22 ## Please refer to the description of @code{nonlin_residmin}. The only
23 ## differences to @code{nonlin_residmin} are the additional arguments
24 ## @var{x} (independent values, mostly, but not necessarily, an array of
25 ## the same dimensions or the same number of rows as @var{y}) and
26 ## @var{y} (array of observations), the returned value @var{fy} (final
27 ## guess for observed values) instead of @var{resid}, that the model
28 ## function has a second obligatory argument which will be set to
29 ## @var{x} and is supposed to return guesses for the observations (with
30 ## the same dimensions), and that the possibly user-supplied function for the
31 ## jacobian of the model function has also a second obligatory argument
32 ## which will be set to @var{x}.
33 ##
34 ## @seealso {nonlin_residmin}
35 ## @end deftypefn
36
37 function [p, fy, cvg, outp] = nonlin_curvefit (f, pin, x, y, settings)
38
39   if (nargin == 1)
40     p = __nonlin_residmin__ (f);
41     return;
42   endif
43
44   if (nargin < 4 || nargin > 5)
45     print_usage ();
46   endif
47
48   if (nargin == 4)
49     settings = struct ();
50   endif
51
52   if (compare_versions (version (), "3.3.55", "<"))
53     ## optimset mechanism was fixed for option names with underscores
54     ## sometime in 3.3.54+, if I remember right
55     optimget = @ __optimget__;
56   endif
57   if (! isempty (dfdp = optimget (settings, "dfdp")))
58     if (ischar (dfdp))
59       dfdp = str2func (dfdp);
60     endif
61     ## settings = optimset \
62     ## (settings, "dfdp", @ (p, varargin) dfdp (p, x, varargin{:}));
63     settings.dfdp =  @ (p, varargin) dfdp (p, x, varargin{:});
64   endif
65
66   [p, fy, cvg, outp] = __nonlin_residmin__ \
67       (@ (p) f (p, x), pin, settings, struct ("observations", y));
68
69   fy += y;
70
71 endfunction
72
73 function ret = __optimget__ (s, name, default)
74
75   if (isfield (s, name))
76     ret = s.(name);
77   elseif (nargin > 2)
78     ret = default;
79   else
80     ret = [];
81   endif
82
83 endfunction
84
85 %!demo
86 %!  ## Example for linear inequality constraints
87 %!  ## (see also the same example in 'demo nonlin_residmin')
88 %!
89 %!  ## independents and observations
90 %!  indep = 1:5;
91 %!  obs = [1, 2, 4, 7, 14];
92 %!  ## model function:
93 %!  f = @ (p, x) p(1) * exp (p(2) * x);
94 %!  ## initial values:
95 %!  init = [.25; .25];
96 %!  ## linear constraints, A.' * parametervector + B >= 0
97 %!  A = [1; -1]; B = 0; # p(1) >= p(2);
98 %!  settings = optimset ("inequc", {A, B});
99 %!
100 %!  ## start optimization
101 %!  [p, model_values, cvg, outp] = nonlin_curvefit (f, init, indep, obs, settings)