Linear algebra

Contents

System of equations

In linear algebra a system of equations can be represented by a coefficient matrix. MATLAB can solve all types of linear equations automatically. We will demonstrate how MATLAB matrix manipulation commands can be used to perform row operations on a system of linear equations.

Consider the system:

4x+3y+z=10

5x+4y+2x=4

x-y+z=15

Back to top

Coefficient matrix

We define a standard coefficient matrix.

A = [4 3 1; 5 4 2; 1 -1 1]
A =

     4     3     1
     5     4     2
     1    -1     1

Back to top

Right-hand side values

These are the values from the right hand side of the equations.

C = [10;4;15]
C =

    10
     4
    15

Back to top

Augmented matrix

Row reduction is done on the augmented matrix.

AA = [A C]
AA =

     4     3     1    10
     5     4     2     4
     1    -1     1    15

Back to top

Swap rows 1 and 3

The syntax manipulates two rows of the matrix at the same time by indexing them in a matrix. The colon indicates all columns.

AA([1 3], :) = AA([3 1], :)
AA =

     1    -1     1    15
     5     4     2     4
     4     3     1    10

Back to top

Eliminate values in rows 2 and 3

We use a trick to manipulate two rows at the same time. The final term in the command is a 2x1 matrix multiplied by a 1x4 matrix. This produces two copies of the first row, each scaled differently.

AA([2 3],:) = AA([2 3],:) - [5;4]*AA(1,:)
AA =

     1    -1     1    15
     0     9    -3   -71
     0     7    -3   -50

Back to top

Divide through second row

AA(2,:) = AA(2,:) ./ 9
AA =

    1.0000   -1.0000    1.0000   15.0000
         0    1.0000   -0.3333   -7.8889
         0    7.0000   -3.0000  -50.0000

Back to top

Row operation

AA(3,:) = AA(3,:) - 7 * AA(2,:)
AA =

    1.0000   -1.0000    1.0000   15.0000
         0    1.0000   -0.3333   -7.8889
         0         0   -0.6667    5.2222

Back to top

Divide through third row

AA(3,:) = AA(3,:) ./ -0.6667
AA =

    1.0000   -1.0000    1.0000   15.0000
         0    1.0000   -0.3333   -7.8889
         0         0    1.0000   -7.8329

Back to top

Add rows

AA(1,:) = AA(1,:) + AA(2,:)
AA =

    1.0000         0    0.6667    7.1111
         0    1.0000   -0.3333   -7.8889
         0         0    1.0000   -7.8329

Back to top

Add scaled rows

AA([1 2], :) = AA([1 2], :) + [-0.6667; 0.3333] * AA(3,:)
AA =

    1.0000         0   -0.0000   12.3333
         0    1.0000   -0.0000  -10.4996
         0         0    1.0000   -7.8329

Back to top

MATLAB can computer determinants automatically

The "det" command will compute a determinant.

det(A)
ans =

     6

Back to top

MATLAB can calculate the row reduced echelon form

A much simpler approach is to just ask MATLAB to compute the row reduced echelon form of the augmented matrix.

rref(AA)
ans =

    1.0000         0         0   12.3333
         0    1.0000         0  -10.5000
         0         0    1.0000   -7.8333

Back to top

Matrix division

MATLAB supports a form of matrix division. In the equation:

AX=C

Solving for X gives:

X=(A^-1)C

MATLAB can perform this calculation with "left division":

A\C
ans =

   12.3333
  -10.5000
   -7.8333

Back to top


Published with MATLAB® 7.4