Failure in initial objective function evaluation. FMINUNC cannot continue. Need some help

조회 수: 5 (최근 30일)
It's my first time to use fminunc solver. R_pq,array_x,array_y,M,N,R00 are constants I have set.
x0 = -pi + (pi+pi)*rand(M,N);
options = optimoptions(@fminunc,'Algorithm','trust-region','SpecifyObjectiveGradient',true);
[x,fval,output]=fminunc(@(x) error_fun(x,R_pq,array_x,array_y,M,N,R00),x0,options);
Without options, the code can function without error. But after I set the options, it shows
Error using error_fun
Too many output arguments.
Error in main_beamshaping_336_XZLZ_AMM_phaseonly>@(x)error_fun(x,R_pq,array_x,array_y,M,N,R00)
(line 128)
[x,fval,output]=fminunc(@(x) error_fun(x,R_pq,array_x,array_y,M,N,R00),x0,options);
Error in fminunc (line 303)
[f,GRAD] = feval(funfcn{3},x,varargin{:});
Error in main_beamshaping_336_XZLZ_AMM_phaseonly (line 128)
[x,fval,output]=fminunc(@(x) error_fun(x,R_pq,array_x,array_y,M,N,R00),x0,options);
Caused by:
Failure in initial objective function evaluation. FMINUNC cannot continue.
I am confused about what is wrong. I would be very grateful indeed for any help

답변 (2개)

Matt J
Matt J 2020년 9월 22일
편집: Matt J 2020년 9월 22일
You have set SpecifyObjectiveGradient=true, but your objective function code does not provide a second output for your gradient calculation.

Walter Roberson
Walter Roberson 2020년 9월 22일
When you have the option SpecifyObjectiveGradient, then your function must be able to return the gradient as well as the objective value. It should check nargout and only set the second output if nargout > 1
When you use that option, your nonlinear constraint function (if any) should have two extra outputs. See https://www.mathworks.com/help/optim/ug/nonlinear-constraints.html#bskkri8 "Including Gradients in Constraint Functions"

카테고리

Help CenterFile Exchange에서 Nonlinear Optimization에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by