Dew MtxVec NET
PolyFit Routines
Summary
Fits a polynomial to data.

Unit
Polynoms

Declaration
Procedure PolyFit(XValues, YValues: TVec; Degree: Integer; Coeff: TVec; R: TMtx; out DegFreedom: Integer; out L2R: TSample; Weights: TVec = nil);

Description
The PolyFit procedure uses the least-squares method to find the fitted polynomial coefficients. X axis points are defined with XValues and Y axis points with YValues. XValues.Length must be equal to YValues.Length. The Degree parameter defines the degree of fitted polynomial. The coefficients of fitted polynomial are returned in the Coeff vector. The R matrix returns the Cholesky factor of the Vandermonde matrix. The DegFreedom parameter returns the degree of freedom. The L2R parameter returns the L2 norm of the residuals. Additional Weights can be specified with the Weights parameter.

Example 1

uses MtxExpr, Math387, MtxVec, MtxVecTee, Polynoms; procedure TForm1.Button1Click(Sender: TObject); var X,Y,YCalc,Delta,Coeff: Vector; R: Matrix; DegF: Integer; L2R : TSample; begin X.Size(100,false); Y.Size(X); X.Ramp; Y.RandGauss(2,3); PolyFit(X,Y,3,Coeff,R,DegF,L2R); //Fit PolyEval(X,Coeff,R,DegF,L2R,YCalc,Delta); // evaluate DrawIt([Y,YCalc],['Original','Polyfit']); end;
#include "MtxVecCPP.h" //MtxVecCPP.cpp must be included in the project #include "Polynoms.hpp" #include "MtxVecTee.hpp" void __fastcall TForm1::BitBtn1Click(TObject *Sender) { Vector X,Y,YCalc,Delta,Coeff; Matrix R; int DegF; double L2R; X = Ramp(100); Y->Size(100); Y->RandGauss(2,3); PolyFit(X,Y,3,Coeff,R,DegF,L2R); //Fit PolyEval(X,Coeff,R,DegF,L2R,YCalc,Delta); // evaluate DrawIt(OPENARRAY(TVec*,(Y,YCalc)),OPENARRAY(AnsiString,("Original","Polyfit"))); }
using Dew.Math; using Dew.Math.Units; using Dew.Math.Tee; namespace Dew.Examples { private void Example() { Vector X = new Vector(0); Vector Y = new Vector(0); Vector YCalc = new Vector(0); Vector Delta = new Vector(0); Vector Coeff = new Vector(0); Matrix R = new Matrix(0,0); int degF; double L2R; X.Ramp(100); y.Size(X); Y.RandomGauss(2.0,3.0); Polynoms.PolyFit(X,Y,3,Coeff,R, out degF, out L2R, null); //Fit Polynoms.PolyEval(X,Coeff,degF,L2R,YCalc,Delta); // Evaluate TeeChart.DrawIt(new Vector[] {Y,YCalc}, new string[] {"Original","Fit"},"Fit results",false); } }


Declaration
Procedure PolyFit(XValues, YValues: TVec; Degree: Integer; Coeff: TVec; Weights: TVec = nil);

Description
Fits a polynomial to data.

Copyright 2008 Dew Research
http://www.dewresearch.com