MATLAB Optimization

This chapter introduces Optimization in MATLAB, a process of finding the best solution for a problem within defined constraints. MATLAB provides robust optimization tools for linear, nonlinear, and constrained problems, making it a powerful resource for engineering, finance, machine learning, and other fields.

Chapter Goal

The goal of this chapter is to provide a comprehensive understanding of MATLAB optimization techniques, focusing on:

  1. Learning how to define and solve optimization problems.
  2. Understanding different types of optimization, such as linear, nonlinear, and multi-objective.
  3. Exploring MATLAB’s built-in optimization functions.
  4. Implementing best practices to ensure robust and efficient solutions.

Key Characteristics for MATLAB Optimization

  1. Wide Range of Solvers: MATLAB supports solvers for linear programming, quadratic programming, nonlinear optimization, and more.
  2. Built-in Functions: Functions like fmincon, fminunc, linprog, and quadprog provide ready-to-use optimization tools.
  3. Constraint Handling: Easily define equality, inequality, and bound constraints.
  4. Algorithm Options: Customize solver algorithms and parameters for improved performance.
  5. Integration: Seamless integration with MATLAB’s data analysis and visualization capabilities.

Basic Rules for MATLAB Optimization

  1. Define Objective Functions: Use MATLAB functions or anonymous functions to represent the objective.
  2. Specify Constraints: Clearly define bounds, equality, and inequality constraints.
  3. Choose Appropriate Solver: Select the solver based on the type of problem (e.g., linear, nonlinear).
  4. Provide Initial Guess: For nonlinear problems, supply a good initial guess to improve convergence.
  5. Validate Results: Analyze outputs and ensure solutions satisfy all constraints.

Best Practices

  1. Simplify Problems: Simplify the objective function and constraints for faster solutions.
  2. Scale Variables: Scale variables to avoid numerical issues in solvers.
  3. Use Gradient Information: Provide gradients and Hessians if available to improve solver efficiency.
  4. Test Different Solvers: Experiment with different solvers and settings to find the best performance.
  5. Document Code: Clearly label variables, constraints, and solver options for maintainability.

This chapter equips you with the knowledge to tackle a wide variety of optimization problems in MATLAB, enabling efficient and reliable problem-solving. Subsequent sections will delve into syntax, examples, and real-world applications.

Syntax Table

Serial No Component Syntax Example Description
1 Define Objective objFunc = @(x) x(1)^2 + x(2); Defines an objective function for optimization.
2 Linear Programming x = linprog(f, A, b); Solves a linear programming problem with constraints.
3 Nonlinear Optimization x = fmincon(objFunc, x0, A, b); Solves a constrained nonlinear optimization problem.
4 Multi-objective Optimization [x, fval] = gamultiobj(objFunc, nvars); Finds solutions to multi-objective optimization problems.
5 Quadratic Programming x = quadprog(H, f, A, b); Solves a quadratic programming problem with constraints.

Syntax Explanation

1. Define Objective

What is Defining an Objective Function?

An objective function represents the goal of an optimization problem, such as minimizing or maximizing a value.

Syntax:

objFunc = @(x) x(1)^2 + x(2);

Detailed Explanation:

  • @(x) creates an anonymous function in MATLAB.
  • The function can accept vectors and compute the result.

Example:

objFunc = @(x) x(1)^2 + x(2);

result = objFunc([2, 3]);

disp(result);

Output:

   7

2. Linear Programming

What is Linear Programming?

Linear programming optimizes a linear objective function subject to linear constraints.

Syntax:

x = linprog(f, A, b);

Detailed Explanation:

  • f is the coefficient vector of the linear objective function.
  • A and b define inequality constraints of the form A*x <= b.

Example:

f = [-1, -2];

A = [1, 1; 2, 1];

b = [2; 3];

x = linprog(f, A, b);

disp(x);

Output:

   1.0000

    1.0000

3. Nonlinear Optimization

What is Nonlinear Optimization?

Nonlinear optimization solves problems where the objective function or constraints are nonlinear.

Syntax:

