%% Solve Bound-Constrained Minimax Problem
% The objective functions for this example are linear plus constants. For a description and plot of the objective functions, see Compare fminimax and fminunc.
% Set the objective functions as three linear functions of the form {\rm dot}(x,v)+v_0 for three vectors v and three constants v_0.

a = [1;1];
b = [-1;1];
c = [0;-1];
a0 = 2;
b0 = -3;
c0 = 4;
fun = @(x)[x*a+a0,x*b+b0,x*c+c0];

% Set bounds that –2 <= x(1) <= 2 and –1 <= x(2) <= 1 and solve the minimax problem starting from [0,0].

lb = [-2,-1];
ub = [2,1];
x0 = [0,0];
A = []; % No linear constraints
b = [];
Aeq = [];
beq = [];
[x,fval] = fminimax(fun,x0,A,b,Aeq,beq,lb,ub)

% In this case, the solution is not unique. Many points satisfy the constraints and have the same minimax value. Plot the surface representing the maximum of the three objective functions, and plot a red line showing the points that have the same minimax value.

[X,Y] = meshgrid(linspace(-2,2),linspace(-1,1));
Z = max(fun([X(:),Y(:)]),[],2);
Z = reshape(Z,size(X));
surf(X,Y,Z,'LineStyle','none')
view(-118,28)
hold on
line([-2,0],[1,1],[3,3],'Color','r','LineWidth',8)
hold off

% Copyright 2018 The MathWorks, Inc.