FUNCTION CURVEFIT,X,Y,W,A,SIGMAA ;+ ; NAME: ; CURVEFIT ; PURPOSE: ; Non-linear least squares fit to a function of an ; arbitrary number of parameters. ; Function may be any non-linear function where ; the partial derivatives are known or can be approximated. ; CATEGORY: ; E2 - Curve and Surface Fitting ; CALLING SEQUENCE: ; YFIT = CURVEFIT(X,Y,W,A,SIGMAA) ; INPUTS: ; X = Row vector of independent variables. ; Y = Row vector of dependent variable, same length as x. ; W = Row vector of weights, same length as x and y. ; For no weighting ; w(i) = 1., instrumental weighting w(i) = ; 1./y(i), etc. ; A = Vector of nterms length containing the initial estimate ; for each parameter. If A is double precision, calculations ; are performed in double precision, otherwise in single prec. ; ; OUTPUTS: ; A = Vector of parameters containing fit. ; Function result = YFIT = Vector of calculated ; values. ; OPTIONAL OUTPUT PARAMETERS: ; Sigmaa = Vector of standard deviations for parameters ; A. ; ; COMMON BLOCKS: ; NONE. ; SIDE EFFECTS: ; The function to be fit must be defined and called FUNCT. ; For an example see FUNCT in the IDL User's Libaray. ; Call to FUNCT is: ; FUNCT,X,A,F,PDER ; where: ; X = Vector of NPOINT independent variables, input. ; A = Vector of NTERMS function parameters, input. ; F = Vector of NPOINT values of function, y(i) = funct(x(i)), output. ; PDER = Array, (NPOINT, NTERMS), of partial derivatives of funct. ; PDER(I,J) = DErivative of function at ith point with ; respect to jth parameter. Optional output parameter. ; PDER should not be calculated if parameter is not ; supplied in call (Unless you want to waste some time). ; RESTRICTIONS: ; NONE. ; PROCEDURE: ; Copied from "CURFIT", least squares fit to a non-linear ; function, pages 237-239, Bevington, Data Reduction and Error ; Analysis for the Physical Sciences. ; ; "This method is the Gradient-expansion algorithm which ; compines the best features of the gradient search with ; the method of linearizing the fitting function." ; ; Iterations are perform until the chi square changes by ; only 0.1% or until 20 iterations have been performed. ; ; The initial guess of the parameter values should be ; as close to the actual values as possible or the solution ; may not converge. ; ; MODIFICATION HISTORY: ; Written, DMS, RSI, September, 1982. ;- ON_ERROR,2 ;RETURN TO CALLER IF ERROR A = 1.*A ;MAKE PARAMS FLOATING NTERMS = N_ELEMENTS(A) ;# OF PARAMS. NFREE = (N_ELEMENTS(Y)