% Chapter 5 Matrices 

format compact

%The norm function returns the magnitude of a vector. This function can be
%employed to check if two matrices are equal by finding the norm of the 
%difference of the two matrices (ex. norm(a - b)) if the answer is 0 the
%two matrices are equal.

%Learn about the linspace function by typing. Please do not turn in the
%output.
help linspace

%  *************************************
%1 [5.2] Use colon notation to create vectors identical to those obtained
%  with the statements that follow. Use multiple statements where necessary.
%  Use Matlab's built-in norm function to test whether the two vectors
%  are equal without printing the elements.
%  *************************************
%  - Do not forget to include the semicolon at the end of each line to
%    suppress the output (except the norm function).
%  - note: x = linspace(a, b, n) is equivalent to x1=a:s:b where s = |b-a|/(n-1)
%          This conversion process will not work if i is irrational.
x = linspace(0, 10, 5);
y = linspace(-5,5);

%  *************************************
%2 [5.2] Use the linspace function to create vectors identical to those
%  obtained with the statements that follow. Use multiple statements where
%  necessary. Again use Matlab's built-in norm function to test whether the
%  two vectors are equal without printing the elements.
%  *************************************
%  - Do not forget to include the semicolon at the end of each line to
%    suppress the output (except the norm function).
%  - note: a:b:n is equivalent to linspace(a,b,n) where n = |(b - a)/s|+1
w1 = 0:10;
x1 = 0:0.2:10;
y1 = -12:12;
z1 = 10:-1:1;

%  *************************************
%3 [5.1t] Use the logspace function to write a one-line expression that
%  creates the vector x = [250, 2500, 25000, 250000]
%  *************************************

%Learn about flip functions by typing. Please do not turn in the output.
lookfor flip

%  *************************************
%4 [5.6t] Enter the Matlab statement to generate matrix A and then obtain 
%  the matrix B from A. Do not enter the B matrix manually.
%  *************************************
%  B =
%     7     8     9
%     4     5     6
%     1     2     3

A = reshape(1:9,3,3)'

%  *************************************
%5 [5.1, 5.4] Using the diag and ones functions create the symmetric
%  n x n, tridiagonal matrix
%  *************************************
%  D =
%     2    -1     0     0
%    -1     2    -1     0
%     0    -1     2    -1
%     0     0    -1     2
 
%  *************************************
%6 [5.1, 5.4] Use the eye and fliplr functions to create the matrix
%  *************************************
% E =
%    0     0     1
%    0     1     0
%    1     0     0

%  *************************************
%7 [5.1, 5.4] Write the one-line expression to create the following matrix
%  *************************************
% F = 
%    1     0     0     0     1
%   -1     1     0     0     1
%   -1    -1     1     0     1
%   -1    -1    -1     1     1
%   -1    -1    -1    -1     1
 
%  *************************************
%8 [5.4] Use the reshape function and colon notation to create the
%  following matrices
%  *************************************
%  G =
%    2     8    14    20
%    4    10    16    22
%    6    12    18    24
%
%  H =
%   -5    -3    -1     1     3     5
%   -4    -2     0     2     4     6
  
%  *************************************
%9 [5.2] Consider the following sequence of statements.
%(a) Why is the first evaluation of A*B allowable, but not the second
% evaluation? (b) Which statement created the problem? (c) Assuming that
% this error was caused by a typo, suggest a correction.
%  *************************************
A = ones(3,2); B = 2*ones(2,3); A*B;
A(2,3) = 2;
A*B
% ??? Error using ==> mtimes
% Inner matrix dimensions must agree.
 
%  *************************************
%10 [5.3] Consider the following sequence of commands. (a) Explain the 
%   error. Do not repeat the error message (b) Assuming that this error 
%   was caused by a misunderstanding of elementary matrix and array 
%   operations, suggest a correction if the author intended to generate 
%   the row vector [0 2 2 0].
%  *************************************
u = 0:3; v = (3:-1:0)';
w = u.*v
%   ??? Error using ==> times
%   Matrix dimensions must agree.
