MATLAB Answers

values of equation in last iteration in fmincon

2 views (last 30 days)
Takis Tsoukalas
Takis Tsoukalas on 14 Apr 2019
Edited: Matt J on 15 Apr 2019
I optimize a problem maximizing EU1 with the fmincon solver..the code below is the objective function i insert in fmincon instruction ...
can i somehow get the value of xw1 equation in last itteration??
function EU1 = eq_obj1(LW_ini,LG_obs,LVmat,LLand,rc,X)
EU1=0;
xw1=0;
for i=1:1:length(rc)
if (LLand(rc(i)))
xw1=xw1+LG_obs(rc(i))*X(i);
end
end
xw1=LW_ini+xw1;
% xw1
% class(xw1)
for i=1:1:length(rc)
for j=1:1:length(rc)
EU1=EU1+LVmat(i,j)*X(i)*X(j);
end
end
EU1=xw1-0.5*EU1/xw1;
%EU1=-EU1;
xw1;

  0 Comments

Sign in to comment.

Accepted Answer

A. Sawas
A. Sawas on 14 Apr 2019
After the fmincon is run and you got the solution vector, make a call to the objective function using the solution vector but in this time add/enable the code in the objective function to display the values you want.
For example assuming you called fmincon like this:
eq_obj = @(x)eq_obj1(LW_ini,LG_obs,LVmat,LLand,rc,x);
[x] = fmincon(eq_obj,x0,A,b);
Edit the objective function to display the value of xw1; then call the objective function:
eq_obj(x);

  3 Comments

Takis Tsoukalas
Takis Tsoukalas on 14 Apr 2019
first of all thanks the quick response..if i display xw1 in the objective function then i am gonna get its value for all iterations for fmincon and one more time for running eq_obj(x);
Catalytic
Catalytic on 14 Apr 2019
Instead of displaying xw1, provide the option of returning it
function [EU1,xw1] = eq_obj1(LW_ini,LG_obs,LVmat,LLand,rc,X)
A. Sawas
A. Sawas on 15 Apr 2019
Catalytic : I agree that will be a better workaround.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 14 Apr 2019
Edited: Matt J on 14 Apr 2019
The computation of xw1 is a trivial one-liner. Just compute it separately from the optimal X given to you by fmincon,
xw1=sum( LG_obs(rc).*LLand(rc).*Xoptimal) + LW_ini

  3 Comments

Matt J
Matt J on 14 Apr 2019
Naturally also, i would encourage you to simplify your objective function in a similar way to make it more efficient
function EU1 = eq_obj1(LW_ini,LG_obs,LVmat,LLand,rc,X)
xw1=sum( LG_obs(rc).*LLand(rc).*X) + LW_ini;
EU1=xw1-0.5*(X(:).'*LVmat*X(:))/xw1;
end
A. Sawas
A. Sawas on 15 Apr 2019
The only drawback I guess of computing the value of xw1 outside the objective fuction will be the need to edit two locations if the equation has changed.
Matt J
Matt J on 15 Apr 2019
Then create a dedicated function to compute xw1 and call that function in the objective as well as everywhere else xw1 is needed,
function xw1=getxw1(LW_ini,LG_obs,LVmat,LLand,rc,X);
xw1=sum( LG_obs(rc).*LLand(rc).*X) + LW_ini;
end
function EU1 = eq_obj1(LW_ini,LG_obs,LVmat,LLand,rc,X)
xw1=getxw1(LW_ini,LG_obs,LVmat,LLand,rc,X);
EU1=xw1-0.5*(X(:).'*LVmat*X(:))/xw1;
end

Sign in to comment.

Sign in to answer this question.