We show our matrix, followed by its inverse.
Then we show A*A-1 and A-1*A to show that our inverse is valid.
Finally, we show our code to create an inverse matrix.
## [,1] [,2] [,3]
## [1,] 2 2 4
## [2,] -4 6 6
## [3,] 16 -1 -8
## [,1] [,2] [,3]
## [1,] 0.1296296 -0.03703704 0.03703704
## [2,] -0.1975309 0.24691358 0.08641975
## [3,] 0.2839506 -0.10493827 -0.06172840
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
new.matrix<-matrix(c(2,-4,16,2,6,-1,4,6,-8),nrow=3)
inverse.maker<-function(a){
cofactor.matrix<-matrix(nrow=3,ncol=3)
inverse.matrix<-matrix(nrow=3,ncol=3)
k = 1
determinant.temporary<-matrix(nrow=2,ncol=2)
#find minors of the matrix, place in position swapped along diagonal
for(m in 1:3){
for(n in 1:3){
for (i in 1:3){
for (j in 1:3){
if(i==m | j==n){}else{
determinant.temporary[k]<-a[j,i]
det(a)
k<-k+1
if(k==5){
cofactor.matrix[m,n]<-det(determinant.temporary)
k=1}
}}}}}
#switch "checkerboard" to negative
cofactor.matrix[1,2]<--cofactor.matrix[1,2]
cofactor.matrix[2,1]<--cofactor.matrix[2,1]
cofactor.matrix[2,3]<--cofactor.matrix[2,3]
cofactor.matrix[3,2]<--cofactor.matrix[3,2]
cofactor.matrix
#divide by determinant
determinant.of.matrix=0
for(i in 1:3){determinant.of.matrix<-(determinant.of.matrix+(cofactor.matrix[i,1]*a[1,i])) }
for(i in 1:3){
for(j in 1:3){
inverse.matrix[i,j]<-(cofactor.matrix[i,j]/determinant.of.matrix)
}}
a<-inverse.matrix
return(a)
}
inverse.matrix<-inverse.maker(new.matrix)
new.matrix
inverse.matrix
round(new.matrix%*%inverse.matrix,digits=1)
round(inverse.matrix%*%new.matrix,digits=1)