new_lsqfit fitpoly2d

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.

Mathematics of the Fit

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

Syntax

CLsqFit = fitpoly1d( x, y, nCoefs, w )

CLsqFit = fitpoly1d( x, y, nCoefs )

where

bullet.gif    x is the array of x values, x[i].

bullet.gif    y is the array of y values, y[i] (the dependent variable).

bullet.gif    nCoefs is the number of coefficients to fit.

bullet.gif    w is an optional array of point weights.

bullet.gif    On success, a CLsqFit object is returned.

bullet.gif    On failure, nil is returned.

Example

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.

x = {}

-- create a table for x values and fill it

y = {}

-- create a table for y values and fill it

-- fill the x and y tables

 

L = fitpoly1d( x, y, 4 )

-- fit 4 coefficients and return the CLsqFit object

if L == nil then Exit("Fit failed\n") end

-- check err == 1 for a successful fit

if L:GetRetCode() ~= 1 then

-- return code not 1 means an error occurred.

  Printf("Fit Error '%s'\n", L:GetErrMsg())

 

end

 

Printf( "Sdev=%lg", L:GetSigmaFit() )

-- list the standard deviation of the fit

 

-- do other things with the fit

Related Topics

CLsqFit class

fitpoly2d function

Array Functions

 

 


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