polyfitwdocumentation

Thepolyfitw函数tion computes weighted polyfits.

Back to Climate Data Tools Contents. polyfitw computes weighted polyfits.

Contents

Syntax

p = polyfitw(x,y,n) p = polyfitw(x,y,n,w) [p,S,mu] = polyfitw(...)

Description

p = polyfitw(x,y,n)calculates theunweightedpolynomial fit ofxvsyto the nth order, exactly like the standard Matlabpolyfit函数tion.

p = polyfitw(x,y,n,w)specifies weights to apply to eachyvalue. For measurements whose formal error estimates are given by err, try using weightsw = 1/err.^2.

[p,S,mu] = polyfitw(...)returns theSstructure and centering/scaling valuesmufor use in polyval. Don't forget, if you return more than one output (meaning[p,S,mu]instead of justp), the values inpwill be scaled according to the values inmu.

Example: 1st order:

Here's some scattered data with a known slope of -12, and some prescribed errors associated with each measurement:

x = [1 1.5 2.1 3 4 6 6.6 7.3 7.5 8 8.6 9 9.5]; err = [1 2 -1 3 6 1 3 -7 4 15 30 25 1]; y = 654 - 12*x + err;% Weights from errors:w = 1. /犯错。^ 2;图散射(x, y, 50 w,'filled') holdonaxistightcb = colorbar; ylabel(cb,'weight') cmoceanamp

Here's how to use the standard Matlab functionpolyfitto find the unweighted slope of the line

p = polyfit(x,y,1)
p = -10.35 650.99

That tells us theunweightedslope is -10.35, although we imposed a slope of -12. The difference is due to the error in the measurements. Notably, you can use the CDT趋势函数tion to get the same answer:

趋势(y,x)
ans = -10.35

The mismatch between the -10.35 slope value and the imposed -12 slope is due to measurement error. Fortunately, we know how much weight to give to each measurement, and we can weight accordingly withpolyfitw:

pw = polyfitw(x,y,1,w)
pw = -11.90 654.34

Now -11.9 is not exactly the -12 slope we imposed, but this is much closer than the estimate from the unweighted trend. Here's the difference:

xi = 1:10; holdonplot(xi,polyval(p,xi)) plot(xi,polyval(pw,xi)) legend('data points','unweighted','weighted')

Author Info

This function is part of theClimate Data Toolbox for Matlab. The function and supporting documentation were written by Chad A. Greene of NASA Jet Propulsion Laboratory.