CLsqFit:SetNumCoefs
The SetNumCoefs method specifies the number of coefficients and independent variables used in the fit.
CLsqFit:SetNumCoefs( nCoefs ) CLsqFit:SetNumCoefs( nCoefs, nDimensions ) CLsqFit:SetNumCoefs( nCoefs[] ) where |
nCoefs is a number that specifies the total number of coefficients to fit.
nDimensions is a number that specifies the number of basis dimensions (number of independent variables) used by the basis function. This parameter is optional and defaults to 1 if not specified. You must use it if you are using a multi-dimensional fit and the basis function is not the built-in n-dimensional polynomial.
nCoefs[] is a Lua table that specifies the number of fit coefficients for the n-dimensional polynomial basis function when n > 1. In this case, nDimensions is determined from the count of nCoefs[].
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 formats 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:
n-dimensional polynomial: If only a single number is used to specify the coefficients, the polynomial is presumed to fit 1 independent variable (1 dimension). If more than 1 dimension is desired, pass the number of coefficients for each dimension in a table using the {} format. For example, SetNumCoefs({2,3}) sets 2 coefficients in x and 3 coefficients in y. Notice the {} around the two values creates a table and passes it to the CLsqFit object where it is interpreted as specifying 2 dimensions.
Hyperplane: The number of coefficients specifies the number of dimensions (independent variables) used in the fit plus 1 to allow for a constant term. For example, to fit 3 variables, such as x, y, t, then use SetNumCoefs(4).
User-specified basis function: Specify both the number of coefficients and the number of dimensions. For example, if the basis function fits 8 coefficients to 3 dimensions, use SetNumCoefs(8,3).
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 |