4 Nonlinear equations solve for 4 variables
Show older comments
%script [ error- fsolve stopped because the problem appears to be locally singular.]
%solving E1,E2,E3,Noi
%Initial guesses
%E1o = 0.00009;
%E2o = 0.00009;
%E3o = 0.0009;
%Noio = 0.1;
X0 = [E1o; E2o; E3o; Noio];
%call fsolve function
%Call fsolve function
fhandle = @fsolve_function;
opts=optimoptions('fsolve','Display','iter','TolFun',1e-30,'TolX',1e-30);
[X,fval,exitflag] = fsolve (fhandle, X0,opts);
disp ('E1, E2, E3, Noi')
%equilibrium composition- function
function F=fsolve_function(X)
%unpack variables
E1=X(1);
E2=X(2);
E3=X(3);
Noi=X(4);%initial value of oxygen
K1=4.669E20;
K2=1.29;
K3=3.44E19;
yo2eq=0.00906;%atm
%Initial guesses
E1o = 0.00009;
E2o = 0.00009;
E3o = 0.0009;
Noio = 0.1;
%input equations
eq1=((2*E1-E2)^2/((Noi-E1)*(Noi+E1-E2+2*E3)))-K1;
eq2=(E2*(E2-2*E3)/(2*E1-E2)/(2*E3-E2))-K2;
eq3=((2*E3-E2)^2*(Noi+E1-E2+2*E3)/(E2-2*E3)^2/(Noi-E1))-K3;
eq4=((Noi-E1-E3)/(Noi+E1-E2+2*E3))-yo2eq;
F=[eq1; eq2; eq3; eq4; ];
end
Answers (1)
%Initial guesses
E1o = 0.00009;
E2o = 0.00009;
E3o = 0.0009;
Noio = 0.1;
X0 = [E1o; E2o; E3o; Noio];
%call fsolve function
%Call fsolve function
fhandle = @fsolve_function;
opts=optimoptions('fsolve','Display','iter','TolFun',1e-30,'TolX',1e-30);
[X,fval,exitflag] = fsolve (fhandle, X0,opts)
disp ('E1, E2, E3, Noi')
%equilibrium composition- function
function F=fsolve_function(X)
%unpack variables
E1=X(1);
E2=X(2);
E3=X(3);
Noi=X(4);%initial value of oxygen
K1=4.669E20;
K2=1.29;
K3=3.44E19;
yo2eq=0.00906;%atm
%input equations
eq1=(2*E1-E2)^2-K1*(Noi-E1)*(Noi+E1-E2+2*E3);
eq2=E2*(E2-2*E3)-K2*(2*E1-E2)*(2*E3-E2);
eq3=(2*E3-E2)^2*(Noi+E1-E2+2*E3)-K3*(E2-2*E3)^2*(Noi-E1);
eq4=(Noi-E1-E3)-yo2eq*(Noi+E1-E2+2*E3);
F=[eq1; eq2; eq3; eq4; ];
end
2 Comments
Mahe Rukh
on 4 Sep 2022
fsolve stopped because the relative size of the current step is less than the
value of the step size tolerance squared and the vector of function values
is near zero as measured by the value of the function tolerance.
This is a message of success - fsolve found a solution.
How to solve this? and all the variables should have poisitve values.
If you need positive solutions, try different initial values or straight away "MultiStart" with the following code.
%Initial guesses
E1o = 0.00009;
E2o = 0.00009;
E3o = 0.0009;
Noio = 0.1;
X0 = [E1o; E2o; E3o; Noio];
X0 = sqrt(X0);
%Call fsolve function
fhandle = @fsolve_function;
opts=optimoptions('fsolve','Display','iter','TolFun',1e-30,'TolX',1e-30);
[X,fval,exitflag] = fsolve (fhandle, X0,opts)
norm(fsolve_function(X))
X = X.^2
%equilibrium composition- function
function F=fsolve_function(X)
%unpack variables
E1=X(1)^2;
E2=X(2)^2;
E3=X(3)^2;
Noi=X(4)^2;%initial value of oxygen
K1=4.669E20;
K2=1.29;
K3=3.44E19;
yo2eq=0.00906;%atm
%input equations
eq1=(2*E1-E2)^2-K1*(Noi-E1)*(Noi+E1-E2+2*E3);
eq2=E2*(E2-2*E3)-K2*(2*E1-E2)*(2*E3-E2);
eq3=(2*E3-E2)^2*(Noi+E1-E2+2*E3)-K3*(E2-2*E3)^2*(Noi-E1);
eq4=(Noi-E1-E3)-yo2eq*(Noi+E1-E2+2*E3);
F=[eq1; eq2; eq3; eq4; ];
end
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!