Error using fmincon function
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
% 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);
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
0 Kommentare
Antworten (2)
Lokesh
am 25 Apr. 2024
Bearbeitet: Lokesh
am 25 Apr. 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 Kommentare
Steven Lord
am 25 Apr. 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
Malay Agarwal
am 25 Apr. 2024
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!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Nonlinear Optimization finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!