This is Using MATLAB:
I am trying to store the solution of this matrix because I wantto do something with the result like find the norm of that answerhowever I am stuck and cannot seem to be able to. Help would beappreciated!
---------------------------------------------
MATLAB CODE:
close all
clear
clc
A = [1 -1 2 -1;
2 -2 2 -3;
1 1 1 0;
1 -1 4 5];
b = [-8 -20 -2 4]';
x = gauss_elim(A,b)
function x = gauss_elim(A, b)
[nrow, ~] = size(A);
nb = length(b);
x = zeros(1,nrow);
% Gaussian elimination
for i = 1:nrow-1
if A(i,i) == 0
t = min(find(A(i+1:nrow,i) ~= 0) + i);
if isempty(t)
disp ('Error: A matrix is singular');
return
end
temp = A(i,:); tb = b(i);
A(i,:) = A(t,:); b(i) = b(t);
A(t,:) = temp; b(t) = tb;
end
for j = i+1:nrow
m = -A(j,i) / A(i,i);
A(j,i) = 0;
A(j,i+1:nrow) = A(j,i+1:nrow) + m*A(i,i+1:nrow);
b(j) = b(j) + m*b(i);
end
end
% Back substitution
x(nrow) = b(nrow) / A(nrow,nrow);
fprintf('Has exact solution:')
for i = nrow-1:-1:1
x(i) = (b(i) - sum(x(i+1:nrow) .* A(i,i+1:nrow))) / A(i,i);
end
end