UC SCI 111, vM+.
Notebook 8

Needs["Graphics`Colors`"];
Off[General::spell1];
<< Graphics`Arrow`

Mathematica: matrices and vectors.

Matrices and vectors.

In Mathematica, a matrix M is represented as a list of lists. That is, every row of M has its own list, and therefore, the list of rows corresponds with M itself. The most straightforward way of defining a matrix in Mathematica is by just typing in all elements, row by row.

Clear["@"]
m =  {{a, b}, {c, d}};
m //  MatrixForm

The command MatrixForm tells Mathematica that we want m to be displayed as a matrix, rather then as a list of lists.

A vector is an ordered list of numbers and that is exactly how you should define vectors in Mathematica.

v = {x, y};
v // MatrixForm

Multiplication of numbers in Mathematica is indicated by  a * or by just a space, like in 2 3.Matrix multiplication however, uses a dot! Note the difference.

m v // MatrixForm

m.v // MatrixForm

Eigenvalues and the characteristic polynomial.

Computing eigenvalues and eigenvectors in Mathematica is as easy as guessing the name of the command. Let's define a matrix.

Clear["@"]
m2  =  {{-2, 2, -3}, {2, 1, -6}, {-1, -2, 
            0}};
m2 // MatrixForm

And guess...

Eigenvalues[m2]

And again...

ev  =  Eigenvectors[m2]

We can plot the eigenvectors as well.

o  =  {0, 0, 0};
\(\(Show[\ 
        Graphics3D[\ 
		{Thickness[0.01], Yellow, Line[{o, ev[\([1]\)]}], 
            Red, \ Line[{o, ev[\([2]\)]}], Blue, Line[{o, ev[\([3]\)]}]\ , 
            PointSize[0.03], 
	\ \ \ \ Black, Point[ev[\([1]\)]], \ 
            Point[ev[\([2]\)]], 
	\ \ 	Point[ev[\([3]\)]]}\ , 
	
          BoxRatios\  -> \ {1, 1, 0.4}, 
	Axes\  -> \ True, 
	
          FaceGrids\  -> \ All]
	];\)\)

Exercise 4.
a) Check that the eigenvectors ev that Mathematica computed are indeed eigenvectors by computing m2.(Subscript[ev, i]) for i =1..3.
b) Determine the characteristic polynomial of m2 by computing  det( m2-λ I ). 
c) Determine the roots of the characteristic polynomial. Do they have the values that you expect?

2- and higher dimensional recurrence.

Linear recursion.

We consider again  the matrix 

	M =  σ(({{8, 16}, {7, 32}}))

and the linear recursion in terms of M.

x(n) = M x(n-1)		(2.1)

Exercise 5. 
Let x(0) =(({{1}, {2}})).
Define the recurrence relation in Mathematica and compute x(25) for  σ ∈ { 3\(\^i\)  |  i =-5,...,0}.

Scaled recursion.

Let's consider the scaled recurrence relation.

	 x(n)   := \(TraditionalForm\`\(M\ \(x(n - 1)\)\)\/\(\(||\)\(\ \)\(M\ \(x(n - 1)\)\)\(\ \)\(||\)\)\)		(3.1)
	 
We know enough to tell Mathematica how to compute values  for our new recurrence relation (3.1).  We redefine the matrix M, now without σ.

\(a = \ {8, 16}; 
b = \ {7, 32}; 
m\  = {a, b}; \ 
    m\  // \ MatrixForm\)

As matrix multiplication  in Mathematica is indicated by a special symbol, it will not surprise you that m^n is not the way to lift M to its n-th power. Indeed, the job is done by the command MatrixPower.

Exercise 6.
a) Define the recurrence relation in Mathematica and compute x(10) for three different choices of x(0). 
b) Prove that x(n)= (- 4/5, 1/5) for every n, when x(0) = (-4,1) and that x(n) = (4/11, 7/11) when x(0) = (4,7).

Dominancy.

Exercise 7.
a) What is the dominant eigenvalue of M ? 
b) Can you see this reflected in the convergence behavior of our recurrence relation (3.1)?
c) Let  (u^1)denote the eigenvector of M that corresponds  with the strictly dominant eigenvalue (Subscript[λ, 1])> 0 and  denote the other eigenvector by (u^2). Let  x(0) = (u^1) + (u^2). Show that x(n) converges to ((u^1)/((((||u))^1)||)).

Basis.

Although we analyzed in the previous exercises only the convergence behavior for three very special initial vectors x(0), we are in fact done!

\(onu1u2[point_]\  := \ 
      Module[
		{o, u1, u2, sols, s1, s2}, 
		Print["v: ", 
          point]; 
		o\  = \ {0, 0}; 
		u1\  = \ {\(-4\), 
            1}; 
		u2\  = \ {4, 7}; 
		sols\  = \ 
          Solve[{point[\([1]\)] == \(-4\)\ c1 + 
                  4\ c2, 
					point[\([2]\)]\  == 
                c1\  + \ 7  c2}, {c1, c2}]; 
		s1 = \ 
          sols[\([1, 1, 2]\)]; 
		s2\  = \ sols[\([1, 2, 2]\)]; 
		Show[
          Graphics[{Arrow[o +  .03, u1 +  .03], 
					Arrow[o +  .03, 
                u2 +  .03], Blue, Arrow[o, point], \ 
              Red, 
					Arrow[o\ , s1\ u1], 
              Arrow[o, 
                s2\ u2], 
					Dashing[{ .01,  .01}], 
					Line[{s1\ u1, point}], \ Line[{s2\ u2, \ point}]}], \ 
          AspectRatio\  -> \ Automatic, \ Axes\  -> \ True]
	]\)

Given an arbitrary vector v, onu1u2 shows how v, the blue vector,  can be build up as a linear combination of (u^1) and (u^2), the black vectors.

r:= Random[
        Integer, {-10, 10}]

onu1u2[{r, r}];

Exercise 8.
a) Let v denote an arbitrary vector in the plane. Prove that there exist unique numbers (Subscript[c, 1])and (Subscript[c, 2])such that v = (Subscript[c, 1])(u^1)+  (Subscript[c, 2])(u^2).
b) Describe the convergence behavior of x(n) defined by (3.1) with initial condition x(0) =  (Subscript[c, 1])(u^1)+  (Subscript[c, 2])(u^2) in terms of (Subscript[c, 1]) and (Subscript[c, 2]).

A 4-dimensional recurrence.

Mathematica's computing powers are of course more impressive in cases where computing by hand gets less attractive. Consider for example the matrix m defined as follows.

	m := (({{1, 0, 0, 0}, {0, 1, 2, 0}, {2, 3, 1, 3}, {1, 1, 0, 2}}))

Exercise 9.
a) Compute the eigenvectors and eigenvalues of m. 
b) Do the eigenvectors span (ℝ^4)?
c) What is the dominant eigenvalue? 
d) Suppose that x(0) = a(0)+b(0)+c(0)+d(0), where a(0), b(0), c(0) and d(0) are eigenvectors that  correspond to different eigenvalues. Prove that x(n), when suitably normalized, converges to an eigenvector that corresponds with the eigenvalue of biggest absolute value. 
e) Compute x(50) for 10 different choices of x(0).
☺ f) Adapt the statement and proof in d) in such a way that it explains the computational results of e).