100% found this document useful (1 vote)
142 views123 pages

Optimization Techniques in MATLAB

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 123

Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.

es

Optimization Techniques in MATLAB

Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es

Optimization Techniques in MATLAB®

Table of Contents

MLOP1222V1
TOC - 1‌
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Table of Contents

© COPYRIGHT 2010-2023 by The MathWorks, Inc.

This Training Course Notebook, along with other Training Course Examples
and Exercises, shall at all times remain the intellectual property of The
MathWorks, Inc. The MathWorks, Inc. reserves all rights in these materials.
No part of these materials may be photocopied, reproduced in any form, or
distributed without prior written consent from The MathWorks, Inc.

The software described in this document is furnished under a license


agreement. The software may be used or copied only under the terms of the
license agreement.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® TOC - 2‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Table of Contents

Table of Contents
1. Introduction
2. Running an Optimization Problem
3. Specifying Objective Functions and Constraints
4. Solvers and Performance
5. Global and Multiobjective Optimization
6. Conclusion
Appendices

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® TOC - 3‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Table of Contents

Introduction
MathWorks® at a Glance������������������������������������������������������������������������1 - 2
MathWorks® Product Overview ����������������������������������������������������������1 - 3
Computer Setup��������������������������������������������������������������������������������������1 - 4‌
What Can You Do with MATLAB®, Optimization Toolbox™, and
Global Optimization Toolbox™?������������������������������������������������������1 - 5‌
Course Learning Outcomes������������������������������������������������������������������1 - 6‌
Course Outline����������������������������������������������������������������������������������������1 - 7‌

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® TOC - 4‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Table of Contents

Running an Optimization Problem‌


Outline ����������������������������������������������������������������������������������������������������2 - 2‌
Chapter Learning Outcomes����������������������������������������������������������������2 - 3‌
Course Example: Designing a Soup Can��������������������������������������������2 - 4‌
Optimization Task Setup ����������������������������������������������������������������������2 - 5‌
What are the Variables? ������������������������������������������������������������������������2 - 6‌
What is the Goal?�����������������������������������������������������������������������������������2 - 7‌
What are the Constraints? ��������������������������������������������������������������������2 - 8‌
Solve the Optimization Problem����������������������������������������������������������2 - 9‌
A Geometric Interpretation��������������������������������������������������������������� 2 - 10‌
Summary����������������������������������������������������������������������������������������������� 2 - 11‌‌

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® TOC - 5‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Table of Contents

Specifying Objective Functions and Constraints‌


Outline ����������������������������������������������������������������������������������������������������3 - 2‌
Chapter Learning Outcomes����������������������������������������������������������������3 - 3‌
Course Example: Minimizing Solar Power Loss��������������������������������3 - 4‌
Types of Constraints������������������������������������������������������������������������������3 - 5‌
Setting up an Optimization Problem��������������������������������������������������3 - 6‌
Solving an Optimization Problem��������������������������������������������������������3 - 7‌
Upper and Lower Bounds ��������������������������������������������������������������������3 - 8‌
Linear Equality and Inequality Constraints����������������������������������������3 - 9‌
Nonlinear Constraints ����������������������������������������������������������������������� 3 - 10‌
Optimization Expressions from Functions ������������������������������������� 3 - 11‌
Summary����������������������������������������������������������������������������������������������� 3 - 12‌‌

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® TOC - 6‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Table of Contents

Solvers and Performance‌


Outline ����������������������������������������������������������������������������������������������������4 - 2‌
Chapter Learning Outcomes����������������������������������������������������������������4 - 3‌
Course Example: Hydroelectric Power������������������������������������������������4 - 4‌
Automatic Tool Selection����������������������������������������������������������������������4 - 5‌
Solvers in the Optimization Toolbox™ ������������������������������������������������4 - 6‌
Examining the Optimization Results��������������������������������������������������4 - 7‌
How Should the Tool be Used?������������������������������������������������������������4 - 8‌
Stopping Criteria������������������������������������������������������������������������������������4 - 9‌
Adjusting Stopping Criteria��������������������������������������������������������������� 4 - 10‌
Examining the Optimization Progress��������������������������������������������� 4 - 11‌
Course Example: Solar Power Tower����������������������������������������������� 4 - 12‌
Summary����������������������������������������������������������������������������������������������� 4 - 13‌‌

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® TOC - 7‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Table of Contents

Global and Multiobjective Optimization‌


Outline ����������������������������������������������������������������������������������������������������5 - 2‌
Chapter Learning Outcomes����������������������������������������������������������������5 - 3‌
Course Example: Solar Power Tower��������������������������������������������������5 - 4‌
Derivative-Free Optimization��������������������������������������������������������������5 - 5‌
Genetic Algorithm����������������������������������������������������������������������������������5 - 6‌
Surrogate Optimization ������������������������������������������������������������������������5 - 7‌
Searching from Multiple Starting Points��������������������������������������������5 - 8‌
Solving Multiobjective Problems����������������������������������������������������������5 - 9‌
Solver Choice��������������������������������������������������������������������������������������� 5 - 10‌
Summary����������������������������������������������������������������������������������������������� 5 - 11‌‌

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® TOC - 8‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Table of Contents

Conclusion
Course Outline����������������������������������������������������������������������������������������6‌- 2
Further Training and Certification������������������������������������������������������6‌- 3
MathWorks® Web Resources����������������������������������������������������������������6‌- 4
Technical Support����������������������������������������������������������������������������������6‌- 5
Course Evaluation����������������������������������������������������������������������������������6‌- 6‌

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® TOC - 9‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Table of Contents

Appendices
Exercises������������������������������������������������������������������������������������������������������ A
Extra Examples �������������������������������������������������������������������������������������������B

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® TOC - 10‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es

Optimization Techniques in MATLAB®

Introduction

1 - 1‌
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Introduction

MathWorks® at a Glance
MathWorks® is the leading developer of mathematical computing
software in the world.
• Privately held
• Over 4000 employees worldwide
• More than 2 million users in 180+ countries

MathWorks products are relied upon by a variety of key industries


to accelerate the pace of discovery, innovation, development, and
learning, including
• Aerospace and defense
• Automotive Office location
• Biological sciences Distributor
• Biotech and pharmaceutical
• Communications
• Electronics • Academic support for research, fellowships, student competitions, and
• Energy production curriculum development
• Financial services
• Industrial automation and machinery MathWorks supports its customers through a worldwide network of offices,
• Medical devices distributors, and resellers.
• Metals, materials, and mining Headquarters (Natick, MA USA)
• Neuroscience
• Railway systems www.mathworks.com
• Semiconductors support@mathworks.com
• Software and internet +1-508-647-7000

MathWorks supports programs that inspire learning and advance education Worldwide Offices
in engineering, science, and math.
For information on any of our worldwide offices, please open the following
• 5000+ universities around the world location on the MathWorks site and choose a country:
• 1800+ MATLAB® and Simulink® based books
www.mathworks.com/company/worldwide/

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 1 - 2‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Introduction

MathWorks® Product Overview


Key Characteristics of MATLAB Key Characteristics of Simulink
• The industry-standard, high-level programming language for algorithm • Linear, nonlinear, discrete-time, continuous-time, hybrid, and multirate
development systems
• Numeric computation • Foundation for Model-Based Design, including multidomain system
• Parallel computing, with multicore and multiprocessor support modeling, real-time testing, automatic code generation, and verification
and validation
• Data analysis and visualization
• Open architecture for integrating models from other tools
• Toolboxes for signal and image processing, statistics, optimization,
symbolic math, and other areas • Applications in controls, signal processing, communications, physical
modeling, and other system engineering areas
• Tools for application development and deployment

Control Systems
Event-Based Modeling Physical Modeling

Signal Processing and Communications


Real-Time Simulation and Verification, Validation, Simulation Graphics and
Testing and Test Reporting
Image Processing and Computer Vision

SIMULINK
Simulation and Model-Based Design
®

Test and Measurement

Parallel Computing Code Generation

MATLAB
The Language of Technical Computing
®

Computational Finance

Math, Statistics, and Database Access and Computational Biology


Application Deployment
Optimization Reporting

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 1 - 3‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Introduction

Computer Setup Typical setup

The installation creates a subdirectory


To get ready for class, you need to install the examples and exercises from
of your chosen root directory My Computer
your course DVD or from a USB drive provided by the instructor. Follow
(the default is C:\class) called
these steps:
coursefiles. This directory has Local Disk (C:)
1. Put the DVD in the DVD drive or plug in the USB drive. subdirectories for each of the courses
you install. class
2. The installer application will open automatically. If the installer
application does not open automatically, open the DVD drive or USB drive Each course directory has a coursefiles
in Windows® Explorer. Run the file English_20XXRX_MMM.exe. subdirectory called exercises
3. Follow the prompts in the installer through the installation process. A that contains all exercises and their mlop
shortcut will be created on your desktop to start MATLAB for this class. solutions.
exercises
There is a subdirectory called work,
which is empty. During class, put all
work
your work in this subdirectory, so that
it is on the path and easy to find.

In the installer, you have the following options:


• Choose a class root directory for your course files.
• Choose the courses for which you need to install the files. (Examples
and exercises for all of our courses are on the DVD or USB drive.)
• Create a shortcut on the desktop to start MATLAB for this class. (This
shortcut runs a startup.m file when starting MATLAB, that is
customized for the installed course files)

Note A minimalistic install can be performed by navigating to the DVD


drive or USB drive in MATLAB, executing the installer.m script inside
the zipfiles folder, and selecting the appropriate courses to install.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 1 - 4‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Introduction

What Can You Do with MATLAB®,


Optimization Toolbox™, and Global
Optimization Toolbox™?
In this course, you will
• Interactively solve optimization problems.
• Pass extra data to objective functions.
• Add constraints to an optimization problem.
• Choose the most appropriate solver for a given problem.
• Set options and tolerances for a solver.
• Use multiple starting locations to find a global minimum.
• Solve problems with multiple objectives.

fmincon

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 1 - 5‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Introduction

Course Learning Outcomes


The attendee will be able to
• Define and solve a specific optimization problem using graphical tools
or Optimization Toolbox™ functions.
• Write objective function files and pass extra parameters.
• Add different kinds of constraints to an optimization problem.
• Select an appropriate solver and algorithm by considering the type of
optimization problem to be solved.
• Interpret the output from the solver and diagnose the progress of an
optimization.
• Increase accuracy and efficiency of an optimization by changing settings
and providing derivative information.
• Use Global Optimization Toolbox™ functionality to solve problems
where classical algorithms fail or work inefficiently.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 1 - 6‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Introduction

Course Outline
Day 1
• Introduction
• Running an Optimization Problem
• Specifying the Objective Functions and Constraints
• Solvers and Performance
• Global and Multiobjective Optimization
• Conclusion

Appendices Tracking the progress of an optimization


• Exercises
• Extra Examples

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 1 - 7‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Introduction

In order to maintain consistency with instructors presentation and enable users to follow along with greater ease,
blank pages have been inserted throughout this course manual.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 1 - 8‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es

Optimization Techniques in MATLAB®

Running an
Optimization
Problem

2 - 1‌
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Running an Optimization Problem

Outline
• Optimization task
• Components of a problem
• The optimization process

hopt = 7.26

ropt = 3.63

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 2 - 2‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Running an Optimization Problem

Chapter Learning Outcomes


The attendee will be able to
• Identify the components of an optimization problem.
• Create and solve an optimization problem using the Live Editor
Optimization Task.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 2 - 3‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Running an Optimization Problem

Course Example: Designing a Soup Can


Consider the problem of designing a soup can. A cylindrical soup can has
two variables that characterize it: radius and height. The cost of producing a
soup can is assumed to be directly proportional to the surface area.
Classify problem
Variables Objective Constraints
A soup can must contain 300 ml of soup. To fit on supermarket shelves, the
can should be no taller than 15 cm. For stability, the height can be no more
than four times the radius. Choose solver
Algorithm Options
This example illustrates the components and process that are applicable to
any optimization problem. Whether implemented in a task or with code, all
optimization problems involve: Define Problem Components
• finding the values of the design variables, Variables Objective Constraints
• possibly subject to constraints,
• that minimize (or maximize) an objective function.

Identify design variables, the objective function, and constraints. Define


Run optimization
them mathematically and implement them in MATLAB®. After performing
the optimization, evaluate your results. If you do not achieve the desired
outcome, you may need to make modifications and repeat the process. Evaluate results
Stop ☺ Modify and Repeat
Note: the form of the objective function and the constraints informs your
choice of solver.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 2 - 4‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Running an Optimization Problem

Optimization Task Setup Try

Open a Live Script and add an Optimization task. Select the recommended
Optimization Toolbox provides a task interface that allows you to define and
Problem-based approach.
run optimization problems interactively.

First, open a Live Script by clicking the New Live Script button under
the HOME tab. Then, click the Task button under the INSERT tab of the
MATLAB® toolstrip.

The Task button opens a dropdown menu. Scroll down and click the
Optimize button. This loads the task into the Live Script.

Two options are presented in the Task: the Problem-based approach and
the Solver-based approach. In this course, the problem-based approach is
chosen in most cases. Exceptions to this will be noted.
For the soup can problem, you can specify the details of the problem by
selecting the appropriate options in each field.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 2 - 5‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Running an Optimization Problem

What are the Variables? Try

In the Live Task, under the Create optimization variables section, create
The design variables or optimization variables of a problem are the parameters
a continuous 1-by-1 optimization variable named radius with a lower
than can be changed to achieve the optimization goal. Performing an
bound of zero and an initial point of 5 cm.
optimization entails finding the “best” values of the design variables.
Click the plus button to the right of optimization variable to add another
In the optimization task, you can create the optimization or design variables optimization variable. Name this variable named height with a lower
under the Create optimization variables section. Here you can specify the bound of zero, an upper bound of 15 cm and an initial point of 8 cm. Keep
name of the variable, the dimensions, the type, bounds and initial point it a continuous 1-by-1 variable.
guess.

In the soup can example, the design variables are the height and radius of
the can. Both of these can be treated as 1-by-1 scalar variables that can take
on continuous values. Note that you can also treat them as the elements of a
2-by-1 or 1-by-2 vector.

The tool allows variable types to be continuous, integer or binary.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 2 - 6‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Running an Optimization Problem

What is the Goal? Try

