Academic Block

Logo of Academicblock.net

Optimization and Solvers in MATLAB

Optimization is a key area in MATLAB, enabling users to find the best solution (minimum or maximum) for a given problem. MATLAB offers a variety of solvers and functions to address optimization problems ranging from linear programming to nonlinear systems.

Types of Optimization Problems

Optimization problems in MATLAB can be classified into different types:

  • Linear Programming (LP)
  • Quadratic Programming (QP)
  • Nonlinear Optimization
  • Constrained and Unconstrained Optimization

Linear Programming

Linear programming involves optimizing a linear objective function subject to linear constraints. MATLAB uses the linprog function for solving LP problems.

% Solve the LP problem: Minimize f = -x - 2y
% Subject to constraints:
% x + y ≤ 2
% x ≥ 0, y ≥ 0
f = [-1; -2]; % Coefficients of the objective function
A = [1 1]; % Coefficients for inequality constraint
b = [2]; % Right-hand side of inequality
lb = [0; 0]; % Lower bounds for x and y
[x, fval] = linprog(f, A, b, [], [], lb);
disp('Optimal solution (x, y):');
disp(x);
disp('Optimal value of the objective function:');
disp(fval);
Optimal solution (x, y):

    0.00
    2.00

Optimal value of the objective function:

   -4.00
    

Solving Linear Equations

MATLAB provides several solvers to solve systems of linear equations. The backslash operator (\) is one of the most efficient methods.

% Solve the system of equations:
% 2x + 3y = 8
% x - 4y = -2
% Define the coefficient matrix A and the right-hand side vector b
A = [2 3; 1 -4];
b = [8; -2];
% Solve using the backslash operator
x = A \ b;
% Display the solution
disp('Solution (x):');
disp(x);
Solution (x):

    2.00
    1.00

Explanation:

  • A: Coefficient matrix.
  • b: Vector representing the constants on the right-hand side of the equations.
  • x: Solution to the system of linear equations.
  • The backslash operator efficiently computes the solution by performing Gaussian elimination.

Quadratic Programming (QP)

Quadratic Programming involves optimizing (minimizing or maximizing) a quadratic objective function subject to linear constraints. MATLAB provides the quadprog function for solving QP problems.

% Solve the QP problem: Minimize f(x) = 0.5 * xT * H * x + fT * x
% Subject to constraints:
% A * x ≤ b
% x ≥ lb
% Define the problem
H = [2 -1; -1 2]; % Quadratic term (must be symmetric and positive semi-definite)
f = [-2; -6]; % Linear term
A = [1 1; -1 2; 2 1]; % Coefficients for inequality constraints
b = [2; 2; 3]; % Right-hand side of inequality constraints
lb = [0; 0]; % Lower bounds on x
% Solve using quadprog
[x, fval] = quadprog(H, f, A, b, [], [], lb);
% Display results
disp('Optimal solution (x):');
disp(x);
disp('Optimal value of the objective function:');
disp(fval);
Optimal solution (x):

    0.80
    1.20

Optimal value of the objective function:

   -7.20

Explanation:

  • H: Matrix of the quadratic terms in the objective function.
  • f: Vector of the linear terms in the objective function.
  • A: Matrix of coefficients for the inequality constraints.
  • b: Vector of the right-hand sides of the inequality constraints.
  • lb: Lower bounds for the decision variables.

The solution x gives the optimal values of the decision variables, and fval provides the minimum value of the objective function.

Nonlinear Optimization

Nonlinear optimization involves minimizing or maximizing nonlinear objective functions. MATLAB provides the fmincon function for constrained optimization and fminunc for unconstrained optimization.

% Solve a nonlinear optimization problem:
% Minimize f(x, y) = x2 + y2
% Subject to x + y = 1
fun = @(x) x(1)^2 + x(2)^2; % Objective function
x0 = [0, 0]; % Initial guess
Aeq = [1, 1]; % Equality constraint coefficients
beq = 1; % Right-hand side of equality constraint
[x, fval] = fmincon(fun, x0, [], [], Aeq, beq);
disp('Optimal solution (x, y):');
disp(x);
disp('Optimal value of the objective function:');
disp(fval);
Optimal solution (x, y):

    0.50
    0.50

Optimal value of the objective function:

    0.50
    

Solving Systems of Equations

MATLAB’s fsolve function is used to solve systems of nonlinear equations. Here’s an example:

% Solve the system of equations:
% x2 + y2 = 1
% x - y = 0
fun = @(x) [x(1)^2 + x(2)^2 - 1; x(1) - x(2)];
x0 = [0.5, 0.5]; % Initial guess
x = fsolve(fun, x0);
disp('Solution (x, y):');
disp(x);
Solution (x, y):

    0.7071
    0.7071
    

