Why do I get an error using "-" along with the message "too many output arguments" when using fmincon?

1 view (last 30 days)
Hello, I am using fmincon to optimize an objective function via a loop through different values of 2 specific parameters.
The optimization is done by defining a policy function. The policy function iteratively optimizes the objective function "fValue2".
"fValue2" has 2 output arguments viz. the objective function's value, and the gradient of the function.
My aim is to maximize the function. I attach an excerpt relevant to my specific problem below.
%Define policy function that will iteratively solve the optimization problem and return
%values of the choice variables and the corresponding optimized function
%value for each given value of 2 heterogeneity parameters
function [Choices, NPV] = fPolicyFunction(theta1, theta2, vector_eps, vector_p, ...
a, Delta, Gamma, m, beta, , ...
points_p, points_eps, tau1, tau2, tau3)
%Define values of parameters that will be fixed throughout such as tau1,
%tau2, Delta etc, set NaN matrices for Choices and NPV etc..
% Set options for FMINCON
options = optimoptions('fmincon','Algorithm','interior-point','SpecifyObjectiveGradient',true, ...
'HessianFcn', @hessianfcn,'Display','off','AlwaysHonorConstraints','bounds');
%Start Loop
for state_eps = 1:points_eps
% insert code which defines 'Upper Limit' as a function of state_eps
% (omitted here for the sake of brevity)
state_p = 1;
[Choices(:, state_p, state_eps), NPV(state_p, state_eps)] = fmincon(@(choices) -fValue2(state_p, ...
vector_p, state_eps, vector_eps, choices, parameters), StartChoices, [], [], [], [], LowerLimit, ...
UpperLimit, [], options);
end
%End of Loop
When I run the above code, I get the following error:-
Error using -
Too many output arguments.
Error in @(choices)-fValue2(state_p,vector_p,state_eps,vector_eps,choices,parameters)
Error in fmincon (line 577)
[initVals.f,initVals.g] = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
My hunch is that the issue is being caused by the "-fValue2" section of the code. I use a "-" in front of the objective function since I wish to maximize. However, maybe the presence of the second output argument in fValue2 (i.e., its gradient) might be causing a problem? I couldn't figure out any other reason why this error might be showing up.
I would be grateful if anyone could help me in this regard. Happy to provide more clarification/explanations if required. Thank you!

Answers (1)

Matt J
Matt J on 28 Sep 2022
You have not shown us fValue2, but more than likely, it does not specify any output arguments to be returned.
  2 Comments

Sign in to comment.

Categories

Find more on Manual Performance Optimization in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by