Assignment #5: Matrix Algebra in R

To start this assignment, we will create 2 matrices: 

Then, inspect to verify if they are square:

We can see that A is square, and B is not. To handle errors for non-square or singular matrices, we can use solve() and det().The solve() function calculates the inverse of a matrix, given it is square and non-singular, and det()calculates the determinant of the square. 

Lets use them on A and B :

When we try solve(A), we get an error message. This is because while A is square, it is singular. This means the inverse of the matrix cannot be calculated, this is also why det(A)returns 0. 

For B, we used tryCatch to capture errors, and when we display them, there is also an error message. This is because B is not square, so it does not have an ordinary inverse. 

A matrix must be square to have an ordinary inverse or determinant, but being square does not guarantee that an inverse exists. Matrix A is square but singular, which is why solve(A) cannot calculate an inverse and det(A) is 0. Matrix B is not square, so neither an ordinary inverse nor a determinant can be calculated. Since these matrices are relatively small, performance is not a major concern. However, with larger matrices or matrices that are close to being singular, numerical stability can become more important because small rounding errors can affect the results.

GitHub: https://github.com/morgan8or/r-programming-assignments/blob/main/assignments/assignment-05/assignment_05.R

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *