FitPoly


The FitPoly function fits a line to 2 tables containing x values and y values. This method creates a CLsqFit object, providing full access to all aspects of the fit.

Syntax

CLsqFit = FitPoly( x[], y[], nCoefs )

CLsqFit = FitPoly( x[], y[], w[], nCoefs )

where

    x[] is a lua table containing the x values.

    y[] is a lua table containing the y values (observations).

    w[] is an optional lua table containing the weight values.

    nCoefs is the number of coefficients, between 1 and 10.

    CLsqFit is a CLsqFit object containing the fit properties and results.

Example

This example fits x and y tables to y = a[1] + a[2]x + a[3]x^2 + a[4]x^3:

x = { 12.7, 14, 15, 17.6, 18.24 }

-- x data

y = { 24.1, 22.6, 25,4, 23.5, 27.6 }

-- y data

L = FitPoly( x, y, 4 )

-- Fit a polyniomial of 4 terms.

if L:GetStatus() == 0 then

-- if fit was not successful, then...

  n = L:GetErrChan()

-- get the error channel

  s = L:GetErrMsg(n)

-- get the error message

  Exit( Sprintf("Error[%d] %s\n",n,s) )

-- exit the script and list the error message

end

 

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

-- list the standard deviation of the fit

 

-- do other things with the fit

Related Topics

FitLine

CLsqFit class