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!