%  Demo of decomposition into random basis


x = [2 1 .4 0 4 -.45 -.6 -.65 -.6 -.45 4 0 .4 1 2];
    

figure(1)
plot(x,'o') 
axis([0 16 -1 5])

randn('seed',3);

A = randn(length(x),length(x));

a = x/A;

% Sort the components from strongest to weakest for better visual effect as
% they are combined.
[temp,order] = sort(abs(a),'descend');

A = A(order,:);
a = a(order);


figure(3)
plot(A')
title('Basis Vectors')


for i=1:length(a)
    
waitforbuttonpress
   
figure(2)
subplot(1,2,1)
plot(a(i)*A(i,:),'o')
hold on
plot(a(i)*A(i,:),'color', [0.5 0.5 0.5])
hold off
title('Component')
axis([0 16 -1 5])
subplot(1,2,2)
plot(a(1:i-1)*A(1:i-1,:),'go')
hold on
plot(a(1:i)*A(1:i,:),'o')
hold off
title('Combination')
axis([0 16 -1 5])

figure(3)
plot(A')
title('Basis Vectors')
hold on
plot(A(i,:),'LineWidth',10)
hold off

end