In the Live Task, under the Define problem section, select the Minimize
The goal of performing an optimization is to minimize or maximize the
button for the Goal. For the Objective, select the Define on one line option
objective function. The objective function takes the design variables as input,
in the dropdown menu. In the text box, write the objective function in
and returns a single performance measure as output.
terms of the can radius and height:
In the soup can example, the objective function is the cost of the construction 2*pi*radius*(radius+height)
materials. Since the cost is assumed to be proportional to the surface area, the
objective function is effectively the surface area of a cylinder, as a function of
its radius and height (the design variables).

cost ∝ S(r, h) = 2πrh + 2πr2

In the Problem-based Live Task, the objective function can be defined under
the Define problem section. For the Goal, select a button to minimize or
maximize the objective function. For the Objective, use the dropdown
menu to specify where the function is defined. You can define it on one line,
or use a local function or function file.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 2 - 7‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Running an Optimization Problem

What are the Constraints? Try

Add the constraint to the problem to limit the height of the can to be below
Constraints are restrictions on the possible values of the design variables.
four times the radius to prevent the can from tipping over easily. Define
These can be simple limits, such as the height of the can being no greater
the expression on one line such that the left-hand side is less-than-or-equal
than 15 cm. They can be relationships between the variables, such as the
to the right-hand side.
height being no more than four times the radius, and the can having a fixed
volume: height <= 4*radius
V (r, y) = πr2 h = 300 Click the plus sign next to the constraint to add another constraint.
Here you will add the liquid volume constraint. In the dropdown menu,
In the Live Task, under the Define problem section, constraints can be
select Function file, click the Browse to file button and select the
specified and added to the problem like the objective function. Next to
fixedvolume_soup.mlx file. The function inputs will appear. Select
Constraints, use the dropdown menu to specify where the function is
radius for the first input and height for the second input.
defined. You can define it on one line, or use a local function or function file.
View the contents of the function file:
When defining the constraint on one line, there is a dropdown menu to edit fixedvolume_soup.mlx
control how the left-hand side relates to the right-hand side, as an equality
or inequality. (<=,>=,=)

When defining the constraint with a local function, click the dropdown
menu to select the local function.
h ≤ 15 V (r, h) = 300
When defining the constraint with function file, click the Browse to file
button to link the function file to the task. You can then specify the Function
inputs, which can be the design variables.

Constraints can be added or removed from the problem by clicking the plus
or minus buttons.
h

r≥0
h≥0

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 2 - 8‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Running an Optimization Problem

Solve the Optimization Problem Try

Under the Select problem-dependent solver options section of the task,


There are also the Specify probem-dependent solver options and Display
under Display progress, check the Current point and Objective value
progress sections of the task for further specifications and visualizations for
boxes. Under the Select task mode section, click the Solve problem button
evaluating the results.
to run the optimization problem, or click the Run button in the Live Editor
To run the optimization, click the Solve problem button under the Select tab.
task mode section near the bottom of the live task. This outputs the solution Extract the soup can radius and height from the solution structure
design variables, and the objective function value. It also produces any output of the problem.
visualizations requested. The task will run automatically by default. You can
r = solution.radius
also click the Run button in the Live Editor tab.
h = solution.height
In the upper right hand corner of the task, there is a green button that allows Extract the optimal objective value by plugging the solution into the
you to turn off the autorun feature, and a menu which allows you to view problem.
the code behind the task. You can also convert the task into editable code.
cost = problem.Objective.evaluate(solution)

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 2 - 9‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Running an Optimization Problem

A Geometric Interpretation Try

View the cost function as a surface above the feasible region in the
With two design variables, the objective function can be thought of as a
radius-height plane.
surface above the design-variable plane. The optimal solution is the point
in the plane corresponding to the lowest point on the surface. edit GeometricVisual.mlx
View the completed Live Task that solves this problem.
edit SoupSolution.mlx

For the soup can example, the feasible region is the curve (corresponding
to the fixed-volume constraint) within the shaded region (corresponding to
the height and radius constraints).

The contours represent different fixed values of the objective function.

The optimal solution corresponds to the point in the feasible region that lies
on the contour with the lowest value.

Constraints limit the possible solutions to live within the feasible region – the
set of points in the design-variable space that satisfy the constraints.

g cost
asin
D ecre

Optimal solution
(approximate location)

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 2 - 10‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Running an Optimization Problem

Summary
• Optimization task
• Components of a problem
• The optimization process
Classify problem
Variables Objective Constraints

Choose solver
Algorithm Options

Define Problem Components


Variables Objective Constraints

Run optimization

Evaluate results
Stop ☺ Modify and Repeat

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 2 - 11‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Running an Optimization Problem

Page intentionally left blank

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 2 - 12‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Running an Optimization Problem

Test Your Knowledge


1. What are the three core components of an optimization problem?
A. Objective function
B. Solver algorithm
C. Tolerances
D. Constraints
E. Design variables

2. What are valid formats for an objective function for use in the Live Script
optimization Task? (Select all that apply)
A. As a local function
B. As a function handle
C. As a symbolic equation
D. As a function file

3. Suppose a problem has five design variables. How are they represented
in MATLAB®?
A. Five scalars
B. A 5-element vector
C. A 5-by-5 matrix
D. A function handle to a function with five inputs

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 2 - 13‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es

Optimization Techniques in MATLAB®

Specifying Objective
Functions and
Constraints

3 - 1‌
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Outline
• Objective function files
• Bounds and linear constraints
• Nonlinear constraints

solver objective

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 2‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Chapter Learning Outcomes


The attendee will be able to
• Write an optimization problem and its design variables.
• Provide the objective as an optimization expression in the problem-based
workflow.
• Specify bounds and constraints using optimization expressions.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 3‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Course Example: Minimizing Solar Power Try

Loss Load sun position data into the MATLAB® workspace.


load sunData.mat
When mounting a solar panel, you want to maximize the photovoltaic
output. You can do this by minimizing the angle between the solar panel’s This file contains position data of the sun during one day in March. The
normal vector and the beam of sunlight. Since the apparent position of the cartesian coordinates of the sun's position are in the coords variable.
sun moves over time and the solar panel does not, this angle changes. So
you need to minimize an overall difference between the solar panel’s normal
vector and the moving beam of sunlight. This gives an optimal angle to tilt
the solar panel.

First, data is needed on the sun’s apparent position over time at the location
of the solar panel. In this case, there are cartesian coordinates of the sun’s
position as if it were at a unit distance from the panel.

Second, a metric is needed for measuring the difference between the solar
panel’s normal vector and the sun beam. Note that the position of the sun
changes, so the metric must take this into account. The metric used here is
the average power loss of the solar panel as the objective function. Loss is
equal to the sine of the angle between the normal vector of the panel and
the sun beam, which can be expressed in terms of their dot product or inner
product.

To account for multiple positions of the sun, take the average loss. This is
the objective function, where the three elements of the normal vector of the
solar panel are the design parameters.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 4‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Types of Constraints
In this chapter requirements are added to the solar panel optimization: Lower Upper
• The solar panel should not point towards the ground, and point above Bounds Panel must point Panel must tilt
trees.
above trees. enough to allow
• The panel should also be tiled enough to allow snow to slide off. snow to slide off.
• The panel should point in the general direction of the sun.
• The normal vector should have a unit magnitude.

Bounds limit the possible values of the design variables to specified ranges. Constraints
Constraints can be classified as either linear or nonlinear and either equality
or inequality constraints. Constraints are linear if they can be written as Linear Nonlinear
weighted sums of design variables in the constraint expression. If not, they Normal vector must
are classified as nonlinear. Equality
have unit magnitude.
Panel must not face
Inequality
away from the sun.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 5‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Setting up an Optimization Problem Try

Define the design variables, the elements of the normal vector of the solar
This chapter introduces the MATLAB® problem-based workflow. Start
panel.
by creating a set of optimization variables (design variables), and an
optimization problem object. Specify the details of the objective function panelnorm = optimvar("panelnorm", [3,1]);
and constraints by using the optimization problem properties. Define the objective, the average power loss of the solar panel, as an
optimization expression.
x = optimvar("x",[8,1]);
loss = mean(sqrt(1-(coords*panelnorm).^2));
The first argument is the name of the design/optimization variables. The
Specify the nature of the optimization problem to minimize power loss.
second argument is the size and shape of the variable. In this case, it is an
eight-by-one array. prob = optimproblem("ObjectiveSense","min");
Add the objective to the optimization problem.
Objective functions and constraints can be written as optimization
expressions using the optimization variables and numeric variables in the prob.Objective = loss
workspace. A possible form of a quadratic objective function: Show the optimization problem so far.
obj = x'*S*x; show(prob)

Optimization variables in MATLAB are symbolic in nature, and can be


arrays of any size. Optimization expressions are polynomials or a set of
rational operations on optimization variables and numeric variables.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 6‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Solving an Optimization Problem Try

The solar panel power loss problem is set up. Try to solve it. First, specify
You can solve an optimization problem by using the solve function. The
an initial guess.
resulting design variables values and objective value represent a solution.
Depending on the nature of the problem, you may have to supply an initial x0.panelnorm = guess;
guess for the design variables. Then input the problem and the initial guess into the solve function,
and output the optimal normal vector of the panel, along with the
x0.x = ones(8,1)/8;
[xvals,objval] = solve(prob,x0)
corresponding loss.
[optnorm,lossSol] = solve(prob,x0)
The solve function accounts for the properties of the optimization
You may check the result visually using the helper function
problem that is input, and chooses a solver automatically that suits the
plotSunTrajectory.mlx.
problem. How to choose this and other properties manually is discussed in
the next chapter. plotSunTrajectory(coords,optnorm)

Note that no constraints have been implemented in the minimization of If no solver is specified by the user, then MATLAB will automatically
the objective. The result here may seem suboptimal. choose a solver that best suits the problem, in the problem-based approach.
Solver choice is discussed more in the next chapter.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 7‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Upper and Lower Bounds Try

To ensure the solar panel does not face the ground and above the nearby
Bounds are expressed using two values or two vectors, one for the lower
trees, you set a lower bound for the z-coordinate of the normal vector
bounds and one for the upper bounds. If the corresponding design variables
upon creating design variables. Use the angle of the tree tops forest as
are written as a vector, each upper and lower bound vector must be the same
the lower bound.
size.
To allow heavy snow to slide off of the protective glass layer of the solar
For variables that do not have a lower or upper bound, you may set the panel, the normal vector of the panel has to be sufficiently tilted away
corresponding element to –inf or inf, respectively. Each element of the from the z-axis. Use the value derived from the coefficient of static friction
lower bound vector must be strictly less than the corresponding element of friction as the upper bound.
the upper bound vector.
panelnorm = optimvar("panelnorm",[3,1],...
The functions zeros and ones may be useful for creating vectors of "LowerBound",[-inf;-inf;forest],...
bounds in case some design variables have the same bounds. You can also "UpperBound",[inf;inf;friction]);
input one scalar value which is implicitly expanded into a vector to match Redefine the objective function optimization expression and problem in
the size of the design variable vector. terms of the new bounded panelnorm variable.
prob.Objective = ...
You can set bounds upon creating the optimization variables, or after the
mean(sqrt(1-(coords*panelnorm).^2));
fact by changing the properties of the optimization problem.
Does this constraint change the result? Check by solving the optimization
w = optimvar("w",[8,1],... problem again.
"LowerBound",0,"UpperBound",0.25)
[optnorm,lossSol] = solve(prob,x0)
prob.Variables.w.LowerBound = 0;
prob.Variables.w.UpperBound = 0.25; Check this result visually with the helper function.
plotSunTrajectory(coords,optnorm)
     
0 w1 0.25
0  w2  0.25
     
 ..  ≤  ..  ≤  .. 
.  .   . 
0 wn 0.25
lb w ub

Vectors the same size as w

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 8‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Linear Equality and Inequality Constraints Try

Since the objective function uses the square of the inner product between
A linear combination of the design variables can be written as a vector dot
the solar panel normal vector and the position of the sun, it is possible that
product using matrix-vector notation:
there are optimal solutions where the panel is facing away from the sun.
 
x1 To avoid getting an incorrect solution, constrain the solar panel to face
  
 x2  towards the average position of the sun by making the inner product
a1 x1 + a2 x2 + · · · + an xn = a1 a2 ··· a n  .  = aT x
 ..  between them non-negative.
xn
avgcoord = mean(coords);
prob.Constraints.SunSide = ...
Thus, any linear equality or inequality constraint can be written in the form avgcoord*panelnorm >= 0;
Aeq x = beq or Ax ≤ b , where A is a vector or matrix of coefficients, x is the
vector of design variables, and b is a scalar constant or vector of values. Solve the problem and visualize the results.
[optnorm,lossSol] = solve(prob,x0)
Multiple linear constraints can be written in this form, where Aeq is a matrix plotSunTrajectory(coords,optnorm)
and beq is a vector. Each row of Aeq represents the coefficients for a different
constraint. Each element of beq is the corresponding constraint value. For c
constraints, Aeq is c -by-n and beq is c -by-1.

Linear equality constraints are specified by putting Aeq and beq in an


optimization expression with the equivalence operator, and passed to the
Constraints property of the optimization problem.

prob.Constraints.cons1 = Aeq*x == beq;

If the operation can be done with a function, rather than matrix


multiplication, you can use that function instead.

prob.Constraints.cons1 = sum(x) == beq;

For inequality constraints, the greater-that-or-equal-to and


less-than-or-equal-to operators are used.

prob.Constraints.cons2 = A*x >= b;

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 9‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Nonlinear Constraints Try

The last constraint to implement is to constrain the normal vector to a unit


