solving system of non-linear equations for array of variables gives 0x1 syms
1 次查看(过去 30 天)
显示 更早的评论
The code is supposed to output numbers for E1, E2, E3 but gives:
E =
struct with fields:
E1: [0×1 sym]
E2: [0×1 sym]
E3: [0×1 sym]
The third equations for E3 is a dependent equation based on the first two (eqn1-eqn2) if that may be a source of the problem.
Code is as follows:
Ep(0.05)
function E = Ep(P)
%Calculatation of extent of reaction
epsilon = sym('E', [1 3]); %Setting extent of reaction as a variable to solve
N0 = [1; %CO2 %Initial Moles of Each Species
3; %H2
0; %Me
0; %H2O
1];%CO
N = [N0(1) - epsilon(1) - epsilon(2); %CO2 %Moles during reation of each species
N0(2) - 3.*epsilon(1) - epsilon(2) - 2.*epsilon(3); %H2
N0(3) + epsilon(1) + epsilon(3); %Me
N0(4) + epsilon(1) + epsilon(3); %H2O
N0(5) + epsilon(2) - epsilon(3)]; %CO
NT = sum(N0) - 2*epsilon(1) - 2*epsilon(3); %Total moles
y = [N/NT]; %molar ratio of each component
KeqP=[(y(3) .* y(4)) ./ (y(1) * y(2) .^3 .*(P^2));
(y(4) .* y(5)) ./ (y(1) .* y(2));
y(3) ./ (y(5) .*y(2) .^2 .*P)];%Calculating Keq=product(yiP)^new(i)
%Calculation of Keq based on P
R = 8.314/1000; %kJ/(molK)
T = 373;
G = [3.0995.*log(P) - 23.038; %co2
0; %h2
3.0864.*log(P) - 3.8754; %ch3oh
3.0971.*log(P) - 1.3816; %h2o
3.1013.*log(P) - 29.689;]; %co
deltaGrxn = [-G(1)-3.*G(2)+G(3)+G(4); %-CO2-3H2+Me+H2O
-G(1)-G(2)+G(4)+G(5); %-Co2-H2+H2O+CO
-G(5)-2.*G(2)+G(3)]; %-CO-2H2O+Me
KeqT=[exp(-deltaGrxn(1)/(R*T));
exp(-deltaGrxn(2)/(R*T));
exp(-deltaGrxn(3)/(R*T))];
KeqPstandard = 0 == KeqP - KeqT%Relationship of thermodynamic and kinetic representations of Keq
E = solve(KeqPstandard, epsilon)
end
1 个评论
Walter Roberson
2022-12-6
KeqPstandard = 0 == KeqP - KeqT
if you stop the code right after that line, and you do
sol = vpasolve(vpa(rhs(KeqPstandard)))
then you get a series of 24 solutions. But if you substitute the solutions back into KeqPstandard, they turn out not to be good solutions to the real problem even if they might be acceptable to the approximated problem.
If you used vpasolve(rhs(KeqPstandard)) then you get a single solution that is [0 0.3 0] -- which gives a division by 0 when you substitute it back.
回答(1 个)
Torsten
2022-12-6
The numerical solver does not find a solution.
Are you sure a solution exists ?
epsilon0 = rand(3,1);
P = 0.05;
options = optimset('MaxFunEvals',1000000,'MaxIter',1000000);
epsilon = fsolve(@(epsilon)Ep(epsilon,P),epsilon0,options);
norm(Ep(epsilon,P))
epsilon=epsilon.^2
function KeqPstandard = Ep(epsilon,P)
epsilon = epsilon.^2;
%Calculatation of extent of reaction
%epsilon = sym('E', [1 3]); %Setting extent of reaction as a variable to solve
N0 = [1; %CO2 %Initial Moles of Each Species
3; %H2
0; %Me
0; %H2O
1];%CO
N = [N0(1) - epsilon(1) - epsilon(2); %CO2 %Moles during reation of each species
N0(2) - 3.*epsilon(1) - epsilon(2) - 2.*epsilon(3); %H2
N0(3) + epsilon(1) + epsilon(3); %Me
N0(4) + epsilon(1) + epsilon(3); %H2O
N0(5) + epsilon(2) - epsilon(3)]; %CO
NT = sum(N0) - 2*epsilon(1) - 2*epsilon(3); %Total moles
y = [N/NT]; %molar ratio of each component
KeqP=[(y(3) .* y(4)) ./ (y(1) * y(2) .^3 .*(P^2));
(y(4) .* y(5)) ./ (y(1) .* y(2));
y(3) ./ (y(5) .*y(2) .^2 .*P)];%Calculating Keq=product(yiP)^new(i)
%Calculation of Keq based on P
R = 8.314/1000; %kJ/(molK)
T = 373;
G = [3.0995.*log(P) - 23.038; %co2
0; %h2
3.0864.*log(P) - 3.8754; %ch3oh
3.0971.*log(P) - 1.3816; %h2o
3.1013.*log(P) - 29.689;]; %co
deltaGrxn = [-G(1)-3.*G(2)+G(3)+G(4); %-CO2-3H2+Me+H2O
-G(1)-G(2)+G(4)+G(5); %-Co2-H2+H2O+CO
-G(5)-2.*G(2)+G(3)]; %-CO-2H2O+Me
KeqT=[exp(-deltaGrxn(1)/(R*T));
exp(-deltaGrxn(2)/(R*T));
exp(-deltaGrxn(3)/(R*T))];
KeqPstandard = KeqP - KeqT;%Relationship of thermodynamic and kinetic representations of Keq
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!