Optimization Problems using MATLAB
Course Title: MATLAB Programming: Applications in Engineering, Data Science, and Simulation
Section Title: Optimization and Nonlinear Systems
Topic: Solving unconstrained and constrained optimization problems.
In this topic, we will explore the concepts of unconstrained and constrained optimization problems, including their definitions, applications, and solution methods using MATLAB. By the end of this topic, you will be able to formulate optimization problems, choose the appropriate solver, and use MATLAB to find the optimal solution.
Unconstrained Optimization Problems
Unconstrained optimization problems are those that do not have any constraints on the variables. The general form of an unconstrained optimization problem is:
Minimize of Maximize: f(x)
, where x
is an n
-dimensional vector of variables.
The goal is to find the values of x
that minimize or maximize the objective function f(x)
.
Example 1: Minimize the function f(x) = x^2 + 2y^2 + x*y
, where x
and y
are variables.
To solve this problem in MATLAB, we can use the fminunc
function, which is an unconstrained minimization solver.
% Define the objective function
function f = objfun(x)
f = x(1)^2 + 2*x(2)^2 + x(1)*x(2);
end
% Initial guess for x
x0 = [0; 0];
% Use fminunc to minimize the objective function
[x, fval] = fminunc(@objfun, x0);
% Display the results
disp('Optimal x:');
disp(x);
disp('Optimal value of the objective function:');
disp(fval);
This code defines the objective function objfun.m
, which takes an input vector x
and returns the value of the objective function. We then use the fminunc
function to find the optimal value of x
that minimizes the objective function.
Constrained Optimization Problems
Constrained optimization problems are those that have constraints on the variables. The general form of a constrained optimization problem is:
Minimize of Maximize: f(x)
, subject to:
g(x) <= 0
, fori
= 1, ... ,m
(inequality constraints)h(x) = 0
, fori
= 1, ... ,p
(equality constraints)x_lower <= x <= x_upper
(bound constraints)
The goal is to find the values of x
that minimize or maximize the objective function f(x)
, subject to the constraints.
Example 2: Maximize the function f(x) = x*y
, subject to the constraints x^2 + y^2 <= 4
and x >= 0
.
To solve this problem in MATLAB, we can use the fmincon
function, which is a constrained minimization solver.
% Define the objective function
function f = objfun(x)
f = -x(1)*x(2); % Maximization problem is equivalent to minimization of -f
end
% Define the inequality constraint function
function [g, g_eq] = confun(x)
g = x(1)^2 + x(2)^2 - 4;
g_eq = [];
end
% Define the bound constraints
x_lower = [0; -Inf];
x_upper = [Inf; Inf];
% Initial guess for x
x0 = [1; 1];
% Use fmincon to solve the constrained optimization problem
[x, fval] = fmincon(@objfun, x0, [], [], [], [], x_lower, x_upper, @confun);
% Display the results
disp('Optimal x:');
disp(x);
disp('Optimal value of the objective function:');
disp(-fval); % Convert back to maximization problem
This code defines the objective function objfun.m
, which takes an input vector x
and returns the value of the objective function. We also define the inequality constraint function confun.m
, which takes an input vector x
and returns the values of the inequality constraint functions. We then use the fmincon
function to find the optimal value of x
that maximizes the objective function, subject to the constraints.
Practical Takeaways:
- MATLAB provides a range of optimization solvers that can be used to solve unconstrained and constrained optimization problems.
- To use these solvers, you need to define the objective function and the constraints in the form required by the solver.
- Carefully consider the problem statement and the choice of solver to ensure that you are getting the optimal solution.
External Links:
- MATLAB documentation for
fminunc
: https://www.mathworks.com/help/optim/ug/fminunc.html - MATLAB documentation for
fmincon
: https://www.mathworks.com/help/optim/ug/fmincon.html
Do you have any questions or need help with this topic? Feel free to leave a comment below.
In the next topic, we will discuss multi-variable and multi-objective optimization.
Images

Comments