Main

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/_Il7GQdL3Sk" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

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$}

{$Aeq \,x = beq$}

{$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.

(:source lang=matlab:)

% 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;

(:sourceend:)

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.

!!!! Method #2: MATLAB fmincon

(:source lang=matlab:)

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(5);

ub = 5.0 * ones(5);

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

(:sourceend:)

## Optimization with MATLAB

## Main.MatlabOptimization History

Hide minor edits - Show changes to output

Added lines 42-45:

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/_Il7GQdL3Sk" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

Changed lines 61-62 from:

lb = 1.0 * ones(~~5~~);

ub = 5.0 * ones(~~5~~);

ub = 5.0 * ones(

to:

lb = 1.0 * ones(4);

ub = 5.0 * ones(4);

ub = 5.0 * ones(4);

Changed line 37 from:

{$~~Aeq~~ \,x = ~~beq~~$}

to:

{$A_{eq} \,x = b_{eq}$}

Added lines 27-52:

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$}

{$Aeq \,x = beq$}

{$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.

(:source lang=matlab:)

% 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;

(:sourceend:)

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.

Changed lines 7-10 from:

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.

Attach:hs71.~~gif~~

Attach:hs71

to:

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)$}

{$\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)$}

Added lines 17-18:

!!!! Method #1: APM MATLAB

Added lines 25-64:

!!!! Method #2: MATLAB fmincon

(:source lang=matlab:)

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(5);

ub = 5.0 * ones(5);

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

(:sourceend:)

Changed line 17 from:

<iframe width="560" height="315" src="//www.youtube.com/embed/~~BZmSH8Y8_fc~~?list=PLLBUgWXdTBDi-E--rwBujaNkTejLNI6ap" frameborder="0" allowfullscreen></iframe>

to:

<iframe width="560" height="315" src="//www.youtube.com/embed/Q2zgz0ag0L0?list=PLLBUgWXdTBDi-E--rwBujaNkTejLNI6ap" frameborder="0" allowfullscreen></iframe>

Changed line 20 from:

This tutorial can also be completed with nonlinear programming optimizers that are available with the [[Main/ExcelSolver|Excel Solver]] and ~~the ~~[[Main/PythonOptimization|Python ~~optimization packages in APMonitor~~]]. Click on the appropriate link for additional information and source code.

to:

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

Changed lines 7-8 from:

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~~. 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~~.

to:

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.

Added lines 11-12:

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.

Added line 14:

-->Attach:hs71_MATLAB.png

Changed line 7 from:

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. 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.

to:

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. 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.

Changed line 7 from:

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. This problem has a nonlinear objective ~~and constraints with both equality and inequality constraints~~.

to:

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. 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.

Added lines 1-36:

(:title Optimization with MATLAB:)

(:keywords Optimization, Nonlinear Programming, MATLAB, spreadsheet, nonlinear, optimization, engineering optimization, university course:)

(:description Optimization with MATLAB - Problem-Solving Techniques for Chemical Engineers at Brigham Young University:)

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. This problem has a nonlinear objective and constraints with both equality and inequality constraints.

Attach:hs71.gif

* [[Attach:hs71_MATLAB.zip|Download HS71 Example Problem in MATLAB]]

(:html:)

<iframe width="560" height="315" src="//www.youtube.com/embed/BZmSH8Y8_fc?list=PLLBUgWXdTBDi-E--rwBujaNkTejLNI6ap" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

This tutorial can also be completed with nonlinear programming optimizers that are available with the [[Main/ExcelSolver|Excel Solver]] and the [[Main/PythonOptimization|Python optimization packages in APMonitor]]. Click on the appropriate link for additional information and source code.

----

(:html:)

<div id="disqus_thread"></div>

<script type="text/javascript">

/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */

var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname

/* * * DON'T EDIT BELOW THIS LINE * * */

(function() {

var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;

dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js';

(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);

})();

</script>

<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

(:htmlend:)

(:keywords Optimization, Nonlinear Programming, MATLAB, spreadsheet, nonlinear, optimization, engineering optimization, university course:)

(:description Optimization with MATLAB - Problem-Solving Techniques for Chemical Engineers at Brigham Young University:)

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. This problem has a nonlinear objective and constraints with both equality and inequality constraints.

Attach:hs71.gif

* [[Attach:hs71_MATLAB.zip|Download HS71 Example Problem in MATLAB]]

(:html:)

<iframe width="560" height="315" src="//www.youtube.com/embed/BZmSH8Y8_fc?list=PLLBUgWXdTBDi-E--rwBujaNkTejLNI6ap" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

This tutorial can also be completed with nonlinear programming optimizers that are available with the [[Main/ExcelSolver|Excel Solver]] and the [[Main/PythonOptimization|Python optimization packages in APMonitor]]. Click on the appropriate link for additional information and source code.

----

(:html:)

<div id="disqus_thread"></div>

<script type="text/javascript">

/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */

var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname

/* * * DON'T EDIT BELOW THIS LINE * * */

(function() {

var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;

dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js';

(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);

})();

</script>

<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

(:htmlend:)