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 x_{1} = 1, x_{2} = 5, x_{3} = 5, and x_{4} = 1.

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:

[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = 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;

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

%[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN]

% = 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))])

% 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

%[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN]

% = 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))])

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.

comments powered by Disqus

Retrieved from http://apmonitor.com/che263/index.php/Main/MatlabOptimization

Page last modified on April 06, 2017, at 04:54 AM