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
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]
b=[5,6,7,8]
5 6 7 8
and then take transposition
b.' % or type directly: [5,6,7,8].'
disp(b)
disp(b')
transpose(b) % or type directly: transpose([5,6,7,8])
[1:2:9; 2:2:10]
zeros(5,2)
eye(4)
[eye(4) zeros(4)]
[zeros(3); 3*eye(3)]
A=[1 2; 3 4]
A(2)
twenty=[1:10;11:20]
first_row=twenty(1,:)
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]
2 3 -4
b=[2 -1 -3]
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
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))
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
AtimeseeB = A .* B
C=[1 2; 3 4]
C'
D=[1+i 2; 3 4]
D'
D.'
trace(C)
det(C)
inv(A)
A^(-1)
resolvent=inv(lambda*eye(2)-E)
eigenvalues=eig(E)
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)
x=A\b
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:
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):
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!')
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
...
end
Z1=(1/(eigenvalues(1)-eigenvalues(2))*(M-eigenvalues(1)*eye(2))
Z2=(1/(eigenvalues(2)-eigenvalues(1))*(M-eigenvalues(2)*eye(2))
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