Linear Algebra Using Matlab

You can peform linear algebra operations very simply using Matlab.

Entering in a Matrix

To enter a matrix into Matlab, type

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

and don't type a semicolon, because you want to see the output.

Basic Operations

Once you have defined two matrices,

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

and they are the same dimension, then you can add them:

A + B =

2  0 0
0 10 0
0  0 8
.

You can subtract them:

A - B =

 0 4 6
12 0 8
 2 4 0
.

Or you can multiply them:

A * B =

-14 2   1
-28 5 -22
-15 0   5
.

Scalar multiplication works the same way:

3 * A =

 3  6  9
18 15 12
 3  6 12
.

Solving the Matrix Equation, Ax=b

To solve the matrix equation, Ax=b, type

A\b

For A=[1 2 3;6 5 4;1 2 4]  and b=[1 ; 13 ; 0], you should get

    2.0000
    1.0000
   -1.0000

Computing a Determinant

To compute the determinant of a matrix, type

det(A)

Using the above matrix A, you should get -7.

Computing the Inverse of a Matrix

To compute the inverse of a matrix type

inv(A)

Using the above matrix A, you should get

-1.7143  0.2857  1.0000
 2.8571 -0.1429 -2.0000
-1.0000  0.0000  1.0000

Computing the Eigenvalues and Eigenvectors

To calculate the eigenvalues and eigenvectors type

[V,D]=eig(A)

Using the above matrix A, you should get

V =

    0.5847   -0.3475    0.1312
   -0.7825   -0.8529   -0.8186
    0.2141   -0.3895    0.5591

D =

   -0.5779         0         0
         0    9.2714         0
         0         0    1.3065

The eigenvalues are located along the matrix D and the correpsonding eigenvectors are the columns of V