CLsqFit:SetNumChannels CLsqFit:SetPtWt

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.

Syntax

bSuccess, sMsg = CLsqFit:SetNumCoefs( nCoefs )

bSuccess, sMsg = CLsqFit:SetNumCoefs( tblCoefs )

bSuccess, sMsg = CLsqFit:SetNumCoefs( nCoefs, nDimensions )

bullet.gif    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).

bullet.gif    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.

bullet.gif    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:

bullet.gif    On success, true is returned with sMsg = "Success".

bullet.gif    On failure, false is returned with sMsg giving a detailed error message.

Remarks

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 ).

Catching Errors

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:

L:SetNumCoefs(3)

bSuccess = L:SetNumCoefs(5,3)

bSuccess, sMsg = L:SetNumCoefs( {2,3} )

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 nCoefs and tblCoefs depends on the basis function being fit:

Polynomial of 1 dimension, y=f(x)

For this case, nCoefs is a single number giving the number of coefficients.

Polynomial of n dimensions with n > 1

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.

Hyperplane

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 ).

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 coefficients 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 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].

L = new_lsqfit()

-- create a CLsqFit object

L:SetBasisFunc("hyperplane")

-- 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 at least 4 more points to the fit

-- at least 5 points needed for fitting 5 coefficients

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, v.8.73 Copyright Ⓒ 2024 Mirametrics, Inc. All Rights Reserved.