1 ## Copyright (C) 2005 Michael Creel <michael.creel@uab.es>
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
17 # [theta, obj_value, conv, iters] = nls_estimate(theta, data, model, modelargs, control, nslaves)
20 # theta: column vector of model parameters
22 # model: name of function that computes the vector of sums of squared errors
23 # modelargs: (cell) additional inputs needed by model. May be empty ("")
24 # control: (optional) BFGS or SA controls (see bfgsmin and samin). May be empty ("").
25 # nslaves: (optional) number of slaves if executed in parallel (requires MPITB)
28 # theta: NLS estimated value of parameters
29 # obj_value: the value of the sum of squared errors at NLS estimate
30 # conv: return code from bfgsmin (1 means success, see bfgsmin for details)
31 # iters: number of BFGS iteration used
33 # please see nls_example.m for examples of how to use this
34 function [theta, obj_value, convergence, iters] = nls_estimate(theta, data, model, modelargs, control, nslaves)
37 error("nls_estimate: 3 arguments required");
40 if nargin < 4 modelargs = {}; endif # create placeholder if not used
41 if !iscell(modelargs) modelargs = {}; endif # default controls if receive placeholder
42 if nargin < 5 control = {Inf,0,1,1}; endif # default controls and method
43 if !iscell(control) control = {Inf,0,1,1}; endif # default controls if receive placeholder
44 if nargin < 6 nslaves = 0; endif
47 global NSLAVES PARALLEL NEWORLD TAG
49 # Send the data to all nodes
50 NumCmds_Send({"data", "model", "modelargs"}, {data, model, modelargs});
54 if (size(control,1)*size(control,2) == 0) # use default bfgs if no control
55 control = {Inf,0,1,1};
57 elseif (size(control,1)*size(control,2) < 11)
62 if strcmp(method, "bfgs")
63 [theta, obj_value, convergence, iters] = bfgsmin("nls_obj", {theta, data, model, modelargs, nslaves}, control);
64 elseif strcmp(method, "sa")
65 [theta, obj_value, convergence] = samin("nls_obj", {theta, data, model, modelargs, nslaves}, control);
69 if nslaves > 0 LAM_Finalize; endif