📄 lsqnonlin.c
字号:
/*
* MATLAB Compiler: 2.0.1
* Date: Tue May 08 21:28:21 2001
* Arguments: "-B" "sgl" "-m" "-W" "mainhg" "-L" "C" "asufit.m" "absfun.m"
* "absfunfree.m" "absfunshift.m" "absfunwidth.m" "dispfit.m" "dispfitdisp.m"
* "dispfitfree.m" "dispfitwidth.m" "seqmodfree.m" "spcfun.m" "spcfun1.m"
* "atamult.m" "aprecon.m"
*/
#include "lsqnonlin.h"
#include "optim_private_checkbounds.h"
#include "optim_private_diagnose.h"
#include "optim_private_nlsq.h"
#include "optim_private_snls.h"
#include "optim_private_startx.h"
static mxArray * Mlsqnonlin(mxArray * * Resnorm,
mxArray * * FVAL,
mxArray * * EXITFLAG,
mxArray * * OUTPUT,
mxArray * * LAMBDA,
mxArray * * JACOB,
int nargout_,
mxArray * FUN,
mxArray * x_,
mxArray * LB,
mxArray * UB,
mxArray * options,
mxArray * varargin);
static mxArray * Mlsqnonlin_fprefcnchk(mxArray * * msg,
int nargout_,
mxArray * funstr,
mxArray * caller,
mxArray * lenVarIn,
mxArray * gradflag);
static mxArray * mlfLsqnonlin_fprefcnchk(mxArray * * msg,
mxArray * funstr,
mxArray * caller,
mxArray * lenVarIn,
mxArray * gradflag);
static void mlxLsqnonlin_fprefcnchk(int nlhs,
mxArray * plhs[],
int nrhs,
mxArray * prhs[]);
static mlfFunctionTableEntry local_function_table_[1]
= { { "fprefcnchk", mlxLsqnonlin_fprefcnchk, 4, 2 } };
/*
* The function "Mlsqnonlin" is the implementation version of the "lsqnonlin"
* M-function from file "C:\MATLABR11\toolbox\optim\lsqnonlin.m" (lines 1-254).
* It contains the actual compiled code for that M-function. It is a static
* function and must only be called from one of the interface functions,
* appearing below.
*/
/*
* function [x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = lsqnonlin(FUN,x,LB,UB,options,varargin)
*/
static mxArray * Mlsqnonlin(mxArray * * Resnorm,
mxArray * * FVAL,
mxArray * * EXITFLAG,
mxArray * * OUTPUT,
mxArray * * LAMBDA,
mxArray * * JACOB,
int nargout_,
mxArray * FUN,
mxArray * x_,
mxArray * LB,
mxArray * UB,
mxArray * options,
mxArray * varargin) {
mxArray * x = mclGetUninitializedArray();
mxArray * HESS = mclGetUninitializedArray();
mxArray * JAC = mclGetUninitializedArray();
mxArray * Jcols = mclGetUninitializedArray();
mxArray * Jrows = mclGetUninitializedArray();
mxArray * Jstr = mclGetUninitializedArray();
mxArray * YDATA = mclGetUninitializedArray();
mxArray * ans = mclInitializeAns();
mxArray * c = mclGetUninitializedArray();
mxArray * cGRAD = mclGetUninitializedArray();
mxArray * caller = mclGetUninitializedArray();
mxArray * ceq = mclGetUninitializedArray();
mxArray * ceqGRAD = mclGetUninitializedArray();
mxArray * computeLambda = mclGetUninitializedArray();
mxArray * confcn = mclGetUninitializedArray();
mxArray * constflag = mclGetUninitializedArray();
mxArray * defaultopt = mclGetUninitializedArray();
mxArray * diagnostics = mclGetUninitializedArray();
mxArray * errmsg = mclGetUninitializedArray();
mxArray * errstr = mclGetUninitializedArray();
mxArray * f = mclGetUninitializedArray();
mxArray * funfcn = mclGetUninitializedArray();
mxArray * fuser = mclGetUninitializedArray();
mxArray * gradconstflag = mclGetUninitializedArray();
mxArray * gradflag = mclGetUninitializedArray();
mxArray * hessflag = mclGetUninitializedArray();
mxArray * l = mclGetUninitializedArray();
mxArray * lFinite = mclGetUninitializedArray();
mxArray * large = mclGetUninitializedArray();
mxArray * lin_eq = mclGetUninitializedArray();
mxArray * lin_ineq = mclGetUninitializedArray();
mxArray * line_search = mclGetUninitializedArray();
mxArray * medium = mclGetUninitializedArray();
mxArray * msg = mclGetUninitializedArray();
mxArray * nargin_ = mclGetUninitializedArray();
mxArray * nargout = mclInitialize(mlfScalar(nargout_));
mxArray * nfun = mclGetUninitializedArray();
mxArray * non_eq = mclGetUninitializedArray();
mxArray * non_ineq = mclGetUninitializedArray();
mxArray * numberOfVariables = mclGetUninitializedArray();
mxArray * u = mclGetUninitializedArray();
mxArray * uFinite = mclGetUninitializedArray();
mxArray * verbosity = mclGetUninitializedArray();
mxArray * warnstr = mclGetUninitializedArray();
mxArray * xstart = mclGetUninitializedArray();
mlfAssign(&nargin_, mlfNargin(1, FUN, x_, LB, UB, options, varargin, NULL));
mclValidateInputs("lsqnonlin", 5, &FUN, &x_, &LB, &UB, &options);
mclCopyArray(&LB);
mclCopyArray(&UB);
mclCopyArray(&options);
mclCopyInputArg(&x, x_);
/*
* %LSQNONLIN Solves non-linear least squares problems.
* % LSQNONLIN solves problems of the form:
* % min sum {FUN(X).^2} where X and the values returned by FUN can be
* % x vectors or matrices.
* %
* % X=LSQNONLIN(FUN,X0) starts at the matrix X0 and finds a minimum to the
* % sum of squares of the functions described in FUN. FUN is usually
* % an M-file which returns a vector of objective functions: F=FUN(X).
* % NOTE: FUN should return FUN(X) and not the sum-of-squares
* % sum(FUN(X).^2)). (FUN(X) is summed and squared implicitly in
* % the algorithm.) See below for more options for FUN.
* %
* % X=LSQNONLIN(FUN,X0,LB,UB) defines a set of lower and upper
* % bounds on the design variables, X, so that the solution is in
* % the range LB <= X <= UB. Use empty matrices for LB and UB
* % if no bounds exist. Set LB(i) = -inf if X(i) is unbounded below;
* % set UB(i) = inf if X(i) is unbounded above.
* %
* % X=LSQNONLIN(FUN,X0,LB,UB,OPTIONS) minimizes with the default optimization
* % parameters replaced by values in the structure OPTIONS, an argument
* % created with the OPTIMSET function. See OPTIMSET for details. Used
* % options are Display, TolX, TolFun, DerivativeCheck, Diagnostics, Jacobian,
* % JacobPattern, LineSearchType, LevenbergMarquardt, MaxFunEvals, MaxIter,
* % DiffMinChange and DiffMaxChange, LargeScale, MaxPCGIter, PrecondBandWidth,
* % TolPCG, TypicalX. Use the Jacobian option to specify that FUN may be
* % called with two output arguments where the second, J, is the Jacobian
* % matrix: [F,J] = feval(FUN,X). If FUN returns a vector (matrix) of m
* % components when X has length n, then J is an m-by-n matrix where J(i,j)
* % is the partial derivative of F(i) with respect to x(j). (Note that the
* % Jacobian J is the transpose of the gradient of F.)
* %
* % X=LSQNONLIN(FUN,X0,LB,UB,OPTIONS,P1,P2,..) passes the
* % problem-dependent parameters P1,P2,... directly to the functions FUN:
* % FUN(X,P1,P2,...). Pass an empty matrix for OPTIONS to use the default
* % values.
* %
* % [X,RESNORM]=LSQNONLIN(FUN,X0,...) returns
* % the value of the squared 2-norm of the residual at X: sum(FUN(X).^2).
* %
* % [X,RESNORM,RESIDUAL]=LSQNONLIN(FUN,X0,...) returns the value of the
* % residual at the solution X: RESIDUAL = FUN(X).
* %
* % [X,RESNORM,RESIDUAL,EXITFLAG]=LSQNONLIN(FUN,X0,...) returns a string
* % EXITFLAG that describes the exit condition of LSQNONLIN.
* % If EXITFLAG is:
* % > 0 then LSQNONLIN converged to a solution X.
* % 0 then the maximum number of function evaluations was reached.
* % < 0 then LSQNONLIN did not converge to a solution.
* %
* % [X,RESNORM,RESIDUAL,EXITFLAG,OUTPUT]=LSQNONLIN(FUN,X0,...) returns a
* % structure OUTPUT with the number of iterations taken in OUTPUT.iterations,
* % the number of function evaluations in OUTPUT.funcCount, the algorithm used
* % in OUTPUT.algorithm, the number of CG iterations (if used) in OUTPUT.cgiterations,
* % and the first-order optimality (if used) in OUTPUT.firstorderopt.
* %
* % [X,RESNORM,RESIDUAL,EXITFLAG,OUTPUT,LAMBDA]=LSQNONLIN(FUN,X0,...) returns
* % the set of Lagrangian multipliers, LAMBDA, at the solution: LAMBDA.lower
* % for LB and LAMBDA.upper for UB.
* %
* % [X,RESNORM,RESIDUAL,EXITFLAG,OUTPUT,LAMBDA,JACOBIAN]=LSQNONLIN(FUN,X0,...)
* % returns the Jacobian of FUN at X.
* %
*
* % Copyright (c) 1990-98 by The MathWorks, Inc.
* % $Revision: 1.24 $ $Date: 1998/10/22 20:11:13 $
* % Andy Grace 7-9-90.
* % revised by Wes Wang 6-29-93.
*
* % The default algorithm is the Levenberg-Marquardt method with a
* % mixed quadratic and cubic line search procedure. A Gauss-Newton
* % method is selected by setting OPTIONS(5)=1.
* %
*
* % ------------Initialization----------------
*
*
* defaultopt = optimset('display','final','LargeScale','on', ...
*/
mlfAssign(
&defaultopt,
mlfNOptimset(
1,
mxCreateString("display"),
mxCreateString("final"),
mxCreateString("LargeScale"),
mxCreateString("on"),
mxCreateString("TolX"),
mlfScalar(1e-6),
mxCreateString("TolFun"),
mlfScalar(1e-6),
mxCreateString("DerivativeCheck"),
mxCreateString("off"),
mxCreateString("Diagnostics"),
mxCreateString("off"),
mxCreateString("Jacobian"),
mxCreateString("off"),
mxCreateString("MaxFunEvals"),
mxCreateString("100*numberOfVariables"),
mxCreateString("DiffMaxChange"),
mlfScalar(1e-1),
mxCreateString("DiffMinChange"),
mlfScalar(1e-8),
mxCreateString("PrecondBandWidth"),
mlfScalar(0.0),
mxCreateString("TypicalX"),
mxCreateString("ones(numberOfVariables,1)"),
mxCreateString("MaxPCGIter"),
mxCreateString("max(1,floor(numberOfVariables/2))"),
mxCreateString("TolPCG"),
mlfScalar(0.1),
mxCreateString("MaxIter"),
mlfScalar(400.0),
mxCreateString("JacobPattern"),
mclCreateEmptyArray(),
mxCreateString("LineSearchType"),
mxCreateString("quadcubic"),
mxCreateString("LevenbergMarq"),
mxCreateString("on"),
NULL));
/*
* 'TolX',1e-6,'TolFun',1e-6,'DerivativeCheck','off',...
* 'Diagnostics','off',...
* 'Jacobian','off','MaxFunEvals','100*numberOfVariables',...
* 'DiffMaxChange',1e-1,'DiffMinChange',1e-8,...
* 'PrecondBandWidth',0,'TypicalX','ones(numberOfVariables,1)','MaxPCGIter','max(1,floor(numberOfVariables/2))', ...
* 'TolPCG',0.1,'MaxIter',400,'JacobPattern',[], ...
* 'LineSearchType','quadcubic','LevenbergMarq','on');
*
* % If just 'defaults' passed in, return the default options in X
* if nargin==1 & nargout <= 1 & isequal(FUN,'defaults')
*/
{
mxArray * a_ = mclInitialize(mlfEq(nargin_, mlfScalar(1.0)));
if (mlfTobool(a_)) {
mlfAssign(&a_, mlfAnd(a_, mlfLe(nargout, mlfScalar(1.0))));
} else {
mlfAssign(&a_, mlfScalar(0));
}
if (mlfTobool(a_)
&& mlfTobool(
mlfAnd(
a_, mlfIsequal(FUN, mxCreateString("defaults"), NULL)))) {
mxDestroyArray(a_);
/*
* x = defaultopt;
*/
mlfAssign(&x, defaultopt);
/*
* return
*/
goto return_;
} else {
mxDestroyArray(a_);
}
/*
* end
*/
}
/*
* xstart=x(:);
*/
mlfAssign(&xstart, mlfIndexRef(x, "(?)", mlfCreateColonIndex()));
/*
* numberOfVariables=length(xstart);
*/
mlfAssign(&numberOfVariables, mlfLength(xstart));
/*
*
* large = 'large-scale';
*/
mlfAssign(&large, mxCreateString("large-scale"));
/*
* medium = 'medium-scale';
*/
mlfAssign(&medium, mxCreateString("medium-scale"));
/*
*
* if nargin < 2, error('LSQNONLIN requires two input arguments');end
*/
if (mlfTobool(mlfLt(nargin_, mlfScalar(2.0)))) {
mlfError(mxCreateString("LSQNONLIN requires two input arguments"));
}
/*
* if nargin < 3, LB=[]; end
*/
if (mlfTobool(mlfLt(nargin_, mlfScalar(3.0)))) {
mlfAssign(&LB, mclCreateEmptyArray());
}
/*
* if nargin < 4, UB=[]; end
*/
if (mlfTobool(mlfLt(nargin_, mlfScalar(4.0)))) {
mlfAssign(&UB, mclCreateEmptyArray());
}
/*
* if nargin < 5, options=[]; end
*/
if (mlfTobool(mlfLt(nargin_, mlfScalar(5.0)))) {
mlfAssign(&options, mclCreateEmptyArray());
}
/*
* if nargout > 5
*/
if (mlfTobool(mlfGt(nargout, mlfScalar(5.0)))) {
/*
* computeLambda = 1;
*/
mlfAssign(&computeLambda, mlfScalar(1.0));
/*
* else
*/
} else {
/*
* computeLambda = 0;
*/
mlfAssign(&computeLambda, mlfScalar(0.0));
/*
* end
*/
}
/*
*
* options = optimset(defaultopt,options);
*/
mlfAssign(&options, mlfNOptimset(1, defaultopt, options, NULL));
/*
* switch optimget(options,'display')
*/
{
mxArray * t_ = mclUnassigned();
mlfAssign(&t_, mlfOptimget(options, mxCreateString("display"), NULL));
/*
* case {'off','none'}
*/
if (mclSwitchCompare(
t_,
mlfCellhcat(
mxCreateString("off"), mxCreateString("none"), NULL))) {
/*
* verbosity = 0;
*/
mlfAssign(&verbosity, mlfScalar(0.0));
/*
* case 'iter'
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -