Week 4 MSDA Math Bridge Assignment

Question 1 - Solve By Hand

Question 1

Question 2 - Solve using R

Solution using matrix multiplication

Let’s represent given system of equation as matrix

A<-matrix(c(1,2,-1,2,1,1,-3,-3,0),3)
B<-matrix(c(5,13,-8),3)
A
##      [,1] [,2] [,3]
## [1,]    1    2   -3
## [2,]    2    1   -3
## [3,]   -1    1    0
B
##      [,1]
## [1,]    5
## [2,]   13
## [3,]   -8

Let’s check determinant of A

det(A)
## [1] 0

Determinant of A is zero. So, the vectors of matrix are linearly dependent. Let’s drop a vector and assume value of varaiable (i.e z = 0) and check again

A1<-A[1:2,1:2]
B1<-B[1:2]
det(A1)
## [1] -3

Now, the determinant is non zero. Let’s solve the equation

solve(A1)%*%B1
##      [,1]
## [1,]    7
## [2,]   -1

So, One of the solutions for the given system equation is x=7, y= -1, z=0

Easy solution using lm() function

Let’s solve the equation using lm() function. Construct a dataframe with variables and result

df1<-as.data.frame(cbind(A,B))
df1
##   V1 V2 V3 V4
## 1  1  2 -3  5
## 2  2  1 -3 13
## 3 -1  1  0 -8
lm(V4~.+0,data=df1)
## 
## Call:
## lm(formula = V4 ~ . + 0, data = df1)
## 
## Coefficients:
## V1  V2  V3  
##  7  -1  NA

From coefficients of the lm() function x=7, y=-1 and z=0

Question 3 - Solve By Hand

Question 3

Question 4 - Solve using R

A=matrix(c(4,-3,0,-3,5,1),3)
B=matrix(c(1,3,4,-2),2)
A
##      [,1] [,2]
## [1,]    4   -3
## [2,]   -3    5
## [3,]    0    1
B
##      [,1] [,2]
## [1,]    1    4
## [2,]    3   -2

Matrix Multiplication

A%*%B
##      [,1] [,2]
## [1,]   -5   22
## [2,]   12  -22
## [3,]    3   -2