To solve the equation Ax = b
for different b
vectors, we can use the inverse of A
. However, calculating the inverse of a matrix can be computationally expensive, and it is recommended to use other methods such as matrix factorization or the backslash operator which is more efficient.
Assuming we are allowed to use the backslash operator in MATLAB, we can simply define a
and each b
vector as follows:
main.m144 chars5 lines
Then, we can easily solve for x
using the backslash operator, for each b
vector:
main.m33 chars4 lines
To calculate the relative error of the right-hand side with respect to the infinity norm, we can use the built-in norm
function as follows:
main.m129 chars4 lines
The output variables r1
, r2
, and r3
will contain the relative error for each b
vector.
To get the large relative error, we can simply find the maximum value among the three outputs:
main.m27 chars2 lines
Note that if A
is singular or ill-conditioned, the backslash operator may not give accurate results. In such cases, other methods such as regularization may be needed to obtain reliable solutions.
gistlibby LogSnag