fitpoly1d
The fitpoly1d function computes a least squares fit to (x,y) data where y=f(x). This function provides a simple way to perform a quick least squares polynomial fit without first setting up a CLsqFit class object. The CLsqFit object is created by this function and returned after a successful fit.
The x values represent the independent variable while the y values represents the dependent variable. The elements x[i], y[i], and w[i] correspond to the i-th point. The data are passed as 1-dimensional arrays (lua tables) x, y, and w, where w is the optional array of weights. If w is not passed, all weights are defined as 1.0. The number of coefficients being fit must be between 1 and 10.
A successful fit returns the CLsqFit object L that is internally created to compute the fit. All fit results including coefficients, covariance, sigma, and others, may be obtained using CLsqFit class methods. The function f(x) may be evaluated to predict a y value at any point xo using CLsqFit:Eval.
The fit is made about the mean values of the x and y arrays. Let each sample point i be x[i] and y[i]. Representing the mean values of x by xm, the function f(x) is fit to the sample points as follows:
y[i] = f( x[i] - xm )
Note that the mean values are computed internally and need not be known outside the CLsqFit object, for example, to evaluate the fit..
CLsqFit = fitpoly1d( x, y, nCoefs, w ) CLsqFit = fitpoly1d( x, y, nCoefs ) where x is the array of x values, x[i]. y is the array of y values, y[i] (the dependent variable). nCoefs is the number of coefficients to fit. w is an optional array of point weights. On success, a CLsqFit object is returned. On failure, nil is returned. |
This example computes a polynomial fit with 4 coefficients. In this example, all points are assumed to have equal weight, so the weight array w is not passed.
|
-- create a table for x values and fill it |
|
-- create a table for y values and fill it |
-- fill the x and y tables |
|
|
-- fit 4 coefficients and return the CLsqFit object |
|
-- check err == 1 for a successful fit |
|
-- return code not 1 means an error occurred. |
|
|
|
|
|
-- list the standard deviation of the fit |
|
-- do other things with the fit |
Mira Pro x64 Script User's Guide, v.8.73 Copyright Ⓒ 2024
Mirametrics, Inc. All Rights Reserved.