orthofit
Fit orthogonal polynomials to data
Contents
Download
Syntax
ys = orthofit(x,y,n) [ys,yi] = orthofit(x,y,n,xi) yi = orthofit(x,y,n,xi) [ys,p] = orthofit(x,y,n)
Description
ys = orthofit(x,y,n) smooths/fits data y(x) in a least-squares sense using a polynomial of degree n and returns the smoothed data ys.
[ys,yi] = orthofit(x,y,n,xi) also returns the values yi of the fitting polynomial at the points of a different xi array.
yi = orthofit(x,y,n,xi) returns only the values yi of the fitting polynomial at the points of the xi array.
[ys,p] = orthofit(x,y,n) returns the polynomial coefficients p for use with polyval.
Notes: orthofit smooths/fits data using a polynomial of degree n developed in a sequence of orthogonal polynomials. orthofit is more appropriate than polyfit for polynomial fitting and smoothing since this method does not involve any matrix linear system but a simple recursive procedure. Degrees much higher than 30 could be used with orthogonal polynomials, whereas badly conditioned matrices may appear with a classical polynomial fitting of degree typically higher than 10.
To avoid using unnecessarily high degrees, you may let the function polydeg.m choose it for you. polydeg finds an optimal polynomial degree according to the Akaike's information criterion.
Examples
Let us create a noisy curve and find the polynomial of degree 25 that best fits the data in a least-squares sense.
x = linspace(0,10,1000); y = sin(x.^3/100).^2 + 0.05*randn(size(x)); [ys,p] = orthofit(x,y,25);
ys represents the smoothed data/fitting polynomial. Let us compare ys with the original data:
plot(x,y,'c.',x,ys)
The coefficients of the polynomial are given by p. Let us define xi by xi = [4 6]. The values of the fitting polynomial at xi can be given directly from orthofit:
xi = [4 6]; yi = orthofit(x,y,25,xi)
yi =
0.3547 0.6919
or by using polyval:
yi = polyval(p,xi) hold on plot(xi,yi,'o',xi,yi,'*')
yi =
0.3547 0.6919
Reference & Information
Méthodes de calcul numérique 2. JP Nougier. Hermes Science Publications, 2001. Section 4.7 pp 116-121
Orthogonal polynomials on Wikipedia.
See also
About the author
Damien Garcia, Eng., Ph.D. Assistant professor, Department of radiology CRCHUM, University of Montreal Hospital Montreal, QC, Canada Damien.Garcia.REMOVE-THIS@BiomeCardio.com