Exchanging information between function, gradient, and Hessian
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Plus de réponses (1)
Sai Kiran
le 8 Mar 2023
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.
1 commentaire
Voir également
Catégories
En savoir plus sur Systems of Nonlinear Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!