I cannot run the "fmincon" solver.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am trying to use "fmincon" to solve a problem, but I get the same error, again and again. This is the error:
Error using fmincon
Supplied objective function must return a scalar value.
Error in EGR_model (line 109)
x = fmincon(@(x) objectiveFunction(x, T_gas_outlet, T_coolant, EGR_mass_flow), x0, [], [], [], [], lb, ub, nonlcon, options);
This is my code:
% Given data sets
T_gas_outlet = Resampled_filtered_data(:, 7)';
T_coolant = Resampled_filtered_data(:, 3)';
EGR_mass_flow = Resampled_filtered_data(:, 6)';
% Set initial guess for the unknown parameters
x0 = [600; 2000]'; % Initial guess for the unknown parameters
% Set bounds for the variables
lb_T_gas_inlet = 200; % Lower bound for T_gas_inlet
ub_T_gas_inlet = 800; % Upper bound for T_gas_inlet
lb_Q = 0; % Lower bound for Q
ub_Q = 5000; % Upper bound for Q
lb = [lb_T_gas_inlet; lb_Q]'; % Lower bounds for the variables
ub = [ub_T_gas_inlet; ub_Q]'; % Upper bounds for the variables
% Define the nonlinear constraint function
nonlcon = @(x) inletConstraint(x, T_coolant);
% Solve the optimization problem
options = optimoptions('fmincon', 'Display', 'iter');
x = fmincon(@(x) objectiveFunction(x, T_gas_outlet, T_coolant, EGR_mass_flow), x0, [], [], [], [], lb, ub, nonlcon, options);
% Extract the estimated values
T_gas_inlet = x(1)'; % EGR gas inlet temperature
Q = x(2)'; % Heat transfer rate
% Display the results
fprintf('Estimated EGR Gas Inlet Temperature: %.2f °C\n', T_gas_inlet);
fprintf('Estimated Heat Transfer Rate: %.2f W\n', Q);
% Objective function
function error = objectiveFunction(x, T_gas_outlet, T_coolant, EGR_mass_flow)
specific_heat_capacity_gas = 1005; % Assumed value
T_gas_inlet = x(1)';
Q = x(2)';
% Calculate the estimated outlet temperature
T_gas_outlet_est = T_gas_inlet - (Q ./ (EGR_mass_flow * specific_heat_capacity_gas));
% Calculate the absolute error
error = abs(T_gas_outlet_est - T_gas_outlet);
end
% Nonlinear constraint function (T_gas_inlet >= T_coolant)
function [c, ceq] = inletConstraint(x, T_coolant)
T_gas_inlet = x(1)';
c = T_coolant - T_gas_inlet <= 0; % Inequality constraint (T_gas_inlet >= T_coolant)
ceq = []; % No equality constraints
end
0 Commenti
Risposte (1)
Matt J
il 25 Giu 2023
error = norm(T_gas_outlet_est - T_gas_outlet).^2;
1 Commento
Torsten
il 25 Giu 2023
Modificato: Torsten
il 25 Giu 2023
And "nonlcon" only accepts one input argument. Thus use
x = fmincon(@(x) objectiveFunction(x, T_gas_outlet, T_coolant, EGR_mass_flow), x0, [], [], [], [], lb, ub, @(x)nonlcon(x,T_coolant), options);
...
function [c, ceq] = inletConstraint(x, T_coolant)
T_gas_inlet = x(1)';
c = T_coolant - T_gas_inlet; % Inequality constraint (T_gas_inlet >= T_coolant)
ceq = []; % No equality constraints
end
Vedere anche
Categorie
Scopri di più su Nonlinear Optimization in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!