%% Examine fseminf Outputs
% 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.
% This problem is formulated and solved in the example docid:optim_ug.mw_638fe91e-7e00-4887-878d-cbc998e873a6, which collects more information about the solution and solution process.
% To solve this problem using fseminf, write the objective function as an anonymous function.

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

% The code for the nonlinear and semi-infinite constraint function seminfcon 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.

A = [];
b = [];
Aeq = [];
beq = [];
lb = 0;
ub = 2;
[x,fval,exitflag,output,lambda] = fseminf(objfun,x0,ntheta,@seminfcon,...
    A,b,Aeq,beq,lb,ub)

%
% The returned values show:
% The problem is solved at x=1/2.
% The value of the objective function, (x-1)^2, is 1/4.
% The solver takes two iterations and four function evaluations to reach the solution.
% The only constraints other than semi-infinite constraints are bounds, so the lambda structure has no linear or nonlinear values. The lambda.lower and lambda.upper fields, which correspond to bounds, are not empty, but have zero values because the solution is not at either bound.
%
% 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 2021 The MathWorks, Inc.