clear; clc;

% a : CH4 flow rate (kmol/hr)
% b : HCN flow rate (kmol/hr)
% d : H2S flow rate (kmol/hr)
% e : Air flow rate (kmol/hr)
% f : CO2 flow rate (kmol/hr)
% g : H2O flow rate (kmol/hr)
% i : SO2 flow rate (kmol/hr)
% j : N2  flow rate (kmol/hr)

% KNOWNS  : b, d, f
% UNKNOWNS: a e g i j

% define the solution vector as
%  [ a  e  g  i  j ]'

% equations:
%   a = f-b
%   4a - 2g = -b - 2d
%   0.42e - g - 2i = 2f
%   1.58e - 2j  = -b
%   i = d

% define the knowns:
b = 50;
d = 100;
f = 500;

% Define the matrix based on our definition of the solution vector.
%     a  e     g  i  j
A = [ 1  0     0  0  0;
      4  0    -2  0  0;
      0  0.42 -1 -2  0;
      0  1.58  0  0 -2;
      0  0     0  1  0 ];

% define the rhs vector (column vector)
rhs = [ f-b; -b-2*d; 2*f; -b; d ];

% solve the system of equations:
x = A\rhs;

% unpack the solution vector.
a = x(1);
e = x(2);
g = x(3);
i = x(4);
j = x(5);

% print out the results
disp('--- REACTANTS ---');
disp('The flow rate of CH4 (kmol/hr) is:');  disp(a);
disp('The flow rate of HCN (kmol/hr) is:');  disp(b);
disp('The flow rate of H2S (kmol/hr) is:');  disp(d);
disp('The flow rate of Air (kmol/hr) is:');  disp(e);
disp(' ');
disp('--- PRODUCTS ---');
disp('The flow rate of CO2 (kmol/hr) is:');  disp(f);
disp('The flow rate of H2O (kmol/hr) is:');  disp(g);
disp('The flow rate of SO2 (kmol/hr) is:');  disp(i);
disp('The flow rate of N2  (kmol/hr) is:');  disp(j);
