CLsqFit:SetNumCoefs
The SetNumCoefs method specifies the number of coefficients and independent variables used in the fit. This method must be called only after calling SetBasisFunc.
bSuccess, sMsg = CLsqFit:SetNumCoefs( nCoefs ) bSuccess, sMsg = CLsqFit:SetNumCoefs( tblCoefs ) bSuccess, sMsg = CLsqFit:SetNumCoefs( nCoefs, nDimensions ) |
nCoefs is the number of coefficients only to be used when fitting either if the built-in basis functions: the 1-dimensional polynomial and the hyperplane. For a hyperplane fit, this is number is dimensions+1 (the constant term + number of dimensions).
tblCoefs is a 1-dimensional array containing the number of coefficients in each dimension only when using the built-in polynomial function with 2 or more dimensions.
nDimensions is passed only when using a user-defined basis function. For this basis function, the number of coefficients can be allocated over any number of dimensions.
Return Value:
On success, true is returned with sMsg = "Success".
On failure, false is returned with sMsg giving a detailed error message.
This method set the number of coefficients and dimensions (independent variables) in the fit. ThenCoefs and tblCoefs parameters specify both the number of dimensions and the number of coefficients in each dimension. If some of the coefficients are forced to a fixed value (see the ForceCoef method), this function includes those forced coefficients. For example, the fit may start out with nCoefs = 10 but 3 would be forced, leaving 7 coefficients fit.
For example, if the basis function fits a line to (x,y) data, as in y = f(x), then this involves 2 coefficients and you would useSetNumCoefs( 2 ). If you were to force the slope coefficient to 1.0 and fit only the intercept, the basis function still would use 2 coefficients so that you would useSetNumCoefs( 2 ).
This function must be called after calling SetBasisFunc so that the coefficients can be correctly configured for the specific basis function. The reason for this requirement is that the various types of basis functions require specific formats as described above. Note that neither of the return parameters is required to be caught by your script. However, you might test for the error condition bSuccess == false to view the returned error message. For example, after calling L = new_lsqfit(), you might call this method as follows. The three different parameter formats are used simply for illustration purposes:
This method configures the CLsqFit object to work with the number of dimensions and coefficients used by the basis function. Three types of basis function are provided: n-dimensional polynomial, hyperplane, and user-supplied function. The interpretation of the nCoefs and tblCoefs depends on the basis function being fit:
For this case, nCoefs is a single number giving the number of coefficients.
For this case, specify tblCoefs as a table containing the number of coefficients in each dimension. For example, fitting a 2-dimensional polynomial f(x,y), tblCoefs is an array of length 2, In this case,SetNumCoefs( {2,3} ) specifies 2 coefficients in the x dimension and 3 coefficients in the y dimension.
The value of nCoefs is 1 greater than the number of dimensions being fit, in order to allow for the constant term. For example, to fit 3 variables, f(x,y,z), use SetNumCoefs( 4 ).
The coefficients can be allocated over any number of dimensions. For example, 7 coefficients could be fit to 3 dimensions. The basis vector determines how the coefficients are assigned. See the topic Basis Functions.
The following script calls SetNumCoefs with nCoefs = 3 to fit a 1-dimensional polynomial of 3 terms. A polynomial is the default unless SetBasisFunc is called to change it.
|
-- create a CLsqFit object |
|
-- set 3 coef for a 2nd order polynomial fit |
|
-- add a point for x = 3.5, y = 5 |
|
-- add a point |
|
-- add a point |
|
-- Fit the 2nd order curve |
The following script fits a 2-dimensional polynomial with 5 coefficients in dimension 1 and 3 coefficients in dimension 2. The total number of coefficients is 15. This uses the tblCoefs parameter to specify the number of coefficients in each dimension.
|
-- create a CLsqFit object |
|
-- set 5 coefs in dimension 1 and 3 coefs in dimension 2 |
|
-- add a point for x = 3.5, y = 5.15, value = 15 |
-- add more points to the fit |
|
|
-- Fit the 5x3 polynomial |
The following script fits a hyperplane of 4 dimensions, f(x,y,z,w), viz-a-vis, f = a[1] + a[2]x + a[3]y + a[4]z + a[5]w. For 4 dimensions, remember to call SetNumCoefs(5) to allow for the constant term, a[1].
|
-- create a CLsqFit object |
|
-- specify basis function number 2, the Hyperplane. |
|
-- set 5 coefficients to fit 4 dimensions + constant |
|
-- add a point with 4 dimensions and value 12.5 |
-- add at least 4 more points to the fit |
-- at least 5 points needed for fitting 5 coefficients |
|
-- Fit the basis function |
The script below fits 5 coefficients to 3 dimensions using a user-defined basis function declared in your script. This uses SetNumCoefs(5,3). Assuming these 3 dimensions are x, y, and z, then the function being fit is f = a[1] + a[2]x + a[3]xy + a[4]z + a[5]z^2. This example is also used in the Basis Functions topic:
|
-- create a CLsqFit object |
|
-- specify the basis function "MyBasisFunc" |
|
-- fit 5 coefficients in 3 dimensions |
|
|
-- add some data points |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- define the basis function |
|
|
-- receives n=5 and V[] with 3 elements |
|
-- declare b as local inside the function |
|
-- the constant term a[1] |
|
-- the x term for a[2] |
|
-- the xy term for a[3] |
|
-- the z term for a[4] |
|
-- the z^2 term for a[5] |
|
-- return the basis vector |
|
|
|
|
|
-- fit the basis function |
Using Multiple Independent Variables
Mira Pro x64 Script User's Guide, v.8.73 Copyright Ⓒ 2024
Mirametrics, Inc. All Rights Reserved.