%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    this is a little matlab script to introduce you to some of the syntax of matlab
%    and how you do basic vector and matrix vector operations.  Have fun. Marc
%
%    To run this script...first start Matlab then either just cut or paste in the lines
%    or just type the name of the script at the prompt.  
%
%       For more help and a matlab tutorial, type helpdesk at the prompt and go to "
%       "Getting Started" under the MATLAB DOCUMENTATION
%  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
 
%--------------------------------------------------------------
%  Fun with matrices:

% consider the 3x3 matrix given in class

A=[  2   1   3 ;
     4   3   8 ;
    -2   0   3 ]

%
%  and the column vector
%

x=[ 1 -2 1 ]';


%
% calculate b=A*x
%

      disp([' b=A*x']);

      b=A*x
      
%
% let v=the third column of A
%

disp([ ' the third column of A can be extracted using v=A(:,3)']);
v=A(:,3)

 

%%%%%%%%%%%%%%%%%%%%%%%b
%   Here's a bit of plotting fun
%   look at the  homemade matlab scripts plotvector2d.m
%   to see how I used other matlab calls to make a simple function that plots 
%  a single 2-D  vector and colors it
%%%%%%%%%%%%%%%%%%%%%%%

%
%  first let's demonstrate vector addition
%

a_1=[ 1 1 ]';
a_2=[ 1 -1 ]';
origin= zeros(2,1); % this is a bit of matlabese for writing zero
                   % matrices or vectors 
figure
plotvector2d(origin,a_1); % this plots an arrow vector a_1 starting at point origin
hold on % this allows us to add things to the graph
plotvector2d(a_1,a_2); % plot the second vector starting at the end of a_1
plotvector2d(origin,a_1+a_2,'r'); %plot the sum of a_1 and a_2 starting at the origin and  paint it red
axis([-3 3 -3 3]);  % set the domain to be useful
xlabel('x'); ylabel('y');  % pretty up the plot
grid; 

%
%  okay now let's do that again in 3-D
% 

b_1=[ 1 2 3 ]';
b_2=[ 1 -1 2 ]';
origin3d= zeros(3,1); % this is a bit of matlabese for writing zero
                   % matrices or vectors 
figure
plotvector3d(origin3d,b_1); % this plots an arrow vector a_1 starting at point origin
hold on % this allows us to add things to the graph
plotvector3d(b_1,b_2); % plot the second vector starting at the end of a_1
plotvector3d(origin3d,b_1+b_2,'r'); %plot the sum of a_1 and a_2 starting at the origin and  paint it red
xlabel('x'); ylabel('y'); zlabel('z')  % pretty up the plot


%%%%%%%%%%%%%%%%
%  back to 2-D to demonstrate that matrix vector nultiplication  is identical
%   to linear combinations of  column vectos
%%%%%%%%%%%%%%%%%%%%%%%%%%%

%
% problem 1) lets let the vector b=x*a_1+y*a_2 where
%

x=5;
y=-2;

%  are a pair of scalars (play around with these for fun)

b=x*a_1+y*a_2 ; % a linear combination of a_1 and a_2

%
% now plot a_1, a_2 in blue and b in red
%
figure
plotvector2d(origin,a_1); % this plots an arrow vector a_1 starting at point origin
hold on 
plotvector2d(origin,a_2); % plot the second vector starting at the origin
plotvector2d(origin,b,'r'); %plot the linear combination of  a_1 and a_2 starting at the origin and  paint it red
xlabel('x'); ylabel('y');  % pretty up the plot
grid; 
title(sprintf('linear combination %g a_1 + %g a_2',x,y));
 
%
% do the same thing with matrices etc
%

A= [ a_1 a_2 ]  % make a matrix out of the columns a_1 and a_2

v= [ x y ]'  % make a vector out of the scalars x and y

b=A*v   % do matrix vector multiplication

%
% and plot this out (note the use of matrix notation)
%
figure
plotvector2d(origin,A(:,1));
hold on
plotvector2d(origin,A(:,2));
plotvector2d(origin,A*v,'r');
xlabel('x'); ylabel('y'); grid;
title('Matrix vector multiplication Ax=b');


%
%  your turn...test and plot these ideas for several 3-D vectors
%