%% Problem-Based Nonlinear Minimization with Linear Constraints
% This example shows how to minimize a nonlinear function subject to linear equality constraints by using the problem-based approach, where you formulate the constraints in terms of optimization variables. This example also shows how to convert an objective function file to an optimization expression by using fcn2optimexpr.
% The example Minimization with Linear Equality Constraints uses a solver-based approach involving the gradient and Hessian. Solving the same problem using the problem-based approach is straightforward, but takes more solution time because the problem-based approach currently does not use gradient or Hessian information.
%
%% Create Problem and Objective
% The problem is to minimize
% \[f(x) = \sum\limits_{i = 1}^{n - 1} {\left( {{{\left( {x_i^2} \right)}^{\left( {x_{i + 1}^2 + 1} \right)}} + {{\left( {x_{i + 1}^2} \right)}^{\left( {x_i^2 + 1} \right)}}} \right)} ,\]
% subject to a set of linear equality constraints Aeq*x = beq. Start by creating an optimization problem and variables.

prob = optimproblem;
N = 1000;
x = optimvar('x',N);

% The objective function is in the brownfgh.m file included in your Optimization Toolbox™ installation. You must convert the function to an optimization expression using fcn2optimexpr because optimization variables are excluded from appearing in an exponent. See Supported Operations on Optimization Variables and Expressions and Convert Nonlinear Function to Optimization Expression.

prob.Objective = fcn2optimexpr(@brownfgh,x,'OutputSize',[1,1]);

%
%% Include Constraints
% To obtain the Aeq and beq matrices in your workspace, execute this command.

load browneq

% Include the linear constraints in the problem.

prob.Constraints = Aeq*x == beq;

%
%% Review and Solve Problem
% Review the problem objective.

show(prob.Objective)

% The problem has one hundred linear equality constraints, so the resulting constraint expression is too lengthy to include in the example. To show the constraints, uncomment and run the following line.

% show(prob.Constraints)

% Set an initial point as a structure with field x.

x0.x = -ones(N,1);
x0.x(2:2:N) = 1;

% Solve the problem by calling solve.

[sol,fval,exitflag,output] = solve(prob,x0)

% The solver stops prematurely because it exceeds the function evaluation limit. To continue the optimization, restart the optimization from the final point, and allow for more function evaluations.

options = optimoptions(prob,'MaxFunctionEvaluations',1e5);
[sol,fval,exitflag,output] = solve(prob,sol,'Options',options)

%
%% Compare with Solver-Based Solution
% To solve the problem using the solver-based approach as shown in Minimization with Linear Equality Constraints, convert the initial point to a vector. Then set options to use the gradient and Hessian information provided in brownfgh.

xstart = x0.x;
fun = @brownfgh;
opts = optimoptions('fmincon','SpecifyObjectiveGradient',true,'HessianFcn','objective',...
    'Algorithm','trust-region-reflective');
[x,fval,exitflag,output] = ...
   fmincon(fun,xstart,[],[],Aeq,beq,[],[],[],opts);
fprintf("Fval = %g\nNumber of iterations = %g\nNumber of function evals = %g.\n",...
    fval,output.iterations,output.funcCount)

% The solver-based solution in Minimization with Linear Equality Constraints uses the gradients and Hessian provided in the objective function. By using that derivative information, the solver fmincon converges to the solution in 22 iterations, using only 23 function evaluations. The solver-based solution has the same final objective function value as this problem-based solution.
% However, constructing the gradient and Hessian functions without using symbolic math is difficult and prone to error. For an example showing how to use symbolic math to calculate derivatives, see Symbolic Math Toolbox Calculates Gradients and Hessians.
%
% Copyright 2018-2019 The MathWorks, Inc.