Error using parameters in fsolve()

2 visualizaciones (últimos 30 días)
João Lazari
João Lazari el 25 de Abr. de 2023
Respondida: Walter Roberson el 25 de Abr. de 2023
Hi everyone,
I'm trying to solve a problem on a binary liquid-liiquid equilibrium using the van Laar equation for the activity coefficient. Through experimental data for the excess enthalpy of mix I've adjusted the A and B parameters. Now I want to calculate the binodal curve.
function b = binodal_vanLaar(x1, x2, T)
% x1 molar fraction of component 1 in fase'
% x2 molar fraction of component 1 in fase "
A = -334.05903 + 2.21014*T - 0.00359*(T^2); %Adjusted from experimental data [298,15 - 325,25K]
B = 0.0000520359*exp(0.03136*T);
gama1_a = exp(A/((1 + ((A*x1)/(B*(1-x1))))^2));
gama2_a = exp(B/((1 + ((B*(1-x1))/(A*x1)))^2));
gama1_b = exp(A/((1 + ((A*x2)/(B*(1-x2))))^2));
gama2_b = exp(B/((1 + ((B*(1-x2))/(A*x2)))^2));
b(1) = x1*gama1_a - x2*gama1_b;
b(2) = (1-x1)*gama2_a - (1-x2)*gama2_b;
end
The idea is to fix the molar fraction of component 1 in fase (') and using fsolve() calculate the molar fraction of component 1 in fase (") and the temperature of equilibrium.
x1 = linspace(0.2800, 0.9700, 1000); %molar fraction of component 1 in fase '
x2 = wrev(x1); %initial guess for the molar fraction of component 1 in fase "
T = 310; %initial guess for the temperature
for i=1:length(x1)
x_a = x1(i);
x_b = x2(i);
fun = @(x_b, T)binodal_vanLaar(x_a, x_b, T);
G2(i,:) = fsolve(fun, [x_b T]);
end
When I try to run a similar code, but with only one variable in the @() it run perfectly ok. But now I'm getting this error mensage.
Not enough input arguments.
Error in teste_binodal_vanLaar>@(x_b,T)binodal_vanLaar(x_a,x_b,T) (line 15)
fun = @(x_b, T)binodal_vanLaar(x_a, x_b, T);
Error in fsolve (line 264)
fuser = feval(funfcn{3},x,varargin{:});
Error in teste_binodal_vanLaar (line 16)
G2(i,:) = fsolve(fun, [x_b T]);
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
Can someone, please, help me understand what I'm doing wrong?
Thanks in advance.

Respuesta aceptada

Walter Roberson
Walter Roberson el 25 de Abr. de 2023
When you use fsolve, the function you pass to fsolve must accept exactly one variable. If you are trying to model multiple model parameters, then put the model parameters all together in one variable and have the code extract from the variable.
For example,
fun = @(x_bT)binodal_vanLaar(x_a, x_bT(1), x_bT(2));

Más respuestas (0)

Productos


Versión

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by