Code covered by the BSD License  

Highlights from
Numerical Methods Using MATLAB, 3e

image thumbnail

Numerical Methods Using MATLAB, 3e

by

 

20 Aug 2002 (Updated )

Companion Software

lspoly(X,Y,M)
function C = lspoly(X,Y,M)

%Input   - X is the 1xn abscissa vector
%        - Y is the 1xn ordinate vector
%        - M is the degree of the least-squares polynomial
% Output - C is the coefficient list for the polynomial

% NUMERICAL METHODS: MATLAB Programs
%(c) 1999 by John H. Mathews and Kurtis D. Fink
%To accompany the textbook:
%NUMERICAL METHODS Using MATLAB,
%by John H. Mathews and Kurtis D. Fink
%ISBN 0-13-270042-5, (c) 1999
%PRENTICE HALL, INC.
%Upper Saddle River, NJ 07458

n=length(X);
B=zeros(1:M+1);
F=zeros(n,M+1);

%Fill the columns of F with the powers of X

for k=1:M+1
   F(:,k)=X'.^(k-1);
end

%Solve the linear system from (25)

A=F'*F;
B=F'*Y';
C=A\B;
C=flipud(C);

Contact us