Separate fmincon and gradient

2 ビュー (過去 30 日間)
Rustem Devletov
Rustem Devletov 2019 年 4 月 27 日
コメント済み: Rustem Devletov 2019 年 4 月 27 日
I was assingned the following task. The goal is to supply gradient to objective function, but from another file. I don't understand how to implement this.
It should be smth like this:
file 1 - objective function
file 2 - gradient
file 3 constraints
file 4 fmincon
Let's take as an example the rosenbrock function
file 1
function [f,g]= rosenbrockwithgrad(x)
% Calculate objective f
f = 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;
if nargout > 1 % gradient required
g = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1));
200*(x(2)-x(1)^2)];
end
file 2
% here the gradient should go, but for now it's in the file 1. The problem is I have to supply it from this file into file 1
file 3
function [c,ceq] = unitdisk(x)
c = x(1)^2 + x(2)^2 - 1;
ceq = [ ];
file 4
fun = @rosenbrockwithgrad;
x0 = [-1,2];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [-2,-2];
ub = [2,2];
nonlcon = @unitdisk;
options = optimoptions('fmincon','SpecifyObjectiveGradient',true);
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
I'm feeling really really despondent. My deadline is upcoming soon and all my thesis paper will be based on fmincon. If I don't implement the code, I won't be able to proceed further.

採用された回答

Matt J
Matt J 2019 年 4 月 27 日
編集済み: Matt J 2019 年 4 月 27 日
The goal is to supply gradient to objective function, but from another file.
I find this requirement difficult to interpret, but here is my best guess:
file 1
function [f,g]= rosenbrockwithgrad(x)
% Calculate objective f
f = 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;
if nargout > 1 % gradient required
g=rosengrad(x);
end
file 2
function g=rosengrad(x)
g = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1));
200*(x(2)-x(1)^2)];
end
  3 件のコメント
Matt J
Matt J 2019 年 4 月 27 日
Then leave SpecifyObjectiveGradient at its default setting.
Rustem Devletov
Rustem Devletov 2019 年 4 月 27 日
Could you contact me please? I have a personal offer for you. My e-mail rustemdevletov@gmail.com My WhatsApp +79274235999

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by