MATLAB Linear Algebra Tutorial
MATLAB Linear Algebra Tutorial
10 9 8
5 4 3
(
6 4 2
5 3 1
(
16 13 10
10 7 4
>> A = [3:5; 8:10]
A =
3 4 5
8 9 10
>> B = reshape(1:6,2,3)
B =
1 3 5
2 4 6
Copyright August 2010 by Stormy Attaway Page 12
>> C = A + B
C =
4 7 10
10 13 16
For any operation that is based on multiplication (this means multiplication, division, and
exponentiation), the array operator has a dot (.) in front of it. For example, using the
matrices A and B above:
>> atimesb = A .* B
atimesb =
3 12 25
16 36 60
>> araisedtob = A .^ B
araisedtob =
3 64 3125
64 6561 1000000
Matrix Multiplication
Matrix multiplication is VERY different from the array multiplication defined above. If a
matrix A has dimensions m x n, then in order to be able to multiply it by a matrix B, B
must have dimensions n x something; well call the column dimension p. In other words,
in order to multiply A * B, the number of rows of B has to be the same as the number of
columns of A. We say that the inner dimensions must agree. The resulting matrix, C,
will have as its dimensions m x p (the outer dimensions). So,
[A]
mxn
[B]
nxp
= [C]
mxp
This just explains the dimensions; it does not yet describe how the elements in the matrix
C are derived. Every element in C is the result of summing the products of
corresponding elements in the rows of A and the columns of B. Any given element in C,
C
ij
, is defined as
C
ij
=
kj
n
k
ik
b a
=1
For example,
A * B = C
(
10 9 8
5 4 3
* =
(
(
(
6 3
5 2
4 1
(
137 56
62 26
The first element in C, C
11
, is found by multiplying corresponding elements in the first
row of A and the first column of B, and summing these, e.g. 3*1 + 4*2 + 5*3 = 26. In
MATLAB, matrix multiplication is accomplished using the * operator.
Copyright August 2010 by Stormy Attaway Page 13
>> A = [3:5; 8:10]
A =
3 4 5
8 9 10
>> B = reshape(1:6,3,2)
B =
1 4
2 5
3 6
>> C = A * B
C =
26 62
56 137
Note that for square matrices, multiplying a matrix A by an identity matrix I with the
same size results in the matrix A (so, multiplying a matrix by I is similar to multiplying a
scalar by 1; it doesnt change it).
>> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
>> A * eye(3)
ans =
8 1 6
3 5 7
4 9 2
Inverse
The definition of the inverse of a matrix is that when multiplying a matrix by its inverse,
the result is the identity matrix. Mathematically, we would write this [A] [A
-1
] = [I].
MATLAB has a built-in function to find an inverse, inv.
>> A = [1 3; 2 4]
A =
1 3
2 4
>> inv(A)
ans =
-2.0000 1.5000
1.0000 -0.5000
>> A * inv(A)
ans =
1 0
0 1
Copyright August 2010 by Stormy Attaway Page 14
Matrix Augmentation
Matrix augmentation means taking a matrix and augmenting by adding more columns, or
another matrix, to it. Note that the dimensions must be correct in order to do this. In
MATLAB, this is called concatenating and can be done for either a vector or matrix by
putting the two together in square brackets. Here is an example in which a matrix A is
augmented with an identity matrix with the same size of A; note that can be obtained with
eye(size(A)):
>> A = [1 3; 2 4]
A =
1 3
2 4
>> [A eye(size(A))]
ans =
1 3 1 0
2 4 0 1
Vector Operations: Dot Product and Cross Product
As long as the dimensions are correct, some of the definitions and operations given on
matrices above are also valid for vectors, since vectors are just a special case of matrices.
There are some operations, however, that are only valid for vectors, including the dot
product and cross product.
For two vectors A and B that have the same length, the dot product is written AB and is
defined as = A
1
B
1
+ A
2
B
2
+ A
3
B
3
+ + A
n
B
n
where n is the length of the
vectors. In MATLAB, there is a function dot to accomplish this.
B A i
n
i
i
=1
The cross product AxB is defined only if A and B are vectors of length 3, and can be
written using the following matrix multiplication:
A x B = = [A
2
B
3
-A
3
B
2
, A
3
B
1
-A
1
B
3
, A
1
B
2
-A
2
B
1
]
(
(
(
0
0
0
1 2
1 3
2 3
A A
A A
A A
(
(
(
3
2
1
B
B
B
MATLAB has a built-in function cross for the cross product.
Copyright August 2010 by Stormy Attaway Page 15
Intro to Linear Algebra: Systems of Equations
A system of linear algebraic equations is of the form
a
11
x
1
+ a
12
x
2
+ a
13
x
3
+ . + a
1n
x
n
= b
1
a
21
x
1
+ a
22
x
2
+ a
23
x
3
+ . + a
2n
x
n
= b
2
a
31
x
1
+ a
32
x
2
+ a
33
x
3
+ . + a
3n
x
n
= b
3
a
m1
x
1
+ a
m2
x
2
+ a
m3
x
3
+ . + a
mn
x
n
= b
m
where the as are the coefficients, the xs are the unknowns, and the bs are constant
values.
Using MATLAB, there are two basic methods for solving such a set of equations:
- By putting it in a matrix form
- Using the solve function which is part of the Symbolic Math Toolbox
Matrix Form
Because of the way that matrix multiplication works, the system of equations shown
above can be written as the product of a matrix of the coefficients A and a column vector
of the unknowns x:
A x = b
=
(
(
(
(
(
(
mn m m m
n
n
n
a a a a
a a a a
a a a a
a a a a
3 2 1
3 33 32 31
2 23 22 21
1 13 12 11
(
(
(
(
(
(
n
x
x
x
x
3
2
1
(
(
(
(
(
(
m
b
b
b
b
3
2
1
We have a matrix multiplication equation of the form Ax = b, and we want to solve for
the unknowns x. This can be accomplished as follows:
A x = b
A
-1
A x = A
-1
b
I x = A
-1
b
x = A
-1
b
So, the solution can be found as a product of the inverse of A and the column vector b.
Copyright August 2010 by Stormy Attaway Page 16
In MATLAB, there are two ways of doing this, using the built-in inv function and matrix
multiplication, and also using the \ operator:
>> A = [3 4 1; -2 0 3; 1 2 4]
A =
3 4 1
-2 0 3
1 2 4
>> b = [2 1 0]'
b =
2
1
0
>> x = inv(A) * b
x =
-1.1818
1.5000
-0.4545
>> A\b
ans =
-1.1818
1.5000
-0.4545
2 x 2 Systems
The simplest system is a 2 x 2 system, with just two equations and two unknowns. For
these systems, there is a simple definition for the inverse of a matrix, which uses the
determinant D of the matrix.
For a coefficient matrix A defined generally as
A = ,
(
22 21
12 11
a a
a a
the determinant D is defined as a
11
a
22
a
12
a
21
.
The inverse A
-1
=
(
11 21
12 22
1
a a
a a
D
Copyright August 2010 by Stormy Attaway Page 17
For example, for the system
x
1
+ 3x
2
= -2
2x
1
+ 4x
2
= 1
This would be written in matrix form as
=
(
4 2
3 1
(
2
1
x
x
(
1
2
The determinant D = 1*4 -3*2 = -2.
The inverse A
-1
=
(
1 2
3 4
2
1
=
(
2 / 1 1
2 / 3 2
So the solution is: = =
(
2
1
x
x
(
2 / 1 1
2 / 3 2
(
1
2
(
2 / 5
2 / 11
MATLAB has a built-in function det to find the determinant of a matrix.
>> A = [1 3; 2 4]
A =
1 3
2 4
>> b = [-2;1]
b =
-2
1
>> det(A)
ans =
-2
>> inv(A)
ans =
-2.0000 1.5000
1.0000 -0.5000
>> x = inv(A) * b
x =
5.5000
-2.5000
Copyright August 2010 by Stormy Attaway Page 18
Elementary Row Operations
Although finding the inverse of a 2 x 2 matrix is straightforward, as is using it to find the
unknowns, it is not so simple for larger systems of equations. Therefore, we resort to
other methods of solution. Once the system is in matrix form, some of these methods
involve transforming matrices using what are called Elementary Row Operations, or
EROs. The important thing to understand about these EROs is that using them to modify
a matrix does not change what the solution to the set of equations will be.
There are 3 EROs:
1. Scaling: multiplying a row by a scalar (meaning, multiplying every element in the
row by the same scalar). This is written sr
i
r
i
, which indicates that row i is
modified by multiplying it by a scalar s.
2. Interchange: interchanging the locations of two rows. This is written as r
i
r
j
which indicates that rows i and j are interchanged.
3. Replacement: replacing all of the elements in one row with that row plus or minus a
scalar multiplied by another row. This is written as r
i
+/- sr
j
r
i
These EROs form the basis for the methods described next.
Gauss Elimination
The Gauss Elimination method is a method for solving a matrix equation Ax=b for x.
The process is:
1. Start by augmenting the matrix A with the column vector b.
2. Perform EROs to transform this augmented matrix to upper triangular form.
3. Use back-substitution to solve for the unknowns in x.
For example, for a 2 x 2 system, the first step is to augment the matrix [A b]:
(
2 22 21
1 12 11
b a a
b a a
Then, EROs are applied to get the augmented matrix into an upper triangular form
(which, for a 2 x 2 matrix means finding an ERO that would change a
21
to 0):
(
'
2
'
22
'
1
'
12
'
11
0 b a
b a a
Here, the primes indicate that the values may have been changed.
Putting this back into the equation form, we have
=
(
'
22
'
12
'
11
0 a
a a
(
2
1
x
x
(
'
2
'
1
b
b
Copyright August 2010 by Stormy Attaway Page 19
So, we now use the last line which says
a
22
x
2
= b
2
to solve first for x
2
:
x
2
= b
2
/ a
22
and then go back to the first line which says
a
11
x
1
+ a
12
x
2
= b
1
to solve for x
1
:
x
1
= (b
1
a
12
x
2
) / a
11
Gauss-Jordan Elimination
The Gauss-Jordan Elimination method starts exactly the same way as the Gauss
Elimination method, but instead of back-substitution to solve for x, EROs are used to get
the augmented matrix into a diagonal form.
1. Start by augmenting the matrix A with the column vector b.
2. Perform EROs to transform this augmented matrix to upper triangular form (forward
elimination).
3. Use back elimination (perform more EROs) to get to a diagonal form
4. Solve for the unknowns in x.
For example, for a 3 x 3 matrix, the matrix is first augmented:
(
(
(
3 33 32 31
2 23 22 21
1 13 12 11
b a a a
b a a a
b a a a
EROs are then performed using forward elimination to get it to upper triangular form:
(
(
(
'
3
'
33
'
2
'
23
'
22
'
1
'
13
'
12
'
11
0 0
0
b a
b a a
b a a a
At this point, the Gauss method would then use back-substitution to solve first for x
3
,
then x
2
, then x
1
. Instead, the Gauss-Jordan method continues with back elimination to get
it to diagonal form:
Copyright August 2010 by Stormy Attaway Page 20
(
(
(
'
3
'
33
'
2
'
22
'
1
'
11
0 0
0 0
0 0
b a
b a
b a
Then, the unknowns can be found easily, in any order using these equations:
x
i
= b
i
/ a
ii
Reduced Row Echelon Form (RREF)
Reduced Row Echelon Form (RREF) takes the Gauss-Jordan elimination method one
step further by performing scaling EROs on all rows so that the a
ii
coefficients on the
diagonal all become ones.
RREF to solve Ax=b for x
To use the RREF method to solve the matrix equation Ax = b for x, the matrix A is
augmented with b, and then the Gauss-Jordan method is followed. The final step is to
scale all rows. For example, for a 3 x 3 matrix,
(
(
(
'
3
'
33
'
2
'
22
'
1
'
11
0 0
0 0
0 0
b a
b a
b a
(
(
(
'
3
'
2
'
1
1 0 0
0 1 0
0 0 1
b
b
b
Then, the solution is simply the last column. MATLAB has a function rref to
accomplish this.
RREF to find inverse
This technique and function can also be used to find the inverse of a matrix A. The
procedure is:
- Augment A with an identity matrix the same size as A
- Follow the procedure above using EROs to reduce the left-side to an identity matrix;
the right side will be the matrix inverse.
For example, for a 3 x 3 matrix, start with [A I]:
(
(
(
1 0 0
0 1 0
0 0 1
33 32 31
23 22 21
13 12 11
a a a
a a a
a a a
and then reduce this to:
Copyright August 2010 by Stormy Attaway Page 21
(
(
(
33 32 31
23 22 21
13 12 11
1 0 0
0 1 0
0 0 1
r r r
r r r
r r r
which is [I A
-1
].
Gauss, Gauss-Jordan, RREF Example
Since the Gauss, Gauss-Jordan and RREF methods to solve Ax = b for x all begin the
same way, we will demonstrate all with one example.
The following 3 x 3 system of equations:
3x
1
+ 2x
2
+ x
3
= 1
x
2
= 2
x
1
+ 2x
2
= 3
can be written in matrix form:
(
(
(
0 2 1
0 1 0
1 2 3
=
(
(
(
3
2
1
x
x
x
(
(
(
3
2
1
To solve for x, we start with the augmented matrix [A b] and perform forward
elimination by finding EROs to get it to upper triangular form:
(
(
(
3 0 2 1
2 0 1 0
1 1 2 3
r
3
1/3 r
1
r
3
(
(
(
3 / 8 3 / 1 3 / 4 0
2 0 1 0
1 1 2 3
r
3
-4/3 r
2
r
3
(
(
(
0 3 / 1 0 0
2 0 1 0
1 1 2 3
Now, for the Gauss method, we use back substitution:
-1/3x
3
= 0 so x
3
= 0
x
2
= 2
3x
1
+ 2x
2
+ x
3
= 1
3x
1
+ 2 -0 = 1
3x
1
= -3
x
1
= -1
Instead for Gauss-Jordan, we continue with back elimination:
Copyright August 2010 by Stormy Attaway Page 22
(
(
(
0 3 / 1 0 0
2 0 1 0
1 1 2 3
r
1
+ 3r
3
r
1
r
1
-2r
2
r
1
(
(
(
0 3 / 1 0 0
2 0 1 0
1 0 2 3
(
(
(
0 1 0 0
2 0 1 0
3 0 0 3
Once in diagonal form, we can solve:
3x
1
= -3 so x
1
= -1
x
2
= 2
-x
3
= 0 so x
3
= 0
For RREF, scale all rows:
(
(
(
0 1 0 0
2 0 1 0
3 0 0 3
1/3 r
1
r
1
-r
3
r
3
(
(
(
0 1 0 0
2 0 1 0
1 0 0 1
(
(
(
0 1 0 0
2 0 1 0
1 0 0 1
So now the last column is x.
We can also use RREF to find A
-1
and solve that way.
Start with the augmented matrix [A I]:
(
(
(
1 0 0 0 2 1
0 1 0 0 1 0
0 0 1 1 2 3
r
1
r
3
(
(
(
0 0 1 1 2 3
0 1 0 0 1 0
1 0 0 0 2 1
r
3
3 r
1
r
3
(
(
(
3 0 1 1 4 0
0 1 0 0 1 0
1 0 0 0 2 1
r
3
+ 4r
2
r
3
(
(
(
3 4 1 1 0 0
0 1 0 0 1 0
1 0 0 0 2 1
r
1
-2 r
2
r
1
(
(
(
3 4 1 1 0 0
0 1 0 0 1 0
1 2 0 0 0 1
So A
-1
= and x = =
(
(
(
3 4 1
0 1 0
1 2 0
(
(
(
3 4 1
0 1 0
1 2 0
(
(
(
3
2
1
(
(
(
0
2
1
Copyright August 2010 by Stormy Attaway Page 23
In MATLAB, to solve Ax=b we begin by augmenting [A b]:
>> A = [3 2 1; 0 1 0; 1 2 0];
>> b = [1:3]';
>> Ab = [A b]
Ab =
3 2 1 1
0 1 0 2
1 2 0 3
Performing an ERO in MATLAB can be accomplished by using assignment statements to
modify rows. For example this ERO
(
(
(
3 0 2 1
2 0 1 0
1 1 2 3
r
3
1/3 r
1
r
3
(
(
(
3 / 8 3 / 1 3 / 4 0
2 0 1 0
1 1 2 3
is done with:
>> Ab(3,:) = Ab(3,:) - 1/3 * Ab(1,:)
Ab =
3.0000 2.0000 1.0000 1.0000
0 1.0000 0 2.0000
0 1.3333 -0.3333 2.6667
MATLAB has a function rref to reduce:
>> rref(Ab)
ans =
1 0 0 -1
0 1 0 2
0 0 1 0
In MATLAB, we can find the inverse of a matrix using rref and then check that using the
inv function:
>> rref([A eye(size(A))])
ans =
1 0 0 0 -2 1
0 1 0 0 1 0
0 0 1 1 4 -3
>> inv(A)
ans =
0.0000 -2.0000 1.0000
0 1.0000 0
1.0000 4.0000 -3.0000
Copyright August 2010 by Stormy Attaway Page 24
Copyright August 2010 by Stormy Attaway Page 25
The solve Function in the Symbolic Math Toolbox
If you have the Symbolic Math Toolbox, the solve equation can also be used to solve sets
of equations. The solution is returned as a structure. Structures are similar to vectors in
that they can store multiple values. However, instead of having elements, structures have
fields that are named. While indexing is used to refer to elements of a vector, the dot
operator is used to refer to the fields in a structure variable.
As an example, we will solve the following 3 x 3 system of equations that was used in the
previous section:
3x
1
+ 2x
2
+ x
3
= 1
x
2
= 2
x
1
+ 2x
2
= 3
For simplicity, we will change the x
1
,x
2
,x
3
to x,y,z, and pass the three equations as strings
to the solve function:
>> result = solve('3*x+2*y+z=1', 'y=2', 'x+2*y=3')
result =
x: [1x1 sym]
y: [1x1 sym]
z: [1x1 sym]
This stores the unknowns in fields called x, y, and z within the result variable. The dot
operator is used to see the actual values, which are stored as symbolic expressions. The
double function can convert them to numbers:
>> result.x
ans =
-1
>> x = double([result.x result.y result.z])'
x =
-1
2
0