Math 21b: Linear Algebra Spring 2007
Mathematica Assignment

Support

Welcome to the Mathematica computer project! In case problems with this assignment, please email math21b@fas.harvard.edu.  Mathematica can demand a lot of resources from computers. Therefore, save the notebook frequently during your work. The actual assignments are in the  magenta colored boxes. This lab is due at the last lecture of the semester. You will have to print out the assignment part with your solutions and hand them in. All problems are listed at the end. There are 7 problems. You have to have 5 correct to get full credit. All problems have sample code in this notebook somewhere.

Cells and Expressions

The notebook is subdivided into cells, bounded by a blue bracket.  This is a "text" cell, the next  is an "input" cell. We compute the dot product of two vectors:

In[225]:=

To evalute an expression, click anywhere on the expresson, hold down the <Shift> key and press <Enter>.  This is how we compute the inverse of a matrix:

In[226]:=

In this notebook, input cells are  boxed in blue for better readability.

Basic  Computations

Mathematica allows to work with vectors and matrices in the same way as with numbers. A dot . is the matrix multiplication or dot product. The following examples should be selfexplanatory.

In[227]:=

In[275]:=

In[229]:=

In[230]:=

In[231]:=

In[232]:=

In[233]:=

In[234]:=

In[235]:=

Eigenvalues

With Eigenvalues[A], we can compute the eigenvalues, with Eigensystem[A], both the eigenvalues and the eigenvectors. What happens for a random matrix? Here, we plot the complex eigenvalues of a random 50x50 matrix.

In[236]:=

It will be one of the homework problem to formulate a conjecture based on your experiments with larger matrices.

Determinants

What is the distribution of the determinant of a random matrix? To see the distribution of a random 10x10 matrix, lets do 100 experiments and plot the results in a sorted manner.

In[239]:=

This distribution approaces the arcsin distribution.

In[241]:=

Here is how we compute the determinant of the 100 x 100 matrix which contains the entry  i*j at the entry (i,j).

In[242]:=

Mechanical systems

Here is an example, on how one can build a more elaborate program which shows the motion of a spinner. The following code is more to illstruate how one can make relatively quickly animations of systems in a computer algebra system. It produces a movie. Run it, grab the bracket of the result and chose under the above Cell menu the entry "Animate selected graphics". It will show you how the system moves.

In[276]:=

Markov processes

A vector is called a probability vector if each entry is nonnegative and the sum of all its entry is 1. A matrix A for which each column is a probability vector is called a Markov matrix or stochastic matrix.

In[258]:=

A  Markov matrix has an eigenvalue 1 because its transpose has the eigenvector [1,1,..., 1]. The eigenvector of the eigenvalue 1 of A  is called the staedy state.

In[260]:=

When iterating the matrix A, it converges to a matrix which has the eigenvector as column vectors.

In[261]:=

Here is a magical square:

In[262]:=

If we divide it by the sum 65 over each column, we obtain a stochastic matrix.

Differential equations

Differential equations can be solved with the command "DSolve". Here is an example, where one does not specify the initial conditions:

In[264]:=

Here is an example, where the initial conditions are specified. This is the solution of the forced driven harmonic oscillator:

In[265]:=

In[266]:=

Fourier series

In[290]:=

For every n, we have a Function s[x,n] which is a Fourier approximation of the function f:

In[295]:=

Sound

Here is how you can play a tune with Mathematica with a given wave form. In the example below, we take the Sin function as the wave form and play a tune.

In[1]:=

Partial differential equations

Here is how Mathematica can solve a partial differential equation. Here it is the wave equation. The result will be given in terms of variables C[1],C[2] which can be general functions:

In[272]:=

Note that the solution capabilities of Mathematica are rather limited in partial differential equations.
Here is a numerical computation of the one dimensional wave equation with inhomogeneous right hand side on the unit interval with boundary conditions zero at 0 and 1.

In[308]:=

Assignment

To get full credit for this  assignment, you have to hand in solutions to the following problems. We kept the assignment short. Once you get again a grip at Mathematica, it should take you not long to do it. You have sample code for each of the problems in the text above. You need to turn in correct answers to 5 problems of the 7 problems.

1) What is the distribution of the eigenvalues of a random matrix, where each entry is a random number between -1,1. Make experiments with larger matrices (sample code is above) and write down your observation in a sentence. How does the eigenvalue cloud look like in your experiments?
2) Find the determinant of the matrix  A=Table[i^j,{i,50},{j,50}] and compute its prime decomposition                         with the function FactorInteger.
3) Consider the magical square A={{17,24,1,8,15},{23,5,7,14,16},{4,6,13,20,22},{10,12,19,21,3},{11,18,25,2,9}} as in the text. The matrix B=A/65 is a stochastic matrix. What do you observe, if you take powers of B? You get full credit if you write down the observation. If you have time, can you explain why for every magical square , this phenomenon takes place?
4) Find the solution of the ordinary differential equation f''[x]+f'[x]+f[x]==Cos[x]+x^2 with initial conditions  f[0]==1;f'[0]==0.
5)  Find the Fourier series of the function  f[x_]: = x^5 on [-Pi,Pi]. You need at least 20 coefficients of the Fourier series.
6) See wether Mathematica can solve the transport equation  d/dt  u(t,x) = d/dx u(t,x) with DSolve.
7) Write you own little tune with the synthesizer provided.

If you find something cool during your experiments, feel free to include it also. In order to work on your project it is a good idea to save this notebook first as a different document, do the assignment directly in that notebook and print out the relevant pages at the very end on a printer. The assignments have to be printed out and turned in the last class.

Harvard University, Mathematics 21b Spring, 2007, Oliver Knill

 Created by Mathematica  (April 16, 2007)