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:
|
i1−i2+i3 | =0 | | | | | | | | | |
−i1+i2−i3 | =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):
-
A1=A· B, A2=B· A, A3=(A′· B′)′, A4=(B′· A′)′
- A1=A′· B′, A2=(A· B)′, A3=B′· A′, A4=(B· A)′
- A1=inv(A· B), A2=inv(A)· inv(B), A3=inv(B· A), A4=inv(B)· inv(A)
- A1=(inv((A· B)′), A2=inv(A′· B′), A3=inv(A′)· inv(B′),
A4=(inv(A)· inv(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≤ i ≤ n 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.
Volkswagen | Fiat | Ford | Peugeot | Toyota |
|
426 | 436 | 364 | 437 | 336 |
Table 1: Cars traded at time 0 for each car brand. |
| current car (j) |
|
|
new car (i) | Volkswagen | Fiat | Ford | Peugeot | Toyota |
|
Volkswagen | 335 | 717 | 586 | 340 | 104 |
Fiat | 375 | 257 | 409 | 551 | 626 |
Ford | 491 | 43 | 614 | 292 | 445 |
Peugeot | 246 | 383 | 373 | 567 | 649 |
Toyota | 554 | 600 | 18 | 250 | 177 |
|
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
A | = floor (10*rand (n)); |
b | = sum (A′)′; |
z | = ones (n,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
| tic, x=A \ b; toc |
| tic, y=inv(A)*b; toc
|
|
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(x−z)) |
| max(abs(y−z))
|
|
Which method produces the most accurate solution?
- 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.