MATLAB TUTORIAL for the Second Cource. Part 1: Matrix Algebra

Prof. Vladimir A. Dobrushkin

This tutorial contains many Matlab scripts.
You, as the user, are free to use all codes for your needs, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately. Any comments and/or contributions for this tutorial are welcome; you can send your remarks to

Email Vladimir Dobrushkin

How to define vectors

A vector is a quantity that has magnitude and direction and that is commonly represented by a directed line segment whose length represents the magnitude and whose orientation in space represents the direction. In mathematics, it is always assumed that vectors can be added or subtracted, and multiplied by a scalar (real or complex numbers). It is also assumed that there exists a unique zero vector (of zero magnitude and no direction), which can be added/subtructed from any vector without changing the outcome. The zero vector is not zero. Wind, for example, had both a speed and a direction and, hence, is conveniently expressed as a vector. The same can be said of moving objects, momentum, forces, electromagnetic fields, and weight. (Weight is the force produced by the acceleration of gravity acting on a mass.)

The first thing we need to know is how to define a vector so it will clear to everyone. One of the common ways to do this is to introduce a system of coordinates, either Cartesian or any other, which includes unit vectors in each direction, usually referred to as an ordered basis. In engineering, traditionally these unit vectors are denoted by i (abscissa), j (ordinate), and k (applicat), called the basis. Once rectangular coordinates (or a basis) are set up, any vector can be expanded through these unit vectors. In three dimensional case, every vector can be expanded as \( {\bf v} = v_1 {\bf i} + v_2 {\bf j} + v_3 {\bf k} ,\) where \( v_1, v_2 , v_3 \) are called the coordinates of the vector v. Coordinates are always specified relative to an ordered basis. Therefore, every vector can be identified with an ordered n-tupple of n real numbers or coordinates. Coordinate vectors of finite-dimensional vector spaces can be represented by either a column vector (which is usually the case) or a row vector, Generally speaking, Matlab distinguishes column vectors from row vectors. The user should specify which one is defined.

I. Basic properties

Start up Matlab, and for now just type in the Command Window. The variables we define show up on the Workplace on the right of your screen. If you omit the ; at the end of the lines you type they also show up in the Command Window. Matlab, like the typical programming language, uses variables. As its name shows, its staple diet is matrices. So the first thing to learn is how to define matrices (and their subset, vectors) in Matlab. Let’s define the simple matrix---called the row-vector:

V=[1 2 3 4 5 6 7 8 9 10]

Is there an easier way to define such a long vector? Indeed, there is. Try this:

			V=[1:1:10]

In this case [a:b:c] defines a vector from a to c with steps of size b. What does [0:5:200] define then? To define a column-vector, Matlab offers some options. One can define a row-vector
	 b=[5,6,7,8]

>> b=
         5 6 7 8
and then take transposition
	b.'  % or type directly: [5,6,7,8].'
