0% found this document useful (0 votes)
1 views17 pages

Tutorial 1 (Introductory Mathematics in R)

Tutorial 1 solutions (Introductory Mathematics in R)

Uploaded by

flappysgt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views17 pages

Tutorial 1 (Introductory Mathematics in R)

Tutorial 1 solutions (Introductory Mathematics in R)

Uploaded by

flappysgt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MA2401: Introductory Mathematics with R

Tutorial 1
Exercise 1 Question 1  
  1 2 1
1 0 1
Let A = and B = 2 1 0 .
1 2 3
1 1 −1
(a) Definite A in R, filling in the entries by its rows.
> A <- matrix(c(1,0,1,1,2,3),2,3,T)
(b) Definite B in R, filling in the entries by its columns.
> B <- matrix(c(1,2,1,2,1,1,1,0,-1),3,3,F)
(c) Use R to determine the size of AB.
> dim(A%*%B)
[1] 2 3
(d) Compute BA.
It is not defined, since B is (3, 3) and A is (2, 3). If we try to compute it in R, we get
> B%*%A
Error in B %*% A : non-conformable arguments
(e) Compute BAT .
> B%*%t(A)
[,1] [,2]
[1,] 2 8
[2,] 2 4
[3,] 0 0
Exercise 1 Question 2
Let      
1 1 1 1 0 1
A= , B= , C= .
1 1 −1 −1 0 0

(a) Compute AB. Is A or B a zero matrix?


> A <- matrix(c(1,1,1,1),2,2,T)
> B <- matrix(c(1,1,-1,-1),2,2,T)
> A%*%B
[,1] [,2]
[1,] 0 0
[2,] 0 0
The product AB is a 2 × 2 zero matrix. However, neither A nor B is a zero matrix.
(b) Compute C2 = CC.
> C%*%C
[,1] [,2]
[1,] 0 0
[2,] 0 0
C2 = 02×2 is a zero matrix. Such matrices are called nilpotent matrices. A square matrix A is called nilpotent if
there is a nonnegative integer k such that Ak = 0.
Exercise 1 Question 2

Let      
1 1 1 1 0 1
A= , B= , C= .
1 1 −1 −1 0 0

(c) Is BC = CB?
> B%*%C
[,1] [,2]
[1,] 0 1
[2,] 0 -1
> C%*%B
[,1] [,2]
[1,] -1 -1
[2,] 0 0
They are not equal.
Exercise 1 Question 3

(a) Use the seq and rep function to define the following vectors.
(i) v1 = (2, 3, 4).
> v1 <- seq(2,4)

(ii) v2 = (1, 3, 5).


> v2<-seq(1,5,by=2)
or > v2<-seq(1,5,length=3)

(iii) v3 = (1, 1, 1).


> v3<- rep(1,3)
Exercise 1 Question 3

(b) Convert the vectors v1 , v2 , v3 defined in (a) to column vectors, then



(i) define A = v1 v2 v3 ,
There is no need to convert the vectors to column vectors to define A.
> A <- cbind(v1,v2,v3)
(ii) compute v1T v2 and v2 v3T .
> v1 <- matrix(v1); v2 <- matrix(v2); v3 <- matrix(v3)
> t(v1)%*%v2
[,1]
[1,] 31
> v2%*%t(v3)
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 3 3 3
[3,] 5 5 5
Exercise 1 Question 3

(c) Normalize v3 defined in (a) to a unit vector.


The norm square of v3 is 3
> crossprod(v3,v3) #since v3 is now a column vector
[,1]
[1,] 3
So,  
1
v3 1  
=√ 1 .
∥v3 ∥ 3 1
Exercise 1 Question 4(a)


 3x1 + 2x2 − 4x3 = 3
2x1 + 3x2 + 3x3 = 15
5x1 − 3x2 + x3 = 14

> A <- matrix(c(3,2,-4,2,3,3,5,-3,1),3,3,T); b <- matrix(c(3,15,14))


> R <- cbind(A,b); View(R)
> R[2,]=R[2,]-(2/3)*R[1,]; R[3,]=R[3,]-(5/3)*R[1,]
> R[3,] <- R[3,]+(19/5)*R[2,]
> R[3,] <- (5/146)*R[3,]; R[2,] <- (3/5)*R[2,]
> R[2,] <- R[2,]-(17/5)*R[3,]
> R[1,] <- R[1,]-2*R[2,]+4*R[3,]
> R[1,] <- R[1,]/3; R
[,1] [,2] [,3] [,4]
[1,] 1 0 0 3
[2,] 0 1 0 1
[3,] 0 0 1 2
⇒ Unique solution: x = 3, y = 1, z = 2.
Exercise 1 Question 4 (a)

May use rref in R


> A <- matrix(c(3,2,-4,2,3,3,5,-3,1),3,3,T); b <- matrix(c(3,15,14))
> R <- cbind(A,b); rref(R)
[,1] [,2] [,3] [,4]
[1,] 1 0 0 3
[2,] 0 1 0 1
[3,] 0 0 1 2
Exercise 1 Question 4(b)



 2x2 + x3 + 2x4 − x5 = 4
