Code covered by the BSD License  

Highlights from
pregress.m

from pregress.m by Elizabeth
A Matlab function pregress.m to carry out linear regression for an arbitrary order n polynomial.

pregress( x,y,m );
function [ A ] = pregress( x,y,m );
%PREGRESS Constructs the coefficient list C of the least-squares polynomial of degree N
%         by performing a linear regression on the data points (x1,F1),..,( xN,FN) 

% Inputs
% x       an arbitrary list of abscissa values
% y       an arbitrary list of ordinate values
% m       the degree of the least-squares polynomial
% Outputs
% A       the coefficient list generated for the least-squares polynomial
%
% Note: 
% The least-squares polynomial of deg m is defined as:
%
%              Pm(x)= a1+ a2*x+a3*x^2+...+am*x^m-1 + am+1*x^m
%                           for the N data points {(xi,yi)}
%%
n=length(x); % # of data points
fx_y=zeros(1:m+1); % B is a coefficient
fx=zeros(n,m+1); % matrix F holds the powers of x
for k=1:m+1;
    % F contains m linear independent functions fj(x) s.t. the linear combination
    % of all fj(x) is f(x)=sum(cjfj(x)), where cj is the coeff of the jth
    % term in the least-squares polynomial fit
    % F must therefore satisfy: 
    % F = [f1(x1),f2(x1),...,fM(x1); f1(x2),....,fM(x2)]
     fx(:,k)=x'.^(k-1);
end
% Compute F'F
prodfx=fx'*fx; 
% Compute B=F'y'
fx_y=fx'*y;
% F'FC=F'y'
A=prodfx\fx_y;
A= A(end:-1:1);

    


Contact us