HW4-Data Science Math

Question 1

Using matrix operations, describe the solutions for the following family of equations:

import Libraries

library(MASS)

Creating the Matrix

A<-matrix(c(1,2,-3,2,1,-3,-1,1,2),nrow=3,ncol=3,byrow=TRUE)

B<-matrix(c(5,13,-8),nrow=3,ncol=1,byrow=TRUE)
A;B
##      [,1] [,2] [,3]
## [1,]    1    2   -3
## [2,]    2    1   -3
## [3,]   -1    1    2

##      [,1]
## [1,]    5
## [2,]   13
## [3,]   -8

a. Find the inverse of the above 3x3 (non-augmented) matrix.

s<-fractions(solve(A))
s
##      [,1] [,2] [,3]
## [1,] -5/6  7/6  1/2
## [2,]  1/6  1/6  1/2
## [3,] -1/2  1/2  1/2

b. Solve for the solution using R.

solve(A,B)
##      [,1]
## [1,]    7
## [2,]   -1
## [3,]    0

c. Modify the 3x3 matrix such that there exists only one non-zero variable in the solution set.

A2=cbind(B,A[,2],A[,3])
A2
##      [,1] [,2] [,3]
## [1,]    5    2   -3
## [2,]   13    1   -3
## [3,]   -8    1    2
A2s<-solve(A2)%*% B
round(A2s,6)
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    0

Question 2

Consider the matrix,q=matrix(c(3,1,4,4,3,3,2,3,2),nrow=3). Let b=c(1,4,5). Use Cramer's rule and R to determine the solution, x, to qx=b, if one exists. Show all determinants.

q=matrix(c(3,1,4,4,3,3,2,3,2),nrow=3)
b=c(1,4,5)
q;b
##      [,1] [,2] [,3]
## [1,]    3    4    2
## [2,]    1    3    3
## [3,]    4    3    2

## [1] 1 4 5

create a gramer's function

gramers<-function(ma,mb,nround_off)
{
  d=det(q)
  dx=det(cbind(mb,ma[,2],ma[,3]))
  dy=det(cbind(ma[,1],mb,ma[,3]))
  dz=det(cbind(ma[,1],ma[,2],mb))
  
  x_val=dx/d
  y_val=dy/d
  z_val=dz/d
  
  result<-c(round(x_val,nround_off),round(y_val,nround_off),round(z_val,nround_off))
  return(result)
}

Solve

gramers(q,b,6)
## [1]  1.461538 -2.538462  3.384615
solve(q,b)
## [1]  1.461538 -2.538462  3.384615