CLsqFit:ForceCoef


The ForceCoef method specifies the value of a specific coefficient. This removes the coefficient from the set being fit and adjusts the fit results accordingly. Use this, for example, if fitting a line and you want the slope to have a specific value such as 1.0.

Syntax

CLsqFit:ForceCoef( nIndex, nValue )

CLsqFit:ForceCoef( nIndex, nValue, nChan )

where

        nIndex is the index of the coefficient in the set of coefficients of the fit. This is a 1-based number.

        nValue is the target value of the coefficient.

        nChan is an optional parameter that specifies the target channel when fitting multi-channel data.

 

This method forces a coefficient to a specified value. The forced coefficient still is included in the total count of fit coefficients set using the SetNumCoefs method. For example, if you use SetNumCoefs(3)and call ForceCoef(2,1.0), then coefficient [2] will be set to the value 1.0 and removed from the set of "free parameters" of the fit. The fit still would use 3 parameters but parameter index 2 would be discarded from those being fit. The basis function remains unchanged when you force coefficients.

If you are doing a complex fitting procedure and do not remember which coefficients were forced, use the IsCoefForced method to determine if a particular coefficient was forced. To remove dorcing and return a coefficient to being a free parameter of the fit, use UnforceCoef. To fit all coefficients after some were forced, you can remove all forcing in one step using ResetForcedCoefs.

Example

The example below illustrates forcing a coefficient in a 2x2 coefficient polynomial fit. After computing the fit, the cross term between x and y is removed by setting it to 0, leaving a fit to an inclined plane rather than a warped plane.

 

L = CLsqFit:new()

-- create the least squares fitting object

L:SetNumCoefs( {2,2} )

-- fit a polynomial with 2 coefficients each in 2 dimensions

L:AddPt( -3, 1, 2 )

-- add an (x,y) point

L:AddPt( 4, 5, 3 )

-- add more points...

L:AddPt( 10, 8, 6 )

 

L:AddPt( 13, 17, 4.5 )

 

L:AddPt( 15, 12, 4 )

 

L:Fit()

-- do a 2x2 fit to 4 coefficients

    -- save the results for comparison

 

n = GetPolyCoefIndex( { 2, 2 } )

-- find the 2,2 coefficient in the 2x2 polynomial fit matrix

L:ForceCoef( n, 0.0 )

-- force the cross term at 2,2 to 0.

L:Fit()

-- re-do the fit with no cross term.

Related Topics

UnforceCoef

IsCoefForced

ResetForcedCoefs

GetPolyCoefIndex

CLsqFit class