function [optx, optval] = globalopt(meshdelta, a, b, c)

% Global optimization of the function in globalfun.m.
% Meshdelta gives the size for a grid of initial values.
% Meshdelta should be a value with 0 < meshdelta < 6.

optima = [];
fvals = [];

lb = [-3, -3];
ub = [3, 3];
options = optimset('LargeScale', 'off', 'Display', 'off');

for i = -3:meshdelta:3
    for j = -3:meshdelta:3
        x0 = [i, j];
        [localx, localval] = ...
        fmincon(@globalfun,x0,[],[],[],[],lb,ub,[],options,a,b,c);
        optima = [optima; localx];
        fvals = [fvals; localval];
    end
end

results = [optima, fvals];
sorted_results = sortrows(results, 3);
optx = sorted_results(1, 1:2);
optval = sorted_results(1, 3);

globalplot(a, b, c);
hold on
h = plot3(optx(1), optx(2), optval, 'ro');
set(h, 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'k')
legend(h, strvcat('Minimizer: ', num2str(optx), ...
                           'Optimum Value: ', num2str(optval)))