Solving Nonlinear Equations

Here is another example of MATLAB’s fsolve function solving systems of nonlinear equations.

% Solve the nonlinear equations:
% x12 + x22 - 10 = 0
% x1 - x23 + 5 = 0
% Define the system of equations as a function
fun = @(x) [x(1)^2 + x(2)^2 - 10; x(1) - x(2)^3 + 5];
% Initial guess
x0 = [1; 1];
% Solve using fsolve
options = optimoptions('fsolve', 'Display', 'iter');
[x, fval] = fsolve(fun, x0, options);
% Display results
disp('Solution (x):');
disp(x);
disp('Function values at solution (fval):');
disp(fval);
Solution (x):

    2.35
   -1.62

Function values at solution (fval):

    0
    0

Explanation:

  • fun: A function handle that defines the system of nonlinear equations.
  • fsolve: Solves the equations using iterative methods.
  • x: The solution to the system of nonlinear equations.
  • fval: The function values at the solution, which should be close to zero.

Constrained Optimization

Constrained optimization involves finding the minimum (or maximum) of an objective function subject to equality and/or inequality constraints. MATLAB’s fmincon function is typically used for these problems.

% Solve the constrained optimization problem:
% Minimize f(x) = x12 + x22
% Subject to:
% x1 + x2 ≤ 1
% x1 - x2 = 0
% x1, x2 ≥ 0
% Objective function
objFun = @(x) x(1)^2 + x(2)^2; % f(x) = x1^2 + x2^2
% Inequality constraint: c(x) <= 0
ineqCon = @(x) x(1) + x(2) - 1; % c(x) = x1 + x2 - 1
% Equality constraint: ceq(x) = 0
eqCon = @(x) x(1) - x(2); % ceq(x) = x1 - x2
% Bounds
lb = [0; 0]; % x1, x2 >= 0
ub = []; % No upper bounds
% Combine constraints into a single function
nonlinCon = @(x) deal([ineqCon(x)], [eqCon(x)]);
% Initial guess
x0 = [0.5; 0.5];
% Solve using fmincon
options = optimoptions('fmincon', 'Display', 'iter'); % Display iterations
[x, fval] = fmincon(objFun, x0, [], [], [], [], lb, ub, nonlinCon, options);
% Display results
disp('Optimal solution (x):');
disp(x);
disp('Optimal value of the objective function:');
disp(fval);
Optimal solution (x):

    0.50
    0.50

Optimal value of the objective function:

    0.50

Explanation:

  • objFun: Defines the objective function.
  • ineqCon: Specifies the inequality constraint as a function.
  • eqCon: Specifies the equality constraint as a function.
  • fmincon: Solves the constrained optimization problem iteratively.
  • x: The optimal values of the decision variables.
  • fval: The minimum value of the objective function.

Unconstrained Optimization

Unconstrained optimization involves finding the minimum (or maximum) of an objective function without any constraints. MATLAB’s fminunc function is commonly used for such problems.

% Solve the unconstrained optimization problem:
% Minimize f(x) = x12 + x22 - 4x1 - 6x2
% Objective function
objFun = @(x) x(1)^2 + x(2)^2 - 4*x(1) - 6*x(2); % f(x) = x1^2 + x2^2 - 4x1 - 6x2
% Initial guess
x0 = [0; 0]; % Initial values for x1 and x2
% Solve using fminunc
options = optimoptions('fminunc', 'Algorithm', 'quasi-newton', 'Display', 'iter');
[x, fval] = fminunc(objFun, x0, options);
% Display results
disp('Optimal solution (x):');
disp(x);
disp('Optimal value of the objective function:');
disp(fval);
Optimal solution (x):

    2.00
    3.00

Optimal value of the objective function:

   -13.00

Explanation:

  • objFun: Defines the objective function.
  • fminunc: Solves the unconstrained optimization problem using a quasi-Newton algorithm.
  • x: The optimal values of the decision variables.
  • fval: The minimum value of the objective function.

Useful MATLAB Functions for Optimization

Function
Explanation
linprog
Solves linear programming problems.
fmincon
Minimizes constrained nonlinear multivariable functions.
fminunc
Minimizes unconstrained nonlinear multivariable functions.
fsolve
Solves systems of nonlinear equations.
lsqnonneg
Solves nonnegative least-squares problems.

Practice Questions

Test Yourself

1. Solve a linear programming problem to minimize f = 3x + 4y with constraints: x + y ≤ 4, x, y ≥ 0.

2. Use fminunc to find the minimum of f(x) = x4 - 3x3 + 2.

3. Solve the system of equations: x2 + y2 = 1 and x - 2y = 0.