Module # 5 Doing Math


> #############################
> # Step 1: Create Matrices
> 
> A <- matrix(1:100, nrow = 10)
> B <- matrix(1:1000, nrow = 10)
> 
> # Check dimensions
> dim(A)
[1] 10 10
> dim(B)
[1]  10 100
> 
> #############################
> # Step 2: Determinant of A
> 
> detA <- det(A)
> print(detA)
[1] 0
> 
> #############################
> # Step 3: Attempt Inverse of A
> 
> invA <- tryCatch(
+   solve(A),
+   error = function(e) e$message
+ )
> print(invA)
[1] "Lapack routine dgesv: system is exactly singular: U[6,6] = 0"
> 
> #############################
> # Step 4: Determinant of B
> 
> detB <- tryCatch(
+   det(B),
+   error = function(e) e$message
+ )
> print(detB)
[1] "'x' must be a square matrix"
> 
> #############################
> # Step 5: Attempt Inverse of B
> 
> invB <- tryCatch(
+   solve(B),
+   error = function(e) e$message
+ )
> print(invB)
[1] "'a' (10 x 100) must be square"
> 
> 
> # Final comment: 
> # Matrix A is square but singular (determinant = 0), so it has no inverse.
> # Matrix B is not square (10×100), so neither its determinant nor inverse is defined.

Comments

Popular posts from this blog

Module # 4 Programming structure assignment

Assignment #10: Building Your Own R Package

Module # 8 Input/Output, string manipulation and plyr package