FF505 – Computational Science
Week 3, Spring 2016 [pdf format]

Before you approach these exercises you are expected to have worked at the exercises from the slides. In particular, the creation and maninpulation of a file in Linux, the creation of a specific matrix and the creation, saving and execution of script.

Work in small groups (eg, two persons) at the computer to solve the following exercises.

1  Solving systems of linear equations

Use Matlab to solve the two systems of linear equations derived in class for the examples on electrical circuits and chemical equations:

     
i1i2+i3=0         
i1+i2i3=0         
4i1+2i2=8         
2i2+5i3=9         
           
     
x1=6x4         
2x1+x2=2x3+6x4         
2x2=12x4          

2  Matrix operations

Use MATLAB to generate random 4× 4 matrices A=rand(4) and B=rand(4). For each of the following, compute A1, A2, A3, and A4 as indicated and determine which of the matrices are equal (you can use MATLAB to test whether two matrices are equal by computing their difference):

  1. A1=A· B, A2=B· A, A3=(A′· B′)′, A4=(B′· A′)′
  2. A1=A′· B′, A2=(A· B)′, A3=B′· A′, A4=(B· A)′
  3. A1=inv(A· B), A2=inv(Ainv(B), A3=inv(B· A), A4=inv(Binv(A)
  4. A1=(inv((A· B)′), A2=inv(A′· B′), A3=inv(A′)· inv(B′),
    A4=(inv(Ainv(B))′

Compare the results with the ones you would instead achive using the Matlab-sepcific, array operations, such as .*.

3  An application of matrix calculus

Car owners occasionally trade in their used car for a new car and marketing people are interested in the following type of question: Assume that you own a Citroen. Will your next car be another Citroen or a Volkswagen? Customers’ choices eventually determine the market share of different brands. Car dealers need estimates of how the market share of their brand (or brands) will change as a function of time. The problem can be dealt with by matrix algebra, and ends up being an exercise in matrix multiplication.

Let the index i, 1≤ in denote car brands, in alphabetic order. That is, 1 is Alfa Romeo, 2 is Aston Martin, 3 is Bentley, and n is Toyota. Let also t=1,2, … denote time, measured in years from an arbitrary initial time, and let Fj(t) be the fraction of cars of type j traded in year t. Assume for simplicity that car owners trade their cars in every year1 and let Cij be the fraction of cars of brand j that are traded in for a new car of brand i.

As an example you will use the following data from Table 1 and 2 that are also available in electronic form from this xls file or this txt file2. (We limit ourselves to n=5 because the matrices we then have are easily visualized on a screen, there are of course more car brands). The data refer to the absolute numbers, divided by 1000, from which fractions can be derived. We will assume that the distribution of the next outcome depends only on the previous outcome and that the trading fractions are constant over time.


VolkswagenFiatFordPeugeotToyota
426436364437336
Table 1: Cars traded at time 0 for each car brand.


current car (j)
 
new car (i)VolkswagenFiatFordPeugeotToyota
Volkswagen335717586340104
Fiat375257409551626
Ford49143614292445
Peugeot246383373567649
Toyota55460018250177
Table 2: Cars traded from a brand to another.

4  Efficiency

Set n=200 and generate an n × n matrix and two vectors in ℝn, both having integer entries, by setting

Afloor (10*rand (n)); 
bsum (A′)′; 
zones (n,1)
  1. The exact solution of the system should be the vector z. Why?. Explain. One could compute the solution in MATLAB using the "\" operation or by computing A−1 and then multiplying A−1 times b. Let us compare these two computational methods for both speed and accuracy. One can use MATLAB’s tic and toc commands to measure the elapsed time for each computation. To do this, use the commands
     ticx=A \ btoc 
     ticy=inv(A)*btoc
    Which method is faster?

    To compare the accuracy of the two methods, we can measure how close the computed solutions x and y are to the exact solution z. Do this with the commands

     max(abs(xz)) 
     max(abs(yz))

    Which method produces the most accurate solution?

  2. Repeat part (a), using n=500 and n=1000.

5  Magic squares

Read the documentation about magic squares doc magic. Are magic squares singular? Use the MATLAB command det(magic(n)) to compute the determinants of the magic squares matrices in the cases n=3,4,…, 10. What seems to be happening? Check the cases n=24 and n=25 to see if the pattern always holds.


1
This is common for fleet owners, but if you think trading a car in every year is a huge waste of money, just change the unit of time to e.g. five years.
2
Tip: In MATLAB the command xlsread can be used to read files in the xls format; the command load to read data in plain text.