Lab 1 Sol
Lab 1 Sol
Workshop questions
1. Show that X T X is a symmetric matrix.
Solution:
(X T X)T = X T (X T )T = X T X.
2. (a) Let
a b
A=
c d
be a nonsingular 2 × 2 matrix. Show by direct multiplication that
−1 1 d −b
A = .
ad − bc −c a
Solution:
1 d −b a b 1 da − bc db − bd
= = I.
ad − bc −c a c d ad − bc −ca + ac −cb + ad
3. Let
−6 3 0
A=
1 −1 1
and
0 6 −1
B= 8 −1 0
0 2 3
(a) Find the product AB
Solution:
24 −39 6
AB =
−8 9 2
(b) Does BA exist?
Solution: No, the dimensions do not match
(c) Can we calculate B T AT ? If so, what is it? Solution:
24 −8
B T AT = (AB)T = −39 9
6 2
T T
4. Let x = 1 6 −4 2 and y = 2 −1 −1 0
1
(b) Are x and y orthogonal ?
Solution: Yes, because xT y = 0
5. Is
1 1 −1 −1
1 −1 1 −1
X=
1 −1 −1 1
1 1 1 1
orthogonal? If not, what value of c makes the matrix cX orthogonal?
Solution: The matrix is not orthogonal, as its columns do not form an orthonormal set (e.g. the
first column has norm > 1). However they do form an orthogonal set, so we can just normalise
each vector to produce an orthogonal matrix. This gives c = 21 .
6. For the following matrices, find the eigenvalues and eigenvectors
1 2
(a)
0 −1
Solution: To find the eigenvalues, we solve the equation
1−λ 2
= (1 − λ)(−1 − λ) = 0.
0 −1 − λ
This gives λ = 1 and λ = −1
To find the eigenvector associated with eigenvalue 1, we solve the system of equation
1 2 x1 x1
= .
0 −1 x2 x2
1
This implies x2 = 0 and x1 can be any value. One solution is x = .
0
1
Similarly, the eigenvector associated with eigenvalue -1 is x =
−1
2 1
(b) Solution: The eigenvalues are λ = 1 and λ = 3
1 2
1
The eigenvector associated with λ = 1 is x = .
−1
1
The eigenvector associated with λ = 3 is x = .
1
Practical exercises
The following are taken from Chapter 2 of spuRs (Introduction to Scientific Programming and Simulation
Using R).
1. Give R assignment statements that set the variable z to
b
(a) xa
(b) (xa )b
(c) 3x3 + 2x2 + 6x + 1 (try to minimise the number of operations required)
(d) the second-to-last digit of x before the decimal point (hint: use floor(x) and/or %%)
(e) z + 1
Solution:
> x <- 123
> a <- 1.1
> b <- 1.2
> # a
> (z <- x^(a^b))
2
[1] 220.3624
[1] 220.3624
> # b
> (z <- (x^a)^b)
[1] 573.6867
> # c
> (z <- 3*x^3 + 2*x^2 + 6*x + 1) #8 operations
[1] 5613598
[1] 5613598
[1] 5613598
> # d
> y <- abs(x)
> (z <- (y %% 100 - y %% 10)/10)
[1] 2
[1] 2
> # e
> (z <- z + 1)
[1] 3
[1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
> # b
> rep(1:5, 1:5)
[1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
3
> # c
> matrix(1, 3, 3) - diag(3)
> # d
> matrix(c(0,0,7, 2,5,0, 3,0,0), 3, 3)
3. Use R to produce a vector containing all integers from 1 to 100 that are not divisible by 2, 3, or 7.
Solution:
[1] 1 5 11 13 17 19 23 25 29 31 37 41 43 47 53 55 59 61 65 67 71 73 79 83 85
[26] 89 95 97
4. Which of the following assignments will be successful? What will the vectors x, y, and z look like
at each stage?
rm(list = ls())
x <- 1
x[3] <- 3
y <- c()
y[2] <- 2
y[3] <- y[1]
y[2] <- y[4]
z[1] <- 0
5. Build a 10 × 10 identity matrix. Then make all the non-zero elements 5. Do this latter step in at
least two different ways.
Solution: