## Optimization with MATLAB

Optimization deals with selecting the best option among a number of possible choices that are feasible or don't violate constraints. MATLAB can be used to optimize parameters in a model to best fit data, increase profitability of a potential engineering design, or meet some other type of objective that can be described mathematically with variables and equations.

Mathematical optimization problems may include equality constraints (e.g. =), inequality constraints (e.g. <, <=, >, >=), objective functions, algebraic equations, differential equations, continuous variables, discrete or integer variables, etc. One example of an optimization problem from a benchmark test set is the Hock Schittkowski problem #71.

$$\min x_1 x_4 \left(x_1 + x_2 + x_3\right) + x_3$$ $$\mathrm{s.t.} \quad x_1 x_2 x_3 x_4 \ge 25$$ $$x_1^2 + x_2^2 + x_3^2 + x_4^2 = 40$$ $$1\le x_1, x_2, x_3, x_4 \le 5$$ $$x_0 = (1,5,5,1)$$

This problem has a nonlinear objective that the optimizer attempts to minimize. The variable values at the optimal solution are subject to (s.t.) both equality (=40) and inequality (>25) constraints. The product of the four variables must be greater than 25 while the sum of squares of the variables must also equal 40. In addition, all variables must be between 1 and 5 and the initial guess is x1 = 1, x2 = 5, x3 = 5, and x4 = 1.

#### Method #2: MATLAB fmincon

The optimize toolbox in MATLAB has linear and nonlinear solvers. One of the most versatile is fmincon, a function minimizer with linear and nonlinear constraints. The fmincon function is:

= fmincon(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS)

Mathematically, this is written as:

$$\min fun(x)$$ $$\mathrm{s.t.} \quad A\,x \le b$$ $$A_{eq} \,x = b_{eq}$$ $$LB \le x \le UB$$ $$ceq(x)=0$$ $$c(x)<0$$ $$x_0 = \mathrm{initial\;guess}$$

Below is an example of solving the Hock Schittkowski problem #71, a minimal example that includes a nonlinear inequality and equality constraint. The nonlinear constraints are in a separate function named nlcon.m.

% create file nlcon.m for nonlinear constraints
function [c,ceq] = nlcon(x)
c = 25.0 - x(1)*x(2)*x(3)*x(4);
ceq = sum(x.^2) - 40;

The objective function is defined as an inline function but it could also be in a separate script such as objective.m. The following script shows how to solve the problem and report the results.

objective = @(x) x(1)*x(4)*(x(1)+x(2)+x(3))+x(3);

% initial guess
x0 = [1,5,5,1];

% variable bounds
lb = 1.0 * ones(4);
ub = 5.0 * ones(4);

% show initial objective
disp(['Initial Objective: ' num2str(objective(x0))])

% linear constraints
A = [];
b = [];
Aeq = [];
beq = [];

% nonlinear constraints
nonlincon = @nlcon;

% optimize with fmincon
% = fmincon(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS)
x = fmincon(objective,x0,A,b,Aeq,beq,lb,ub,nonlincon);

% show final objective
disp(['Final Objective: ' num2str(objective(x))])

% print solution
disp('Solution')
disp(['x1 = ' num2str(x(1))])
disp(['x2 = ' num2str(x(2))])
disp(['x3 = ' num2str(x(3))])
disp(['x4 = ' num2str(x(4))])

#### Method #3: Call Python GEKKO from MATLAB

MATLAB can call Python functions, such as Gekko to solve large-scale optimization problems.

The video link below demonstrates how to solve the problem with Python Gekko but the script source shows how those same functions are called with MATLAB.

clear all
% Initialize model
m = py.gekko.GEKKO();
% Initialize Variables
x1 = m.Var(pyargs('value',1,'lb',1,'ub',5));
x2 = m.Var(pyargs('value',5,'lb',1,'ub',5));
x3 = m.Var(pyargs('value',5,'lb',1,'ub',5));
x4 = m.Var(pyargs('value',1,'lb',1,'ub',5));
% Define Equations
m.Equation(x1*x2*x3*x4>=25);
m.Equation(x1^2+x2^2+x3^2+x4^2==40);
% Objective
m.Obj(x1*x4*(x1+x2+x3)+x3)
% Solve
m.solve();
% Extract values from Python lists using curly brackets
disp(['x1: ' num2str(x1.VALUE{1})]);
disp(['x2: ' num2str(x2.VALUE{1})]);
disp(['x3: ' num2str(x3.VALUE{1})]);
disp(['x4: ' num2str(x4.VALUE{1})]);

This tutorial can also be completed with nonlinear programming optimizers that are available with the Excel Solver and Python Optimization. Click on the appropriate link for additional information and source code.