CLsqFit:SetNumCoefs


The SetNumCoefs method specifies the number of coefficients and independent variables used in the fit.

Syntax

CLsqFit:SetNumCoefs( nCoefs )

CLsqFit:SetNumCoefs( tblCoefs )

LsqFit:SetNumCoefs( nCoefs, nDimensions )

bullet.gif    nCoefs is the number of coefficients in a 1-dimensional fit or the number of terms in a hyperplane fit. For the hyperplane, this equals the number of dimensions plus 1 for the constant term.

bullet.gif    tblCoefs is a 1-dimensional array containing the number of coefficients in each dimension. The number of table elements determines the dimension of the fit.

bullet.gif    nDimensions is used only for the case of a user-defined basis function. In this case, the number of coefficients can be allocated over any number of dimensions.

Remarks

This method set the number of coefficients and dimensions (independent variables) in the fit. ThenCoefs and tableCoefs 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 actually fit.

the basis function fits a line to (x,y) data, then the function uses 2 coefficients and you would useSetNumCoefs( 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 useSetNumCoefs( 2 ). Alternatively, you could modify the basis function to fit only the intercept coefficient and then you would use SetNumCoefs( 1 ).

Relationship to the Basis Function

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 nChan of tblChan parameter depends on the basis function being fit:

Polynomial of n dimensions

If nCoefs is a number, then a 1-dimensional polyomial is assumed. The value of nCoefs gives the number of coefficients. If the polynomial has more than 1 dimension, pass the number of coefficients for each dimension as a table using thetblCoefs argument. For example, SetNumCoefs({2,3}) passes a 2 element table which fits 2 coefficients in x and 3 coefficients in y, and determines that the fit involves 2 dimensions.

Hyperplane

The value of nCoefs determines the number of dimensions in the fit. The value of nCoefs is 1 greater than the fit dimension to allow for the constant term. For example, to fit 3 variables, such as x, y, z, use SetNumCoefs( 4 ).

User-provided basis function

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.

Examples

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.

L = new_lsqfit()

-- create a CLsqFit object

L:SetNumCoefs( 3 )

-- set 3 coef for a 2nd order polynomial fit

L:AddPt( 3.5, 5 )

-- add a point for x = 3.5, y = 5

L:AddPt( -12, 14 )

-- add a point

L:AddPt( -2, -4.25 )

-- add a point

L:Fit()

-- 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 coefficnets in each dimension.

L = new_lsqfit()

-- create a CLsqFit object

L:SetNumCoefs( {5, 3} )

-- set 5 coefs in dimension 1 and 3 coefs in dimension 2

L:AddPt( 3.5, 5.15, 15 )

-- add a point for x = 3.5, y = 5.15, value = 15

-- add more points to the fit

 

L:Fit()

-- Fit the 5x3 polynomial

 

The following script fits a hyperplane of 4 variables. Here, SetNumCoefs passesnCoefs=5 to fit 4 dimensions plus the constant term.

L = new_lsqfit()

-- create a CLsqFit object

L:SetBasisFunc( 2 )

-- specify basis function number 2, the Hyperplane.

L:SetNumCoefs( 5 )

-- set 5 coefficients to fit 4 dimensions + constant

L:AddPt( {1,17,-3,41 }, 12.5 )

-- add a point with 4 dimensions and value 12.5

-- add more points to the fit

 

L:Fit()

-- 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:

L = new_lsqfit()

-- create a CLsqFit object

L:SetBasisFunc( "MyBasisFunc" )

-- specify the basis function "MyBasisFunc"

L:SetNumCoefs( 5, 3 )

-- fit 5 coefficients in 3 dimensions

 

 

-- add some data points

 

L:AddPt( {1.1,17.4,-3.1}, 15.92 )

 

L:AddPt( {1.2,18.1,-2.2}, 19.1 )

 

L:AddPt( {1.5,16.2,-1.9}, 11.2 )

 

L:AddPt( {1.6,17.1,-2.1}, 6.5 )

 

L:AddPt( {1.7,17.4,-1.8}, 15.4 )

 

L:AddPt( {1.8,16.9,-2.9}, 12.25 )

 

L:AddPt( {1.4,15.8,-3.4}, 8.2 )

 

L:AddPt( {1.9,16.6,-3.2}, 10.66 )

 

 

 

-- define the basis function

 

function MyBasisFunc( n, V )

-- receives n=5 and V[] with 3 elements

  local b = {}

-- declare b as local inside the function

  b[1] = 1

-- the constant term a[1]

  b[2] = V[1]

-- the x term for a[2]

  b[3] = V[1]*V[2]

-- the xy term for a[3]

  b[4] = V[3]

-- the z term for a[4]

  b[5] = V[3] * V[3]

-- the z^2 term for a[5]

  return b

-- return the basis vector

end

 

 

 

bSuccess = L:Fit()

-- fit the basis function

Related Topics

CLsqFit class, GetNumCoefs , Using Multiple Independent Variables, Basis Functions, SetBasisFunc , GetBasisDim


Mira Pro x64 Script User's Guide, Copyright Ⓒ 2023 Mirametrics, Inc. All Rights Reserved.