Introduction to Matrices

Math 245 - Mathematical Models
Goal: Understand how to use Mathematica to deal with matrices and vectors.

Matrices

Introduction

A matrix is defined as a list of the row vectors (as lists).

m1=Table[i+j,{i,3},{j,3}]

MatrixForm shows the list of lists in matrix format.

MatrixForm[m1]

m2={{0,-1,-2},{1,0,-1},{2,1,0}};
MatrixForm[%]

Multiplication

To multiply matrices, use a period (.)

m1.m2;
MatrixForm[m1.m2]

The product AB is not necessarily equal to BA.

MatrixForm[m2.m1]

If you try to multiply two incompatible matrices, you will get an error.

m3={{Pi,E},{3,42}}

m1.m3

It is important that you use the period (.) instead of the star (*), because * multiplies piecewise instead of using matrix multiplication.

m1*m2;
MatrixForm[%]

Powers of a matrix

To take the power of a matrix, use the MatrixPower command.  It works for numerical and variable expressions.

MatrixPower[m1,4]

MatrixPower[m2,3]

MatrixPower[m1,n]

Remember to use the N command to see the numerical values instead of the exact values.

N[MatrixPower[m1,n]]

N[MatrixPower[m2,n]]

Again, make sure to use MatrixPower instead of ^ because ^ acts entry-wise.

m1^4

Leslie matrix example from class

leslie={{3/2,2},{1/2,0}};
MatrixForm[leslie]

leslie.{{4},{1}}

leslie.{4,1}

leslie.{-1,1}

power=MatrixPower[leslie,n];

MatrixForm[N[power]]

Markov chain example from class

markov={{6/10,3/10},{4/10,7/10}};
MatrixForm[N[markov]]

markov.{2/3,1/3}

MatrixPower[markov,n]

When you take the limit as n goes to infinity, you get the equilibrium solution.

Limit[
MatrixPower[markov,n].{0,1},
n->Infinity]

Alternatively, you can solve for the equilibrium solution directly using the Solve command.

Solve[markov.{x1,x2}=={x1,x2},{x2,x1}]