## Optimization with MATLAB

## Main.MatlabOptimization History

Hide minor edits - Show changes to markup

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

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.

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.

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

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

lb = 1.0 * ones(5); ub = 5.0 * ones(5);

lb = 1.0 * ones(4); ub = 5.0 * ones(4);

$$Aeq \,x = beq$$

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

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.

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.

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

#### Method #1: APM MATLAB

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

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

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

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

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.

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.

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.

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.

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.

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.

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.

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

(: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 Excel Solver and the 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:)