Code covered by the BSD License  

Highlights from
polyfit3

4.0

4.0 | 3 ratings Rate this file 22 Downloads (last 30 days) File Size: 2.69 KB File ID: #4262

polyfit3

by

 

15 Dec 2003 (Updated )

Fit polynomial to data, with new features as forced to zero coefficients and data weighting.

| Watch this File

File Information
Description

POLYFIT3(X,Y,N,NUL,W) finds the coefficients of a polynomial P(X) of degree N that fits the data, P(X(I))~=Y(I), in a least-squares sense. Any of the coefficients can be forced to be zero, and data can be weighted.

NUL is a vector with coefficients forced to be zero in usual order.

W is a vector containing the weights of data.

POLYFIT3(X,Y,N,NUL) assumes that all weights of data are equal to 1.
POLYFIT3(X,Y,N,[],W) allows to weight data without forcing any coefficient to be zero.

The original usage is still available.

Tested under MATLAB 6.0.0.

DEMO (for not weighted data).
x=linspace(0,11,12);
noise=(rand(size(x))-0.5);
y=3-x.^3+2*x.^5+noise;
[p1,S] = polyfit3(x,y,5)
w=ones(size(x));
[p2,S] = polyfit3(x,y,5,[2 4 5],w)
close all;
figure;
plot(x,y,'.r'); hold on;
x=linspace(0,11);
plot(x,polyval(p1,x),'b');
plot(x,polyval(p2,x),'k');

DEMO (For weighted data).
x=linspace(-5,5,11);
noise=(rand(size(x))-0.5);
y=x.^4+noise; y(11)=y(11)+100;
[p1,S] = polyfit3(x,y,4)
w=ones(size(x)); w(11)=0.1;
[p2,S] = polyfit3(x,y,4,[2 3 4 5],w)
close all;
figure;
plot(x,y,'.r'); hold on;
x=linspace(-5,5,11);
plot(x,polyval(p1,x),'b');
plot(x,polyval(p2,x),'k');

MATLAB release MATLAB 6.0 (R12)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (4)
31 Oct 2011 Andrey Kan

thank you for a useful implementation.

memory usage of this function can be reduced by replacing lines:
W=diag(w);
WV=W*V;
Wy=W*y;
WVfull=W*Vfull;

with the following code:
W = repmat(w, 1, size(V, 2));
WV=W.*V;
Wy=w.*y;
W = repmat(w, 1, size(Vfull, 2));
WVfull=W.*Vfull;

21 May 2011 Jan

just what I was looking for

05 Jan 2011 Eugene  
12 Jul 2004 S F

A very useful function !

But I have found a problem that needs to be fixed ! when used with "polyval" the function returns in some cases a wrong delta (error estimation) ! :-(

Updates
16 Dec 2010

Error fixed in the calculation of S.normr and S.R. Now p and structure S created by POLYFIT3 can be used by polyval and prediction error estimates DELTA is correctly obtained.

Contact us