fitpoly1d CLsqFit:AddPtWt

fitpoly2d


The fitpoly2d function computes a two-dimensional least squares fit to (x,y,z) data where z=f(x,y). 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,y) pairs represent the independent variables while the z value represents the dependent variable. An array of point weights, w[i] is optional. The array elements x[i], y[i], z[i], and optional w[i] correspond to the i-th point. These data are passed as 1-dimensional arrays (lua tables) x, y, z, and w, where w is the optional array of point 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 in each axis (up to 100 coefficients in total).

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,y) may be evaluated to predict a z value at any point (x0,y0) 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], y[i],z[i]) and the optional point weight by w[i]. Representing the mean value of x by xm and the mean value of y by ym, the function f(x,y) is fit to the sample points as follows:

  z[i] = f( x[i] - xm, y[i] - ym )

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 = fitpoly2d( x, y, nCoefsX, nCoefsY, w )

CLsqFit = fitpoly2d( x, y, nCoefsX, nCoefsY )

where

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

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

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

bullet.gif    nCoefsX is the number of coefficients to fit along the x axis.

bullet.gif    nCoefsY is the number of coefficients to fit along the y axis.

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 2 coefficients in the x direction and 3 coefficients in the y direction. 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

z = {}

-- create a table for z values and fill it

-- fill the x, y, and z tables

 

L = fitpoly2d( x, y, z, 2, 3 )

-- fit 2x3 coefs 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

fitpoly1d function

Array Functions

 

 

 


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