CLsqFit:SetNumCoefs
The SetNumCoefs method specifies the number of coefficients and independent variables used in the fit.
CLsqFit:SetNumCoefs( nCoefs, nDimensions* ) CLsqFit:SetNumCoefs( tableCoefs ) |
This method configures the CLsqFit object for the number of coefficients and dimensions, or independent variables, used by the basis function. For this reason, the basis function must be known before this method is called. If no basis function is declared, then this rule does not apply because Mira defaults to the built-in n-dimensional polynomial basis function.
If fitting the n-dimensional polynomial, the coefficient argument indicates the number of dimensions and so you do not explicitly specify the number of dimensions. Otherwise you must specify both the total number of coefficients and the total number of dimensions. The basis function dimensions can be fetched using the GetBasisDim method.
The nCoefs or tableCoefs parameter specifies the number of coefficients expected by the specified basis function even if some of the coefficients are removed from the fit and forced to a fixed value (see the ForceCoef method), For example, if the basis function fits a line to (x,y) data, then the function uses 2 coefficients and you would use SetNumCoefs( 2 ). If you were to force the slope coefficient to 1.0 and fit only the intercept then, since the basis function still is defined to use 2 coefficients, you still would use SetNumCoefs( 2 ). Alternatively, you could modify the basis function to fit only the intercept coefficient and then you would use SetNumCoefs( 1 ).
If you use the default, built-in n-dimensional polynomial basis function with more than one dimension (e.g., n=2 as for fitting the surface f(x,y)), then the number of coefficients is specified using a lua table rather than a number. These different forms are shown in the examples below.
This method configures the CLsqFit object to work with the number of dimensions and coefficients used by the basis function. Depending on the basis function being used, this method uses one of the syntax options described above:
The first example, below, defaults to fitting the built-in n-dimensional polynomial. This script specifies 3 coefficients for fitting a 2nd order polynomial to 1 variable. Since only 1 argument is specified, the polynomial fits 1 independent variable.
|
-- create a CLsqFit object |
|
-- set 3 coefficients 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 example fits a 2-dimensional polynomial with 5 coefficients in dimension 1 (e.g., "x") and 3 coefficients in dimension 2 (e.g., "y"). Notice that the 2 arguments are combined into 1 argument using a table.
|
-- create a CLsqFit object |
|
-- set 5 coefficients in x and 3 coefs in y using a table |
|
-- add a point for x = 3.5, y = 5.15, value = 15 |
|
-- add more points |
|
-- Fit the 5x3 polynomial |
The next example passes both the number of coefficients to fit and the number of dimensions of the independent variable. In this case, we are fitting a hyperplane of 4 variables and 5 coefficients (one coefficient is the constant term). Both arguments must be passed as a number.
|
-- create a CLsqFit object |
|
-- specify basis function number 2, the Hyperplane. |
|
-- set 5 coefficients and 4 dimensions (variables) |
|
-- add a point with 4 dimensions and value 12.5 |
|
-- add more points |
|
-- Fit the basis function |
The final example passes both the number of coefficients to fit and the number of dimensions of the independent variable. In this case, we are fitting 9 coefficients to a basis function named "F" that takes 5 independent variables. Both arguments must be passed as a number.
|
-- create a CLsqFit object |
|
-- specify a basis function named "F" |
|
-- set 9 coefficients and 5 independent variables |
|
-- add a point with 5 dimensions and value 12.5 |
|
-- add more points |
|
-- Fit the basis function |
CLsqFit class, GetNumCoefs, Using Multiple Independent Variables, Basis Functions, SetBasisFunc, GetBasisDim