Hi, I have been trying to carry out the mole balance of certain reactions on excel, but the equations have refused to converge, hence my switch to matlab.

9 views (last 30 days)
I have represented the variables (#moles of certain gases) I want to calculate by x(1) - x(5),
These are the set of equations I formulated. It seems pretty straightforward but I'm not getting the results I desire.
close all, clear all, clc, format compact
x0 = [0,0,0,0,0,0]; %initial guess values
fun = @eqns_J;
options = optimoptions('fsolve','Display','iter','StepTolerance',1e-14,'MaxFunctionEvaluations',5000,'MaxIterations',2000)
x = fsolve(fun,x0,options)
%These are the equations I want to solve, and I don't want any of the solutions to be negative
function F = eqns_J(x);
F(1) = x(3)+x(1)-0.3995;
F(2) = (4*x(1))+(2*x(2))+(2*x(4))-0.6519;
F(3) = x(2)+(2*x(3))-0.03788;
F(4) = x(5)-0.005449;
F(5) = 6.314 - ((x(1)*16.042)+(x(2)*18.016)+(x(3)*44.010)+(x(4)*34.086)+(x(5)*28.020));
end
With this code I'm getting a solution but with a negative x(2).
I saw some suggestions on here that Ican try fmincon and I did, but it still doesn't converge;
function massbalance_const
close all, clear all, clc, format compact
x0 = [0.5;0.1;0.7;0.4;0.1]; %initial guess values
A = [];
b = [];
Aeq = [1 0 1 0 0 ; 4 2 0 2 0; 0 1 2 0 0; 0 0 0 1 0; 0 0 0 0 1]; %equality constraints equations for the mole balances
beq = [0.3995; 0.6519; 0.0379; 0.0055; 0.0054];
lb = zeros(1,5);
ub = inf(1,5);
nonlcon = [];
options = optimoptions('fmincon','Algorithm','sqp','Display','iter','ConstraintTolerance',1e-30,'StepTolerance',1e-30);;
x = fmincon(@fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
function f = fun(x)
f = 6.314-((x(1)*16.042)+(x(2)*18.016)+(x(3)*44.010)+(x(4)*34.086)+(x(5)*28.020));
end
end
I also tried increasing the iterations, but it doesn't converge and the solution it outputs does not satisfy the mole balance equations I have. .
Any suggestions, help and advice on how best I can modify the solution would be really appreciated.
Thank you!
  4 Comments
Chiderah Chukwuka
Chiderah Chukwuka on 1 Dec 2021
Thank you, I tried that but still no convergence. I'm guessing the issue can be traced to the assumptions (data) that I'm using, thanks for pointing that out.

Sign in to comment.

Accepted Answer

Alan Weiss
Alan Weiss on 25 Nov 2021
You have a set of linear equations. You can represent your equations in terms of a matrix A and vector b in the usual framework
% A*x = b
with the following entries:
A = [1 0 1 0 0
4 2 0 2 0
0 1 2 0 0
0 0 0 0 1
16.042 18.016 44.010 34.086 28.020];
b = [0.3995
0.6519
0.03788
0.005449
6.314];
You get the solution using backslash:
x = A\b
x = 5×1
0.2710 -0.2191 0.1285 0.0031 0.0054
You can check whether other solutions might exist by checking if the determinant of A is zero:
det(A)
ans = -256.5600
The determinant is not zero, so the solution is unique. In other words, there is exactly one solution to your system of equations, and it has a negative value of x(2).
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Chiderah Chukwuka
Chiderah Chukwuka on 1 Dec 2021
Thank you. This definitely makes sense, I guess I have to go back to the drawing board and crosscheck all the data I'm using for accuracy.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!