If you want to create 101 separate variables for storing each value, that's not recommended. Pre-allocate an array for fval
for storing the values of faval
in each iteration as shown below:
fval = zeros(101,1); %Pre-allocating memory for fval
for k = 0 : 100
ii = k * 0.01;
options = optimset('Display','iter-detailed', ...
'Algorithm','interior point', ...
'Diagnostics','on');
options.TolCon = 0;
options.TolFun = 0;
[X,faval,exitfag,output,lambda,grad,hessian]=fmincon(@myfun9,0,[],[],[],[],ii,1, ...
@mycon,options);
%Storing value of faval in fval(k+1). Note that indexing starts from 1 in MATLAB
fval(k+1) = faval;
end
By the way, it seems from your code that you're not interested in the values of all other parameters i.e. X
,exitfag
,output
,lambda
,grad
,hessi
, because these parameters will be overwritten in each iteration. If you're not interested in these values. You can skip storing them using tilde (~
). So you might also want to use the following instead:
[~,faval]=fmincon(@myfun9,0,[],[],[],[],ii,1, @mycon,options);
% exitfag, output, lambda, grad, hessi are skipped automatically as per fmincon doc
Suggested reading:
1. Dynamic Variables
2. Preallocation
3. Why does MATLAB have 1 based indexing?
4. Tilde as an Argument Placeholder