MATLAB Answers

Find the x value from a specific iteration of fmincon

44 views (last 30 days)
How can I extract the the value of x at the iteration 5 when f(x)= 0.300877 after the optimization procedure.
Max Line search Directional First-order
Iter F-count f(x) constraint steplength derivative optimality Procedure
0 3 1 -1
1 9 0.953127 -0.9375 0.125 -2 12.5
2 16 0.808446 -0.8601 0.0625 -2.41 12.4
3 21 0.462347 -0.836 0.25 -12.5 5.15
4 24 0.340677 -0.7969 1 -4.07 0.811
5 27 0.300877 -0.7193 1 -0.912 3.72
6 30 0.261949 -0.6783 1 -1.07 3.02
7 33 0.164971 -0.4972 1 -0.908 2.29
8 36 0.110766 -0.3427 1 -0.833 2

  0 Comments

Sign in to comment.

Accepted Answer

Massimo Zanetti
Massimo Zanetti on 5 Dec 2016
Edited: Massimo Zanetti on 13 Dec 2016
From the help page about Output Functions Output Functions you can extract this way to save iterates of the solver. Assume you want to minimize the function f(x1,x2) = 4*x1^2 + 2*x2^2, then:
  1. Define your problem by nesting functions into one main function:
function [x fval history] = myproblem(x0)
history = [];
options = optimset('OutputFcn', @myoutput);
[x fval] = fminsearch(@objfun, x0,options);
function stop = myoutput(x,optimvalues,state);
stop = false;
if isequal(state,'iter')
history = [history; x];
end
end
function z = objfun(x)
z = 4*x(1)^2+2*x(2)^2;
end
end
  1. Trigger the solver (with one starting value x0) and retrieve all iterates the solver has computed in the matrix history
x0 = [1,2];
[x,val,history] = myproblem(x0);
Inspect matrix history, each row is the k-th iterate. The same procedure works for fmincon.

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!