Main

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.

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

%width=300px%[[https://gekko.readthedocs.io/en/latest/|Attach:gekko.png]]

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/SH753YX2K1A" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe>

(:htmlend:)

(:toggle hide gekko button show="Show GEKKO Solution in 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})]);

(:sourceend:)

(:divend:)

----

(: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

Changed lines 98-99 from:

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

to:

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

Added lines 101-102:

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.

Added lines 95-130:

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

%width=300px%[[https://gekko.readthedocs.io/en/latest/|Attach:gekko.png]]

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/SH753YX2K1A" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe>

(:htmlend:)

(:toggle hide gekko button show="Show GEKKO Solution in 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})]);

(:sourceend:)

(:divend:)

----

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:)