Exchanging information between function, gradient, and Hessian
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello there
I have a constrained nonlinear optimzation problem. Denote the objective, constraints, and their respective gradient vectors and Hessian matricies as
.
The Hessian of the Lagrangian function is .
The gradient vectors of the objective and constraints functions are given in the (conditionalized) output as
function [f,df] = myObj(x)
f; %compute f
if nargout > 1
df; %compute df
end
end
function [g,dg] = myConst(x)
g(k); % compute g_k
if nargout > 1
dg(k); % compute dg_k
end
end
NOTE: The gradient vectors are only calculated when fmincon needs to evaluate them
Fmicon requests that the Hessian is supplied via a seperate function,
function [H] = myHess(x,lam)
H; %compute H
end
However, in the case of my problem, the computation of H requires the gradient vectors Obviously, I don't want to recompute them inside myHess. Instead, I want to pass the information to myHess as
function [H] = myHess(x,lam,df,dg)
H; %compute H using df and dg
end
My Question: How can I efficiently use the results of df and dg inside myHess?
I have seen the page on using nested functions: https://ch.mathworks.com/help/optim/ug/objective-and-nonlinear-constraints-in-the-same-function.html. However, here they did not show what to do when I have conditionalized outputs.
Thanks a lot!
댓글 수: 0
채택된 답변
추가 답변 (1개)
Sai Kiran
2023년 3월 8일
Hi,
As per my understanding you dont want to recompute the df,dg. You can directly pass the functions myObj & myConst in the myHess function to get the df, dg .
Please follow the below example for better understanding.
myHess function uses the func1 and func2 outputs.
function [H] = myHess(a,b)
H=func1(a)+func2(b);
end
function [a]=func1(x)
a=x*x;
end
function [b]=func2(y)
b=y*y;
end
Name the matlab file as myHess.m and use the myHess function.
I hope it helps!
Thanks.
참고 항목
카테고리
Help Center 및 File Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!