The problem of curve fitting may be stated as follows:
Given a tabulated set of data values {xi, yi} and the general form of a mathematical model (a function f(x) with unspecified parameters), determine the parameters of the model that minimize an error criterion. The problem of surface fitting involves tabulated data of the form {xi, yi, zi} and a function f(x, y) of two spatial dimensions.
For example, we can use the CURVEFIT routine to determine the parameters A and B of a user-supplied function f(x), such that the sums of the squares of the residuals between the tabulated data {xi, yi} and function are minimized. We will use the following function and data:
f (x) = a (1 -e-bx)
xi = [0.25, 0.75, 1.25, 1.75, 2.25]
yi = [0.28, 0.57, 0.68, 0.74, 0.79]
First we must provide a procedure written in IDL to evaluate the function, f, and its partial derivatives with respect to the parameters a0 and a1:
PRO funct, X, A, F, PDER F = A[0] * (1.0 - EXP(-A[1] * X)) ; If the function is called with four parameters, ; calculate the partial derivatives: IF N_PARAMS() GE 4 THEN BEGIN ; PDER's column dimension is equal to the number of ; elements in xi and its row dimension is equal to ; the number of parameters in the function F: pder = FLTARR(N_ELEMENTS(X), 2) ; Compute the partial derivatives with respect to ; a0 and place in the first row of PDER: pder[*, 0] = 1.0 - EXP(-A[1] * X) ; Compute the partial derivatives with respect to ; a1 and place in the second row of PDER: pder[*, 1] = A[0] * x * EXP(-A[1] * X) ENDIF END
| Note |
Next, we can use the following IDL commands to find the function's parameters:
;Define the vectors of tabulated: X = [0.25, 0.75, 1.25, 1.75, 2.25] ;data values: Y = [0.28, 0.57, 0.68, 0.74, 0.79] ;Define a vector of weights: W = 1.0 / Y ;Provide an initial guess of the function's parameters: A = [1.0, 1.0] ;Compute the parameters a0 and a1: yfit = CURVEFIT(X, Y, W, A, SIGMA_A, FUNCTION_NAME = 'funct') ;Print the parameters, which are returned in A: PRINT, A
IDL prints:
0.787386 1.71602
Thus the nonlinear function that best fits the data is:
f (x) = 0.787386 ( 1 --e-1.71602x )
Below is a brief description of IDL routines for curve and surface fitting.