Problem using fsolve() for a Chemical equilibrium
9 views (last 30 days)
Show older comments
João Lazari
on 17 Nov 2020
Edited: Walter Roberson
on 17 Nov 2020
Hey all,
I'm trying to solve a chemical equilibrium problem using fsolve(), but I'm having problems making it work. Hope someone here can help me.
I'm interested in obtaining the molar fraction (y) of the spicies (CH4,H2O...) in the equilibrium. At the begining I only have 2 mols of CH4 and 3 mols of H2O, that then react and leave a misture of the five spicies.
For the equations I have:
function Y = fun1(x)
%CH4, H2O --> CH4, H2O, CO, CO2, H2
%"i" [CH4, H2O, CO, CO2, H2]
%"k" [C, H, O]
A = [2,14,3]; %Initial mols of "k"
a = [1, 0, 4; 0, 1, 2; 1, 1, 0; 1, 2, 0; 0, 0, 2]; %Number of "k" atoms in spicie "i"
DelG = [19720, -192420, -200240, -395790, 0]; %Delta G0 @ 1000K (J/mol)
R = 8.314; %J/mol.K
T = 1000; %K
n = x(1:length(a)); % number of mol of specie "i"
Lamb = x(length(a)+1:length(x)); %Lagrange operators
for k=1:length(A) %Atomic balance
for i=1:length(a)
t(i) = n(i)*a(i,k);
AB(k) = sum(t)-A(k);
end
end
for i=1:length(a) %Minimization of "Del G"
for k=1:length(A)
y(i) = n(i)/sum(n);
Eq(i) = (DelG(i)/(R*T)) + log(y(i)) + (sum(Lamb(k)*a(i,k))/(R*T));
end
end
Y = [Eq, AB];
end
For the fsolve() part I have:
n0 = [2, 3, 0, 0, 0]; %initial guess for the number of mols for each spicie
Lamb0 = [0, 0, 0]; %initial guess for the Lagrange operators
x0 = [n0, Lamb0];
[x,fval] = fsolve('fun1', x0, []);
n = x(1:5);
for i=1:length(n)
y(i) = n(i)/sum(n);
end
As a result I keep getting the following message:
Error using trustnleqn (line 28)
Objective function is returning undefined values at initial point. FSOLVE cannot continue.
Error in fsolve (line 368)
[x,FVAL,JACOB,EXITFLAG,OUTPUT,msgData]=...
Error in Questao3 (line 9)
[x,fval] = fsolve('funcaoQ3_2', x0, []);
I've already checked the equations and even tryed to write them 1by1, but the same thing occurs.
Hope someone can help, and thanks in advance.
0 Comments
Accepted Answer
Walter Roberson
on 17 Nov 2020
y(i) = n(i)/sum(n);
Eq(i) = (DelG(i)/(R*T)) + log(y(i)) + (sum(Lamb(k)*a(i,k))/(R*T));
Your first two n values are non-zero but the other 3 are 0. so n(i) will be 0 for i=3 so y(3)=0 and log(y(3)) will be -inf
4 Comments
Walter Roberson
on 17 Nov 2020
Edited: Walter Roberson
on 17 Nov 2020
I recommend against using length(a) . a is a 2D array, and length(a) is defined as:
if isempty(a)
length is 0
else
length is max(size(a))
end
a is 5 x 3 so length() for it should be 5, but it is not clear that is that is the value you are expecting.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!