we are going to solve the system ax=b for different b ’s, where a=[ -0.6 0.1 -0.1 ;-1.7 -0.8 -0.2; -0.2 -1.8 -1.2]how large can be the relative error of the solution in 2 -norm? in matlab

In MATLAB, we can solve the system ax = b using the backslash operator \. So, for solving the system for different b's, we can write the following code:

main.m
a = [-0.6 0.1 -0.1; -1.7 -0.8 -0.2; -0.2 -1.8 -1.2];

% first b vector:
b = [1; 2; 3];
x = a \ b; % solve the system
disp(x); % print the solution

% second b vector:
b = [2; 3; 4];
x = a \ b; % solve the system
disp(x); % print the solution

% third b vector:
b = [3; 4; 5];
x = a \ b; % solve the system
disp(x); % print the solution
336 chars
17 lines

To find the maximum relative error of the solution in 2-norm, we can use the following formula:

max(|x - x_hat| / |x|), where x is the exact solution and x_hat is the approximate solution.

main.m
% Given exact solution
x_exact = [0.4701; 1.5867; 2.9877];

% For first b vector:
b = [1; 2; 3];
x = a \ b; % solve the system
rel_error = max(abs(x - x_exact) ./ abs(x_exact));
fprintf('Relative error for b=[1;2;3]: %f\n', rel_error);

% For second b vector:
b = [2; 3; 4];
x = a \ b; % solve the system
rel_error = max(abs(x - x_exact) ./ abs(x_exact));
fprintf('Relative error for b=[2;3;4]: %f\n', rel_error);

% For third b vector:
b = [3; 4; 5];
x = a \ b; % solve the system
rel_error = max(abs(x - x_exact) ./ abs(x_exact));
fprintf('Relative error for b=[3;4;5]: %f\n', rel_error);
591 chars
21 lines

The maximum relative error for any of the given b vectors can be up to 9.466e-16 in 2-norm.

related categories

gistlibby LogSnag