Nonlinear constraints can be written as optimization expressions as well.
vector. Create an optimization expression that gives the magnitude of the
You can implement both nonlinear equality and nonlinear inequality
normal vector of the solar panel.
constraints.
mag = sum(panelnorm.^2);
prob.Constraints.cons3 = R*x/(x'*S*x) >= 1;
Add a constraint to the problem such that the magnitude is of unit size.
They can also be written in a function file or function handle and prob.Constraints.Unit = mag == 1;
converted to an optimization expression. You must convert function
Solve the problem and visualize the results.
handles to optimization expressions to add them to the opimization
problem, just like the objective function. [optnorm,lossSol] = solve(prob,x0)
plotSunTrajectory(coords,optnorm)
prob.Constraints.cons4 = ...
fcn2optimexpr(@constraint,x) <= 10;

You may have multiple constraints (and the objective) implemented in the
same function file. If a constraint or objective function is simple enough,
you can write it purely as an optimization expression.

[p,q] = fcn2optimexpr(@constraints,x,R,S,...
"ReuseEvaluation",true)
prob.Constraints.cons5 = p == 4;
prob.Constraints.cons6 = q <= 0;

Use the name-value pair "ReuseEvaluation",true to make


your problem run faster if elements of your problem rely on a common
calculation. It is best to enable reusable values only for expressions that
share a value.

For cone programming problems, there is a special kind of nonlinear


constraint, known as a second-order cone constraint.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 10‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Optimization Expressions from Functions Try

The new objective function is written in a MATLAB® function file.


Only light hitting the front of the solar panel can be used to generate
power. This is not yet accounted for. Some of the sun position data points edit solarObjective.mlx
may end up behind the panel. You must change the power loss function Turn this function into an optimization expression.
such that any data point with an angle from the normal vector greater than
90 degrees is changed to 90 degrees. lossFcn = fcn2optimexpr(@solarObjective, ...
panelnorm, coords);
This sort of adjustment may not be captured as a constraint or in just one Add this new objective function to the optimization problem.
line of code, so a simple optimization expression does not suffice. Instead,
prob.Objective = lossFcn;
use MATLAB® functions.
Solve the problem and visualize the results.
MATLAB function handles and function files can also be used to define [optnorm,lossSol] = solve(prob,x0)
objective functions and constraints. Recall the @ symbol followed by the plotSunTrajectory(coords,optnorm)
function name to create a function handle from a function file. A function
handle is a MATLAB variable that acts as a reference to a function. To
include a function handle into your optimization problem, use a conversion You must specify the inputs as the arguments after the function handle in
function fcn2optimexpr. fcn2optimexpr.

f = @objective; obj = fcn2optimexpr(@objective,x);

If there are two outputs to your function, you can ask for two outputs
from fcn2optimexpr. The outputs to the function handle are given
corresponding optimization expressions in the workspace.

[obj2,const] = fcn2optimexpr(@objective,x,y);

objective.mlx
function y = objective(x)

. . .

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 11‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Summary Try

• Objective functions To see the Problem-based Live Task for this problem, open and run the
• Bounds and linear constraints file LiveTask_SolarPanel.mlx
• Nonlinear constraints edit LiveTask_SolarPanel.mlx

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 12‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Test Your Knowledge


1. Which of the following can be an output of the objective?
A. An anonymous function handle
B. Design variable
C. Objective function value
2. How can you provide an objective function as input for an optimization
problem?
A. Function handle
B. Character array
C. Double matrix
D. Optimization expression
3. “All variables are nonnegative” is best expressed as:
A. A bound constraint
B. A linear equality constraint
C. A nonlinear inequality constraint
4. (Select all that apply.) Suppose a problem has nine design variables. How
might you specify two linear equalities and three linear inequalities?
A. A 5-by-9 matrix and a 5-by-1 vector for all constraints
B. A 9-by-5 matrix and a 5-by-1 vector for all constraints
C. A 2-by-9 matrix and a 2-by-1 vector for the equalities, and a 3-by-9 matrix
and a 3-by-1 vector for the inequalities
D. A 9-by-2 matrix and a 2-by-1 vector for the equalities, and a 9-by-3 matrix
and a 3-by-1 vector for the inequalities
E. Write out all five constraints as separate optimization expressions.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 13‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Specifying Objective Functions and Constraints

Page intentionally left blank

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 3 - 14‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es

Optimization Techniques in MATLAB®

Solvers and
Performance

4 - 1‌
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Outline
• Classifying the problem and objective
10

• Choosing an algorithm 8

• Obtaining diagnostic information 4

• Setting tolerances 0

-2

-4

• Solving discrete problems -6

-8
2

1 2
1
0
0
-1
-1
-2 -2

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 2‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Chapter Learning Outcomes


The attendee will be able to
• For a given problem, use automatic solver choice to solve the problem.
• Interpret solver output and diagnose the progress of an optimization.
• Increase accuracy or efficiency of an optimization by changing solver
options and derivative information.

3 0.7 10

2.5 0.6 8

2 6
0.5
4
1.5
0.4
2
1
0.3 0
0.5
-2
0.2
0
-4
-0.5 0.1
-6
-1 0 -8
1 1 2
0.8 1 0.8 1 1 2
0.6 0.8 0.6 0.8
1
0.6 0.6 0
0.4 0.4
0.4 0.4 0
0.2 0.2 -1
-1

Linear/Cone
0.2 0.2
0 0 0 0 -2 -2

f (x) = A x T Quadratic f (x) = xT Ax General f (x)


100 120

90 200
100
80 150

100
70
80 50

60 0

50 60 -50

-100
40
-150
40
30 -200

20 -250
20
-300
10 5
-5
0
0
0 0
0 5 10 15 20 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 -5 5

Integer Programming Least-squares/ Global Optimization


curve fitting
© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 3‌
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Course Example: Hydroelectric Power Try

Set up the problem to maximize revenue for the hydroelectric dam. Open
We want to maximize the revenue of a hydroelectric dam. The price of
and run live script.
electricity and inflow are tracked hourly. Price is measured in dollars per
megawatt-hour ($/MWh), and inflow is measured in cubic meters per edit HydroProblem.mlx
second. We have the price and inflow time series data. So to maximize HydroProblem
revenue is a matter of controlling the turbine flow and spill flow of the dam This script creates the design variables, objective function and linear
over time. Both of these flows sum as project flow. constraints.
Design variables
After formulating an optimization problem mathematically, the first step
The turbine flow and spill flow tracked for every hour. in the optimization process is to classify it and, thus, choose an appropriate
solver. In the Problem-based approach, this step is performed automatically.
Objective function

The goal is to maximize the total revenue generated from selling electricity.

Constraints

The spill flow and turbine flow cannot be negative. Turbine flow cannot
exceed 710 m³/s. The minimum project flow is 14 m³/s. The change in flow
must be smaller than 14 m³/s per hour. The reservoir can store 6.1e7 m³
to 1.22e8 m³, and by the end of the day must have 1.1e8 m³. Also, place an
upper bound on the second derivative of turbine flow to limit how much
work the valve does to extend its life.

The objective function has an optimal point, given the imposed constraints.
For this problem, the optimal point is a spill flow and turbine flow schedule
Spill
that maximizes revenue for the hydroelectric dam.


Flow
Reservoir
In Flow Storage Project Flow
MATLAB® chooses a solver, in this case fmincon, based on the form of this Turbine →
objective function and the constraints. A solver is an Optimization Toolbox™ Flow
function that solves a certain class of optimization problems. Optimization
routines such as fmincon are iterative, evaluating the objective function Electricity
repeatedly during the optimization process.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 4‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Automatic Tool Selection Try

Run the optimization problem and plot the resulting flow schedule using
For general optimization problems fmincon is a good solver. However, in
the helper function makeHydroPlot.mlx.
this case the objective function is quadratic and the constraints are linear.
This means we can use quadratic programming to solve the problem. Such [s,rev] = solve(prob)
problems have efficient solution methods that take advantage of the simple makeHydroPlot(s.SpillFlow,s.TurbineFlow,...
structure of the problem. inFlow,price,store0,k1,k2)
Note from the output that the solver chosen by MATLAB is quadprog.
Using the solver that is best fitted to the problem can significantly increase You can manually choose the fmincon solver by providing an initial
the efficiency of your code. In this case, the solver quadprog is chosen guess and specifying the solver with the Solver name-value pair.
automatically.
[sf,revf] = solve(prob,s,"Solver","fmincon");
There may be numerous methods, or algorithms, you can use to solve a
given class of problem. Each solver in Optimization Toolbox therefore has
different available options, including which algorithm to use.

Classify problem
Variables Objective Constraints
Minimizes Minimizes specific
Choose solver any f (w) form for f (w)
Algorithm Options

Define Problem Components


Variables Objective Constraints
o g
c on d pr
n a
Run optimization f mi qu

Specific form
Evaluate results
for f (w)
Stop ☺ Modify and Repeat

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 5‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Solvers in the Optimization Toolbox™ Least-squares


Linear Nonlinear
For general problems, there are two important special cases of the form of
the objective function: linear and quadratic. Linear objectives can be written lsqnonlin
None \
in the form f (x) = AT x , where a is an n -by-1 vector. Quadratic objectives lsqcurvefit

Constraints
can be written in the form f (x) = ( 12 )xT Ax + B T x, where a is an n-by-n lsqlin lsqnonlin
matrix and b is an n-by-1 vector. Bounds lsqnonneg lsqcurvefit

Most of the Optimization Toolbox solvers, including fmincon, assume that Linear lsqlin fmincon
the design variables can take any value, subject to the constraints. Integer
programming problems form an important class of optimization problems Nonlinear fmincon fmincon
in which the variables are required to have discrete (integer) values, as well
as further constraints. A common specific application of minimization is least-squares fitting, such
as fitting a curve to data. In this case, the objective function is of the general
form
Objective Function 
f (c) = F (c)2 = Fk (c)2
Linear Quadratic General k

fminsearch
None N/A quadprog where f is a function defined by the parameters c. In the case of curve
fminunc
fitting, f has a specific form representing the error between the curve and
quadprog fminbnd
Constraints

linprog the measured data:


Bounds coneprog* fmincon
  2
f (c) = g(x; c) − y2 = g(xk ; c) − yk
k
quadprog
Linear linprog fmincon
coneprog* Here g is the function of the fitted curve, and x and y are the data to fit.
 2
fmincon
Nonlinear fmincon fmincon f (c) = g(xk ; c) − yk
coneprog k

Use the tables here to guide your choice of solver, based on the objective type
*Some quadprog problems can be converted into coneprog problems. and the form of the constraints. In the Problem-based approach this choice
is made automatically. You can also specify a solver explicitly. Computation
Note that only the solvers fmincon and fminunc will be used to
time can be measured using the tic and toc functions.
evaluate a problem when fcn2optimexpr is involved in the setup of the
optimization problem.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 6‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Examining the Optimization Results Try

To extend the life of the valve for turbine flow, we place an upper limit on
By default, solvers provide a summary message when they terminate that
the amount of work the valve does.
explains whether the optimization was successful. You can also get more
detailed diagnostic information as extra outputs from the solver function: prob.Constraints.Stress = ...
diff(diff(TurbineFlow)).^2 <= 64;
[x,f,flag,output] = solve(problem);
This is a nonlinear constraint. This forces MATLAB to use fmincon as
double(flag)
the solver. So we must supply an initial guess for the design variables.
The termination flag summarizes the reason the solver terminated. s0.TurbineFlow = 10*ones(size(TurbineFlow));
• A positive value means that a solution is found successfully. s0.SpillFlow = zeros(size(TurbineFlow));

• A negative value indicates a problem. Additional output arguments can be retrieved to gather diagnostic
information, like the termination flag.
• A value of 0 means that a solution was not found before the iteration
limit or function evaluation limit is reached. [s,rev,flag,output] = solve(prob,s0);
double(flag)
For a detailed description of the meaning of flag, see the documentation Plot the resulting flow schedule.
for your specific solver.
makeHydroPlot(s.SpillFlow,s.TurbineFlow,...
inFlow,price,store0,k1,k2)

output

<0 0 >0

The structure variable output contains summary information about the


optimization, such as the number of iterations and function evaluations
performed, and the final value of the first-order optimality measure.
The exitflag info for each solver is in the documentation.
doc fmincon
docsearch solve

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 7‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

How Should the Tool be Used? Try

By default, fmincon uses the interior-point algorithm, but is this


Many solvers have multiple algorithms associated with them. You can set
the best choice? Time how long the solver takes to run using the tic and
the algorithm for a solver as an option using the optimoptions function:
toc functions.
opt = optimoptions(...,"Algorithm","sqp"); tic;
[x,f] = solve(prob,"options", opt); [s,rev,flag,output] = solve(prob,s0);
t1 = toc
The output opts from optimoptions is a special-purpose variable
that can be passed as the last input to the solver function named in the first Try sqp by using the algorithm name-value pair in optimoptions.
input (fmincon, in this example). opt = optimoptions("fmincon","algorithm","sqp");
tic;
The optimoptions function accepts a list of property names, [s,rev,flag] = solve(prob,s0,"Options",opt);
such as Algorithm, and corresponding property values, such as t2 = toc
interior-point-convex. The following guidelines can help you ratio = t1/t2
choose the appropriate value for the Algorithm option for the solvers
shown. The documentation in MATLAB lists details on the algorithms available for
each solver.
fmincon docsearch("Choosing the Algorithm")
• Use interior-point [default] first.
• If your objective function returns a gradient and you have only
bounds or only linear equality constraints (but not both), use lsqcurvefit and lsqnonlin
trust-region-reflective. • If you have constraints (other than just bounds) or you have more
• For small- or medium-sized problems, try sqp or active-set to parameters (design variables) than function values in the least-squares
speed up the optimization. sum, use levenberg-marquardt.

fminunc lsqlin
• If your objective function does not return a gradient, use • If you have linear constraints, use active-set.
quasi-newton.
coneprog
quadprog • Uses interior-point [default].
• Use interior-point-convex [default] first.
• If you have only bounds or only linear equality constraints (but not
both), use trust-region-reflective.
• Use active-set if problem is indefinite.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 8‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Stopping Criteria Try

Make the first-order optimality tolerance more strict. Is first-order


A basic result in calculus is that the derivative of a function must be zero
optimality achieved?
at a local minimum or maximum. In multiple dimensions, this generalizes
to the norm of the gradient vector being zero. The mathematical details opt = optimoptions("fmincon",...
become more complicated when constraints are added, but the first-order "OptimalityTolerance",1e-9);
optimality measure behaves essentially the same as the derivative (or norm [s,rev,flag] = solve(prob,s0,"options",opt)
of the gradient).
• The first-order optimality measure must be zero at a minimum.
• The first-order optimality measure being zero does not guarantee that
the point found is a minimum.

Solvers iterate until one of the following events occur:


• The first-order optimality measure falls below
OptimalityTolerance.
• The value of the objective function changes (from one iteration to the
next) less than FunctionTolerance.
• The norm of the vector of design variables changes (from one iteration
to the next) less than StepTolerance.
• More than MaxIterations iterations have been performed.
• The objective function has been evaluated more than
MaxFunctionEvaluations times.

If one of the first three conditions occurs, the termination flag is positive. If
either of the last two conditions occurs, the flag is zero.

minimum
=⇒ f  (x) = 0

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 9‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Adjusting Stopping Criteria Try

Potentially increase the accuracy by shrinking StepTolerance as well.


Use optimoptions to set any of the convergence tolerances or iteration
limits. For many solvers, tolerances such as OptimalityTolerance are opt = optimoptions("fmincon",...
interpreted as relative tolerances. "OptimalityTolerance",1e-9,...
"StepTolerance",1e-20);
opt = optimoptions("fmincon",...
Run the optimization with these options and plot the results.
"OptimalityTolerance",1e-8,...
"MaxFunctionEvaluations",6000); [s,rev,flag] = solve(prob,s0,"Options",opt);
[x,f] = solve(...,"options",opt); makeHydroPlot(s.SpillFlow,s.TurbineFlow,...
inFlow,price,store0,k1,k2)
Reducing OptimalityTolerance or StepTolerance can result
in a more accurate result, up to a limit, but can also increase the time In such a situation, you can consider:
the optimization takes to complete. It may be necessary to increase
MaxIterations or MaxFunctionEvaluations. If the tolerances • Rewriting your objective function to handle such exceptions.
are too small, the solution is not any more accurate, but the solver wastes • Using an algorithm, such as interior point, that enforces feasibility at
time iterating needlessly. Conversely, if a high degree of accuracy is not of each iteration.
paramount importance, you can save time by increasing the convergence • Modifying your constraints and/or tolerances to constrain the feasible
tolerances. region more tightly.
You can also use optimoptions to set ConstraintTolerance,
the tolerance for how accurately the constraints must be satisfied. If an
iteration produces a solution that violates the constraints by more than
ConstraintTolerance, that solution is rejected, but the solver does
not terminate. A solution that is within ConstraintTolerance of the
constraints is considered to be valid, even though it is technically outside the
feasible region.

Note that your objective function should not rely on the variables being
constrained precisely by given constraints. For example, an objective function f  (x) ≈ 0
that calculates the square root of a variable may produce a complex result close enough?
even if that variable is bounded below by 0. As well as that variable possibly
being slightly negative, within the bounds of ConstraintTolerance,
some algorithms allow intermediate iterations to violate the constraints.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 10‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Examining the Optimization Progress Try

Use the optimoptions function to view diagnostic information about


In addition to the final state, you can obtain information regarding each
the progress of the optimization.
iteration of the optimization by setting options using optimoptions:
diagnostics = ["optimplotfval";"optimplotx";...
opt = optimoptions("fmincon",... "optimplotfirstorderopt"];
"Display","iter",... opt = optimoptions("fmincon","Display","iter",...
"PlotFcn",["optimplotfval","optimplotx"]); "PlotFcn",diagnostics);
[x,f,flag,output] = solve(...,"options",opt); [s,rev,flag] = solve(prob,s0,"options",opt)

The "Display" option controls how much information is displayed in


the Command Window or Live Script. Change its value from the default See the documentation for the full list of provided plotting functions.
"final" to "iter" to show information at every iteration. You can docsearch Optimization Options Reference
also show no information at all ("off") or show information only if there optimoptions(...,"Display","iter")
is a problem ("notify").

You can see the information graphically by setting the "PlotFcn" option.
The value of this option should be a function handle or string to the plotting First-order Norm of​
function you want to use. Generally this is one of the predefined functions Iter F-count f(x) Feasibility optimality step
0 93 -1.154441e-02 4.441e-16 1.001e-01​
provided with Optimization Toolbox: 1 186 -1.184680e-02 5.551e-16 1.006e-01 5.942e-04​
2 279 -2.904327e-02 2.220e-16 3.422e+00 3.636e-02​
• optimplotx to see the values of the design variables 3 372 -4.082861e-02 2.220e-16 1.989e+00 3.401e-02​
4 465 -4.335443e-02 1.110e-16 1.723e+00 1.441e-02​
• optimplotfval to see the value of the objective function 5 558 -4.576117e-02 2.220e-16 5.829e-01 1.655e-02​
• optimplotfirstorderopt to see the value of the first-order 6 651 -4.807888e-02 0.000e+00 2.648e-01 1.951e-02
7 744 -5.037998e-02 0.000e+00 7.291e-01 2.438e-02
optimality measure. 8 837 -5.172487e-02 1.110e-16 5.193e-01 2.176e-02
• optimplotfunccount plots the function count. optimoptions(...,"Display","iter")
• optimplotconstrviolation plots the maximum constraint
violation.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 11‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Course Example: Solar Power Tower Try

Open and run a script that solves this problem which has integer design
Another way to harvest solar energy is with concentrated solar power, using
variables, bounds and linear constraints.
solar power towers. An array of adjustable mirrors known as “heliostats” are
situated around a target tower. The heliostats follow the sun throughout the edit SolarPowerTower.mlx
day, directing sunlight to the top of a tower to flash steam water. The steam SolarPowerTower
is then used to generate electric power using a turbine.
Objective functions
Design variables
Maximize revenue given that we have time series data taken at regular
The number of heliostats directing sunlight to the target tower over time,
intervals on solar irradiance and electricity price during three different
and the temperature of the water in the tower. There can only be an integer
days.
amount of heliostats pointed at the tower.
For some problems you may have discrete design variables. If your
Constraints
objective function and constraints are linear as well, then MATLAB
There is a limited amount of land for heliostats. The temperature of the
chooses the intlinprog solver. The Optimization Toolbox provides the
target is held within a range as to steam water but not melt the tower. The
intlinprog solver for linear integer programming. This solver allows for
amount of energy delivered to the target converts the maximum amount
mixed integer linear problems: where some design variables are integers,
of water into steam. The change in the amount of heliostats pointed at the
while others may be continuous. For these type of problems, the objective
target is limited to reduce gimbal wear.
function can be expressed as f (x) = AT x, where A is an n-by-1 vector.

v = optimvar("v",[4,1],"Type","integer")
Target
Tower The algorithm used by intlinprog uses continuous variables that are
restricted to integer values at each step. For the sake of efficiency, this
Heat water to restriction is performed to within a specified tolerance. This means that the
generate steam solution values are not necessarily exact integers. Use optimoptions to
Sunlight set the IntegerTolerance option, if more or less precision is required.

Heliostats

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 12‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Summary
• Classifying the problem and objective
10

• Choosing a solver and algorithm 8

• Obtaining diagnostic information 4

• Setting tolerances 0

-2

-4

• Solving discrete problems -6

-8
2

1 2
1
0
0
-1
-1
-2 -2

nonlinear Y
constraints fmincon
?
N

linear form of quadratic


quadprog
objective
linprog general

number of >1
design variables

Y Y
constrained constrained
? ?
fminbnd N N

fminsearch fminsearch
fminunc fminunc

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 13‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Page intentionally left blank

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 14‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Solvers and Performance

Test Your Knowledge


1. What determines which solver is used for a problem?
A. The number of design variables and the type of objective function
B. The number of design variables and the type of constraints
C. The type of objective function and the type of constraints

2. A quadratic problem with linear constraints is solved best with


A. coneprog
B. linprog
C. fmincon
D. quadprog

3. To change the algorithm of a solver,


A. Use the algorithm function.
B. Pass an optional string to the solver function.
C. Change the solver’s settings in the preferences.
D. Use the optimoptions function.

4. If an optimization fails to find an optimal solution before reaching the


maximum allowed number of iterations, the output flag will be:
A. Negative
B. Zero
C. Positive
D. Empty

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 4 - 15‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es

Optimization Techniques in MATLAB®

Global and
Multiobjective
Optimization

5 - 1‌
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Global and Multiobjective Optimization

Outline
• Using multiple starting points
• Derivative-free methods
• Minimizing multiple objective functions

x0 x0 x0 x0

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 5 - 2‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Global and Multiobjective Optimization

Chapter Learning Outcomes


The attendee will be able to
• Determine when a Global Optimization Toolbox™ solver may be an
appropriate choice.
• Use multiple starting points to improve the likelihood of finding a global
minimum.
• Use derivative-free methods to solve problems that are unsuitable for
classical methods.
• Use multiobjective optimization for problems with multiple goals.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 5 - 3‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Global and Multiobjective Optimization

Course Example: Solar Power Tower Try

Open and run a script that solves this problem, which has integer design
Here you will build upon the Solar Power Tower example of the previous
variables, bounds, linear and nonlinear constraints.
chapter. The goal is to see if a higher revenue is possible using global
optimization techniques. You can use genetic algorithm or surrogate edit SolarPowerTowerNL.mlx
optimization, which allow for integer design variables. You will keep the SolarPowerTowerNL
amount of heliostats an integer as was done in the previous version of this With the problem-based approach taken, MATLAB automatically chooses
problem. the genetic algorithm solver for this problem, given the constraints and
bounds. This may take approximately five to ten minutes to run. So a
Additional design variables and constraints have been added to this
solution file is provided here.
problem, making it nonlinear. The rate at which water is pumped is turned
into design variable with bounds and linear constraints on it. One of the load NLSolution.mat
previous constraints on heat now becomes nonlinear as a result of this HelioSchedulePlots(s.N,s.Temp,s.Pump)
change.

Target
Tower

Heat water to
generate steam
Sunlight

Heliostats

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 5 - 4‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Global and Multiobjective Optimization

Derivative-Free Optimization The is an example in the documentation of direct search optimization using
Mount Washington topography.
Even when derivative information is not supplied, classical solvers such as openExample("globaloptim/mtwashdemo")
fmincon still rely on the assumption that the objective function has smooth
derivatives. If the objective function is not smooth, including the case that
the objective has a stochastic (random) component, classical methods may
not provide adequate solutions. In this case, use a derivative-free solver, such
as a direct search method, genetic algorithm or surrogate optimization.

Direct search methods evaluate the objective function at a variety of points,


called a mesh, and then use that information to iterate the solution and define
a new mesh. The mesh is expanded or contracted, based on the objective
values. The solver successfully terminates when the mesh contracts below a
threshold size around a local minimum.

Genetic algorithms (GA) also evaluate the objective function at several


points, which are thought of as individuals in a population. A selection of
∇f = ? fmincon
the best points is used to generate a new population, by making random
alterations to the chosen points. This process is repeated until successive
generations do not significantly improve the solution.

Surrogate optimization explores by evaluating an approximated objective


function built from an interpolation of randomly chosen points. A merit
function is applied to select the best point using the interpolation. The
process is repeated until the the previous best point is sufficiently close to
the current best point.
Repeatedly Sample
Note that fmincon may work on a problem that is not smooth. In the several points
absence of gradient information from the objective function, fmincon
uses finite differences to approximate the gradient. If the objective function
Pattern Search
can be closely approximated by a smooth function, fmincon may find a
Algorithm
reasonable solution without significant problems.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 5 - 5‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Global and Multiobjective Optimization

Genetic Algorithm Try

Global optimization can be done within the Problem-based Live Task as


Genetic algorithms (GA) solve optimization problems by mimicking the
well. Open the script containing a Live Task that solves this problem with
process of biological evolution. Much of the terminology used with GA
the genetic algorithm and displays diagnostics.
follows the biological analogies.
edit SolarPowerTower_Task.mlx
Biological evolution applies selective pressure to a population of individuals.
The better suited an individual is to its environment, the more likely it is
to survive and reproduce. Its offspring are likely to inherit a number of
those advantageous traits. Over many generations, those traits will become
dominant and continue to develop, if they increase the individuals’ fitness.

Similarly, GA works iteratively on


populations of starting points.

At each iteration, the fitness of each


individual is evaluated. The fitness function
is essentially the objective function.
Depending on their fitness values, the
points are divided into three groups.
Crossbreeding involves applying Mutation is a random change to a
Unlike biological evolution, the fittest a function that combines solution parent solution.
individuals (that is the solution points points, such as a randomly
that give the lowest value of the objective weighted average of the parents.
function), also known as the elite, reproduce
themselves exactly. The elite thus persist to
the next generation (the population at the
next iteration). There is now a new population to
begin the next generation.
The remaining individuals become parents
that will reproduce to create individuals The process is repeated for many
for the next generation. The parents are generations, until the overall
divided into a group that will reproduce by fitness of the population converges
crossbreeding, and another group that will to a steady value.
reproduce by mutation.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 5 - 6‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Global and Multiobjective Optimization

Surrogate Optimization Try

Open and run a script that compares the results of genetic algorithm and
Surrogate optimization has two phases. The first is construction of a
surrogate optimization. This takes about ten minutes to run.
surrogate. This is where random points are created within the bounds of
the problem. Then the objective function is evaluated at those points. Then edit SolarPowerTowerGS.mlx
interpolation between these points is implemented via radial basis functions,
resulting in a surrogate of the objective function. The second phase is the Sample random points from
search for the minimum. Random points created within surrogate. Chose best point
bounds. (adaptive point).
• Several random points are sampled from within the bounds of the
problem.
• A merit function is applied, based on the surrogate value at the sample
points.
• The best point is chosen as a candidate by the merit function value. This
is known as the adaptive point.
• The objective function is then evaluated at this point. This value is then
used to update the surrogate at that point.

This process repeats. Points are sampled near the incumbent point, the merit
is evaluated, the best point is selected, objective is evaluated there, and the
interpolant is updated accordingly. This continues until the points sampled
are close to the points where the objective was previously evaluated.

The merit function depends on surrogate values s, the sample point at


x, and the distance d between the sample point and evaluated point. The
weight w cycles through four values during the search for minimum phase:
0.3, 0.5, 0.8, 0.95.

Surrogate optimization is intended for problems that take a long time to


solve. There is an option to save a checkpoint file.

opt = optimoptions("surrogateopt",... Objective function evaluated at Evaluate objective at this point.


"CheckpointFile","checkfile.mat") points. Interpolation implemented
to create surrogate.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 5 - 7‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Global and Multiobjective Optimization

Searching from Multiple Starting Points Try

Open and run a script that solves the solar power tower problem using the
A simple way to find a global minimum is to perform multiple
MultiStart solver. Note that the integer constraint on the number of
local minimizations, starting from different locations. You can use
heliostats is relaxed. Instead, these numbers are rounded and the values
GlobalSearch and MultiStart solvers to search from multiple
are checked after the fact.
starting points.
edit SolarPowerTowerMS.mlx
To use either algorithm, GlobalSearch uses a scatter-search mechanism
to create starting points. MultiStart creates uniformly distributed start
points within the bounds, or uses user-supplied start points. GlobalSearch MultiStart
Use to efficiently find a global min- Use to find multiple minima, to run
GlobalSearch analyzes starting points and selects the ones likely to imum (on a single processor). in parallel, or to use a solver other
improve the best local minimum found so far. MultiStart runs all start than fmincon.
points, or all points feasible within constraints if the user specifies as such. Selectively samples starting points Runs from all prescribed starting
points
The following command creates a GlobalSearch solver variable gs that
uses default options for the global search algorithm. Cannot be run in parallel Can be run in parallel

gs = GlobalSearch; Uses fmincon only Uses fmincon, fminunc,


lsqcurvefit, or lsqnonlin
You can use name-value arguments to set options for the global search
algorithm, such as the number of starting points to use at the first stage:

gs = GlobalSearch("NumStageOnePoints",100);

The outputs from solve when using GlobalSearch are essentially the
same as from a single solver such as fmincon.

[x,f] = solve(problem,guess,gs);

Use the same process to create and use MultiStart solvers. When using
Local minimum
the solve function, you must provide either the number of starting points, or
the starting points themselves.
Global minimum

x0 x0 x0 x0
© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 5 - 8‌
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Global and Multiobjective Optimization

Solving Multiobjective Problems Try

Use a multiobjective genetic algorithm to find an optimal Pareto front. Plot


Here you will reframe the problem as a multiobjective optimization problem.
the Pareto front and choose a working point.
Maximizing revenue remains one of the objectives. Minimizing the total
cost of pumping water to the tower is added as the second objective. The edit SolarPowerTowerMultiObj.mlx
design variables and constraints have not changed.

As with intlinprog, you can specify the indices of the design variables Property Name Description
that are required to be integers. However, ga can handle nonlinear objective PopulationSize Number of individuals in the

Population Parameters
functions and nonlinear constraints. The ga and gamultiobj functions population at each iteration.
provide default crossbreeding and mutation functions, and default settings EliteCount Number of individuals selected as the
for various parameters, such as population size and the convergence elite at each iteration (see description
tolerance. You can provide your own functions and set your own values of on next page).
all the parameters using optimoptions.
CrossoverFraction Fraction of parents
In multiobjective optimization, the result is often not a singular solution. (PopulationSize-EliteCount)
Instead, the result is a set of solutions along a Pareto front curve. A user that reproduces by crossbreeding. The
can then choose a point from the Pareto front arbitrarily, unless there is an remainder reproduce by mutation.
CreationFcn Function that creates the initial

Functions
inflection in the curve, in which case the inflection point is typically chosen.
population.
CrossoverFcn Function that generates children from
parents by crossbreeding.
MutationFcn Function that generates children from
parents by mutation.
FitnessScalingFcn Function that scales fitness values
before the selection process.
SelectionFcn Function that chooses which (non elite)
The goal attainment fgoalattain solver is a specific way of avoiding individuals become parents.
arriving at multiple solutions. This formulation allows objectives to be
achieved with relative precision around user-specified goals. The user
supplies a weighting vector, where each element corresponds to a goal, Goal attainment can be used as a hybrid function with gamultiobj for
and indicates the percentage of precision for that goal. The third output some problems to further optimize the Pareto front.
argument gives the attainment factor. If it is negative, the goals have been
overachieved, if positive, they have been underachieved. options = optimoptions("gamultiobj",...
"HybridFcn","fgoalattain")

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 5 - 9‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Global and Multiobjective Optimization

Solver Choice
When looking for a global minimum (or maximum), use these steps to try If there are multiple objective functions, there is another set of solvers to try:
solvers: 1. For a single solution try fgoalattain or fminimax. Both can
1. Try GlobalSearch first, to find global solution using local solver handle bounds, linear and nonlinear constraints. They arrive at a single
fmincon. solution using the goal attainment problem formulation. The first has
2. Next try MultiStart. Uses local solvers on variety of start points. user-supplied weights and minimizes a slack variable, and the second is
unscaled: minimizing the maximum objective.
3. Try patternsearch. Robust but less efficient as no gradient used.
Provably converges on nonsmooth problems. Can handle all constraint 2. For a set of solutions, try paretosearch or gamultiobj. The first
types. uses patternsearch, the second uses ga to arrive at a Pareto front.
4. Try surrogateopt. Find global solution using fewest objective
function evaluations. More overhead than other solvers. Requires
finite bounds. Can have integer constraints and nonlinear inequality
constraints. For problems that have time-consuming objective functions. Objective and Constraints
5. Try particleswarm next, if the problem is unconstrained or has Smooth Nonsmooth
bounds. Little supporting theory, but more efficient than patternsearch. Single linprog, quadprog fminbnd, fminseach
6. Try ga next. It can handle all constraint types, including integer constraints. local fmincon, fminsearch patternsearch, ga
Little supporting theory, and less efficient than patternsearch and solution fminbnd, intlinprog particleswarm
particleswarm. lsqcurvefit,... simulannealbnd
surrogateopt
7. The last to try is simulannealbnd. Can have unconstrainted and

Desired Solution
unbounded problems. Only takes bound constraints. Least efficient Multiple GlobalSearch patternsearch, ga
local MultiStart particleswarm
solver. Provably converges, but extremely slow.
solution simulannealbnd
surrogateopt
Single fgoalattain Single GlobalSearch patternsearch, ga
Desired Solution

solution fminimax global MultiStart particleswarm


Solution patternsearch, ga simulannealbnd
Multiple paretosearch
particleswarm surrogateopt
Pareto gamultiobj
simulannealbnd
front
surrogateopt
solutions

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 5 - 10‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Global and Multiobjective Optimization

Summary
• Using multiple starting points
• Derivative-free methods
• Minimizing multiple objective functions

x0 x0 x0 x0

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 5 - 11‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Global and Multiobjective Optimization

Page intentionally left blank

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 5 - 12‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Global and Multiobjective Optimization

Test Your Knowledge


1. (Select all that apply) Which of the following are typical reasons for
using algorithms from Global Optimization Toolbox?
A. The classical algorithm runs into a local minimum.
B. The classical algorithm takes too long.
C. The objective function is stochastic or not differentiable.
D. The solution is restricted to discrete values, for example, integers only.
2. Which of these functions runs a classical algorithm from many starting
points?
A. createOptimProblem
B. ga
C. GlobalSearch
D. patternsearch
3. Multiobjective solvers can only be found in the Global Optimization
toolbox.
A. True
B. False

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 5 - 13‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es

Optimization Techniques in MATLAB®

Conclusion

6 - 1‌
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Conclusion

Course Summary
Day 1
• Introduction
• Running an Optimization Problem
• Specifying the Objective Functions and Constraints
• Choosing a Solver and Improving Performance
• Global and Multiobjective Optimization
• Conclusion

Appendices
• Exercises
• Extra Examples

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 6 - 2‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Conclusion

Further Training and Certification Where do you go from here?

MathWorks® offers introductory and intermediate courses in MATLAB®, Related courses


Simulink®, and Stateflow®, as well as advanced courses in subjects such • Statistical Methods in MATLAB®
as control design, signal and image processing, test and measurement, • Machine Learning with MATLAB®
optimization, statistics, and financial analysis.

Public instructor-led training


Public instructor-led courses are offered in locations worldwide. In addition,
many of our distributors offer training at other international sites.

On-site instructor-led training


MathWorks also offers custom training courses, which can be taught at your
own facility. Our engineers can incorporate company- or industry-specific
examples into a curriculum of your choosing.

Online instructor-led or self-paced


All of our training courses (except those requiring specialized hardware) are
also offered online. You get the same quality instruction with the convenience
of being in your home or office. Additionally, a limited number of our
courses are available in a self-paced format. Take the courses whenever you
choose and complete them on your schedule.

Free online self-paced


MathWorks offers a set of free self-paced online trainings to get you started
with our products. To see the full list of courses, follow the link below.
matlabacademy.mathworks.com

MathWorks Certification Program


MathWorks offers certification programs for MATLAB. Certification For course information and the latest training schedule and locations:
establishes a standard of excellence that demonstrates your MATLAB www.mathworks.com/training
proficiency to customers, industry peers, and your employer. For
organizations, certification is a strategic investment that pays off through Don’t see it on the schedule? Contact us and we’ll make it happen:
increased productivity and project success.
training@mathworks.com

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 6 - 3‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Conclusion

MathWorks® Web Resources Try


www.mathworks.com
The MathWorks web site at www.mathworks.com contains a wealth of
resources beyond the materials provided for this course, including

Products and services You should create a MathWorks account by selecting the Log In link in
• Product information the mathworks.com navigation bar and then selecting the Create
Account link on the next page. A MathWorks account will allow you to
• Training schedule view information about your product licenses, receive technical support,
and participate in the MathWorks community.
Support
• View and report bugs
• Documentation
• Download products
• Products & services home • View and report bugs • Create MathWorks account
Community
• MATLAB Answers • Training schedule • Documentation • Manage your account
• File Exchange • Consulting offerings • Downloads • Obtain product updates
• Cody

Events
• Webinars
• Seminars
• Conferences and tradeshows

• File exchange • Webinars


• MATLAB Newsgroup • Seminars
• MATLAB Answers • Conferences
• Blogs

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 6 - 4‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Conclusion

Additional Resources
With a MathWorks account, you can request technical support, report
product bugs, and submit product enhancement requests from the
MathWorks web site at www.mathworks.com.

To create a new support request, select the Support link in the


mathworks.com navigation bar and then select the Contact support
link. Inside the next page, select Create Service Request.

Set the request type to Technical Support and then choose a specific
category of support. Now you can enter the information describing your
request and submit it to technical support.

To view known bugs and recommended workarounds, you can refer to the
Bug Reports page at the link below.

www.mathworks.com/support/bugreports/

Other useful web-based technical support capabilities


• Communicate with MathWorks support staff.
• Attach related documents to a service request.
• Receive email notifications of new information about your service
requests.
• After the request has been satisfied, the system archives your entire
correspondence history for review at any time.
• To learn more, go to www.mathworks.com/support/.

If you have a need for a higher degree of personalized attention or a custom


solution, MathWorks Consulting Offerings can help. This is a team of
product experts who can work closely with you and your colleagues to
accelerate your projects with a focus on business value: faster development,
reduced costs, higher quality, and increased collaboration. Like Training
Services, Consulting is a paid service.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 6 - 5‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Conclusion

Course Certificate & Evaluation


Thank you for attending MathWorks training!

Your course completion certificate is available online. To access, select the


My Courses link at mathworks.com/training.

Please take a moment to fill out a brief course evaluation. When completing
the evaluation, enter the email address that you used to register for this
course.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® 6 - 6‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es

Optimization Techniques in MATLAB®

Appendix A:
Exercises

A - 1‌
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Exercises
All exercise and solution files are found in the Exercises subfolder
of the C:\class\coursefiles\mlop folder created by the course
installer.

Exercise Page Reference Exercise Page Reference

Loan Rate I ................................................................. A-3 Chapter 2 Loan Rate III .............................................................. A-19 Chapter 4
Minimizing a Mathematical Function .................... A-4 Chapter 2 Minimizing a Mathematical Function II ................ A-20 Chapter 4
Optimal Cabin Construction I ................................ A-5 Chapter 2 Using Hessian Information ...................................... A-21 Chapter 4
Optimize a Portfolio .................................................. A-6 Chapter 2 A Catenary Curve ...................................................... A-22 Chapter 4
Loan Rate II ................................................................ A-7 Chapter 3 Chip Production II .................................................... A-23 Chapter 4
Projectile Range ......................................................... A-8 Chapter 3 Plant and Storage Location....................................... A-24 Chapter 4
Global Positioning System ....................................... A-9 Chapter 3 Mars Orbit Prediction................................................ A-25 Chapter 4
Vapor Pressure ........................................................... A-10 Chapter 3 Efficient Frontier ....................................................... A-26 Chapter 4
Air Conditioning I ..................................................... A-11 Chapter 3 Weber’s Problem ........................................................ A-27 Chapter 5
Chip Production I .................................................... A-12 Chapter 3 Hunter’s Riddle .......................................................... A-28 Chapter 5
Mean-Gini Portfolio Optimization ......................... A-13 Chapter 3 Chip Production III .................................................. A-29 Chapter 5
Optimal Cabin Construction II ............................... A-14 Chapter 3 Optimal Cabin Construction III ............................. A-30 Chapter 5
Optimize a Portfolio II .............................................. A-15 Chapter 3 Plant and Storage Location II ................................... A-31 Chapter 5
A Manufacturing Process ......................................... A-16 Chapter 4 Mars Orbit Prediction II ........................................... A-32 Chapter 5
Estimating Model Parameters .................................. A-17 Chapter 4 Optimize a Larger Portfolio ..................................... A-33 Chapter 5
Air Conditioning II ................................................... A-18 Chapter 4

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 2‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Loan Rate I Solution


edit loanrate1_soln.mlx
Reference: Chapter 2
Topics: Using the Optimization task and Live Script

Exercise
Compute the remaining balance of a $100,000 loan at 3% nominal annual
interest rate after 10 years for a given monthly payment. The goal is to
determine a monthly payment such that the loan has been paid back at the
end of the 10 years. You can do this by
• solving Balance(payment) = 0 or
• minimizing (Balance(payment))^2.

1. Open the Optimization Live Task and select the Problem-based


approach.
2. Create a 1-by-1 payment design variable.
3. Write the objective function in terms of the payment variable.
4. Run the optimization task and extract the remaining balance and optimal
payment amount.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 3‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Minimizing a Mathematical Function Solution


edit simpleOptimTask.mlx
Reference: Chapter 2
Topics: Using Optimization Tasks and Live Script

Exercise

Find the minima in the following problem using the Optimization Live Task

min (x1 − 3)2 + (x2 − 2)2


x
subject 26 − (x1 − 5)2 − x22 ≥ 0
to:
10 − 4x1− x2 ≥ 0
x1 ≥ 0, x2 ≥ 0
1. What is the objective function? What are the design variables? What are
the constraints?
2. Set up the problem in the Live Task and run the optimization.
3. Visualize the objective function using a contour plot. Add in the
constraints feasible regions visually using the patch and alphaShape
functions.
>> obj = prob.Objective.evaluate(x)
>> nf = prob.Constraints.constraint1.infeasibility(x)

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 4‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Optimal Cabin Construction I Solution


edit CabinTaskPB.mlx
Reference: Chapter 2
Topics: Using Optimization Tasks and Live Script

Exercise

Minimize the cost of building a cabin in an Optimization Task. (Neglect the


cost of doors, windows, etc. Assume walls will be 8 feet tall.)
1. The cost is proportional to the total length of interior and exterior walls,
the total floor size, and the construction of the ceiling and roof. The
general layout is four rooms, and one hallway with a pitched roof. The
sum of all these is the objective function.

2. We want the pitch of the roof to be greater than 4/12.


3. Each room should be greater than 100 square feet.
4. The hallway should be no narrower than 3 feet.
5. The cabin inself should not exceed the lot size of 25-by-25 feet.
6. Supply an initial guess to the design variables, and run the
optimization.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 5‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Optimize a Portfolio Solution


edit PortfolioTask.mlx
Reference: Chapter 2
Topics: Optimization Live Task
6. Specify the risk-free rate as: RFrate = 1.0228^(1/250)
Exercise 7. Impose the following constraints:
The performance of the portfolio of stocks will be measured using the
• No stock can be more than 25% of the total portfolio.
Sharpe ratio. The Sharpe ratio balances the expected excess return of the
portfolio against its risk (standard deviation). The excess return is the • The weights must sum to 1.
difference between the return of the portfolio and the return of a risk-free • The risk must be no greater than a threshold (standard deviation ≤ 0.02)
investment such as a government bond. • No more than 40% of the stocks in the portfolio can come from any one
1. Load stock data from the file StockInfo.mat. country. (The first 3 stocks are US, the next 3 are Australian, the last 2
2. Plot the stock returns variable R against the stock risk variable v. are German.)
3. Open the optimization Live Task in a Live Script. Choose the 8. Supply an intial guess for the weights, run the optimization and visualize
Problem-based approach. the optimal allocation using the helper function threshpie.mlx.
4. Create a design variable of weights w representing the allocation of each
stock. The allocation of a stock is the proportion of the total portfolio
allotted to that stock.
5. The goal will be to maximize the objective function, which is a financial
metric known as the Sharpe ratio. This uses the allocations w, the
covariance S, and the returns R of the stocks in the portfolio.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 6‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Loan Rate II Solution


edit loanrate2_soln.mlx
Reference: Chapter 3
Topics: Additional input arguments

Exercise

Compute the remaining balance of a $100,000 loan at 2%-4% nominal


annual interest rate after 10 years for a given monthly payment. The goal is
to determine a monthly payment such that the loan has been paid back at
the end of the 10 years.
1. Create an optimization variable for monthly payment. Define the
optimization problem.
2. Optimize for an interest rate of 3%.
3. Find a suitable payment for monthly interest rates ranging from 2% in
steps of 0.1% to 4% in a for-loop.
4. Plot interest rates on the x-axis and the obtained monthly payments on
the y-axis.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 7‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Projectile Range Solution


edit ProjectileRangeSoln.mlx
Reference: Chapter 3
Topics: Implementing objective functions in problem-based workflow.
1. Write a MATLAB optimization expression to compute the range of the
Exercise projectile, which is calculated using the formula below. Regard V and θ
as design variables and g = 9.8 meters/(second)2 and h = 1 meter .
In this exercise, you will write an objective function for the range ( R ) of a
projectile. Suppose that an object of unit mass is projected from a point h 2. The constraints are that the cannon cannot tilt further than straight up.
meters above the horizontal, with initial speed V and initial angle θ to the The cannon ball cannot travel faster than 10 m/s.
horizontal. The projectile is subject to a constant acceleration due to gravity 3. Run the optimization using the solve function.
(g). The setup is shown in the following diagram. Your goal is to maximize  

the range of the projectile. r=
v cos θ 2 2
v sin θ + v sin θ + 2gh
g

g
V

h
r(θ, v, h)

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 8‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Global Positioning System Solution


edit gpsObj_Sol.mlx
Reference: Chapter 3
Topics: Writing objective functions.
Assume that the object, whose position (p1 , p2 , p3 ) is to be determined, is
Exercise underwater. That is, the following constraints have to be satisfied:

The position of an object should be determined using data provided by 5. Create the optimization variables with upper and lower bounds for each
three satellites. To this end, write a function according to the following variable.
specifications: 6. Write the nonlinear constraint(s) as an optimization expression.
1. The objective function should have the following variables:
– p: A 3-element vector containing 3-D position of object.
– satData: A structure variable with fields p21 + p22 + p23 ≤ 40,411,449
−6357 ≤ p1 ≤ 6357
• xPos: 3-element vector of x-coordinates of each satellite
−6357 ≤ p2 ≤ 6357
• yPos: 3-element vector of y-coordinates of each satellite −6357 ≤ p3 ≤ 6357
• zPos: 3-element vector of z-coordinates of each satellite
• dist: 3-element vector of distances from object to each satellite
2. For each satellite, the function should calculate the error given by:
3. The objective function should return the sum of the errors for each
 
(xP os − p1 )2 + (yP os − p2 )2 + (zP os − p3 )2 − dist2 

satellite computed above as a scalar number.


4. Load the satData structure from the gpsData.mat file. You can
use this to test your function. Call your function with a first input of
zeros(3,1) and a second input of satData. Your function should
return the value 4.5796e+08.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 9‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Vapor Pressure Solution


edit vaporPressure.mlx
Reference: Chapter 3
Topics: Fitting, Writing Objective Functions

Exercise

This exercise finds an equation of state for some measured ethanol data. The
equation for pressure as a function of temperature is:
log Pvap (T ) = A log T + B/T + C + DT 2

This equation has 4 parameters A, B, C, and D, which need to be estimated


from the data. To estimate these parameters, you can minimize the sum of
squared differences between the model and the measured ethanol data. This
can be stated mathematically as:
N
 2
min [log Pi − log Pvap (Ti )]
i=1

where Pi are the n measurements of the pressure at temperature Ti and


log Pvap (Ti ) are the model values of the pressure at the given temperatures.

1. Load the temperature and pressure data (T & P) from the


EthanolData.mat file.
2. Implement the vapor pressure equation above in the form of an
optimization expression. The inputs are a vector containing the four
parameters and a vector of temperatures. The output will be a vector
containing the logarithmic vapor pressures at the input temperatures.
3. Create an optimization problem and objective function to minimize
the squared differences between the data and model. The inputs are the
parameters and the experimental temperatures and pressures.
4. Solve the problem with an initial guess of x0=[-5;-6e3;50;6e-7].
5. Plot log(P) versus T for both the experimental data and the fit.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 10‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Air Conditioning I Solution


edit AC_Sol.mlx
Reference: Chapter 3
Topics: Writing Objective Functions Create anonymous function handle and test the nonlinear constraint
function.
Exercise edit AC_simulate.mlx
The goal is to optimize an air conditioning (AC) unit, by minimizing power
consumption under the constraints of maintaining the inside temperature 4. Test your nonlinear constraint function by evaluating the objective
within a desired range. The design vector is the vector of AC usages for each function and nonlinear constraint for the three conditions from step 2.
hour. A typical usage profile is depicted to the right: low usage at night and
high during day.

If the air conditioning is running at x percent, the power usage has been
found to be f(x) = 2.4087*x.^3 - 3.5839*x.^2 + 2.1752*x
as a fraction of maximum power usage.
1. Write an objective function that accepts an optimization variable vector
of AC usages as an input and returns the total power consumption. The
total power consumption is computed by evaluating the above f(x) for
each component of the input vector and summing the results.
2. Load AC_test.mat and test your objective function with different
scenarios using the evaluate function.
• AC_Usage1: AC is off all the time.
• AC_Usage2: AC runs at full power all the time.
• AC_Usage3: AC runs low at night, high during day.

Implement nonlinear constraints. The inside temperature should be between


18 and 20 degrees Celsius at all times. In addition, the final temperature
should be 19.5 degrees Celsius to match the initial state.
3. Create an optimization expression that:
• Accepts an AC usage profile as an input and, as an additional parameter,
a function handle that gives outside temperature over time.
• Calls AC_simulate.mlx (provided) with the above arguments. This
function returns a column vector of simulated inside temperatures.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 11‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Chip Production I Solution


edit chipSolution.mlx
Reference: Chapter 3
Topics: Providing objective function as input to optimization problem. Helper function:
Turning function handles into optimization expressions. edit chipObjective.mlx

Exercise
5. The upper-right corner of the uppermost, rightmost chip must be within
When placing chips on a silicon wafer, you want to minimize the area of the limits of the wafer.
the disk that is not occupied by a chip. The following steps will guide you
through a preliminary implementation of the corresponding optimization
problem.
1. Import the optimization problem parameters from the file
chipParams.mat. This file contains a structure with the following
fields:
– radius (of the wafer), set to 600 mm
– width (of the chip), set to 40 mm
– height (of the chip), set to 35 mm
2. Use the params structure and the existing function
chipObjective.mlx to define a function handle that can be
converted into an optimization expression and added to the problem.
3. Assume that the center of the wafer is at the origin of a 2-D set of axes.
The chip placement is defined by the coordinates of the upper-right
corner of the uppermost, rightmost chip on the disk. This is based on
the assumption that all other chips are tightly packed around and aligned
with this first chip. Define the initial chip coordinates to be
√  
x0 = 0.5 · radiuswafer · 1 1

Apply the following constraints for the two design variables, which are the
x- and y-coordinates of the upper-right corner of the uppermost chip in the
rightmost column on the wafer:
4. The center of the uppermost, rightmost chip must have nonnegative
coordinates.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 12‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Mean-Gini Portfolio Optimization Solution


edit mgVarsSol.mlx
Reference: Chapter 3
Topics: Distinguishing different types of constraints; specifying bounds Helper function:
and linear constraints. edit portGiniObj.mlx

Exercise

In this exercise, you will specify bounds and linear constraints for a portfolio
optimization problem. The mean-Gini portfolio optimization model
provides an alternative to standard mean-variance portfolio theory. The
latter assumes that asset returns come from a normal distribution, whereas
the mean-Gini framework is independent of the asset return distribution.

Given a 1-by-n row vector of portfolio weights w and an M -by- n


historical return series matrix R , the objective function to be minimized is
the following portfolio Gini coefficient
g(w, R) = 2 cov(S, F (S)),

where S = RwT is the vector of overall portfolio returns, and F (S) is the
cumulative distribution function (CDF) of S .
1. An implementation of this objective function can be found in the file
portGiniObj.mlx, and sample historical return series for N = 6
stocks are stored in mgPortData.mat. Create an optimization
problem with a function handle for portGiniObj.mlx as the
objective function, and with the data from mgPortData.mat. Use
the fcn2optimexpr function. The design variables are the portfolio
weights.
2. The portfolio weights w1 , w2 , …, wN must sum to one, and the mean
portfolio return, given by mean(portReturns), must be a fixed
constant (0.0084). Classify these types of constraints.
3. Impose portfolio diversification constraints: any given stock must not
exceed 40% of the overall portfolio, and any given stock must not form
less than 0.4% of the overall portfolio. Classify these constraints.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 13‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Optimal Cabin Construction II Solution


edit CabinSolution.mlx
Reference: Chapter 3
Topics: Problem-based Approach

Exercise

Minimize the cost of building a cabin with the problem-based workflow.


(Neglect the cost of doors, windows, etc.)
1. Write or convert the Live Task from “Optimal Cabin Construction I”
into code.
2. Run the optimization.
3. Compute the total height, width and length of the cabin from the
optimal solution.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 14‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Optimize a Portfolio II Solution


edit PortfolioSolution.mlx
Reference: Chapter 3
Topics: Problem-based optimization objective functions and constraints.

Exercise
1. Load StockInfo.mat into the MATLAB workspace.
2. Specify the risk-free rate as: RFrate = 1.0228^(1/250)
3. Use optimvar to create the weight/allocation design variable. Impose
bounds here such that weights are positive and no greater than 0.25.
4. Create optimization expressions defining portfolio risk, return and
Sharpe ratio.
5. Use the optimproblem function to create an optimization problem
with an objective sense to maximize the objective.
6. Make the Sharpe ratio expression the objective function of the problem.
7. Add constraints to the problem:
• The weights must sum to 1.
• The risk must be no greater than a threshold (standard deviation ≤ 0.02)
• No more than 40% of the stocks in the portfolio can come from any one
country. (The first 3 stocks are US, the next 3 are Australian, the last 2
are German.)
8. Supply an intial guess for the weights, run the optimization and visualize
the optimal allocation using the helper function threshpie.mlx.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 15‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

A Manufacturing Process Solution


edit manufactureSol
Reference: Chapter 4
Topics: Solvers
Your goal is to determine the number of units of each product that should be
Exercise produced each week in order to maximize the company’s revenue while not
creating more products than there is time to work on them.
A company manufactures technical products (P1 , P2 , P3 , P4 ) for an aerospace
firm. In the manufacturing process, each product spends a certain amount 1. Let xi be the number of units of product i made by the company.
of time in the electronics department, the hardware department, and the Determine the objective function and classify it.
inspection department, as summarized in the following table. The sell-price 2. Determine the constraints and classify them. Remember that the
of each product is also given. For example, product P4 requires 1 hour in the number of units of each product must be bounded below by zero, and
hardware department and sells for $150 US. the number of available hours in each department limits the number of
P1 P2 P3 P4 products that can be made.
In Electronics 0.5 1 1 0.5 3. Based on the classification of the constraints and objective function,
(hrs) determine the appropriate solver that will be chosen by default.
In Hardware (hrs) 0.5 0.5 1 1 4. Now, assume that you have to produce at least 20 units of each product
In Inspection (hrs) 0.5 0.5 1 0.5 in order to fulfill a contract. Implement this new constraint and run a
Sell Price ($100US) 1 1.5 2.5 1.5 new optimization.
5. Is each department being utilized to its full capacity when producing the
During a week, each department is available for a certain number of hours optimal amount of each product?
that is limited by the number of people working, as summarized in the
following table.
Department Available Hours
Electronics 160
Hardware 200
Inspection 240

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 16‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Estimating Model Parameters Solution


edit modelParamSol.mlx
Reference: Chapter 4
Topics: Solvers Helper function:
edit BSprice.mlx
Exercise

In this exercise, you will determine the parameters of a model that best
match observed data. In particular, you will estimate the volatility of Apple
stock using the Black-Scholes model and option pricing data.
1. Load the data contained in AppleOpts.mat and visualize it with the
commands

plot(Strike,Ask,".")
xlabel("Strike Price")
ylabel("Option Price")
2. The Black-Scholes model asserts that the option price is determined from
the strike price via the calculation performed inside the BSprice.mlx
function file. Plot several curves representing the predicted option price
using the command

plot(Strike,BSprice(Strike,v,0.0005))

where v takes values equal to 0.02, 0.01, 0.005, 0.001.


3. The calculation inside the BSprice function depends on two
unknown parameters: the stock’s volatility (vol) and the risk-free rate
(RFrate), in addition to the independent variable given by the stock’s
strike price (Strike). Treat the parameters vol and Rfrate as
design variables, and attempt to minimize the error given by
║Ask – BSprice(Strike,vol,RFrate)║2
4. When starting the optimization, consider using the initial values

vol0 = 0.7;
RFrate0 = 0.3;

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 17‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Air Conditioning II Solution


edit AC_Sol_2.mlx
Reference: Chapter 4
Topics: Solvers Helper function:
edit AC_visualize.mlx
Exercise

The goal is to optimize an air conditioning (AC) unit, by minimizing power


consumption under the constraints of maintaining the inside temperature
within a desired range. The objective function and the nonlinear constraint
have been created in the previous parts of this exercise. Now you can solve
the problem.
1. Which solver is chosen for the minimization problem with a nonlinear
objective function and nonlinear constraints?
2. Specify the following in the optimization problem:
– Objective function:
f(x) = 2.4087*x.^3 - 3.5839*x.^2 + 2.1752*x
– Initial guess ACInit
– No linear equations or inequalities
– Lower bounds: a vector of zeros
– Upper bounds: a vector of ones
– Nonlinear constraint
3. Call AC_visualize.mlx (provided) to visualize the results. Use the
optimized AC usage profile as first input.
4. Modify it so the problem is converted to the solver-based approach.
5. Use optimoptions function to create an options variable so that
– Relevant information about the state of the optimization will be displayed
after each iteration.
– The gradient supplied by the generated function file.
– The Hessian is supplied by the function:
hess(x) = @(x,lambda) diag(6*2.4087*x - 2*3.5839)

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 18‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Loan Rate III Solution


edit loanrate3_soln.mlx
Reference: Chapter 4
Topics: Display options, analyzing diagnostic output

Exercise

In this exercise, you will deal with this and also check whether the
optimization was successful for each interest rate.
1. Replace the objective function from “Loan Rate II” with a function
handle that uses the abs function and use fcn2optimexpr function
to add it to the problem.

objfun = @(payment) abs(FB(i) - payment*FA(i))


2. Issue a warning when the solver indicates that it ran into a potential
problem or when the remaining balance differs from 0 by more than
$0.01.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 19‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Minimizing a Mathematical Function II Solution


edit simpleObjSol.mlx
Reference: Chapter 4
Topics: Improving performance with gradient information. Helper functions:
edit simpleObjWGradient.mlx
Exercise edit simpleNLConsWGradient.mlx
In this exercise, you will supply gradient information to a solver-based
optimization routine.
1. Set up problem detailed in Minimizing a Mathematical Function exercise
with the problem-based workflow. Convert the problem into a structure.
2. Use optimoptions to use the gradients of the objective function and
nonlinear constraint in the optimization process.
3. Check to make sure the gradient is correct.

opts=optimoptions("fmincon",...
"SpecifyObjectiveGradient",true,...
"SpecifyConstraintGradient",true,...
"CheckGradients",true);
4. Measure how long it takes to execute the optimization with and without
the gradient check.
5. Open the generated function files containing the gradient information.

edit generatedConstraints.m
edit generatedObjective.m
6. (Bonus) Use custom function files to specify gradients.

edit simpleObjWGradient.mlx
edit simpleNLConsWGradient.mlx

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 20‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Using Hessian Information Solution


edit HessianSol.mlx
Reference: Chapter 4
Topics: Improving performance with second-order derivative information.
2. To provide the Hessian to the optimization routine, you need to first
Exercise A. Write a function, called computeHessian, with the signature

In this exercise, you will supply an optimization routine with second order H = computeHessian(x, lambda)
derivative information in addition to the gradient. Second order derivative
information is captured in a Hessian matrix. The Hessian matrix associated where H is the Hessian matrix, x is a vector of design variables, and
with a scalar function, L, has entries lambda is a structure with fields
∂2L
HL (i, j) = lambda.ineqnonlin: a vector containing λi
∂xi ∂xj
lambda.eqnonlin: a vector containing λeqi
For a general optimization problem, define the Lagrangian as the function
In the body of the computeHessian function, define H as
 
L(x) = f (x) + λi ci (x) + λeqi ceqi (x),
H = (H for obj) +
lambda.ineqnonlin*(H for constraint)
where f (x) is the objective function, ci (x) are inequality constraints, ceqi (x)
B. Modify the optimization options structure from the last exercise with the
are equality constraints, and λi and λeqi are scalars known as Lagrange
command
multipliers. It follows that you can write
 
opts = optimoptions(opts...
HL = Hf + λi Hci + λeqi Hceqi 'HessianFcn',@computeHessian);
3. Run the optimization using fmincon and the opts structure defined
1. Compute, manually, the 2-by-2 Hessian matrix of the Lagrangian above. Do you notice a decrease in execution time compared to only
associated with the optimization problem presented in the Minimizing supplying gradient information?
a Mathematical Function II exercise and modified in the Using Gradient
Information exercise.

Note that, for this particular problem, only the associated objective function
and one inequality constraint have a nonzero second derivative. Therefore,
you can write
HL = Hf + λHc

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 21‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

A Catenary Chain Solution


edit catenarysol.mlx
Reference: Chapter 4
Topics: Gradient information, Sparsity (medium vs. large scale)
5. Change the solver Algorithm to trust-region-reflective.
Exercise This invokes a matrix-free large-scale method, which is appropriate
because this problem is sparse. Compare the run time time to the others.
The goal of this exercise is to calculate the shape of a chain of masses
6. Set the HessPattern option to the hpat variable produced with
connected together with springs and held pinned between two walls. Under
the spdiags function in the command:
the influence of gravity, the chain will assume the shape of minimum energy.
hpat = spdiags(ones(2*N,7),-3:3,2*N,2*N);
The energy of such a chain is a function of the positions of the masses and
This tells the solver that the problem is banded about the diagonal and
is given by
significantly reduces the number of required calculations. Compare
k  2
N
 N
 −1
E = Eg + ES = mgyi + (xi+1 − xi )2 + (yi+1 − yi )2 − le this run time to those from the other methods. Which solver options
2
i=1 i=1 are best for this problem?
where m is the mass (in kg) of each connecting point, g = 9.8 m/s2 is the 7. Plot the catenary curve.
acceleration due to gravity, k is the spring stiffness (in N/m) and le is the
unstretched length of the springs (in m).

You will use different options on the above problem and time them to
determine which is fastest.
1. Set up the problem such that the optimization variables are the positions
of each link in the chain. The objective function is the energy. The
constraints are the position of the endpoints of the chain.
2. Use optimoptions to choose sqp for the Algorithm. This solver
should be appropriate for the above equations since they are quadratic
in the point positions. Observe the run time for this method with tic
and toc.
3. Convert problem to solver-based using the prob2struct function.
4. Next, switch the SpecifyObjectiveGradient flag to true in
the next code section using optimoptions. Adding the gradients
results in a significant improvement.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 22‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Chip Production II Solution


edit chipAlgorithmSolution.mlx
Reference: Chapter 4
Topics: Solvers and optimization options Helper function:
edit chipGraphics.mlx
Exercise

In this exercise, you will supply custom options to an optimization routine


to improve the outcome of the optimization.
1. To get an objective function handle and an initial guess, run the solution
file from the exercise Chip Production I, chipSolution.mlx.
2. Define a set of options using the optimoptions function:
– The DiffMinChange should be set to params.width.
– The Display should be set to off.
– The PlotFcn should be set to
@(x,dummy,s)chipGraphics(x,s,params)
3. Call solve function with the options variable.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 23‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Plant and Storage Location Solution


edit setupPlantStorage.mlx
Reference: Chapter 4
Topics: Solvers and optimization options Helper function:
edit plotPlantsAndStorageLocations.mlx
Exercise

Suppose there are a number of plants with static geographic positions. The storage locations should also be within the same district as the plants. In
Raw material is transported between each plant and the different storage this district there are 5 plants. The goal is to place 3 storage locations within
locations. the district, and determine how much material to transport, such that the
cost is minimized.
1. Write the objective function: 4. Solve the problem and plot the resulting locations with the
The cost of transporting this material is proportional to the distance plotPlantsAndStorageLocations function. Set the optimization
travelled d and quantity of material w. The goal is to minimize the cost. options to display output during the optimization process.

2. Write the design variables:


The design variables for this problem are the coordinates of the storage
locations, and the quantity of raw material being transported.

3. Write the constraints:


Each plant has a minimum requirement of raw material r, and each storage
location has a maximum capacity c. The amount of material transported
must be positive.

For security, the distance between any given plant and storage location must
be 2 km or greater.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 24‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Mars Orbit Prediction Solution


edit MarsCurveFit.mlx
Reference: Chapter 4
Topics: Solvers and optimization options
4. There are no constraints. Minimize the objective function with initial
Exercise parameter values: [238; 0.75; 0.1; 2*pi/0.03; 0]

To get a craft from Earth to Mars, a maneuver known as a Hohmann transfer 5. Use optimoptions to display iterations and plot function value and
can be used to preserve fuel. The sun is treated as a central gravitational design variable values.
body, and the craft goes from Earth’s orbit to an elliptical orbit around the
sun, such that it intersects with Mars’ orbit just in time to enter the planet’s 6. Plot the argument of the perifocus data and the predicted values
gravitational range on influence. This trip requires precise timing and together.
accurate prediction of the relative positions of bodies. To predict position
variables over time, curve fitting can be used: particularly minimizing the
error between data and a parametric function.

1. Load Mars orbit data from MarsOrbitParams.mat and plot the


argument of perifocus over time. The goal is to fit a parametric curve to this
data.

plot(MarsData.tsc,MarsData.W,"b-")

The data for this exercise is from:


Giorgini, JD and JPL Solar System Dynamics Group, NASA/JPL Horizons
On-Line Ephemeris System,
<http://ssd.jpl.nasa.gov/?horizons>,
data retrieved 2020-06-26.

2. Set up the optimization problem so that the design variables


are the coefficients in the definition of the parametric curve.

3. The objective function is the error (root mean square) between the
fitted parametric curve and the real data points. So the curve predicts the
position of Mars.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 25‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Efficient Frontier Solution


edit EFSolution.mlx
Reference: Chapter 4
edit EFSolutionQP.mlx
Topics: Solvers and optimization options

Exercise 7. Use the tic and toc functions to time how long it takes the for-loop
The efficient frontier is the curve of points in the “reward-risk” plane that to run. Take note of the solver used by default (fmincon).
represents the least risky portfolio possible for a given reward. You can find 8. Plot these values with the risk and reward of each stock.
the efficient frontier by stepping through a range of values for the portfolio 9. Change the objective function to be the square of risk. This will change
return, and minimizing the standard deviation (risk) of the portfolio for the default solver to quadprog.
each (fixed) value of the return.
In this exercise, the objective function to minimize is the standard devia- 10. Time how long the for-loop takes to run with this solver.
tion, and the fixed return value becomes an equality constraint. Because
return is calculated as a weighted average, it is a linear equality constraint.
All allocations must be between 0 and 1, and they must sum to 1. The port-
folio return must equal a fixed value. This minimization problem is run
repeatedly for various values of fixed portfolio returns.
1. Load StockInfo.mat
2. Specify a range of fixed returns to inspect.
givenReturn = linspace(min(R),max(R),200);
3. The design variables for this problem are the allocations, or weights, of
each stock in the portfolio.
4. The constraints are that the weights are positive and no greater than
1. The weights must also sum to 1. The return of the portfolio must be
equal to a given fixed return (this can be done within a for-loop).
5. The goal is to minimize risk for a given fixed return. The objective
function is risk.

6. Use optimoptions to turn off the display for the solver. Use a for-loop
to optimize risk for each value in givenReturn. You will then have a
minimum risk value for each return.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 26‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Weber’s Problem Solution


edit weberRunSol.mlx
Reference: Chapter 5
Topics: Global optimization

Exercise

A warehouse should be located near three store locations. There are two
stores at (-10, -10) and (5, 8) which need two truck loads per week and
a store at (25, 30) which requires one truck load a week. The warehouse
should not be located near the nuclear power plant at (0,0). It is equally
important to have the warehouse close to both of the bigger stores and to
stay away from the nuclear power plant.

An objective function for this problem takes the form of weighted distance:
4
 
f (x) = wi (x1 − zi1 )2 + (x2 − zi2 )2
i=1

with truck loads per week: w = [2; -4; 2; 1]

and geographic locations: z = [-10 -10; 0 0; 5 8; 25 30]

Consider only a geographic region where the x-coordinate is between -20


and 35 and the y-coordinate is between -20 and 40.

Set up and solve this problem using multistart or globalsearch


solver with the solve function. Start with 100 points.

Visualize the objective function using the evaluate function and the
contour plot function. Plot the geographic locations of all the facilities
with the contour plot.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 27‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Hunter’s Riddle Solution


edit hunt_programmatic.mlx
Reference: Chapter 5
Topics: Global optimization, integer programming, genetic algorithms Helper functions:
edit hunt_create.mlx
Exercise edit hunt_mutate.mlx
edit hunt_crossover.mlx
A hunter was asked by his wife about his success. He answered that he had
hunted 100 animals – deer, foxes and rabbits – and had received $100 for
them on the market. Can you tell the wife how many deer, foxes and rabbits
the hunter had brought to the market, if it is known that the price for deer is
$10, the price for a fox is $3 and the price for a rabbit is $0.50?

Hints:
• This is an integer problem. Use genetic algorithms to solve it.
• Solving an equation A*x = b is the same as minimizing (A*x–b).^2.
Use this and total revenue being $100 to write the fitness function. Set
FitnessLimit=0 (Why? Can we do better?).
• Lower bound: 1 for each kind of animal (assumption that none is 0)
• Upper bound: The revenue from any kind of animal should not exceed
$100, no more than 100 animals of any kind.
• Creation function: Choose (reasonable) random integers for deer and
foxes and choose number of rabbits in order to have 100 animals total
• Mutation function: Make small random integer changes to the numbers
of deer and foxes (within the bounds) and choose rabbits accordingly.
• Crossover function: Randomly choose numbers of deer and foxes from
a parent and choose rabbits accordingly.
• Two subpopulations with 30 individuals each, simulated over a
maximum of 500 generations, should give you excellent results.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 28‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Chip Production III Solution


edit chipSolutionInt.mlx
Reference: Chapter 5
Topics: Global optimization

Exercise

Because the objective function of the chip production optimization problem


has an integer output (minus the number of chips placed on the wafer),
traditional solvers from the Optimization Toolbox can be expected to run
into problems when trying to approximate gradient information. Thus, it
seems advisable to use solvers from the Global Optimization Toolbox as an
alternative.
1. Open the solution file chipSolution.mlx.
2. Change the function input of optimvar to restrict the values to an
integer.

x = optimvar("x",[1,2],"LowerBound",lb,...
"Type","integer")
3. Run the solve function and take note of the solver used.
4. Use the chipGraphics.mlx function file to visualize the solution.

chipGraphics(solution.x,"init",params)

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 29‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Optimal Cabin Construction III Solution


edit CabinSolutionMulti.mlx
Reference: Chapter 5
Topics: Multiobjective Optimization

Exercise

Minimize the cost of building a cabin, and maximize the volume of the
cabin. (Neglect the cost of doors, windows, etc.)
1. Open the file CabinSolution.mlx.
2. Add a second objective function characterizing cabin volume.
3. Use gamultiobj solver and plot the resulting Pareto front.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 30‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Plant and Storage Location II Solution


edit PlantStorageGlobal.mlx
Reference: Chapter 5
Topics: Global Optimization

Exercise
Optimization Toolbox™ solvers for nonlinear problems, such as fmincon,
look for a local minimum, starting with a user-supplied initial guess.
Although you can verify that the returned solution is a local minimum, can
you determine whether it is the global minimum?

When the number of design variables is large, or the objective function is


not smooth, it is particularly difficult to be sure whether the given solution
is truly the best of all possibilities.

1. Run the solution from last Plant and Storage Location exercise:
setupPlantStorage.mlx which minimizes cost.
2. Convert the problem to a structure to use the solver-based approach.
3. Run global optimization solvers on this problem: GlobalSearch and
MultiStart with 60 starting points.
4. Plot the resulting storage locations using the
plotPlantsAndStorageLocations function.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 31‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Mars Orbit Prediction II Solution


edit MarsCurveFitSurrogate.mlx
Reference: Chapter 5
Topics: Surrogate optimization

Exercise

The data for this exercise is from:


Giorgini, JD and JPL Solar System Dynamics Group, NASA/JPL Horizons
On-Line Ephemeris System,
<http://ssd.jpl.nasa.gov/?horizons>,
data retrieved 2020-06-26.

The fitted curve from the previous Mars Orbit Prediction exercise may not
be satisfactory. It appears there could be another sine function term.

To fit this curve, use surrogate optimization to minimize the root


mean square. This will require bounds on the parameters. The first five
parameters can be used from the last exercise. The new sine term will
require three additional parameters.

Open the script MarsCurveFitSurrogate_Start.mlx to get


started.

Note that surrogate optimization is intended for problems that take a long
time to solve. There is an option that allows the user to save a checkpoint
file.

opts = optimoptions("surrogateopt",...
"CheckpointFile","checkfile.mat")

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 32‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix A: Exercises

Optimize a Larger Portfolio Solution


edit BigPortfolioSolution.mlx
Reference: Chapter 4 and 5
edit BigPortfolioMultiSolution.mlx
Topics: Optimization options and global/multiobjective optimization

Exercise
The goal here is to a similar optimization to the one found in “Optimize a
Portfolio II”. However, this portfolio has more stocks, and less constraints
placed on it. You will change one of the constraints into a second objective
function to perform a multiobjective optimization.
1. Load FTSEStocks.mat
2. Specify the risk-free rate as: RFrate = 1.0228^(1/250)
3. Maximize the Sharpe ratio of the portfolio.
4. Limit the allocation sum to 1 and the risk to be no greater than 0.02.
5. Use the optimoptions function to tell the solver to use the sqp
algorithm, to display the iterations during solving, and to plot the
function value, design variable values and the first order derivative.
6. Run the solver and take note of the results.
7. Rework the problem such that there is no constraint on risk. Instead,
risk will become a second objective function to be minimized which the
Sharpe ratio is maximized.
8. Run the solve function, and plot the resulting Pareto front.
9. Choose a working point and compare the results to those of part 6.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® A - 33‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es

Optimization Techniques in MATLAB®

Appendix B: Extra
Examples

B - 1‌
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix B: Extra Examples

Course Example: Suspension System Try

View the suspension model in Simulink.


The Simulink® model suspension.slx models the response of an
automotive suspension system to a bump. >> open suspension
Open the script susp_problem.mlx and run the first section to load
The model requires:
the model parameters, which are stored in a structure named params.
• Values that define the stiffness and damping coefficients for the >> edit susp_problem
suspension springs.
• A set of fixed parameters that describe the physical configuration of the
car.

These are assumed to be in the Simulink source workspace in the form of a


vector x and a structure params that has the following fields:
• g (9.81): Gravity in m/s²
• Lf (0.9): Distance of front suspension from center of gravity in m
• Lr (1.2): Distance of rear suspension from center of gravity in m
• rf (0.45): Distance of front seats from center of gravity in m
• rr (1.08): Distance of rear seats from center of gravity in m
• rt (1.32): Distance of trunk from center of gravity in m
• Mb (1300): Mass of loaded car in kg
• Iyy (2100): Car Inertia about y-axis in kg·m²
• hb (0.1): Height of test bump in m

When run for a prescribed time span, the model returns the times and the
corresponding values of the angular acceleration θ̈ and vertical acceleration
z̈ .

The goal is to choose the stiffness and damping coefficients to minimize


passenger discomfort when driving over the test bump at a predefined
speed. The passenger discomfort is measured by the sum of peak and total
acceleration for both front and rear passengers.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® B - 2‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix B: Extra Examples

What are the Variables? Try

Run the second section of susp_problem.mlx to define the design


The four design variables are:
variables using the optimvar function. Within the optimvar function,
Stiffness of front suspension Kf lower bounds and upper bounds are specified for each design variable.

Damping coefficient of front suspension Cf The following physical limitations are placed on the values of the four design
variables:
Stiffness of rear suspension Kr
1. Stiffness of front suspension 104 ≤ Kf ≤ 105
Damping coefficient of rear suspension Cr 2. Damping coefficient of front suspension 100 ≤ Cf ≤ 104
3. Stiffness of rear suspension 104 ≤ Kr ≤ 105
4. Damping coefficient of rear suspension 100 ≤ Cr ≤ 104

Kf = optimvar("Kf", "LowerBound", 10^4,...


"UpperBound", 10^5)

Kf Kr
Cf Cr

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® B - 3‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix B: Extra Examples

What is the Goal? Try

The objective function is defined in a function file discomfort.mlx.


The goal is to choose the suspension parameters to minimize passenger
This function file simulates the suspension over time using the Simulink
discomfort as measured by the sum of peak and total acceleration.
model suspension.slx.
The Simulink model suspension.slx returns the angular and vertical >> edit discomfort
acceleration, measured over a range of times. From this, the acceleration of
Run the third section of susp_problem.mlx to create an optimization
the passengers can be calculated as
problem using the optimproblem function. The fcn2optimexpr
a = (z̈ + rf · θ̈)2 + (z̈ − rr · θ̈)2 function passes the function handle of discomfort.mlx to define the
The discomfort, then, is calculated as (max(a) + sum(a))/1000. objective function.

If the objective function cannot be written in a closed-form symbolic


expression, then a function handle can be passed to the optimization problem
by using the fcn2optimexpr function. This converts the function into
an optimization expression.

prob = optimproblem
prob.Objective = fcn2optimexpr(@discomfort, ...
[Kf, Cf, Kr, Cr], params)

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® B - 4‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix B: Extra Examples

What are the Constraints? Try

Run the fourth section of susp_problem.mlx to add constraints to the


Without constraints, the optimal stiffness parameters are unphysical
optimization problem.
(negative), and the damping coefficients are unrealistically high. There is
also no guarantee that the car would be level at rest or that other physical
requirements would be met.

For the car to be level when at rest, the springs must also satisfy the condition

L f k f = Lr k r

This constraint is added to the optimization problem through the command:

prob.Constraints.CarLevel = params.Lf * Kf == ...


params.Lr * Kr

The following physical constraints are also imposed: Limitations on natural


• The system’s natural frequencies of vibration are below 2 Hz: frequencies of vibration &
1

kf (Lf + Lr ) 1

kr (Lf + Lr )
damping ratios
≤2 ≤2
π 8Mb · Lr π 8Mb · Lf

• The damping ratios are between 0.3 and 0.5:

 
Nonlinear
0.3 ≤ cf
Lf + Lr
≤ 0.5 0.3 ≤ cr
Lf + Lr
≤ 0.5 Constraints
8kf · Mb · Lr 8kr · Mb · Lf

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® B - 5‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix B: Extra Examples

Solve the Problem Try

Run the fifth section of susp_problem.mlx to create a structure to


Passenger discomfort is calculated from the angular and vertical acceleration,
supply the solver with an initial guess, and use the solve function on the
which is, in turn, calculated by the Simulink model suspension.slx.
problem.
This model can be run programmatically for a given time interval using
the sim function. The model assumes that the source workspace contains a Run the sixth section of susp_problem.mlx to visualize the results.
vector x and a structure params. This uses a helper function visualizebump.mlx.
>> edit visualizebump
Pass the problem into the solve function to find a solution. The first
output is the optimal design variable values, and the second output is the
corresponding objective function value.

[xOpt, fval] = solve(prob, x0)

suspension.slx


Kf
 Cf  params
x= 
 Kr 
t
Cr

Passenger
acceleration: z̈(t) θ̈(t)
a = (z̈ + rf · θ̈)2 + (z̈ − rr · θ̈)2

Discomfort: (max(a)+sum(a))/1000
© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® B - 6‌
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix B: Extra Examples

Creating an Optimization Problem Try

Structure Make a structure, from the optimization problem, to use the solver-based
approach.
You can create a structure programmatically using the prob2struct >> ps = prob2struct(prob,x0)
function to convert from the problem-based approach to the solver-based
approach, or start with the createOptimProblem function.
You can test your problem structure by applying the appropriate local solver:
You can provide all the components of the optimization problem using
[x,f,flag] = fmincon(myprob);
parameter name-parameter value pairs. The table below lists the parameter
names for the problem components.

Component Parameter Name(s)


Objective function objective
Starting point (used to determine x0
dimensionality)
Bounds lb ub
Linear constraints Aeq beq Aineq bineq
Nonlinear constraints nonlcon prob2struct createOptimProblem
Options options

Note that when starting from the solver-based approach, that the objective
function is always minimized. So if you want to maximize it, you minimize
the negative of that function.

myprob = createOptimProblem("fmincon","objective",@myobjective,"x0",ones(n,1),"lb",zeros(n,1),...
"Aeq",ones(1,n),"beq",1,"options",optimoptions("fmincon","Algorithm","interior-point"));

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® B - 7‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix B: Extra Examples

Providing Derivative Information Try

To provide derivative information use the options field of the problem


Optimization algorithms commonly use the gradient of the objective
structure.
function to calculate the next iteration. The gradient of a function of
multiple variables is the vector of derivatives of the function with respect to >> ps.options = optimoptions("fmincon",...
each variable. "SpecifyConstraintGradient",true)
The prob2struct function generates objective and nonlinear
In the problem-based workflow, MATLAB automatically differentiates the constraint function files. You can also supply your own function file by
objective and constraint functions, unless they are defined in function files adding a function handle to the problem structure in place of these.
(and added to the problem using the fcn2optimexpr function). In that
case, the Optimization Toolbox solvers approximate derivatives using some >> edit generatedConstraints.m
form of finite difference formula. This requires evaluating the objective
function numerous times. opt = optimoptions("fmincon",...
"SpecifyConstraintGradient",true,...
Converting a problem into a structure can generate objective and constraint "SpecifyObjectiveGradient",true,...
function files in your current folder. They are listed in the structure as "CheckGradients",true)
function handles and can be used. You can also replace them in the structure
with other function file handles. With some algorithms, you can also supply second derivative information
in the form of the Hessian matrix. For solvers and algorithms that
It is therefore more efficient and more accurate to use an exact gradient do not have nonlinear constraints, the Hessian matrix is supplied as
if possible. You can provide the gradient to your objective function by a third output from the objective function. For fmincon with the
returning it as a second output from your function. Note that it should be interior-point algorithm, the Hessian matrix is supplied as a
an n-by-1 vector (where n is the number of design variables). separate function file.
Use optimoptions to set the SpecifyObjectiveGradient option See the documentation for details and examples. Calculating gradients
to true, to make the solver use the user-supplied derivative information. and Hessian matrices manually can be difficult and time-consuming. With
You can also provide the gradient to the nonlinear constraints in the same Symbolic Math Toolbox™ you can calculate analytic derivatives automatically.
way. Use optimoptions to set the SpecifyConstraintGradient
option to true. Use the CheckGradients option to have the solver
estimate the derivative information with finite differences and compare this
to the gradients you have supplied. This may be left out or set to false (the
default value) if you have already checked the gradients in your problem.
This speeds up the evaluation.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® B - 8‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Appendix B: Extra Examples

Function Files for Derivative Information Try

Use the fmincon function to find the solution instead of solve. The
An objective function definition without a gradient has one output: the
change in syntax here is due to the problem becoming “Solver-based”
function value. If you are supplying a gradient to the solver this is specified
rather than “Problem-based”.
as the second output of the function.
>> [xOpt,fval,flag] = fmincon(ps)
function [val, grad] = Obj(x,c) >> visualizebump([xOpt(3); xOpt(1);...
val = mean(1-(c*x).^2); xOpt(4);xOpt(2)],params)
grad = -2*mean(c*x.*c);

To supply this to the solver, supply an anonymous function handle in the function [cIn,cEq,gradIn,gradEq] = Constr(x)
cIn = [];
problem structure. To create an anonymous function handle from a function
cEq = sum(x.^2) - 1;
file, use the @ symbol and specify the design variable in parentheses. Then
gradIn = [];
write the function name and the inputs. The same can be done for any
gradEq = 2*x;
nonlinear constraints.

problem.objective = @(x) Obj(x,c) The constraint function is passed as a function handle to the problem
structure.
Every nonlinear constraint can be written in the form f (x) = 0 or f (x) ≤ 0
problem.nonlcon = @(x) Constr(x)
for some nonlinear function f . Multiple constraints can be written in the
same form, with f representing a vector-valued function, with each element
corresponding to the function value of a single constraint.

In MATLAB®, nonlinear constraints are specified using a function that


accepts a single input (the vector of design variables) and returns two
outputs, or four outputs if you include gradients. The first output is a vector
of function values for the inequality constraints. The second output is a
vector of function values for the equality constraints. The third and the
fouth outputs are the respective gradients of the first two outputs. If there
are no equality or inequality constraints or gradients, return an empty array
([]) for the appropriate output.

© 2023 by The MathWorks, Inc. Optimization Techniques in MATLAB® B - 9‌


Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es
Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es

mathworks.com/training
© 2020 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc.
See mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may
be trademarks or registered trademarks of their respective holders.

Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es Coro Cantero - coro.cantero@tekniker.es

You might also like