You are on page 1of 4

Linear Algebra Using MATLAB

Many of the applications of linear algebra such as matrices, determinants,


systems of equations and the eigenvalue problem can all be easily handled using
MATLAB. You should already have a basic idea of how to create and manipulate
matrices (arrays) in MATLAB. The following example demonstrates how to create
a matrix (square), find its inverse and then multiply the matrix times its inverse to
check that it equals the unit matrix
% Create a 3x3 Matrix
and Find Its Inverse
A=[1,2,0;3,2,1;1,2,1]
Ainv=inv(A)
% Check on Answer
Check=A*Ainv

A =

Ainv =

Check =

0.0000

0.5000

-0.5000

0.5000

-0.2500

0.2500

-1.0000

1.0000

1.0000

0.0000

-0.0000

1.0000

0.0000

0.0000

1.0000

Of course not all square matrices will have an inverse and our previous theory
indicated that the inverse will only exist if the matrix A is nonsingular; i.e. if
det(A)0. Next let us focus on several example application problems.
Systems of Linear Algebraic Equations
Consider the following system of equations with unknowns x, y, z which can also
be cast as a matrix equation
2x 2 y z 1
x 5y z 2
3x 4 y 3z 9

2 2 1 x 1
1 5 1 y 2


3 4 3 z 9

Thus such systems can always be cast in the form AX b where

2 2 1
x
1


A 1 5 1 , X y , b 2

z
9
3 4 3

MATLAB can easily solve such systems using the inverse method whereby
X A1b . The code and output for this particular problem is
% Solve a 3x3 System of
Linear Algebraic Equations
A=[2,2,-1;1,-5,1;3,4,-3];
b=[1;2;9];
X=inv(A)*b
% Check on Answer
Check_Answer=A*X

X =

-0.8000
-1.8000
-6.2000

Check_Answer =

1.0000
2.0000
9.0000

This system of equations could also be solved using Cramers Rule (text, Section
7.7) using a little more programming and incorporating determinants. The code
below does the job
% Solve a 3x3 System of Linear Algebraic
Equations
% Using Cramer's Rule
A=[2,2,-1;1,-5,1;3,4,-3];
b=[1;2;9];
A1=A;A1(:,1)=b;
A2=A;A2(:,2)=b;
A3=A;A3(:,3)=b;
D=det(A);D1=det(A1);D2=det(A2);D3=det(A3);
X(1)=D1/D;X(2)=D2/D;X(3)=D3/D
% Check on Answer
Check_Answer=A*X

X =

-0.8000
-1.8000
-6.2000

Check_Answer =

1.0000
2.0000
9.0000

Note that the two previous methods of solution (Inverse and Cramers Rule) are
not computationally efficient for large systems that would have say more than 102
equations. For such cases, techniques of Gauss elimination, Gauss Seidel

iteration, and methods of Doolittle, Crout and Cholesky are often used (see
Kreysizg, Chapter 20) .
Eigenvalue Problem
As previously discussed the matrix eigenvalue or principal value problem involves
the solution of the matrix equation
AX X

(1)

Here we limit the generality by choosing the square matrix A to be real and
symmetric. The quantity is called the eigenvalue or principal value and the
column matrix or vector X is referred to as the eigenvector or principal direction.
Recall that equation (1) can be written as
( A I ) X 0

(2)

where I is the unit matrix. Relation (2) is a homogeneous system of equations and
has a non-trivial solution iff the determinant of the coefficient matrix vanishes;
i.e.
det( A I ) 0

(3)

Expanding the determinant yields the characteristic equation whose roots are the
eigenvalues of the problem. Back substitution of these eigenvalues into relation
(1) or (2) allows determination of the corresponding eigenvectors. Note that
usually the eigenvectors are normalized to have unit length.
For a general problem with matrix A being at least 3x3 or larger, many of these
solution steps cannot be done easily by hand computation. MATLAB offers a
numerical solution scheme that will easily determine both eigenvalues and
eigenvectors for such general problems using the simple commands eig(A).
Consider the following example for the matrix A

3 1 1
A 1 0 2

1 2 0

The code below calculates both the eigenvalues and eigenvectors for this case.
V =
% Determine Eigenvalues and
Eigenvectors Of 3x3 Matrix
A=[3,1,1;1,0,2;1,2,0];
[V,L]=eig(A)

-0.0000

0.5774

-0.8165

0.7071

-0.5774

-0.4082

-0.7071

-0.5774

-0.4082

-2.0000

1.0000

4.0000

L =

Note the eigenvalues are output into the diagonal elements of the L array, and
the eigenvectors are output as the columns of the V array. These values are easily
verified by the exact hand calculation solution

1 2 , 2 1, 3 4 ; X (1)

0
1
2
1 ( 2)
1 ( 3)
1

1 , X
1 , X
1
2
3
6
1
1
1

You might also like