I cannot run the "fmincon" solver.
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Matt J
2023년 6월 25일
error = norm(T_gas_outlet_est - T_gas_outlet).^2;
댓글 수: 1
Torsten
2023년 6월 25일
편집: Torsten
2023년 6월 25일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Nonlinear Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!