%% Minimize Function with Semi-Infinite Constraints
% Minimize the function
% (x-1)^2,
% subject to the constraints
% 0\le x\le 2
% g(x,t) = (x - 1/2) - (t - 1/2)^2 \le 0 for all 0\le t\le 1.
% The unconstrained objective function is minimized at x = 1. However, the constraint
% g(x,t) \le 0 for all 0\le t \le 1
% implies x\le 1/2. Notice that (t-1/2)^2\ge 0, so
% \max_t g(x,t) = x - 1/2.
% Therefore,
% \max_t g(x,t)\le 0 when x \le 1/2.
% To solve this problem using fseminf, write the objective function as an anonymous function.

objfun = @(x)(x-1)^2;

% Write the semi-infinite constraint function seminfcon, which includes the nonlinear constraints [ ], initial sampling interval for t (0 to 1 in steps of 0.01), and the semi-infinite constraint function g(x, t). The code for the seminfcon function appears at the end of this example.
% Set the initial point x0 = 0.2.

x0 = 0.2;

% Specify the one semi-infinite constraint.

ntheta = 1;

% Solve the problem by calling fseminf and view the result.

x = fseminf(objfun,x0,ntheta,@seminfcon)

%
% The following code creates the seminfcon function.

function [c, ceq, K1, s] = seminfcon(x,s)

% No finite nonlinear inequality and equality constraints
c = [];
ceq = [];

% Sample set
if isnan(s)
    % Initial sampling interval
    s = [0.01 0];
end
t = 0:s(1):1;

% Evaluate the semi-infinite constraint
K1 = (x - 0.5) - (t - 0.5).^2;
end

% Copyright 2019 The MathWorks, Inc.