⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpfitfun.pro

📁 非线性最小二乘-LM法 是用IDL语言编写的
💻 PRO
📖 第 1 页 / 共 2 页
字号:
;+; NAME:;   MPFITFUN;; AUTHOR:;   Craig B. Markwardt, NASA/GSFC Code 662, Greenbelt, MD 20770;   craigm@lheamail.gsfc.nasa.gov;   UPDATED VERSIONs can be found on my WEB PAGE: ;      http://cow.physics.wisc.edu/~craigm/idl/idl.html;; PURPOSE:;   Perform Levenberg-Marquardt least-squares fit to IDL function;; MAJOR TOPICS:;   Curve and Surface Fitting;; CALLING SEQUENCE:;   parms = MPFITFUN(MYFUNCT, X, Y, ERR, start_params, ...);; DESCRIPTION:;;  MPFITFUN fits a user-supplied model -- in the form of an IDL;  function -- to a set of user-supplied data.  MPFITFUN calls;  MPFIT, the MINPACK-1 least-squares minimizer, to do the main;  work.;;  Given the data and their uncertainties, MPFITFUN finds the best set;  of model parameters which match the data (in a least-squares;  sense) and returns them in an array.;  ;  The user must supply the following items:;   - An array of independent variable values ("X").;   - An array of "measured" *dependent* variable values ("Y").;   - An array of "measured" 1-sigma uncertainty values ("ERR").;   - The name of an IDL function which computes Y given X ("MYFUNCT").;   - Starting guesses for all of the parameters ("START_PARAMS").;;  There are very few restrictions placed on X, Y or MYFUNCT.  Simply;  put, MYFUNCT must map the "X" values into "Y" values given the;  model parameters.  The "X" values may represent any independent;  variable (not just Cartesian X), and indeed may be multidimensional;  themselves.  For example, in the application of image fitting, X;  may be a 2xN array of image positions.;;  Data values of NaN or Infinity for "Y", "ERR" or "WEIGHTS" will be;  ignored as missing data if the NAN keyword is set.  Otherwise, they;  may cause the fitting loop to halt with an error message.  Note;  that the fit will still halt if the model function produces;  infinite or NaN values.;;  MPFITFUN carefully avoids passing large arrays where possible to;  improve performance.;;  See below for an example of usage.;; USER FUNCTION;;  The user must define a function which returns the model value.  For;  applications which use finite-difference derivatives -- the default;  -- the user function should be declared in the following way:;;    FUNCTION MYFUNCT, X, P;     ; The independent variable is X;     ; Parameter values are passed in "P";     YMOD = ... computed model values at X ...;     return, YMOD;    END;;  The returned array YMOD must have the same dimensions and type as;  the "measured" Y values.;;  User functions may also indicate a fatal error condition;  using the ERROR_CODE common block variable, as described;  below under the MPFIT_ERROR common block definition.;;  See the discussion under "EXPLICIT DERIVATIVES" and AUTODERIVATIVE;  in MPFIT.PRO if you wish to compute the derivatives for yourself.;  AUTODERIVATIVE is accepted by MPFITFUN and passed directly to;  MPFIT.  The user function must accept one additional parameter, DP,;  which contains the derivative of the user function with respect to;  each parameter at each data point, as described in MPFIT.PRO.;; CONSTRAINING PARAMETER VALUES WITH THE PARINFO KEYWORD;;  The behavior of MPFIT can be modified with respect to each;  parameter to be fitted.  A parameter value can be fixed; simple;  boundary constraints can be imposed; limitations on the parameter;  changes can be imposed; properties of the automatic derivative can;  be modified; and parameters can be tied to one another.;;  These properties are governed by the PARINFO structure, which is;  passed as a keyword parameter to MPFIT.;;  PARINFO should be an array of structures, one for each parameter.;  Each parameter is associated with one element of the array, in;  numerical order.  The structure can have the following entries;  (none are required):;  ;     .VALUE - the starting parameter value (but see the START_PARAMS;              parameter for more information).;  ;     .FIXED - a boolean value, whether the parameter is to be held;              fixed or not.  Fixed parameters are not varied by;              MPFIT, but are passed on to MYFUNCT for evaluation.;  ;     .LIMITED - a two-element boolean array.  If the first/second;                element is set, then the parameter is bounded on the;                lower/upper side.  A parameter can be bounded on both;                sides.  Both LIMITED and LIMITS must be given;                together.;  ;     .LIMITS - a two-element float or double array.  Gives the;               parameter limits on the lower and upper sides,;               respectively.  Zero, one or two of these values can be;               set, depending on the values of LIMITED.  Both LIMITED;               and LIMITS must be given together.;  ;     .PARNAME - a string, giving the name of the parameter.  The;                fitting code of MPFIT does not use this tag in any;                way.  However, the default ITERPROC will print the;                parameter name if available.;  ;     .STEP - the step size to be used in calculating the numerical;             derivatives.  If set to zero, then the step size is;             computed automatically.  Ignored when AUTODERIVATIVE=0.;             This value is superceded by the RELSTEP value.;;     .RELSTEP - the *relative* step size to be used in calculating;                the numerical derivatives.  This number is the;                fractional size of the step, compared to the;                parameter value.  This value supercedes the STEP;                setting.  If the parameter is zero, then a default;                step size is chosen.;;     .MPSIDE - the sidedness of the finite difference when computing;               numerical derivatives.  This field can take four;               values:;;                  0 - one-sided derivative computed automatically;                  1 - one-sided derivative (f(x+h) - f(x)  )/h;                 -1 - one-sided derivative (f(x)   - f(x-h))/h;                  2 - two-sided derivative (f(x+h) - f(x-h))/(2*h);;              Where H is the STEP parameter described above.  The;              "automatic" one-sided derivative method will chose a;              direction for the finite difference which does not;              violate any constraints.  The other methods do not;              perform this check.  The two-sided method is in;              principle more precise, but requires twice as many;              function evaluations.  Default: 0.;;     .MPMAXSTEP - the maximum change to be made in the parameter;                  value.  During the fitting process, the parameter;                  will never be changed by more than this value in;                  one iteration.;;                  A value of 0 indicates no maximum.  Default: 0.;  ;     .TIED - a string expression which "ties" the parameter to other;             free or fixed parameters as an equality constraint.  Any;             expression involving constants and the parameter array P;             are permitted.;             Example: if parameter 2 is always to be twice parameter;             1 then use the following: parinfo[2].tied = '2 * P[1]'.;             Since they are totally constrained, tied parameters are;             considered to be fixed; no errors are computed for them.;             [ NOTE: the PARNAME can't be used in a TIED expression. ];;     .MPPRINT - if set to 1, then the default ITERPROC will print the;                parameter value.  If set to 0, the parameter value;                will not be printed.  This tag can be used to;                selectively print only a few parameter values out of;                many.  Default: 1 (all parameters printed);;     .MPFORMAT - IDL format string to print the parameter within;                 ITERPROC.  Default: '(G20.6)'  (An empty string will;                 also use the default.);;  Future modifications to the PARINFO structure, if any, will involve;  adding structure tags beginning with the two letters "MP".;  Therefore programmers are urged to avoid using tags starting with;  "MP", but otherwise they are free to include their own fields;  within the PARINFO structure, which will be ignored by MPFIT.;  ;  PARINFO Example:;  parinfo = replicate({value:0.D, fixed:0, limited:[0,0], $;                       limits:[0.D,0]}, 5);  parinfo[0].fixed = 1;  parinfo[4].limited[0] = 1;  parinfo[4].limits[0]  = 50.D;  parinfo[*].value = [5.7D, 2.2, 500., 1.5, 2000.];  ;  A total of 5 parameters, with starting values of 5.7,;  2.2, 500, 1.5, and 2000 are given.  The first parameter;  is fixed at a value of 5.7, and the last parameter is;  constrained to be above 50.;; COMPATIBILITY;;  This function is designed to work with IDL 5.0 or greater.;  ;  Because TIED parameters rely on the EXECUTE() function, they cannot;  be used with the free version of the IDL Virtual Machine.;;; INPUTS:;   MYFUNCT - a string variable containing the name of an IDL function.;             This function computes the "model" Y values given the;             X values and model parameters, as desribed above.;;   X - Array of independent variable values.;;   Y - Array of "measured" dependent variable values.  Y should have;       the same data type as X.  The function MYFUNCT should map;       X->Y.;;   ERR - Array of "measured" 1-sigma uncertainties.  ERR should have;         the same data type as Y.  ERR is ignored if the WEIGHTS;         keyword is specified.;;   START_PARAMS - An array of starting values for each of the;                  parameters of the model.  The number of parameters;                  should be fewer than the number of measurements.;                  Also, the parameters should have the same data type;                  as the measurements (double is preferred).;;                  This parameter is optional if the PARINFO keyword;                  is used (see MPFIT).  The PARINFO keyword provides;                  a mechanism to fix or constrain individual;                  parameters.  If both START_PARAMS and PARINFO are;                  passed, then the starting *value* is taken from;                  START_PARAMS, but the *constraints* are taken from;                  PARINFO.; ;; RETURNS:;;   Returns the array of best-fit parameters.;;; KEYWORD PARAMETERS:;;   BESTNORM - the value of the summed squared residuals for the;              returned parameter values.;;   COVAR - the covariance matrix for the set of parameters returned;           by MPFIT.  The matrix is NxN where N is the number of;           parameters.  The square root of the diagonal elements;           gives the formal 1-sigma statistical errors on the;           parameters IF errors were treated "properly" in MYFUNC.;           Parameter errors are also returned in PERROR.;;           To compute the correlation matrix, PCOR, use this:;           IDL> PCOR = COV * 0;           IDL> FOR i = 0, n-1 DO FOR j = 0, n-1 DO $;                PCOR[i,j] = COV[i,j]/sqrt(COV[i,i]*COV[j,j]);;           If NOCOVAR is set or MPFIT terminated abnormally, then;           COVAR is set to a scalar with value !VALUES.D_NAN.;;   CASH - when set, the fit statistic is changed to a derivative of;          the CASH statistic.  The model function must be strictly;          positive. WARNING: this option is incomplete and untested.;;   DOF - number of degrees of freedom, computed as;             DOF = N_ELEMENTS(DEVIATES) - NFREE;         Note that this doesn't account for pegged parameters (see;         NPEGGED).;;   ERRMSG - a string error or warning message is returned.;;   FTOL - a nonnegative input variable. Termination occurs when both;          the actual and predicted relative reductions in the sum of;          squares are at most FTOL (and STATUS is accordingly set to;          1 or 3).  Therefore, FTOL measures the relative error;          desired in the sum of squares.  Default: 1D-10;;   FUNCTARGS - A structure which contains the parameters to be passed;               to the user-supplied function specified by MYFUNCT via;               the _EXTRA mechanism.  This is the way you can pass;               additional data to your user-supplied function without;               using common blocks.;;               By default, no extra parameters are passed to the;               user-supplied function.;;   GTOL - a nonnegative input variable. Termination occurs when the;          cosine of the angle between fvec and any column of the;          jacobian is at most GTOL in absolute value (and STATUS is;          accordingly set to 4). Therefore, GTOL measures the;          orthogonality desired between the function vector and the;          columns of the jacobian.  Default: 1D-10;;   ITERARGS - The keyword arguments to be passed to ITERPROC via the;              _EXTRA mechanism.  This should be a structure, and is;              similar in operation to FUNCTARGS.;              Default: no arguments are passed.;;   ITERPROC - The name of a procedure to be called upon each NPRINT;              iteration of the MPFIT routine.  It should be declared;              in the following way:;;              PRO ITERPROC, MYFUNCT, p, iter, fnorm, FUNCTARGS=fcnargs, $;                PARINFO=parinfo, QUIET=quiet, ...;                ; perform custom iteration update;              END;         ;              ITERPROC must either accept all three keyword;              parameters (FUNCTARGS, PARINFO and QUIET), or at least;              accept them via the _EXTRA keyword.;          ;              MYFUNCT is the user-supplied function to be minimized,;              P is the current set of model parameters, ITER is the;              iteration number, and FUNCTARGS are the arguments to be;              passed to MYFUNCT.  FNORM should be the;              chi-squared value.  QUIET is set when no textual output;              should be printed.  See below for documentation of;              PARINFO.;;              In implementation, ITERPROC can perform updates to the;              terminal or graphical user interface, to provide;              feedback while the fit proceeds.  If the fit is to be;              stopped for any reason, then ITERPROC should set the;              common block variable ERROR_CODE to negative value (see;              MPFIT_ERROR common block below).  In principle,;              ITERPROC should probably not modify the parameter;              values, because it may interfere with the algorithm's;              stability.  In practice it is allowed.;;              Default: an internal routine is used to print the;                       parameter values.;;   MAXITER - The maximum number of iterations to perform.  If the;             number is exceeded, then the STATUS value is set to 5;             and MPFIT returns.;             Default: 200 iterations;;   NAN - ignore infinite or NaN values in the Y, ERR or WEIGHTS;         parameters.  These values will be treated as missing data.;         However, the fit will still halt with an error condition;         if the model function becomes infinite.;;   NFEV - the number of MYFUNCT function evaluations performed.;;   NFREE - the number of free parameters in the fit.  This includes;           parameters which are not FIXED and not TIED, but it does;           include parameters which are pegged at LIMITS.;;   NITER - the number of iterations completed.;;   NOCOVAR - set this keyword to prevent the calculation of the;             covariance matrix before returning (see COVAR);;   NPEGGED - the number of free parameters which are pegged at a;             LIMIT.;;   NPRINT - The frequency with which ITERPROC is called.  A value of;            1 indicates that ITERPROC is called with every iteration,;            while 2 indicates every other iteration, etc.  Note that;            several Levenberg-Marquardt attempts can be made in a;            single iteration.;            Default value: 1;;   PARINFO - Provides a mechanism for more sophisticated constraints;             to be placed on parameter values.  When PARINFO is not;             passed, then it is assumed that all parameters are free

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -