-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathezimage.m
More file actions
118 lines (96 loc) · 3.75 KB
/
Copy pathezimage.m
File metadata and controls
118 lines (96 loc) · 3.75 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
function ezimage(fun)
% Please report bugs and inquiries to:
%
% Name : Rody P.S. Oldenhuis
% E-mail : oldenhuis@gmail.com
% Licence: 2-clause BSD (See Licence.txt)
% If you find this work useful, please consider a donation:
% https://www.paypal.me/RodyO/3.5
% If you want to cite this work in an academic paper, please use
% the following template:
%
% Rody Oldenhuis, orcid.org/0000-0002-3162-3660. "testfunctions" <version>,
% <date you last used it>. MATLAB Test functions for global optimization
% algorithms. https://nl.mathworks.com/matlabcentral/fileexchange/23147-testfunctions
% make sure the path is OK
prevpath = path;
path(path, genpath(fileparts(mfilename('fullpath'))));
% Path must be reset, even if something fails;
% use TRY-CATCH to accomplish this
% TODO: use onCleanup? -> incompatible with older MATLAB...
try
% EZIMAGE() takes no input, or a function handle
if (nargin == 0)
% available functions
functions = dir([fileparts(mfilename('fullpath')) filesep 'single-objective-unconstrained']);
functions = cellfun(@(x) x(1:end-2), {functions.name}, 'uniformoutput', false);
functions = functions(3:end);
% show list
clc
fprintf(1, 'Single-objective functions:\n');
for ii = 1:numel(functions)
fprintf(1, ['[', num2str(ii), '] ', functions{ii}, '\n']);
end
fprintf(1, '\n');
% select one
while true
num = str2double(input('Type the number of the function you wish to plot: ', 's'));
if isfinite(num) && (num <= numel(functions)), break, end
end
% set proper function
evalc(['fun = @', functions{num}]);
else
if ~isa(fun, 'function_handle')
path(prevpath);
error('ezimage:no_function', ...
'Please provide a function handle to the function to plot.');
end
end
% evaluate function with no argument to get some
% info about the function
[dims, lb, ub, solution, minimum] = feval(fun);
% if the number of dimensions exceeds 3, we can't plot
if (dims > 2) || ~isfinite(dims) % [inf] means arbitrary # dimensions
path(prevpath);
error('ezimage:too_many_dimensions',...
'Given function requires %d dimensions to be plotted, and we live in a 3-D world.', dims+1);
end
% initialize
x1 = linspace(lb(1), ub(1), 150);
x2 = linspace(lb(2), ub(2), 150);
x3 = zeros(length(x1), length(x2));
% simply loop through the function (most functions expect
% [N x 2] vectors as input, so meshgrid does not work)
for ii = 1:length(x1)
for jj = 1:length(x2)
x3(ii, jj) = fun([x1(ii), x2(jj)]);
end
end
% build simple grid for axes
[x1, x2] = meshgrid(x1, x2);
% draw surface
figure, hold on
surf(x1', x2', x3, 'Linestyle', 'none')
% draw minima
if ~isnan(solution)
plot3(solution(:,1), solution(:,2), repmat(minimum, size(solution,1), 1), ...
'r.', 'MarkerSize', 20);
end
% tiles, labels, legend
xlabel('x_1'), ylabel('x_2'), zlabel('F(x_1, x_2)')
if (size(solution,1) > 1)
legend([func2str(fun), '-function'], 'Global Minima')
else
legend([func2str(fun), '-function'], 'Global Minimum')
end
% finalize
view(-40, 30)
set(gcf, 'renderer', 'openGl');
% something's wrong
catch ME
path(prevpath);
rethrow(ME);
end
% reset previous path
path(prevpath);
end