How to solve the error occurring when optimize function using 'fminunc'

1 visualización (últimos 30 días)
function [LL] = fn3(par, A, B, Nsample, Z1, VV, SigE_Sol, Ndraw, A_Sol, B_Sol, IU, WR, NN, S)
%, A, B, Nsample, Z1, VV, SigE_Sol, Ndraw, A_Sol, B_Sol,IU, WR, NN, S
strcat('E');
% global A B Nsample Z1 VV SigE_Sol Ndraw A_Sol B_Sol IU WR NN S
A_Sol(A~=0) = par(1:length(A(A~=0))); %Logit L(theta)
B_Sol(B~=0) = par((length(A(A~=0))+1):(length(A(A~=0))+length(B(B~=0))));
LL = 0;
for i = 1:Nsample;
ZR = reshape(repelem(Z1(i,:), Ndraw), [Ndraw, 3])'+(VV((i-1) * Ndraw + 1 + NN : i * Ndraw + NN, :) * chol(SigE_Sol))';
Ut = reshape(repelem(S(i, :) * A_Sol', Ndraw), [Ndraw, 3]) + (B_Sol * ZR(:, 1 : Ndraw))';
Ut = exp(Ut);
Ut = Ut ./ [sum(Ut, 2), sum(Ut, 2), sum(Ut, 2)];
Ut = Ut .* reshape(repelem(IU(i,:), Ndraw), [Ndraw, 3]);
Pr = log(sum(Ut, 2)) .* WR((i-1) * Ndraw + 1 : i * Ndraw);
LL = LL-sum(Pr);
end
end
This code is a part of the EM(Expectation Maximization) algorithm and fn3 function is that i want to optimize. The output of the function is LL. I want to get the optimized value of LL and the par then optimize LL. This is call back function and nonlinear model.
par = horzcat(A_Sol(A_Sol~=0)', B_Sol(B_Sol~=0)');
options = optimoptions(@fminunc, 'MaxIter', 100, 'GradObj', 'on', 'Hessian', 'on');
optfnc = @(par) fn3(par, A, B, Nsample, Z1, VV, SigE_Sol, Ndraw, A_Sol, ...
B_Sol, IU, WR, NN, S);
[optimpar, LL] = fminunc(optfnc, par, options);
Above is the syntax that optimize fn3. From line 1 to 4 is done quite well but the problem is line 5 '[optimpar, LL] = fminunc(optfnc, par, options)'. I got this error message.
Error using fn3
Too many output arguments.
Error in @(par)fn3(par,A,B,Nsample,Z1,VV,SigE_Sol,Ndraw,A_Sol,B_Sol,IU,WR,NN,S)
Error in fminunc (line 279)
[f,GRAD,HESS] = feval(funfcn{3},x,varargin{:});
Error in Corr_SUR_Matlab_Optimize (line 212)
[optimpar, LL] = fminunc(optfnc, par, options);
Caused by:
Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue.
I have researched the reason why this error message is occurred but i couldn't find the solution. This error message have blocked me about two weeks.... Are there some programmers who can help me?

Respuestas (1)

Walter Roberson
Walter Roberson el 4 de Dic. de 2015
You are not providing enough output variables for fn3 considering the options you set.
If you can compute the gradient of fun and the GradObj option is set to 'on', as set by
options = optimoptions('fmincon','GradObj','on')
then fun must return the gradient vector g(x) in the second output argument.
If you can also compute the Hessian matrix and the Hessian option is set to 'on' via options = optimoptions('fmincon','Hessian','user-supplied') and the Algorithm option is trust-region-reflective, fun must return the Hessian value H(x), a symmetric matrix, in a third output argument.
  1 comentario
Alan Weiss
Alan Weiss el 4 de Dic. de 2015
In other words, try running your problem with no options and see what happens.
Alan Weiss
MATLAB mathematical toolbox documentation

Iniciar sesión para comentar.

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by