CLsqFit:AddPt
The AddPt method adds a data point to the fit. The number of coordinate values (independent variables) must match the dimensions of the fit. All points are assigned unit weight. To add points with nonzero weights, use the AddPtWt method.
CLsqFit:AddPt( x, val ) CLsqFit:AddPt( x, y, val ) CLsqFit:AddPt( tblX, val ) CLsqFit:AddPt( tblX, tblRgb ) |
x is the coordinate for dimension 1 of the point when fitting 1-dimension.
tblX is a 1-dimensional array containing n elements equal to the number of dimensions in the fit. Use this form when the basis function takes more than one independent variable.
val is the numeric value of the observation at the coordinate x or the coordinate vector tblX.
tblRgb is a table of RGB channel values when fitting RGB data.
y is the second dimension for fitting numeric data to 2-dimensional (x,y) data. It is just a special form that avoids using a table for (x,y) when fitting the built-in 2-dimensional polynomial.
The AddPt and AddPtWt methods provide the only way to add sample points. The AddPt method adds points with unit weight (1.0) whereas AddPtWt adds points with a specified weight. Regardless of how a point is added, you can later change its weight using SetPtWt.
This method allows both numeric data and multi-channel RGB data for the value parameter. To use multi-channel data, call SetNumChannels and specify 3 or 4 channels, then add points using a able for RGB or RGBa data.
Once added to the sample, a point can be deleted or its weight can be changed. In addition, you can alter a point's independent variables and observed value. If you want to permanently delete all sample points, call the ResetPoints method. After using ResetPoints, new points can be added to the same CLsqFit object using AddPt or AddPtWt.
The following examples illustrate how various forms of AddPt are used to add sample data. The first example specifies 2 coefficients for fitting a line, or 1st order polynomial. Since only 1 argument is specified, the polynomial fits 1 independent variable; such a fit could be described as y = a[1] + a[2] x.
L = new_lsqfit() |
-- create a CLsqFit object (defaults to polynomial) |
L:SetNumCoefs( 2 ) |
-- set 2 coefficients |
L:AddPt( 3.5, 5.15 ) |
-- add a point for x = 3.5, y = 5.15 |
L:AddPt( -12, 14 ) |
-- add a point |
L:AddPt( -2, -4.25 ) |
-- add a point |
L:Fit() |
-- Fit the line |
The next example fits a hyperplane of 4 dimensions (hence 5 coefficients because one coefficient is the constant term). Since more than one dimension is being fit, the dependent variables must be specified using a table with 4 elements.
|
-- create a CLsqFit object (defaults to polynomial) |
|
-- select basis function 2, the "Hyperplane". |
|
-- set 5 coefficients = 4 dimensions (variables) + constant |
|
-- add a point with 4 dimensions and observed value 12.5 |
|
-- add another point |
-- add more points to the fit |
|
|
-- Fit the basis function |
The final example below specifies a user-defined basis function named "Func" which fits data for which the dependent variable is a 3-channel RGB value. The fit involves 2 dimensions (independent variables) with 4 coefficients fit to each dimension. The RGB value argument must be passed as a string:
|
-- create a CLsqFit object |
|
-- setup the fit for 3 channel (RGB) data |
|
-- specify a basis function named F |
|
-- set 27 coefficients: 3 variables with 3 coefs each |
|
-- pass a table for 2 dimensions plus a table with RGB values |
-- add more points to the fit |
|
|
-- Fit the basis function |
For an example that adds pixel data directly from a displayed image, see Using CLsqFit with Image Pixels.
Using Multiple Independent Variables
Using CLsqFit with Image Pixels
Mira Pro x64 Script User's Guide, v.8.73 Copyright Ⓒ 2024
Mirametrics, Inc. All Rights Reserved.