% LEP 8-6 : Complex Reactions in a Liquid Phase CSTR
%

xguess = [2  1  1  0.1]; % initial guess vector for Ca,Cb,Cc,and Cd
sol=fsolve(@MNLEfun,xguess);
Ca=sol(1,1);
Cb=sol(1,2);
Cc=sol(1,3);
Cd=sol(1,4);
Scd=Cc/(Cd+0.001);
Cao=2;
X=(Cao-Ca)/Cao;
disp('Calculated values of NLE variables are')
Ca 
Cb
Cc
Cd
X
disp('Exiting selectivity is')
Scd

%

function fx = MNLEfun(x); 
Ca = x(1); 
Cb = x(2); 
Cc = x(3); 
Cd = x(4); 
% Explicit equations
k1a = 10; 
k2c = 15; 
Cao = 2; 
Cbo = 2; 
V = 2500; 
v = 100; 
r2c = 0 - (k2c * Ca ^ 2 * Cc ^ 3); 
r1a = 0 - (k1a * Ca * Cb ^ 2); 
r1b = 2 * r1a; 
r2a = 2 / 3 * r2c; 
r1c = 0 - r1a; 
r2d = -1 / 3 * r2c; 
rb = r1b; 
ra = r1a + r2a; 
rc = r1c + r2c; 
rd = r2d; 
Scd = Cc / (Cd + 0.001); 
% Nonlinear equations
fx(1,1) = v * Cao - (v * Ca) + ra * V; 
fx(2,1) = v * Cbo - (v * Cb) + rb * V; 
fx(3,1) = 0 - (v * Cc) + rc * V; 
fx(4,1) = 0 - (v * Cd) + rd * V; 
end

%