% -----------------------------------------------------
% linear1.m
% -----------------------------------------------------
% Solve a set of linear algebraic equations. 
% -----------------------------------------------------
% Notes: Use Gaussian Elimination. 
% -----------------------------------------------------
% Copyright and license details available at matlabmarina.com
%------------------------------------------------------

% clear all variables and the command window
clear 
clc

% Define the system of equations
A = [3.0, -0.1, -0.2; 
    0.1, 7.0, -0.3; 
    0.3, -0.2, 10.0];
b = [7.85; -19.3; 71.4];

% Solve for x in Ax=b using Gaussian Elimination
x = A\b;

% Extracting the results/unknowns
x1 = x(1);
x2 = x(2);
x3 = x(3);

% Verification: b_verify should be the same as b
b_verify = A*x;

% Displaying the results/unknowns
for k=1:1:length(x)
    
    fprintf('x%i = %.2f\n',k,x(k))
    
end
fprintf('\n')
fprintf('Verifying b = b_verify\n')

for k=1:1:length(b)
    
    fprintf('b%i = %.2f\n',k,b(k))
    fprintf('b_verify%i = %.2f\n\n',k,b_verify(k))
end