x2 + x4 − x5 = 3


 4x1 + 6x2 + x3 + 4x4 − 3x5 = 8
2x1 + 2x2 + x4 − x5 = 2

> A <- matrix(c(0,2,1,2,-1,0,1,0,1,-1,4,6,1,4,-3,2,2,0,1,-1),4,5,T); b <- matrix(c(4,3,8,2))


> R <- cbind(A,b); fractions(rref(R))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 -1/2 1/2 -2
[2,] 0 1 0 1 -1 3
[3,] 0 0 1 0 1 -2
[4,] 0 0 0 0 0 0
So, we may let x4 = s and x5 = t, be parameters, s, t ∈ R. Then,
1
x1 = −2 + (s − t), x2 = 3 − s + t, x3 = −2 − t, x4 = s, , x5 = t, , s, t ∈ R.
2
Exercise 1 Question 4(b)

Alternatively,
> A <- matrix(c(0,2,1,2,-1,0,1,0,1,-1,4,6,1,4,-3,2,2,0,1,-1),4,5,T); b <- matrix(c(4,3,8,2));
> Solve(A,b,fractions=TRUE)
x1 - 1/2*x4 + 1/2*x5 = -2
x2 + x4 - 1*x5 = 3
x3 + x5 = -2
0 = 0
Exercise 1 Question 4(c)


 x − 4y + 2z = −2
x + 2y − 2z = −3
x − y = 4

> A <- matrix(c(1,-4,2,1,2,-2,1,-1,0),3,3,T); b <- matrix(c(-2,-3,4))


> Solve(A,b,fractions=TRUE)
x1 - 2/3*x3 = -8/3
x2 - 2/3*x3 = -1/6
0 = 13/2
System is inconsistent, no solution.
Exercise 1 Question 5

Consider the following linear system 


 x1 + ax2 + 2x3 = 0
x1 + x3 = 1
x1 + ax3 = 2

for some a ∈ R.
(a) What is the condition on a such that the system has no solution.
(b) What are the conditions on a such that the system has a unique solution? Find the unique solution.
(c) What is the condition on a such that the system has infinitely many solutions? Write down the general solution.
     
1 a 2 0 1 a 2 0 1 a 2 0
R −R1 R −R2
 1 0 1 1  −−2−−→  0 −a −1 1  −−3−−→  0 −a −1 1 
R3 −R1
1 0 a 2 0 −a a−2 2 0 0 a−1 1
Cases to consider: a = 0, a = 1, and otherwise, a ̸= 0, 1.
Exercise 5 Question 5

Case: a = 1. In this case, the augmented matrix becomes


 
1 1 2 0
 0 −1 −1 1 
0 0 0 1

which shows that the system is inconsistent.


Case: a = 0. In this case, the augmented matrix becomes
   
1 0 2 0 1 0 0 2
R1 +2R2
 0 0 −1 1  −−−−→  0 0 −1 1 
R3 −R2
0 0 −1 1 0 0 0 0

which shows that the system has infinitely many solutions. The general solution is

x = 2, y = s, z = −1, , s ∈ R.
Exercise 1 Question 5
Otherwise, if a ̸= 0, 1,
   2   2−a 
1 a 2 0 1 a 0 1−a 1 0 0 1−a
− 1a R2 1 −1 R2 − 1a R3 1 R1 −aR2 1
−−1−−→  0 1 a a
 −−−−−→  0 1 0 1−a
 −−−−→  0 1 0 1−a

R1 −2R2
a−1 R2 0 0 1 1
0 0 1 1
0 0 1 1
a−1 a−1 a−1

In this case, the system has a unique solution


2−a 1 1
x= , y= , z= .
1−a 1−a a−1

▶ System has no solution when a = 1.


▶ System has unique solution when a ̸= 0, 1. The unique solution is

2−a 1 1
x= , y= , z= .
1−a 1−a a−1
▶ System has infinitely many solutions when a = 0. The general solution is

x = 2, y = s, z = −1, , s ∈ R.
Exercise 1 Question 6

   
2 −1 a b
Let A = . Find all the matrices B = such that AB = BA.
2 1 c d
   
2a − c 2b − d 2a + 2b b−a
AB = = BA = if and only if
2a + c 2b + d 2c + 2d d −c

− 2b − c = 0
a + b − d = 0
2a − c − 2d = 0
2b + c = 0
Exercise 1 Question 6

Solving the system,


> A <- matrix(c(0,-2,-1,0,1,1,0,-1,2,0,-1,-2,0,2,1,0),4,4,T);
b <- matrix(numeric(4))
> Solve(A,b,fractions=TRUE)
x1 - 1/2*x3 - 1*x4 = 0
x2 + 1/2*x3 = 0
0 = 0
0 = 0
Thus, a general solution is
1 1
a = s + t, b = − s, c = s, d = t, s, t ∈ R,
2 2
that is  
s/2 + t −s/2
B= .
s t

Remark: Question asked for all matrices B, not enough to give B = A−1 or I, or 0.

You might also like