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

2 visualizzazioni (ultimi 30 giorni)
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!

Risposte (1)

Matt J
Matt J il 28 Set 2022
You have not shown us fValue2, but more than likely, it does not specify any output arguments to be returned.
  2 Commenti
Vikramsinh
Vikramsinh il 28 Set 2022
Hello,
Thanks so much for your response. I attach below code pertaining to fValue2 for your reference.
function[NPVCashFlow,g] = fValue2(state_p,vector_p,state_eps,vector_eps,choices,parameters)
%insert code which defines parameter values for beta, delta, p, theta etc...
%define NPV
NPVCashFlow = -p .* Y - Phi - (KHigh + KLow) + beta .* ((fOutput(KHigh, a, theta1, epsilon) - ...
TaxLiabilityHigh - ShiftingCostHigh + (1 - Delta) .* KHigh) + ...
(fOutput(KLow, a, theta2, epsilon) - TaxLiabilityLow - ShiftingCostLow ...
+ (1 - Delta) .* KLow));
if nargout>1 %gradient required
%Y_FOC
Y_FOC = -p + ((beta.*m.*(ShiftingCostHigh.*TrueBaseHigh + ShiftingCostLow.*TrueBaseLow))./Y);
%KHigh_FOC
KHigh_FOC = -1 + beta + beta.*(F_KHigh - Delta).*(1-TauHigh.*(1-AlphaHigh)-TauX.*AlphaHigh ...
- ShiftingCostHigh - TrueBaseHigh.*CostBHigh);
%KLow_FOC
KLow_FOC = -1 + beta + beta.*(F_KLow - Delta).*(1-TauLow.*(1-AlphaLow)-TauX.*AlphaLow - ...
ShiftingCostLow - TrueBaseLow.*CostBLow);
g = [KHigh_FOC;KLow_FOC;Y_FOC]' ;
end
return

Accedi per commentare.

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by