Home > matutils > linfit.m

linfit

PURPOSE ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SYNOPSIS ^

function [m,b, varM, varB]=linfit(x,y)

DESCRIPTION ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 [m, b, varM, varB] =linfit(x,y)

 x is a length m column vector of e.g. time
 y is a m*n array where each column is a set of observations at x
 b is 1*n vector of constants
 m is 1*n vector of slopes
 least squares linear fits of x versus each column of y

  sjcm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [m,b, varM, varB]=linfit(x,y)
0002 
0003 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0004 %
0005 % [m, b, varM, varB] =linfit(x,y)
0006 %
0007 % x is a length m column vector of e.g. time
0008 % y is a m*n array where each column is a set of observations at x
0009 % b is 1*n vector of constants
0010 % m is 1*n vector of slopes
0011 % least squares linear fits of x versus each column of y
0012 %
0013 %  sjcm
0014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0015 
0016 x = repmat(x,[1,size(y,2)]);
0017 
0018 Sx  = nansum(x,1);
0019 Sxx = nansum(x.^2,1);
0020 S   = size(x,1)*ones(size(Sx));
0021 
0022 Sy  = nansum(y,1);
0023 Sxy = nansum(x.*y,1);
0024 
0025 Delta = S.*Sxx-Sx.^2;
0026 b = (Sxx.*Sy-Sx.*Sxy)./Delta;
0027 m = (S.*Sxy-Sx.*Sy)./Delta;
0028 
0029 varB = Sxx./Delta;
0030 varM = S./Delta;
0031 return
0032 
0033 
0034 
0035 
0036

Generated on Sun 14-Jun-2015 17:12:45 by m2html © 2005