ax=b for different b ’s a=[-0.3 -0.5 1.2; -0.9 1.3 -1.4 ; -0.5 -1.8 -1.8]he relative error of the right hand side is at most 0.0028 in ∞ -norm calculate to get the large relative error in matlab

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.m
a=[-0.3 -0.5 1.2; -0.9 1.3 -1.4; -0.5 -1.8 -1.8];
b1 = [1; 0; 0]; % first vector
b2 = [0; 1; 0]; % second vector
b3 = [0; 0; 1]; % third vector
144 chars
5 lines

Then, we can easily solve for x using the backslash operator, for each b vector:

main.m
x1 = a\b1;
x2 = a\b2;
x3 = a\b3;
33 chars
4 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.m
r1 = norm(b1 - a*x1, Inf) / norm(b1, Inf);
r2 = norm(b2 - a*x2, Inf) / norm(b2, Inf);
r3 = norm(b3 - a*x3, Inf) / norm(b3, Inf);
129 chars
4 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.m
r_max = max([r1, r2, r3]);
27 chars
2 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.

related categories

gistlibby LogSnag