% BRIEF RECAP of October 22th, 1998 NEEP271 lecture
% written by Fabian Waleffe 98/10/22
%
% Demo of some generalized linear curve fit (see oct 20)

% First let's generate some data to play with:

t=[0:0.02:1]'*2*pi;

y=5*sin(t) + 3*sin(2*t) + sin(3*t);

plot(t,y,'-o')

% given the data points t and y can we fit them to a function:
% y(t) = a sin(t) + b sin(2 t) + c sin(3 t) ?
%
% First we write this in "A x =b" form:

A=[sin(t)  sin(2*t)  sin(3*t)];

% the coefficients a,b,c are obtained using Matlab's "A\y"

A\y

% If we switch the columns around:

A=[sin(3*t)  sin(t)  sin(2*t)];

% then we switch a,b,c in the same way

A\y

% We can do polynomial fits this way: 
% to fit the (t,y) data to y(t)= a t^2 + b t + c

A=[t.^2 t ones(length(t),1)];
A\y

% (NOTE the use of the function "ones", see "help ones")

% This is the same as doing the fit using polyfit:

p=polyfit(t,y,2)

plot(t,y,'o',t,polyval(p,t),t,A*(A\y),'--')

% NOTE that A*(A\y) is not equal to y! Do you understand what A*(A\y) is ?

