Problem Set 1:

2. Using the procedure outlined in section 1 of the weekly handout, write a function tocompute the inverse of a well-conditioned full-rank square matrix using co-factors. Your function should be myinverse(A).

A <- matrix(c(2,3,6,6,3,5,5,8,9),nrow=3)
myinverse <- function(A){
  #generate empty I matrix first
  I <- diag(1,nrow(A),ncol(A))
  #iterate over the rows
   for (i in 1:nrow(A)) { 
        #iterate over the columns
        for (j in 1:ncol(A)){ 
          #Calucate the value for each cell in the matrix
          Mmini <- A[-i,-j]
          #-1 raised to a power provides the appropriate signs as the mini det is calculated for the 2x2s
          I[i,j] <- ((-1)^(i+j))*det(Mmini)
        }
   }
  #finish the calculation by dividing by the det of A
  return(t(I)/det(A))
}

#Original Matrix cbind to Inverse matrix B
B=myinverse(A)
A%*%B
##              [,1] [,2]         [,3]
## [1,] 1.000000e+00    0 1.110223e-16
## [2,] 2.220446e-16    1 2.220446e-16
## [3,] 5.551115e-16    0 1.000000e+00
#althought the values are in scinotation you can see the 1's along the diagonal and zero/near zero in the other cells.