Problem using fsolve() for a Chemical equilibrium
Show older comments
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.
Accepted Answer
More Answers (0)
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!