# a. Define the matrices
A = matrix(c(5, -1, 5, -1, 10, 1, 5, 1, -3), nrow = 3, byrow = TRUE)
B = matrix(c(5, 3, -2, 1, 1, 0), nrow = 2, byrow = TRUE)
C = matrix(c(2, -3, 1, 4), nrow = 2, byrow = TRUE)
x = matrix(c(5, 1, 4), nrow = 3, byrow = FALSE)
# b.Compute BA
BA = B %*% A
BA
## [,1] [,2] [,3]
## [1,] 12 23 34
## [2,] 4 9 6
# c.Matrix multiplication that doesn't work
tryCatch({
AB = A %*% B # This multiplication will fail
print(AB) # If no error occurs (unexpected), print the result
}, error = function(e) {
print("Matrix multiplication AB is not possible due to incompatible dimensions.")
print(e) # Optional: Display the error message for debugging
})
## [1] "Matrix multiplication AB is not possible due to incompatible dimensions."
## <simpleError in A %*% B: non-conformable arguments>
# d.Compute inner product
x_T_x = t(x) %*% x
x_T_x
## [,1]
## [1,] 42
# e.Solve A.y = x
y = solve(A, x)
y
## [,1]
## [1,] 0.8665049
## [2,] 0.1699029
## [3,] 0.1674757
# f.Compute L2 norm of A
L2_norm_A = max(svd(A)$d)
L2_norm_A
## [1] 10.19274
# g.Compute the 2-condition number of A
condition_number = max(svd(A)$d) / min(svd(A)$d)
condition_number
## [1] 1.847177