-
Notifications
You must be signed in to change notification settings - Fork 0
/
mineplotfval.m
75 lines (70 loc) · 3.08 KB
/
mineplotfval.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function stop = mineplotfval(~,optimValues,state,varargin)
% OPTIMPLOTFVAL Plot value of the objective function at each iteration.
%
% STOP = OPTIMPLOTFVAL(X,OPTIMVALUES,STATE) plots OPTIMVALUES.fval. If
% the function value is not scalar, a bar plot of the elements at the
% current iteration is displayed. If the OPTIMVALUES.fval field does not
% exist, the OPTIMVALUES.residual field is used.
%
% Example:
% Create an options structure that will use OPTIMPLOTFVAL as the plot
% function
% options = optimset('PlotFcns',@optimplotfval);
%
% Pass the options into an optimization problem to view the plot
% fminbnd(@sin,3,10,options)
% Copyright 2006-2010 The MathWorks, Inc.
stop = false;
switch state
case 'iter'
if isfield(optimValues,'fval')
if isscalar(optimValues.fval)
plotscalar(optimValues.iteration,optimValues.fval);
else
plotvector(optimValues.iteration,optimValues.fval);
end
else
plotvector(optimValues.iteration,optimValues.residual);
end
end
function plotscalar(iteration,fval)
% PLOTSCALAR initializes or updates a line plot of the function value
% at each iteration.
if iteration == 0
plotfval = plot(iteration,fval,'kd','MarkerFaceColor',[1 0 1]);
title(getString(message('MATLAB:optimfun:funfun:optimplots:TitleCurrentFunctionValue',sprintf('%g',fval))),'interp','none');
xlabel(getString(message('MATLAB:optimfun:funfun:optimplots:LabelIteration')),'interp','none');
set(plotfval,'Tag','optimplotfval');
ylabel(getString(message('MATLAB:optimfun:funfun:optimplots:LabelFunctionValue')),'interp','none')
else
plotfval = findobj(get(gca,'Children'),'Tag','optimplotfval');
newX = [get(plotfval,'Xdata') iteration];
newY = [get(plotfval,'Ydata') fval];
set(plotfval,'Xdata',newX, 'Ydata',newY);
set(get(gca,'Title'),'String',getString(message('MATLAB:optimfun:funfun:optimplots:TitleCurrentFunctionValue',sprintf('%g',fval))));
end
function plotvector(iteration,fval)
% PLOTVECTOR creates or updates a bar plot of the function values or
% residuals at the current iteration.
if iteration == 0
xlabelText = getString(message('MATLAB:optimfun:funfun:optimplots:LabelNumberOfFunctionValues0',sprintf('%g',length(fval))));
% display up to the first 100 values
if numel(fval) > 100
xlabelText = {xlabelText,getString(message('MATLAB:optimfun:funfun:optimplots:LabelShowingOnlyFirst100Values'))};
fval = fval(1:100);
end
plotfval = bar(fval);
title(getString(message('MATLAB:optimfun:funfun:optimplots:TitleCurrentFunctionValues')),'interp','none');
set(plotfval,'edgecolor','none')
set(gca,'xlim',[0,1 + length(fval)])
xlabel(xlabelText,'interp','none')
set(plotfval,'Tag','optimplotfval');
ylabel(getString(message('MATLAB:optimfun:funfun:optimplots:LabelFunctionValue')),'interp','none')
else
plotfval = findobj(get(gca,'Children'),'Tag','optimplotfval');
% display up to the first 100 values
if numel(fval) > 100
fval = fval(1:100);
end
set(plotfval,'Ydata',fval);
end