Home > atmlab > math > polycoeffit.m

polycoeffit

PURPOSE ^

POLYCOEFFIT Polynomial fit for selected coefficients

SYNOPSIS ^

function [p,A] = polycoeffit(x,y,c,w)

DESCRIPTION ^

 POLYCOEFFIT   Polynomial fit for selected coefficients

    Similar to *polyfit* but has some extended functionality. First of all,
    you can here specify exactly what polynomial coefficients to involve in
    the fit (and not just n first as for *polyfit). This is done by the vector
    *c* where you list the coeffients to include. For example, if you want to
    fit a straight line to some data, but want to enforce that it to pass
    origo you set c=[1]. That is, you here enforce coefficient to be zero.

    The fit of y(x) is obtained as A*[p;s].

    The optional argument *w* allows to consider the uncertainty for each
    data point. The function *lscov* is then used, and *w* fits the third
    argument of that function. If the uncertainties are uncorrelated, *w*
    can be set to be a vector where the standard choice is to set the
    weights to 1/sigma^2 (the inverse variance). 

 FORMAT   [p,A] = polysinfit(x,y,c,[w])
        
 OUT   p    As for *polyfit*.
       A    Fitting matrix.
 IN    x    As for *polyfit*.
       y    As for *polyfit*.
       c    Polynomial coefficients to include, e.g. [1 2].
 OPT   w    Fitting weight for each data point in *y*. As passed on as 
            argument three of *lscov* (there called W or V).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

polycoeffit.m

SOURCE CODE ^

0001 % POLYCOEFFIT   Polynomial fit for selected coefficients
0002 %
0003 %    Similar to *polyfit* but has some extended functionality. First of all,
0004 %    you can here specify exactly what polynomial coefficients to involve in
0005 %    the fit (and not just n first as for *polyfit). This is done by the vector
0006 %    *c* where you list the coeffients to include. For example, if you want to
0007 %    fit a straight line to some data, but want to enforce that it to pass
0008 %    origo you set c=[1]. That is, you here enforce coefficient to be zero.
0009 %
0010 %    The fit of y(x) is obtained as A*[p;s].
0011 %
0012 %    The optional argument *w* allows to consider the uncertainty for each
0013 %    data point. The function *lscov* is then used, and *w* fits the third
0014 %    argument of that function. If the uncertainties are uncorrelated, *w*
0015 %    can be set to be a vector where the standard choice is to set the
0016 %    weights to 1/sigma^2 (the inverse variance).
0017 %
0018 % FORMAT   [p,A] = polysinfit(x,y,c,[w])
0019 %
0020 % OUT   p    As for *polyfit*.
0021 %       A    Fitting matrix.
0022 % IN    x    As for *polyfit*.
0023 %       y    As for *polyfit*.
0024 %       c    Polynomial coefficients to include, e.g. [1 2].
0025 % OPT   w    Fitting weight for each data point in *y*. As passed on as
0026 %            argument three of *lscov* (there called W or V).
0027 
0028 
0029 % 2009-11-19   Created by Patrick Eriksson.
0030 
0031 function [p,A] = polycoeffit(x,y,c,w)
0032   
0033 if ~isequal(size(x),size(y))
0034   error( 'The *x* and *y* vectors must have the same size.' );
0035 end
0036 
0037 x = x(:);
0038 y = y(:);
0039 
0040   
0041 A = zeros( length(y), length(c) );
0042 
0043 for i = 1 : length(c)
0044   if c(i) == 0
0045     A(:,i) = 1;
0046   elseif c(i) == 1
0047     A(:,i) = x;
0048   else
0049     A(:,i) = x.^c(i);
0050   end
0051 end
0052 
0053 if nargin == 3
0054   p = A \ y;
0055 else
0056   p = lscov( A, y, w );
0057 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005