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.
조회 수: 3 (최근 30일)
이전 댓글 표시
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
Rik
2021년 11월 25일
Your edits improved the question already. I still don't see why your assumption is valid. How are you sure that there exists a solution at all?
This will not help anything, but you should remove the "close all, clear all, clc". You're already using a function to keep your workspace clean and you're not using any figures, so closing them is not needed.
채택된 답변
Alan Weiss
2021년 11월 25일
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
You can check whether other solutions might exist by checking if the determinant of A is zero:
det(A)
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!