disp(b)
disp(b')

You can drop the period if your vector has only real valued entries. If the vector contains complex entries, then the command b' provides complex conjugate column-vector. There is a dedicated command for transposition:
	transpose(b)  % or type directly: transpose([5,6,7,8])

When defining a whole matrix, vectors can be combined: try for example

			[1:2:9; 2:2:10] 

A useful functonality in Matlab is the easy definition of some special matrices. A zero matrix of size m x n can be defined by typing zeros(m,n):

			zeros(5,2)

An identity matrix of size m can defined by typing eye(m) . The following command generate a \( 4 \times 4 \) identity matrix:

			eye(4)

Matrices can easily be put together, horizontally and vertically:

	 [eye(4) zeros(4)]

	[zeros(3); 3*eye(3)]
You can make your life easier by knowing a few simple tricks that can be applied when working with matrices. Elements of matrices in Matlab can be referred to very easily: matrix(x,y) refers to the element in row x column y. matrix(z) refers to the element in position z, when counting is done columns first. So for example in the matrix
	 A=[1 2; 3 4]
typing the command
	 A(2)
refers to 3. When you want to refer to a whole row of a matrix, say to the second row of the matrix
	twenty=[1:10;11:20]
you can do so by typing
	first_row=twenty(1,:)
which means, choose row 1 and all columns. Equivalently,
	third_column=twenty(:,3)

chooses the third column. Another interesting thing that you might want to make use of, particularly once you know how to define functions, which we will discuss shortly is user input: M=input('What is your favorite matrix?') prompts the user to input a matrix and define M.

Example. Write the three-dimensional vecor \( {\bf a}=2{\bf i}+3{\bf j}-4{\bf k} \) as the sum of two vectors, one parallel, and one perpendicular to \( {\bf b}=2{\bf i}-{\bf j}-3{\bf k} .\) Here i, j, and k are unit vectors along abscissa, ordinate, and applicate, respectively.

	a=[2 3 -4]
a=
     2 3 -4
	b=[2 -1 -3]
b=
      2 -1 3
parallel=
(((a(1)*b(1))+(a(2)*b(2))+(a(3)*b(3)))/(((b(1)).^2)+((b(2)).^2)+((b(3)).^2)))*b
parallel=
      1.8571 -0.9286 -2.7857
perpendicular= a-parallel

perpendicular =
       0.1429   3.9286   -1.2143

You can find the parallel vector directly, iusing dot product:

p=dot((dot (a,b)\abs(b).^2),b)

Example. Let a= [1,3,-4] and b= [-1,1,-2] find the dot product of the vector a with the cross product of a and b: \( a \cdot (a \times b) . \)

a=[1 3 -4]; b=[-1,1,-2]; 
dot(a,cross(a,b))
Since the answer is zero, this answer holds for arbitrary two vectors.

 

Matrix algebra

Matlab can carry out a wide array (no pun intended) of operations on the matrices that you define. Try addition and multiplication for starters. Observe what happens:

A = [1 2; 3 4]
B = [5 6; 7 8]
AplusB = A + B
AtimesB = A * B
AtimesB is A times B, which is the result of standard matrix multiplication from linear algebra. However, often one is interested in the element by element multiplication of two matrices. The way to do this in Matlab is using the .* operator, where the dot indicated element by element calculation. Try it, and compare the results:
			AtimeseeB = A .* B
Other operations that we have discussed in class include calculating the transpose, the complex conjugate and the adjoint of a matrix. The transpose of a real-valued matrix
			C=[1 2; 3 4]
is simply
			C'
while for a matrix
		D=[1+i 2; 3 4]
that also contains some complex entries
			D'
calculates the complex conjugate. To calculate the transpose of such a matrix, one has to use the familiar .:
			D.'
Determinants and Inverses

Matlab can also easily calculate things like the trace(C)
		trace(C)
and the determinant
			det(C)
Writing inv(A) or A^(-1) calculate the inverse:
			inv(A)
			A^(-1)
Combining these operations it is easy to find the value of the resolvent for any particular lambda. Let us refer back to matrix E, again. Then the resolvent for the matrix E is simply
			resolvent=inv(lambda*eye(2)-E)
Eigenvalues and Eigenvectors
Matlab can also calculate the eigenvalues and eigenvectors. Start out with finding the eigenvalues:
			eigenvalues=eig(E)
If you need to see eigenvalues along with eigenvectors, type:
E= [7 4; -10 -5]
[V,D]=eig(E)

where in the output, matrix V corresponds to the matrix of eigenvectors and matrix D is a diagonal matrix where each entry is an eigenvalue.

The coefficients of the characteristic polynomial are returned by

			poly(E)
Linear Systems of Algebraic Equations
The system of algebraic equations
\[ {\bf A} \, {\bf x} = {\bf b} , \]
where A is a given matrix and b is specified column vector, may have a unique solution x, may have infinite many solutions, or may gave no solution at all. Another convenient way, and one that is computationally different is to type A\b, which is roughly the same as inv(A)*b. So if one wants to solve the system of equations Ax=b, it is enough to type
			x=A\b
Diagonalization

We show how to define a function of a square matrix using diagonalization procedure. This method is applicable only for such matrices, and is not suatable for defective matrices. Recall that a matrix A is called diagonalizable if there exists a nonsingular matrix S such that \( {\bf S}^{-1} {\bf A} {\bf S} = {\bf D} , \) a diagonal matrix. In other words, the matrix A is similar to a diagonal matrix. An \( n \times n \) square matrix is diagonalizable if and only if there exist n linearly independent eigenvectors, so geometrical multiplicities of each eigenvalue are the same as its algebraic multiplicities. Then the matrix S can be built from eigenvectors of A, column by column.

Let A be a square \( n \times n \) diagonalizable matrix, and let D be the corresponding diagonal matrix of its eigenvalues:

\[ {\bf D} = \begin{bmatrix} \lambda_1 & 0 & 0 & \cdots & 0 \\ 0&\lambda_2 & 0& \cdots & 0 \\ \vdots & \vdots & \vdots & \cdots & \vdots \\ 0&0&0& \cdots & \lambda_n \end{bmatrix} , \]

where \( \lambda_1 , \lambda_2 , \ldots , \lambda_n \) are eigenvalues (that may be equal) of the matrix A.

Let \( {\bf x}_1 , {\bf x}_2 , \ldots , {\bf x}_n \) be linearly independent eigenvectors, corresponding to the eigenvalues \( \lambda_1 , \lambda_2 , \ldots , \lambda_n .\) We build the nonsingular matrix S from these eigenvectors (every column is an eigenvector):

\[ {\bf S} = \begin{bmatrix} {\bf x}_1 & {\bf x}_2 & {\bf x}_3 & \cdots & {\bf x}_n \end{bmatrix} . \]
For any reasonable (we do not specify this word, it is sufficient to be smooth) function defined on the spectrum (set of all eigenvalues) of the diagonalizable matrix A, we define the function of this matrix by the formula:
\[ f \left( {\bf A} \right) = {\bf S} f\left( {\bf D} \right) {\bf S}^{-1} , \qquad \mbox{where } \quad f\left( {\bf D} \right) = \begin{bmatrix} f(\lambda_1 ) & 0 & 0 & \cdots & 0 \\ 0 & f(\lambda_2 ) & 0 & \cdots & 0 \\ \vdots & \vdots & \vdots & \cdots & \vdots \\ 0&0&0& \cdots & f(\lambda_n ) \end{bmatrix} . \]

 

Positive Matrices and Square Roots
Sylvester Formula

In this section, we write a function sylvester(M) which returns the auxiliary matrices of matrix M. We provide the code that can work with 2x2 matrices.

Open up the editor, and start out the program with a function header

			function [Z1 Z2]=sylvester(M)

It is good practice to explain what your function does, so you should then type as a comment with something like this: This function takes a matrix M as an input, and returns its fundamental matrix.

We want M to be inputted by the user, so type

			M=input('Please input a matrix!')
Now the first step is to return a vector with the eigenvalues of M:
			eigenvalues=eig(M);
The Sylvester method only works on matrices with simple eigenvalues, so we need to check the multiplicity of eigenvalues. This is equivalent to checking whether the size of the unique vector that contains the unique values of the vector eigenvalues is the same as the size of the original vector. The function that returns the size of a matrix is size(), while the function that returns the vector of unique values is unique().
			if size(unique(eigenvalues))~=2
          display('The matrix you entered does not have simple eigenvalues, thus the Sylvester method  cannot be used');
             else 
             ...
             end
After the else, that is if we do in fact have simple eigenvalues we can code the substantive part of the command. After that is done, we will have to put and end to finish the if statement. Once we have the eigenvalues, we want to calculate the auxiliary matrices.
			Z1=(1/(eigenvalues(1)-eigenvalues(2))*(M-eigenvalues(1)*eye(2))
          Z2=(1/(eigenvalues(2)-eigenvalues(1))*(M-eigenvalues(2)*eye(2))
Thus the complete function should look like this
			function [Z1 Z2]=sylvester(M)
          %   This function takes a matrix M as an input, and returns its auxiliary matrices.
           
            eigenvalues=eig(M);
            
            if size(unique(eigenvalues))~=2
                 display('The matrix you entered does not have simple eigenvalues, thus the Sylvester method  cannot be used');
            else 
                 Z1=(1/(eigenvalues(1)-eigenvalues(2)))*(M-eigenvalues(1)*eye(2))
                 Z2=(1/(eigenvalues(2)-eigenvalues(1)))*(M-eigenvalues(2)*eye(2))
             end
             end

The Resolvent Method
Spectral Decomposition Method
Applications