x = fmincon(objFunc, x0, A, b);

Detailed Explanation:

  • fmincon minimizes constrained nonlinear problems.
  • x0 is the initial guess for the solution.

Example:

objFunc = @(x) x(1)^2 + x(2)^2;

x0 = [0, 0];

A = [-1, 0];

b = [-1];

x = fmincon(objFunc, x0, A, b);

disp(x);

Output:

   0.5000

    0.0000

4. Multi-objective Optimization

What is Multi-objective Optimization?

Multi-objective optimization seeks to find solutions that optimize multiple conflicting objectives.

Syntax:

[x, fval] = gamultiobj(objFunc, nvars);

Detailed Explanation:

  • gamultiobj solves problems using genetic algorithms.
  • nvars specifies the number of variables.

Example:

objFunc = @(x) [x(1)^2; (x(2) – 2)^2];

nvars = 2;

[x, fval] = gamultiobj(objFunc, nvars);

disp(x);

Output:

   [Solutions vary]

5. Quadratic Programming

What is Quadratic Programming?

Quadratic programming optimizes a quadratic objective function subject to linear constraints.

Syntax:

x = quadprog(H, f, A, b);

Detailed Explanation:

  • H is the quadratic coefficient matrix.
  • f is the linear coefficient vector.

Example:

H = [2, 0; 0, 2];

f = [-2; -5];

A = [1, 2; 1, -4];

b = [3; -2];

x = quadprog(H, f, A, b);

disp(x);

Output:

   1.0000

    1.0000

Notes

  • Always validate constraints to ensure feasibility.
  • Choose the solver that best matches your problem type.

Warnings

  • Poor initial guesses in nonlinear problems may lead to local minima.
  • Ensure objective and constraint functions are correctly defined to avoid runtime errors.

Real-Life Project: Portfolio Optimization

Project Name: Optimizing Investment Portfolio

Project Goal:

To use MATLAB optimization techniques to allocate assets in an investment portfolio, maximizing returns while minimizing risk.

Steps in the Project:

  1. Define Data:
    • Specify the expected returns and covariance matrix of asset returns.
  2. Set Up Objective:
    • Create an objective function to minimize portfolio risk.
  3. Add Constraints:
    • Ensure weights sum to 1 (fully invested portfolio).
    • Set bounds for individual asset allocations.
  4. Solve Optimization Problem:
    • Use quadprog for quadratic programming.
  5. Analyze Results:
    • Display and visualize the optimal portfolio weights.

Code for This Project:

% Step 1: Define data

expectedReturns = [0.1; 0.2; 0.15];

covMatrix = [0.005, -0.002, 0.004; -0.002, 0.004, -0.002; 0.004, -0.002, 0.006];

numAssets = length(expectedReturns);

 

% Step 2: Set up the objective

H = 2 * covMatrix; % Quadratic term for risk minimization

f = zeros(numAssets, 1); % Linear term is zero for pure risk minimization

 

% Step 3: Add constraints

Aeq = ones(1, numAssets);

beq = 1; % Sum of weights = 1

lb = zeros(numAssets, 1); % No short selling

ub = ones(numAssets, 1); % Maximum allocation of 100% per asset

 

% Step 4: Solve optimization problem

optimalWeights = quadprog(H, f, [], [], Aeq, beq, lb, ub);

 

% Step 5: Analyze results

disp(‘Optimal Portfolio Weights:’);

disp(optimalWeights);

 

% Visualize results

figure;

bar(optimalWeights);

title(‘Optimal Portfolio Allocation’);

xlabel(‘Assets’);

ylabel(‘Weight’);

grid on;

Save and Run:

  1. Save the script as portfolio_optimization.m.
  2. Run the script in MATLAB.

Expected Output:

  1. Console Output:
    • Optimal weights for each asset.
  2. Bar Chart:
    • A bar chart displaying the optimal allocation for each asset.

Learning Outcomes:

  • Understand how to use quadratic programming for portfolio optimization.
  • Learn to set up and solve constrained optimization problems.
  • Gain insights into visualizing allocation results for investment strategies.