Error using fmincon function

6 vues (au cours des 30 derniers jours)
Nazia
Nazia le 25 Avr 2024
Commenté : Steven Lord le 25 Avr 2024
% Define constants for Antoine's equation
A = 8.07131;
B = 1730.63;
C = 233.426;
% Define the operating temperature at the outlet of the evaporator (in Celsius)
T = 70; % You can adjust this value based on the actual temperature
% Define the partial pressure of water at the outlet of the evaporator (kPa)
Pi = 30;
% Define the mass flow rate of seawater entering the evaporator (in kg/s)
Fseawater = 0.020; % kg/s
% Define the objective function to maximize water recovery
objective = @(x_vap) -calculate_water_recovery(x_vap, A, B, C, T, Pi, Fseawater);
% Define lower and upper bounds for the decision variable (mole fraction of water in vapor phase)
lb = 0; % Lower bound
ub = 1; % Upper bound
constraint = @(x) 20 - calculate_water_recovery(x, A, B, C, T, Pi, Fseawater);
% Perform optimization
options = optimoptions('fmincon', 'Display', 'iter');
[x_vap_opt, ~, exitflag] = fmincon(objective, 0.5, [], [], [], [], lb, ub, [], [], options);
Error using solution>@(x_vap)-calculate_water_recovery(x_vap,A,B,C,T,Pi,Fseawater)
Too many input arguments.

Error in objfunEvaluator (line 5)
fval = feval(Objfun, x, self.FunArgs.AdditionalParameters{:});

Error in OptimFunctions/objective (line 271)
[fval_, fgrad_, hess_] = self.ObjectiveFunAndGrad(self,self.FunFcn{3},...

Error in OptimFunctions/objective_first_eval (line 614)
[fval,self] = self.objective(X0);

Error in fmincon (line 500)
[initVals.f,initVals.g,HESSIAN,funObj] = funObj.objective_first_eval(X);

Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
if exitflag > 0
% Calculate water recovery and display results
[Lwater, Vwater] = calculate_water_recovery(x_vap_opt, A, B, C, T, Pi, Fseawater);
disp(['Optimal mole fraction of water in vapor phase: ', num2str(x_vap_opt)]);
disp(['Optimal mass flow rate of freshwater product: ', num2str(Vwater), ' kg/s']);
disp(['Optimal percent water recovered: ', num2str(Lwater), '%']);
else
disp('Optimization failed to converge.');
end
function [Lwater, Vwater] = calculate_water_recovery(xi_vap, A, B, C, T, Pi, Fseawater)
% Calculate the saturation vapor pressure using Antoine's equation
Pisat = exp(A - B / (C + T));
% Calculate the mole fraction of water in the liquid phase using Raoult's law
xi_liq = Pi / Pisat;
% Calculate the mole fraction of water in the vapor phase
xi_vap = 1 - xi_liq;
% Calculate the mass flow rate of freshwater product
Vwater = xi_vap * (0.0180153); % kg/s
% Calculate the percent water recovered
Lwater = (1 - (Fseawater - Vwater) / Fseawater) * 100;
end

Réponses (2)

Lokesh
Lokesh le 25 Avr 2024
Modifié(e) : Lokesh le 25 Avr 2024
Hi Nazia,
It appears you are encountering the "Too many input arguments" error while using the 'fmincon' function.
The correct syntax for the 'fmincon' function is as follows: "x = fmincon(fun, x0, A, b, Aeq, beq, lb, ub, nonlcon, options)." Based on your code, it seems that you are passing an extra argument in your function call. To resolve this issue, you should modify your function call from:
%Existing function call
[x_vap_opt, ~, exitflag] = fmincon(objective, 0.5, [], [], [], [], lb, ub, [], [], options);
%Modified function call
[x_vap_opt, ~, exitflag] = fmincon(objective, 0.5, [], [], [], [], lb, ub, [], options);
This adjustment removes the extra argument, aligning the call with the expected syntax and should solve the issue.
Refer to the following MATLAB documentation for more information on 'fmincon':
  2 commentaires
Nazia
Nazia le 25 Avr 2024
Thank you. I also want to include the constraint in my optimization problem, in the modified function call, but that does not work. Do you know how to go about this?
Steven Lord
Steven Lord le 25 Avr 2024
Then you should include constraint instead of the last [] in the call.
[x_vap_opt, ~, exitflag] = fmincon(objective, 0.5, ... % objective function and x0
[], [], [], [], ... % linear inequality and equality constraints
lb, ub, ... % lower and upper bounds
constraint, ... % nonlinear constraints
options); % options

Connectez-vous pour commenter.


Malay Agarwal
Malay Agarwal le 25 Avr 2024
Hi @Nazia,
I understand that you are trying to use the “fmincon” function and are receiving an error.
The error is “Too many input arguments”, which is raised when a function is given more input arguments than it expects.
According to the documentation of “fmincon”, the function accepts at most 10 arguments: https://www.mathworks.com/help/optim/ug/fmincon.html#d126e98025.
In your code, you are passing 11 arguments to the function, which results in the “Too many input arguments” error. Please replace the call to “fmincon” with the following call:
[x_vap_opt, ~, exitflag] = fmincon(objective, 0.5, [], [], [], [], lb, ub, [], options);
This does not raise the error since there are exactly 10 arguments and the code has the following output:
Hope this helps!

Catégories

En savoir plus sur Nonlinear Optimization dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by