100% found this document useful (3 votes)
932 views

Instant Download MATLAB Optimization Toolbox User S Guide The Mathworks PDF All Chapters

User

Uploaded by

syazaasae
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
932 views

Instant Download MATLAB Optimization Toolbox User S Guide The Mathworks PDF All Chapters

User

Uploaded by

syazaasae
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Download the full version of the textbook now at textbookfull.

com

MATLAB Optimization Toolbox User s Guide The


Mathworks

https://textbookfull.com/product/matlab-
optimization-toolbox-user-s-guide-the-mathworks/

Explore and download more textbook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

MATLAB Global Optimization Toolbox User s Guide The


Mathworks

https://textbookfull.com/product/matlab-global-optimization-toolbox-
user-s-guide-the-mathworks/

textbookfull.com

MATLAB Econometrics Toolbox User s Guide The Mathworks

https://textbookfull.com/product/matlab-econometrics-toolbox-user-s-
guide-the-mathworks/

textbookfull.com

MATLAB Bioinformatics Toolbox User s Guide The Mathworks

https://textbookfull.com/product/matlab-bioinformatics-toolbox-user-s-
guide-the-mathworks/

textbookfull.com

Humanistic Psychology: Current Trends and Future Prospects


Richard House

https://textbookfull.com/product/humanistic-psychology-current-trends-
and-future-prospects-richard-house/

textbookfull.com
Out of Eden: The Surprising Consequences of Polygamy 1st
Edition David P. Barash

https://textbookfull.com/product/out-of-eden-the-surprising-
consequences-of-polygamy-1st-edition-david-p-barash/

textbookfull.com

Home to You 1st Edition Lacey Black & Kaylee Ryan [Black

https://textbookfull.com/product/home-to-you-1st-edition-lacey-black-
kaylee-ryan-black/

textbookfull.com

Further Advances in Pragmatics and Philosophy Part 2


Theories and Applications Alessandro Capone

https://textbookfull.com/product/further-advances-in-pragmatics-and-
philosophy-part-2-theories-and-applications-alessandro-capone/

textbookfull.com

Netter's Head and Neck Anatomy for Dentistry 3 3rd Edition


Neil S. Norton Phd

https://textbookfull.com/product/netters-head-and-neck-anatomy-for-
dentistry-3-3rd-edition-neil-s-norton-phd/

textbookfull.com

The subversive copy editor advice from Chicago or how to


negotiate good relationships with your writers your
colleagues and yourself Second Edition Saller
https://textbookfull.com/product/the-subversive-copy-editor-advice-
from-chicago-or-how-to-negotiate-good-relationships-with-your-writers-
your-colleagues-and-yourself-second-edition-saller/
textbookfull.com
Strategy- An International Perspective - 6th ed. edition
Bob De Wit

https://textbookfull.com/product/strategy-an-international-
perspective-6th-ed-edition-bob-de-wit/

textbookfull.com
Optimization Toolbox™
User's Guide

R2020a
How to Contact MathWorks

Latest news: www.mathworks.com

Sales and services: www.mathworks.com/sales_and_services

User community: www.mathworks.com/matlabcentral

Technical support: www.mathworks.com/support/contact_us

Phone: 508-647-7000

The MathWorks, Inc.


1 Apple Hill Drive
Natick, MA 01760-2098
Optimization Toolbox™ User's Guide
© COPYRIGHT 1990–2020 by 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. No part of this manual may be photocopied or reproduced in any form
without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through
the federal government of the United States. By accepting delivery of the Program or Documentation, the government
hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer
software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014.
Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain
to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and
Documentation by the federal government (or other entity acquiring for or through the federal government) and shall
supersede any conflicting contractual terms or conditions. If this License fails to meet the government's needs or is
inconsistent in any respect with federal procurement law, the government agrees to return the Program and
Documentation, unused, to The MathWorks, Inc.
Trademarks
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be
trademarks or registered trademarks of their respective holders.
Patents
MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for
more information.
Revision History
November 1990 First printing
December 1996 Second printing For MATLAB® 5
January 1999 Third printing For Version 2 (Release 11)
September 2000 Fourth printing For Version 2.1 (Release 12)
June 2001 Online only Revised for Version 2.1.1 (Release 12.1)
September 2003 Online only Revised for Version 2.3 (Release 13SP1)
June 2004 Fifth printing Revised for Version 3.0 (Release 14)
October 2004 Online only Revised for Version 3.0.1 (Release 14SP1)
March 2005 Online only Revised for Version 3.0.2 (Release 14SP2)
September 2005 Online only Revised for Version 3.0.3 (Release 14SP3)
March 2006 Online only Revised for Version 3.0.4 (Release 2006a)
September 2006 Sixth printing Revised for Version 3.1 (Release 2006b)
March 2007 Seventh printing Revised for Version 3.1.1 (Release 2007a)
September 2007 Eighth printing Revised for Version 3.1.2 (Release 2007b)
March 2008 Online only Revised for Version 4.0 (Release 2008a)
October 2008 Online only Revised for Version 4.1 (Release 2008b)
March 2009 Online only Revised for Version 4.2 (Release 2009a)
September 2009 Online only Revised for Version 4.3 (Release 2009b)
March 2010 Online only Revised for Version 5.0 (Release 2010a)
September 2010 Online only Revised for Version 5.1 (Release 2010b)
April 2011 Online only Revised for Version 6.0 (Release 2011a)
September 2011 Online only Revised for Version 6.1 (Release 2011b)
March 2012 Online only Revised for Version 6.2 (Release 2012a)
September 2012 Online only Revised for Version 6.2.1 (Release 2012b)
March 2013 Online only Revised for Version 6.3 (Release 2013a)
September 2013 Online only Revised for Version 6.4 (Release 2013b)
March 2014 Online only Revised for Version 7.0 (Release 2014a)
October 2014 Online only Revised for Version 7.1 (Release 2014b)
March 2015 Online only Revised for Version 7.2 (Release 2015a)
September 2015 Online only Revised for Version 7.3 (Release 2015b)
March 2016 Online only Revised for Version 7.4 (Release 2016a)
September 2016 Online only Revised for Version 7.5 (Release 2016b)
March 2017 Online only Revised for Version 7.6 (Release 2017a)
September 2017 Online only Revised for Version 8.0 (Release 2017b)
March 2018 Online only Revised for Version 8.1 (Release 2018a)
September 2018 Online only Revised for Version 8.2 (Release 2018b)
March 2019 Online only Revised for Version 8.3 (Release 2019a)
September 2019 Online only Revised for Version 8.4 (Release 2019b)
March 2020 Online only Revised for Version 8.5 (Release 2020a)
Contents

Acknowledgments

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx

Getting Started
1
Optimization Toolbox Product Description . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Key Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2

First Choose Problem-Based or Solver-Based Approach . . . . . . . . . . . . . . 1-3

Solve a Constrained Nonlinear Problem, Problem-Based . . . . . . . . . . . . . 1-5

Solve a Constrained Nonlinear Problem, Solver-Based . . . . . . . . . . . . . . 1-11


Typical Optimization Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
Problem Formulation: Rosenbrock's Function . . . . . . . . . . . . . . . . . . . . . 1-11
Define the Problem in Toolbox Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12
Run the Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13
Interpret the Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-16

Set Up a Linear Program, Solver-Based . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18


Convert a Problem to Solver Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18
Model Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18
Solution Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-19
Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-24

Set Up a Linear Program, Problem-Based . . . . . . . . . . . . . . . . . . . . . . . . . 1-25


Convert a Problem to Solver Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-25
Model Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-25
First Solution Method: Create Optimization Variable for Each Problem
Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-26
Create Problem and Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-27
Create and Include Linear Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . 1-27
Solve Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-27
Examine Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-28
Second Solution Method: Create One Optimization Variable and Indices
..................................................... 1-29
Set Variable Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-29
Create Problem, Linear Constraints, and Solution . . . . . . . . . . . . . . . . . . 1-29
Examine Indexed Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-30
Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-30

v
Setting Up an Optimization
2
Optimization Theory Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2

Optimization Toolbox Solvers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3

Optimization Decision Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4

Choosing the Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6


fmincon Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6
fsolve Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
fminunc Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
Least Squares Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
Linear Programming Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9
Quadratic Programming Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9
Large-Scale vs. Medium-Scale Algorithms . . . . . . . . . . . . . . . . . . . . . . . . 2-10
Potential Inaccuracy with Interior-Point Algorithms . . . . . . . . . . . . . . . . 2-10

Problems Handled by Optimization Toolbox Functions . . . . . . . . . . . . . . 2-12

Complex Numbers in Optimization Toolbox Solvers . . . . . . . . . . . . . . . . . 2-14

Types of Objective Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16

Writing Scalar Objective Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17


Function Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17
Anonymous Function Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-18
Including Gradients and Hessians . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-19

Writing Vector and Matrix Objective Functions . . . . . . . . . . . . . . . . . . . . 2-26


What Are Vector or Matrix Objective Functions? . . . . . . . . . . . . . . . . . . . 2-26
Jacobians of Vector Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-26
Jacobians of Matrix Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-27
Jacobians with Matrix-Valued Independent Variables . . . . . . . . . . . . . . . . 2-27

Writing Objective Functions for Linear or Quadratic Problems . . . . . . . 2-29

Maximizing an Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-30

Matrix Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-31

Types of Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-32

Iterations Can Violate Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-33


Intermediate Iterations can Violate Constraints . . . . . . . . . . . . . . . . . . . 2-33
Algorithms That Satisfy Bound Constraints . . . . . . . . . . . . . . . . . . . . . . . 2-33
Solvers and Algorithms That Can Violate Bound Constraints . . . . . . . . . . 2-33

Bound Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-34

Linear Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-35


What Are Linear Constraints? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-35
Linear Inequality Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-35

vi Contents
Linear Equality Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-36

Nonlinear Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-37


Including Gradients in Constraint Functions . . . . . . . . . . . . . . . . . . . . . . 2-38
Anonymous Nonlinear Constraint Functions . . . . . . . . . . . . . . . . . . . . . . 2-38

Or Instead of And Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-41

How to Use All Types of Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-45

Objective and Nonlinear Constraints in the Same Function . . . . . . . . . . 2-48

Objective and Constraints Having a Common Function in Serial or


Parallel, Problem-Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-52

Passing Extra Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-57


Extra Parameters, Fixed Variables, or Data . . . . . . . . . . . . . . . . . . . . . . . 2-57
Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-57
Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-58
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-59

What Are Options? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-60

Options in Common Use: Tuning and Troubleshooting . . . . . . . . . . . . . . 2-61

Set and Change Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-62

Choose Between optimoptions and optimset . . . . . . . . . . . . . . . . . . . . . . 2-63

View Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-66

Tolerances and Stopping Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-68

Tolerance Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-71

Checking Validity of Gradients or Jacobians . . . . . . . . . . . . . . . . . . . . . . . 2-74


Check Gradient or Jacobian in Objective Function . . . . . . . . . . . . . . . . . . 2-74
How to Check Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-74
Example: Checking Derivatives of Objective and Constraint Functions . . 2-75

Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-80

Examining Results
3
Current Point and Function Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2

Exit Flags and Exit Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3


Exit Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3
Exit Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4
Enhanced Exit Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4

vii
Exit Message Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7

Iterations and Function Counts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-9

First-Order Optimality Measure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11


What Is First-Order Optimality Measure? . . . . . . . . . . . . . . . . . . . . . . . . 3-11
Stopping Rules Related to First-Order Optimality . . . . . . . . . . . . . . . . . . 3-11
Unconstrained Optimality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11
Constrained Optimality Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
Constrained Optimality in Solver Form . . . . . . . . . . . . . . . . . . . . . . . . . . 3-13

Iterative Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14
Common Headings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14
Function-Specific Headings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-15

Output Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21

Lagrange Multiplier Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-22

Hessian Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24


Returned Hessian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24
fminunc Hessian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24
fmincon Hessian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-25

Plot Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27


Plot an Optimization During Execution . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27
Using a Plot Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27

Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-32


What Is an Output Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-32
Example: Using Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-32

Steps to Take After Running a Solver


4
Overview of Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2

When the Solver Fails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3


Too Many Iterations or Function Evaluations . . . . . . . . . . . . . . . . . . . . . . 4-3
Converged to an Infeasible Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6
Problem Unbounded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7
fsolve Could Not Solve Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8

Solver Takes Too Long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9


Enable Iterative Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9
Use Appropriate Tolerances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9
Use a Plot Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9
Use 'lbfgs' HessianApproximation Option . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Enable CheckGradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Use Inf Instead of a Large, Arbitrary Bound . . . . . . . . . . . . . . . . . . . . . . 4-10
Use an Output Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10

viii Contents
Use a Sparse Solver or a Multiply Function . . . . . . . . . . . . . . . . . . . . . . 4-10
Use Parallel Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11

When the Solver Might Have Succeeded . . . . . . . . . . . . . . . . . . . . . . . . . . 4-12


Final Point Equals Initial Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-12
Local Minimum Possible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-12

When the Solver Succeeds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18


What Can Be Wrong If The Solver Succeeds? . . . . . . . . . . . . . . . . . . . . . 4-18
1. Change the Initial Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18
2. Check Nearby Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-19
3. Check your Objective and Constraint Functions . . . . . . . . . . . . . . . . . 4-20

Local vs. Global Optima . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-22


Why the Solver Does Not Find the Smallest Minimum . . . . . . . . . . . . . . . 4-22
Searching for a Smaller Minimum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-22
Basins of Attraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-23

Optimizing a Simulation or Ordinary Differential Equation . . . . . . . . . . 4-26


What Is Optimizing a Simulation or ODE? . . . . . . . . . . . . . . . . . . . . . . . . 4-26
Potential Problems and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26
Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-30

Optimization App
5
Optimization App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
Optimization App Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
Specifying Certain Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6
Importing and Exporting Your Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8

Optimization App Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12


Optimize Without Using the App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12
Set Options Using Live Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12
Set Options: Command Line or Standard Scripts . . . . . . . . . . . . . . . . . . . 5-14
Choose Plot Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15
Pass Solver Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16

Nonlinear algorithms and examples


6
Unconstrained Nonlinear Optimization Algorithms . . . . . . . . . . . . . . . . . . 6-2
Unconstrained Optimization Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
fminunc trust-region Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
fminunc quasi-newton Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4

fminsearch Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-9

Unconstrained Minimization Using fminunc . . . . . . . . . . . . . . . . . . . . . . 6-11

ix
Minimization with Gradient and Hessian . . . . . . . . . . . . . . . . . . . . . . . . . 6-13

Minimization with Gradient and Hessian Sparsity Pattern . . . . . . . . . . . 6-16

Constrained Nonlinear Optimization Algorithms . . . . . . . . . . . . . . . . . . . 6-19


Constrained Optimization Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-19
fmincon Trust Region Reflective Algorithm . . . . . . . . . . . . . . . . . . . . . . . 6-19
fmincon Active Set Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-22
fmincon SQP Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-29
fmincon Interior Point Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-30
fminbnd Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-32
fseminf Problem Formulation and Algorithm . . . . . . . . . . . . . . . . . . . . . . 6-32

Tutorial for the Optimization Toolbox™ . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-36

Banana Function Minimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-49

Minimizing an Expensive Optimization Problem Using Parallel Computing


Toolbox™ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-56

Nonlinear Inequality Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-61

Nonlinear Constraints with Gradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-63

fmincon Interior-Point Algorithm with Analytic Hessian . . . . . . . . . . . . . 6-66

Linear or Quadratic Objective with Quadratic Constraints . . . . . . . . . . . 6-71

Nonlinear Equality and Inequality Constraints . . . . . . . . . . . . . . . . . . . . 6-75

Optimization App with the fmincon Solver . . . . . . . . . . . . . . . . . . . . . . . . 6-77


Step 1: Write a file objecfun.m for the objective function. . . . . . . . . . . . . 6-77
Step 2: Write a file nonlconstr.m for the nonlinear constraints. . . . . . . . . 6-77
Step 3: Set up and run the problem with the Optimization app. . . . . . . . . 6-77

Minimization with Bound Constraints and Banded Preconditioner . . . . 6-81

Minimization with Linear Equality Constraints, Trust-Region Reflective


Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-87

Minimization with Dense Structured Hessian, Linear Equalities . . . . . . 6-90


Hessian Multiply Function for Lower Memory . . . . . . . . . . . . . . . . . . . . . 6-90
Step 1: Write a file brownvv.m that computes the objective function, the
gradient, and the sparse part of the Hessian. . . . . . . . . . . . . . . . . . . . 6-91
Step 2: Write a function to compute Hessian-matrix products for H given a
matrix Y. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-91
Step 3: Call a nonlinear minimization routine with a starting point and
linear equality constraints. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-91
Preconditioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-93

Symbolic Math Toolbox™ Calculates Gradients and Hessians . . . . . . . . . 6-94

Using Symbolic Mathematics with Optimization Toolbox™ Solvers . . . 6-105

x Contents
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Code Generation in fmincon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-114
What Is Code Generation? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-114
Code Generation Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-114
Generated Code Not Multithreaded . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-115

Code Generation for Optimization Basics . . . . . . . . . . . . . . . . . . . . . . . . 6-116


Generate Code for fmincon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-116
Modify Example for Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-116

Static Memory Allocation for fmincon Code Generation . . . . . . . . . . . . 6-120

Optimization Code Generation for Real-Time Applications . . . . . . . . . . 6-122


Time Limits on Generated Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-122
Match the Target Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-122
Set Coder Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-122
Benchmark the Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-123
Set Initial Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-123
Set Options Appropriately . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-123
Global Minimum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-124

One-Dimensional Semi-Infinite Constraints . . . . . . . . . . . . . . . . . . . . . . 6-125

Two-Dimensional Semi-Infinite Constraint . . . . . . . . . . . . . . . . . . . . . . . 6-128

Analyzing the Effect of Uncertainty Using Semi-Infinite Programming


........................................................ 6-131

Nonlinear Problem-Based
7
Rational Objective Function, Problem-Based . . . . . . . . . . . . . . . . . . . . . . . 7-2

Solve Constrained Nonlinear Optimization, Problem-Based . . . . . . . . . . . 7-4

Convert Nonlinear Function to Optimization Expression . . . . . . . . . . . . . . 7-8

Constrained Electrostatic Nonlinear Optimization, Problem-Based . . . . 7-12

Problem-Based Nonlinear Minimization with Linear Constraints . . . . . . 7-17

Include Derivatives in Problem-Based Workflow . . . . . . . . . . . . . . . . . . . 7-20


Why Include Derivatives? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-20
Create Optimization Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-20
Convert Problem to Solver-Based Form . . . . . . . . . . . . . . . . . . . . . . . . . . 7-21
Calculate Derivatives and Keep Track of Variables . . . . . . . . . . . . . . . . . 7-21
Edit the Objective and Constraint Files . . . . . . . . . . . . . . . . . . . . . . . . . . 7-22
Run Problem Using Two Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-22

Output Function for Problem-Based Optimization . . . . . . . . . . . . . . . . . . 7-25

Solve Nonlinear Feasibility Problem, Problem-Based . . . . . . . . . . . . . . . 7-30

xi
Multiobjective Algorithms and Examples
8
Multiobjective Optimization Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2
Multiobjective Optimization Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2
Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-3

Compare fminimax and fminunc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-6

Using fminimax with a Simulink® Model . . . . . . . . . . . . . . . . . . . . . . . . . . 8-8

Signal Processing Using fgoalattain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12


Step 1: Write a file filtmin.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12
Step 2: Invoke optimization routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12

Generate and Plot a Pareto Front . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-15

Multi-Objective Goal Attainment Optimization . . . . . . . . . . . . . . . . . . . . . 8-18

Minimax Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-24

Linear Programming and Mixed-Integer Linear Programming


9
Linear Programming Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Linear Programming Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Interior-Point linprog Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Interior-Point-Legacy Linear Programming . . . . . . . . . . . . . . . . . . . . . . . . 9-6
Dual-Simplex Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-9

Typical Linear Programming Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-13

Maximize Long-Term Investments Using Linear Programming: Solver-


Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-15

Mixed-Integer Linear Programming Algorithms . . . . . . . . . . . . . . . . . . . 9-26


Mixed-Integer Linear Programming Definition . . . . . . . . . . . . . . . . . . . . 9-26
intlinprog Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-26

Tuning Integer Linear Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-35


Change Options to Improve the Solution Process . . . . . . . . . . . . . . . . . . 9-35
Some “Integer” Solutions Are Not Integers . . . . . . . . . . . . . . . . . . . . . . . 9-36
Large Components Not Integer Valued . . . . . . . . . . . . . . . . . . . . . . . . . . 9-36
Large Coefficients Disallowed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-36

Mixed-Integer Linear Programming Basics: Solver-Based . . . . . . . . . . . 9-37

Factory, Warehouse, Sales Allocation Model: Solver-Based . . . . . . . . . . . 9-40

Traveling Salesman Problem: Solver-Based . . . . . . . . . . . . . . . . . . . . . . . 9-49

xii Contents
Optimal Dispatch of Power Generators: Solver-Based . . . . . . . . . . . . . . . 9-55

Mixed-Integer Quadratic Programming Portfolio Optimization: Solver-


Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-65

Solve Sudoku Puzzles Via Integer Programming: Solver-Based . . . . . . . 9-72

Office Assignments by Binary Integer Programming: Solver-Based . . . . 9-79

Cutting Stock Problem: Solver-Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-86

Factory, Warehouse, Sales Allocation Model: Problem-Based . . . . . . . . . 9-90

Traveling Salesman Problem: Problem-Based . . . . . . . . . . . . . . . . . . . . . 9-98

Optimal Dispatch of Power Generators: Problem-Based . . . . . . . . . . . . 9-104

Office Assignments by Binary Integer Programming: Problem-Based 9-113

Mixed-Integer Quadratic Programming Portfolio Optimization: Problem-


Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-118

Cutting Stock Problem: Problem-Based . . . . . . . . . . . . . . . . . . . . . . . . . 9-125

Solve Sudoku Puzzles Via Integer Programming: Problem-Based . . . . 9-129

Minimize Makespan in Parallel Processing . . . . . . . . . . . . . . . . . . . . . . 9-135

Investigate Linear Infeasibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-139

Problem-Based Optimization
10
Problem-Based Optimization Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2

Problem-Based Workflow for Solving Equations . . . . . . . . . . . . . . . . . . . . 10-4

Optimization Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-6


What Are Optimization Expressions? . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-6
Expressions for Objective Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-6
Expressions for Constraints and Equations . . . . . . . . . . . . . . . . . . . . . . . 10-7
Optimization Variables Have Handle Behavior . . . . . . . . . . . . . . . . . . . . 10-9

Pass Extra Parameters in Problem-Based Approach . . . . . . . . . . . . . . . 10-11

Review or Modify Optimization Problems . . . . . . . . . . . . . . . . . . . . . . . . 10-14


Review Problem Using show or write . . . . . . . . . . . . . . . . . . . . . . . . . . 10-14
Change Default Solver or Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-14
Correct a Misspecified Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-16
Duplicate Variable Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-19

xiii
Named Index for Optimization Variables . . . . . . . . . . . . . . . . . . . . . . . . . 10-20
Create Named Indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-20
Use Named Indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-21
View Solution with Index Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-22

Examine Optimization Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-25


Obtain Numeric Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-25
Examine Solution Quality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-26
Infeasible Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-26
Solution Takes Too Long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-27

Create Efficient Optimization Problems . . . . . . . . . . . . . . . . . . . . . . . . . 10-28

Separate Optimization Model from Data . . . . . . . . . . . . . . . . . . . . . . . . . 10-30

Problem-Based Optimization Algorithms . . . . . . . . . . . . . . . . . . . . . . . . 10-32

Variables with Duplicate Names Disallowed . . . . . . . . . . . . . . . . . . . . . . 10-34

Expression Contains Inf or NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-35

Supported Operations on Optimization Variables and Expressions . . . 10-36


Notation for Supported Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-36
Operations Returning Optimization Expressions . . . . . . . . . . . . . . . . . . 10-36
Operations Returning Optimization Variables . . . . . . . . . . . . . . . . . . . . 10-38
Operations on Optimization Expressions . . . . . . . . . . . . . . . . . . . . . . . . 10-38
Operations Returning Constraint Expressions . . . . . . . . . . . . . . . . . . . . 10-38
Some Undocumented Operations Work on Optimization Variables and
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-38
Unsupported Functions and Operations Require fcn2optimexpr . . . . . . 10-38

Mixed-Integer Linear Programming Basics: Problem-Based . . . . . . . . 10-40

Create Initial Point for Optimization with Named Index Variables . . . . 10-43

Quadratic Programming
11
Quadratic Programming Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Quadratic Programming Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
interior-point-convex quadprog Algorithm . . . . . . . . . . . . . . . . . . . . . . . . 11-2
trust-region-reflective quadprog Algorithm . . . . . . . . . . . . . . . . . . . . . . . 11-7
active-set quadprog Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-11

Quadratic Minimization with Bound Constraints . . . . . . . . . . . . . . . . . . 11-15


Step 1: Load the Hessian and define f, lb, and ub. . . . . . . . . . . . . . . . . . 11-15
Step 2: Call a quadratic minimization routine with a starting point xstart.
.................................................... 11-15

Quadratic Minimization with Dense, Structured Hessian . . . . . . . . . . . 11-17


Take advantage of a structured Hessian . . . . . . . . . . . . . . . . . . . . . . . . 11-17

xiv Contents
Step 1: Decide what part of H to pass to quadprog as the first argument.
.................................................... 11-17
Step 2: Write a function to compute Hessian-matrix products for H. . . . 11-17
Step 3: Call a quadratic minimization routine with a starting point. . . . 11-18
Preconditioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-19

Large Sparse Quadratic Program with Interior Point Algorithm . . . . . 11-21

Bound-Constrained Quadratic Programming, Solver-Based . . . . . . . . . 11-24

Quadratic Programming for Portfolio Optimization Problems, Solver-


Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-28

Quadratic Programming with Bound Constraints: Problem-Based . . . . 11-34

Large Sparse Quadratic Program, Problem-Based . . . . . . . . . . . . . . . . . 11-37

Bound-Constrained Quadratic Programming, Problem-Based . . . . . . . 11-40

Quadratic Programming for Portfolio Optimization, Problem-Based . . 11-44

Code Generation for quadprog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-51


What Is Code Generation? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-51
Code Generation Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-51
Generated Code Not Multithreaded . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-52

Generate Code for quadprog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-53


First Steps in quadprog Code Generation . . . . . . . . . . . . . . . . . . . . . . . 11-53
Modify Example for Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-54

Quadratic Programming with Many Linear Constraints . . . . . . . . . . . . 11-57

Least Squares
12
Least-Squares (Model Fitting) Algorithms . . . . . . . . . . . . . . . . . . . . . . . . 12-2
Least Squares Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2
Linear Least Squares: Interior-Point or Active-Set . . . . . . . . . . . . . . . . . . 12-2
Trust-Region-Reflective Least Squares . . . . . . . . . . . . . . . . . . . . . . . . . . 12-3
Levenberg-Marquardt Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-6

Nonlinear Data-Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-9

lsqnonlin with a Simulink® Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-17

Nonlinear Least Squares Without and Including Jacobian . . . . . . . . . . 12-21

Nonnegative Linear Least Squares, Solver-Based . . . . . . . . . . . . . . . . . 12-24

Optimization App with the lsqlin Solver . . . . . . . . . . . . . . . . . . . . . . . . . 12-27


The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-27

xv
Setting Up the Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-27

Jacobian Multiply Function with Linear Least Squares . . . . . . . . . . . . . 12-30

Large-Scale Constrained Linear Least-Squares, Solver-Based . . . . . . . 12-34

Shortest Distance to a Plane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-38

Nonnegative Linear Least Squares, Problem-Based . . . . . . . . . . . . . . . . 12-40

Large-Scale Constrained Linear Least-Squares, Problem-Based . . . . . 12-44

Nonlinear Curve Fitting with lsqcurvefit . . . . . . . . . . . . . . . . . . . . . . . . . 12-48

Fit a Model to Complex-Valued Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-50

Fit an Ordinary Differential Equation (ODE) . . . . . . . . . . . . . . . . . . . . . 12-54

Nonlinear Least-Squares, Problem-Based . . . . . . . . . . . . . . . . . . . . . . . . 12-62

Fit ODE, Problem-Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-77

Nonlinear Data-Fitting Using Several Problem-Based Approaches . . . 12-84

Write Objective Function for Problem-Based Least Squares . . . . . . . . . 12-92

Systems of Equations
13
Equation Solving Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
Equation Solving Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
Trust-Region Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
Trust-Region-Dogleg Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-4
Levenberg-Marquardt Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-5
fzero Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-6
\ Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-6

Nonlinear Equations with Analytic Jacobian . . . . . . . . . . . . . . . . . . . . . . . 13-7


Step 1: Write a file bananaobj.m to compute the objective function values
and the Jacobian. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-7
Step 2: Call the solve routine for the system of equations. . . . . . . . . . . . . 13-8

Nonlinear Equations with Finite-Difference Jacobian . . . . . . . . . . . . . . . 13-9

Nonlinear Equations with Jacobian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-11


Step 1: Write a file nlsf1.m that computes the objective function values and
the Jacobian. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-11
Step 2: Call the solve routine for the system of equations. . . . . . . . . . . 13-11

xvi Contents
Nonlinear Equations with Jacobian Sparsity Pattern . . . . . . . . . . . . . . . 13-13
Step 1: Write a file nlsf1a.m that computes the objective function values.
.................................................... 13-13
Step 2: Call the system of equations solve routine. . . . . . . . . . . . . . . . . 13-13

Nonlinear Systems with Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-15


Solve Equations with Inequality Constraints . . . . . . . . . . . . . . . . . . . . . 13-15
Use Different Start Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-15
Use Different Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-16
Use lsqnonlin with Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-16
Set Equations and Inequalities as fmincon Constraints . . . . . . . . . . . . . 13-17

Solve Nonlinear System of Equations, Problem-Based . . . . . . . . . . . . . 13-19

Solve Nonlinear System of Polynomials, Problem-Based . . . . . . . . . . . . 13-21

Follow Equation Solution as a Parameter Changes . . . . . . . . . . . . . . . . 13-23

Nonlinear System of Equations with Constraints, Problem-Based . . . . 13-30

Parallel Computing for Optimization


14
What Is Parallel Computing in Optimization Toolbox? . . . . . . . . . . . . . . . 14-2
Parallel Optimization Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2
Parallel Estimation of Gradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2
Nested Parallel Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-3

Using Parallel Computing in Optimization Toolbox . . . . . . . . . . . . . . . . . 14-5


Using Parallel Computing with Multicore Processors . . . . . . . . . . . . . . . 14-5
Using Parallel Computing with a Multiprocessor Network . . . . . . . . . . . . 14-5
Testing Parallel Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-6

Minimizing an Expensive Optimization Problem Using Parallel Computing


Toolbox™ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-8

Improving Performance with Parallel Computing . . . . . . . . . . . . . . . . . 14-13


Factors That Affect Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-13
Factors That Affect Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-13
Searching for Global Optima . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-14

Argument and Options Reference


15
Function Input Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-2

Function Output Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-4

xvii
Optimization Options Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-6
Optimization Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-6
Hidden Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-16

Current and Legacy Option Name Tables . . . . . . . . . . . . . . . . . . . . . . . . 15-21

Output Function Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-26


What Are Output Functions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-26
Structure of the Output Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-26
Fields in optimValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-27
States of the Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-31
Stop Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-31

intlinprog Output Function and Plot Function Syntax . . . . . . . . . . . . . . 15-33


What Are Output Functions and Plot Functions? . . . . . . . . . . . . . . . . . . 15-33
Custom Function Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-33
optimValues Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-34

Functions
16

xviii Contents
Acknowledgments

xix
Acknowledgments

Acknowledgments
MathWorks® would like to acknowledge the following contributors to Optimization Toolbox
algorithms.

Thomas F. Coleman researched and contributed algorithms for constrained and unconstrained
minimization, nonlinear least squares and curve fitting, constrained linear least squares, quadratic
programming, and nonlinear equations.

Dr. Coleman is Professor of Combinatorics and Optimization at the University of Waterloo.

Yin Zhang researched and contributed the large-scale linear programming algorithm.

Dr. Zhang is Professor of Computational and Applied Mathematics at Rice University.

xx
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
1

Getting Started

• “Optimization Toolbox Product Description” on page 1-2


• “First Choose Problem-Based or Solver-Based Approach” on page 1-3
• “Solve a Constrained Nonlinear Problem, Problem-Based” on page 1-5
• “Solve a Constrained Nonlinear Problem, Solver-Based” on page 1-11
• “Set Up a Linear Program, Solver-Based” on page 1-18
• “Set Up a Linear Program, Problem-Based” on page 1-25
1 Getting Started

Optimization Toolbox Product Description


Solve linear, quadratic, integer, and nonlinear optimization problems

Optimization Toolbox provides functions for finding parameters that minimize or maximize objectives
while satisfying constraints. The toolbox includes solvers for linear programming (LP), mixed-integer
linear programming (MILP), quadratic programming (QP), nonlinear programming (NLP),
constrained linear least squares, nonlinear least squares, and nonlinear equations. You can define
your optimization problem with functions and matrices or by specifying variable expressions that
reflect the underlying mathematics.

You can use the toolbox solvers to find optimal solutions to continuous and discrete problems,
perform tradeoff analyses, and incorporate optimization methods into algorithms and applications.
The toolbox lets you perform design optimization tasks, including parameter estimation, component
selection, and parameter tuning. It can be used to find optimal solutions in applications such as
portfolio optimization, resource allocation, and production planning and scheduling.

Key Features
• Nonlinear and multiobjective optimization of smooth constrained and unconstrained problems
• Solvers for nonlinear least squares, constrained linear least squares, data fitting, and nonlinear
equations
• Quadratic programming (QP) and linear programming (LP)
• Mixed-integer linear programming (MILP)
• Optimization modeling tools
• Graphical monitoring of optimization progress
• Gradient estimation acceleration (with Parallel Computing Toolbox™)

1-2
First Choose Problem-Based or Solver-Based Approach

First Choose Problem-Based or Solver-Based Approach


Optimization Toolbox has two approaches to solving optimization problems or equations: problem-
based and solver-based. Before you start to solve a problem, you must first choose the appropriate
approach.

This table summarizes the main differences between the two approaches.

Approaches Characteristics
“Problem-Based Optimization Setup” Easier to create and debug
Represents the objective and constraints symbolically
Requires translation from problem form to matrix form, resulting in a
time
Does not allow direct inclusion of gradient or Hessian; see “Include De
Problem-Based Workflow” on page 7-20
See the steps in “Problem-Based Optimization Workflow” on page 10-2
“Problem-Based Workflow for Solving Equations” on page 10-4
Basic linear example: “Mixed-Integer Linear Programming Basics: Pro
page 10-40 or the video Solve a Mixed-Integer Linear Programming Pr
Optimization Modeling

Basic nonlinear example: “Solve a Constrained Nonlinear Problem, Pro


on page 1-5

Basic equation-solving example: “Solve Nonlinear System of Equations


Based” on page 13-19
“Solver-Based Optimization Problem Harder to create and debug
Setup” Represents the objective and constraints as functions or matrices
Does not require translation from problem form to matrix form, resulti
solution time
Allows direct inclusion of gradient or Hessian
Allows use of a Hessian multiply function or Jacobian multiply function
memory in large problems

See “Quadratic Minimization with Dense, Structured Hessian” on page


“Jacobian Multiply Function with Linear Least Squares” on page 12-30
See the steps in “Solver-Based Optimization Problem Setup”
Basic linear example: “Mixed-Integer Linear Programming Basics: Sol
page 9-37

Basic nonlinear example: “Solve a Constrained Nonlinear Problem, So


page 1-11

Basic equation-solving examples: “Examples” on page 16-0

1-3
1 Getting Started

See Also

More About
• “Problem-Based Optimization Setup”
• “Solver-Based Optimization Problem Setup”

1-4
Solve a Constrained Nonlinear Problem, Problem-Based

Solve a Constrained Nonlinear Problem, Problem-Based


Typical Optimization Problem

This example shows how to solve a constrained nonlinear optimization problem using the problem-
based approach. The example demonstrates the typical work flow: create an objective function,
create constraints, solve the problem, and examine the results.

Note:

If your objective function or nonlinear constraints are not composed of elementary functions, you
must convert the nonlinear functions to optimization expressions using fcn2optimexpr. See the
last part of this example, Alternative Formulation Using fcn2optimexpr on page 1-0 , or “Convert
Nonlinear Function to Optimization Expression” on page 7-8.

For the solver-based approach to this problem, see “Solve a Constrained Nonlinear Problem, Solver-
Based” on page 1-11.

Problem Formulation: Rosenbrock's Function

Consider the problem of minimizing Rosenbrock's function

2 2
f (x) = 100 x2 − x12 + (1 − x1) ,

over the unit disk, meaning the disk of radius 1 centered at the origin. In other words, find x that
minimizes the function f (x) over the set x12 + x22 ≤ 1. This problem is a minimization of a nonlinear
function subject to a nonlinear constraint.

Rosenbrock's function is a standard test function in optimization. It has a unique minimum value of 0
attained at the point [1,1]. Finding the minimum is a challenge for some algorithms because the
function has a shallow minimum inside a deeply curved valley. The solution for this problem is not at
the point [1,1] because that point does not satisfy the constraint.

This figure shows two views of Rosenbrock's function in the unit disk. The vertical axis is log-scaled;
in other words, the plot shows log(1 + f (x)). Contour lines lie beneath the surface plot.
rosenbrock = @(x)100*(x(:,2) - x(:,1).^2).^2 + (1 - x(:,1)).^2; % Vectorized function

figure1 = figure('Position',[1 200 600 300]);


colormap('gray');
axis square;
R = 0:.002:1;
TH = 2*pi*(0:.002:1);
X = R'*cos(TH);
Y = R'*sin(TH);
Z = log(1 + rosenbrock([X(:),Y(:)]));
Z = reshape(Z,size(X));

% Create subplot
subplot1 = subplot(1,2,1,'Parent',figure1);
view([124 34]);
grid('on');
hold on;

% Create surface

1-5
Random documents with unrelated
content Scribd suggests to you:
made much more interesting than they otherwise would be, by using
them in practical service whenever an opportunity occurs.
“But, although we want to be careful not to be vain or foolish
because we have had the privilege of doing such things as it is the
duty of every scout to do, when he gets a chance, we must
recognize good work when we see it, so as to encourage one
another to do more such work, and to do it better and better as time
goes on.”
He then turned and spoke in a low voice to Mr. Miller, who
immediately gave the command:
“Perkins—front and center!”
Jack immediately marched down and stood directly opposite the
Chairman.
“Perkins,” said the Chairman, “the Court of Honor, especially
convened to consider the work of the ship’s company at the Salem
fire, have decided that your work ashore was of excellent quality and
deserves the commendation of the Court. The Court commends you
for carrying out your instructions promptly and with good judgment.”
Jack saluted, and, at the word of command, marched back to his
regular position.
Malloy, Young, Ellsworth, and Smith were then ordered to “front and
center” and stood “at attention” before the Chairman.
“It gives me pleasure to tell you, boys,” said the Chairman, “that you
have been especially commended for good work at the Salem fire.”
All saluted, and at the words “About face—march”, from Mr. Miller,
returned to their positions with a grin upon their faces.
Then Tom was ordered forward, and received a commendation for
efficient first-aid work in helping to save a woman by stopping the
flow of blood from a severed artery. There was general surprise at
Tom’s first-aid commendation, for none of the boys knew about the
incident except Dick and Chippie.
“Just like him,” muttered Malloy; “with all his talk about Gray and the
cat, he hasn’t let on about himself at all!”
“Gray, front and center,” was the next command.
Dick took his position “at attention” before the Chairman, with the
most solemn face you could possibly imagine.
“Boys,” said the Chairman, addressing them all together, “the Court
finds that the case of Gray is somewhat different from the others
and requires special attention. Not only did he do good work in
helping in a rescue and in moving belongings to a place of safety,
but he actually saved life, although the life was only that of a little
animal,—a little girl’s pet. The regular medal for ‘life saving’ could
not be awarded in such a case, because that is reserved as a reward
for the saving of human life only; for, while the law recognizes the
sacredness of all harmless life, it makes an important distinction
between the value of human life and that of an animal. However, the
qualities of unselfishness and courage which Gray has shown in
saving a kitten from the third story of a burning house are qualities
which he or any other would need in saving human life under similar
circumstances.”
While the Chairman was saying these words, Mr. Miller was watching
Dick’s face attentively, for it was getting paler and paler, more and
more troubled.
“The Court, therefore,” resumed the Chairman, “not only
commends”—here the Chairman paused as he noticed the
expression on Dick’s face.
“What’s the matter, Gray?” he asked kindly, in a low voice.
“I—I—can’t take it!” muttered Dick. “Don’t ask me now. May I speak
to Mr. Miller for a minute?”
All this had been said in an undertone, and the whole company was
stirred by a feeling, partly of curiosity, and partly of discomfort. Mr.
Miller was standing within earshot, and the Chairman nodded to him.
Then Mr. Miller beckoned to Dick and led him down the ladder into
the cabin.
The boys were then ordered to remain “at ease”, and every one
looked at his neighbor as if to say:
“I wonder what is going to happen next?”
Presently Mr. Miller appeared on deck again and reported to the
Chairman in a low voice; and, in a few minutes more, he went back
to get Dick, and they both stood alongside the Chairman.
The general feeling of suspense by this time had grown extremely
acute, when the Chairman said:
“Boys, Gray has asked permission to say something to us all before
we go on with awarding the honors. I ask you to listen to him with
careful attention. His only object in talking to you now is to do his
full duty as a Boy Scout under very trying circumstances.”
Dick had had a good, frank talk with Mr. Miller in the cabin below,
and had cleared his mind of a weight that had been pressing down
on him for some time past, although he had not, until that morning,
faced the necessity for action with perfect clearness. He now had
determined to make a clean breast of the whole matter and to
accept his just penalty—whatever that might be—like a man.
“I want to say, fellows,” said he, “that the umpire was wrong in the
game at Hull, when he decided in my favor about second base. I
never touched it. I got around to third and then home without ever
having come within six inches of second base, except after I had
been fairly put out. I haven’t any excuse; I knew it was wrong, but I
did not know how wrong it was. I have done other things like that
before, and I thought they were smart. Now you know it, and maybe
I shall lose my scout badge. I have lost the right to wear it, anyway,
and you can see as well as I that I can’t accept any honor for what
happened at Salem.”
He then turned to the Chairman, who said with a very quiet voice:
“You may go below for the present, Gray.”
The Chairman, Mr. Miller, and Mr. Wentworth then consulted
together, and finally asked Jack Perkins to join them. The whole
company was impressed by the solemnity of the incident. The
Chairman explained to the boys that the question of awarding an
honor to Gray under the present circumstances must be
reconsidered in the light of his confession.
“But,” said he, “I can assure you of one thing: however wrong Gray
has been in the past, I respect him for his straight-forwardness to-
day. Let me warn you boys against treating this matter in any way
that might appear to be mean or disrespectful to a fellow scout. It
would be a base and brutal thing to treat lightly such humiliation as
this.”
Judging from the faces of the boys as they separated when they
were dismissed, it seemed as if the warning of the Chairman were
hardly necessary. They were all sorry for Dick and greatly surprised,
but owing to his frank and public confession, they had a feeling of
confidence that everything would turn out right in the end.
CHAPTER XII
Another Meeting of the Club

During the evening recreation hour, when everybody was on deck,


Tom, Chippie, and Dick had a quiet talk below, sitting on Tom’s
locker. Each one of the three felt the urgent need of their getting
together and talking over, with the utmost frankness, everything that
had happened since the morning of the baseball game at Hull; but
now that they were sitting alone face to face, there did not seem so
very much to say after all, because of their talk on the way back
from Salem and Dick’s manly confession before all the boys.
“I’m glad you came out with it all, this morning, Dick,” said Tom. “It
must have hurt to have all the fellows know about it, and there was
nothing to force you to tell except your honor.”
“Yes,” chimed in Chippie, “you might have covered it up and taken
the commendation, and no one would have been any the wiser!”
“Yep,” retorted Dick, with a grim look, “but how do you think I would
have felt? Don’t you suppose that dirty trick I played at Hull would
have gnawed inside of me? I don’t suppose you fellows know how
much it hurts—when you have a right to know better. If it hadn’t
been for our talk on the way back from Salem, and the cat, and all
that they brought up to me about honor and the scout law, maybe I
might have forgotten the Hull business—for the time, anyway; but
I’m mighty glad I didn’t. Say, boys,” said he, with a sudden gleam of
animation, “it’s like when you’ve eaten something that goes against
you; you can’t get right till you’ve thrown it up!”
“To-morrow is Sunday,” said Tom, “and we expect to drop anchor in
Northbridge to-night. All hands are going ashore to church in the
morning, and there’s liberty in the afternoon. What do you fellows
say to a meeting of the Club on Duck Island?”
“A-1!” answered Dick.
“Bet your life!” exclaimed Chippie, “I’d like to hear that story over
again about ‘After you, pilot’, and the fellow that went down the
manhole. I say, Tom, I think we ought to write that out and keep it
in a secret and safe place in the shack. I’m thinking we may like to
remind ourselves of that story once in a while. You see this time it
was Dick that made a break, and I guess it was because he forgot all
about his oath in the excitement of the game. But maybe next time
it would be you or me, Tom, that would be tempted in some such
way. We might as well be prepared for the worst, and then the
enemy can never catch us asleep.”
“You’re a good one, Chip,” cried Dick, and gave his comrade a thump
on the back. “That makes me feel that we’re working together, and
that the Club is a real live thing. Before you said that, I had a
sneaking feeling that nobody could be as bad as I was, and that
makes a fellow feel kind of washed out and discouraged.”
“Now it’s time to go on deck, for the fellows will be coming down to
prayers in a few minutes,” said Tom. “We’ll write that story out
carefully with ink in the shack to-morrow afternoon”; and, as they
approached the companionway, Chippie gave Dick a shove up ahead
of them and whispered in his ear: “After you, pilot!”
There were only a few minutes to wait on deck before prayers, and
Dick felt relieved and refreshed, first, because the load of
unacknowledged guilt had rolled off him and had seemed to sink to
the bottom of the sea; and secondly, because he felt the respect and
sympathy of his brothers-in-arms. Dick Number 1 was in the saddle,
and ready for action.
They came to anchor at Northbridge at about nine o’clock that
evening, and the next morning all hands landed and went to church.
The Northbridge boys then joined their families for dinner, while the
others returned on board and landed again in the afternoon to take
a hike through the surrounding country. Tom, Dick, and Chippie met,
according to their agreement, at the shack on Duck Island at four
o’clock.
“You call the meeting to order, Tom,” said Chippie, “that’s your
business; and, as Dick’s brother was the one who told us the story, I
vote Dick had better tell it to us the best he knows how, only slowly
—and I’ll write it down, and you can tell us when you think we’re off
the track.”
“Chip’s a pretty good manager, eh, Dick?” said Tom. “Here he orders
me to call the meeting to order and then goes on and tries to run it
himself! What do you think of that, sonny?”
“All right, Tom, it’s your show,” said Chippie, “only for goodness’
sake, begin.”
“This meeting will now come to order,” said Tom solemnly. “Does
everybody agree to what Chippie has said? If so, get your pen and
paper ready, Chip, and you, Dick, begin the story.”
There was a pause, and then Dick asked: “What was the name of
the ship?”
“Tecumseh,” said Tom.
“Write that down, Chip,” said Dick, “the name of the ship was
Tecumseh”; and then he went on with the facts of the story, helped
along by suggestions from Tom and Chippie. It took them quite a
long time to get through, but finally they came to the last sentence:
“And so Captain Craven gave up his life to do his duty as a good
scout, and to keep his oath and law; and so, to remind them of his
brave and courteous act, the officers of the Navy have the custom of
saying, ‘After you, pilot’ when one of them steps back to make way
for another; and also, because we want to be reminded of his brave
and courteous act and faithfulness to his oath and law, we—the
members of the Triangle Club of Duck Island—have written out this
story to read once in a while, and have taken ‘After you, Pilot’ as our
motto.”
“I move,” cried Chippie, “that we adopt this as the charter of the
Club.”
“What’s a charter?” asked Dick.
“It means what the Club’s about,” said Chippie quickly.
“Well, if that’s so,” answered Dick, “I think there ought to be
something else in the Charter.”
“What’s that?” asked the others eagerly.
“Why, what we said in the beginning, when we first read the story,
that each one of us pledges himself to do his best, first, to keep the
law himself as well as he can, and then to help the others to do the
same thing; and—not only that—but each one will tell any other,
when he sees him sliding off, the way you talked to me coming back
from Salem.”
Dick’s motion was unanimously agreed to, and then all three stood
up and took hold of hands to confirm their pledge, thus also forming
the triangle which was the symbol of the Club. They then all three
solemnly signed the document, affixing their scout signs:
CHAPTER XIII
A Green Hand

While the meeting of the Triangle Club was going on at the Grays’
farm, Mr. Wentworth went ashore with the yeoman,—Bob Brackett—
to meet a new boy who was to join the ship that afternoon.
As soon as they had arrived at the landing, however, they were
approached by a tall, elderly man who looked as if he had come
from the country, and had rather a small boy at his side in the Land
Scout uniform.
“My name is Jackson,” said he, addressing Mr. Wentworth, “and I
have brought my nephew, William Brown, for whom, I understand, a
berth has been reserved on the Bright Wing.”
“Yes, sir,” replied Mr. Wentworth cordially, “and I am very much
obliged to you for being on hand so promptly, for you have saved
me the trouble of looking you up at the hotel.”
After shaking hands, Mr. Wentworth turned to Brown with a friendly
nod, and then, facing Bob, he said:
“This boy with the mail bag, Brown, is our yeoman, and we shall
have to wait until he has been to the post office, before returning to
the ship. Do you think, Mr. Jackson, that your nephew would like to
go to the post office with the yeoman? It might be pleasant for him
to make friends with one of the crew before he goes aboard.”
Mr. Jackson assented, and Billy Brown saluted awkwardly by
throwing his arm out on one side and then bringing it up to the back
of his head. “He’s a queer duck,” thought Bob, but his meditations
were interrupted by Mr. Wentworth.
“You can go now, Yeoman, and be as quick as you can. Remember
you are on duty, and you have no other errand but to post the
outgoing and to fetch the incoming mail.”
Mr. Wentworth spoke with decision; but his tone was so friendly that
any boy would have felt inclined to obey him.
The two scouts went off together at a smart pace; and, as they
walked up the street, Mr. Wentworth noticed a great contrast in the
way they held themselves. The same thought seemed to have
entered Mr. Jackson’s mind.
“I’m in great hopes,” he said, “that the drill and discipline on the
Bright Wing may improve Billy’s physical condition. He is a studious
boy, but he does not get enough fun or activity out of doors.”
“I understand, sir,” said Mr. Wentworth; “he has an intelligent face
and a good bright eye, but his body isn’t well gathered together. He
seems to have a little stoop in the shoulders. For how long is he to
be on board, sir?”
“His berth is engaged for two weeks, but if there is room for him, he
might possibly stay longer.”
“Well,” said Mr. Wentworth, “a good deal can be done in two weeks
if a boy is intelligent and willing,—I mean in the way of a start. A
good brain and willing disposition are the best sort of help to a boy
for getting his body into shape; for, if he can get a strong idea of
physical balance into his mind, it will give him an interest in keeping
his body up to the mark. But, of course, he needs practice and
exercise, too. I understand, sir, that you are also coming on board?”
“Yes,” replied Mr. Jackson, “I’m going as far as Provincetown, and
then I shall have to take the train for home. I have not had a good
sail for many years, and am looking forward with great pleasure to
being on a Boy Scout ship.”
Mr. Wentworth was very glad of this opportunity to find out the
special needs of the new boy, and he looked forward particularly to
reporting everything he had learned to the Chairman and Mr. Miller.
“I shouldn’t wonder,” thought he to himself, “if we made a good
light-weight boxer of the lad before he leaves the ship; but the main
thing is to teach him to gather himself together and to swim and
row well.” He had a quick eye for such things.
The conversation had not continued much longer when Bob and Billy
reappeared with a heavy load of letters and bundles in the mail bag.
Fred Hamilton, the boatswain, was in charge of the launch that lay
alongside the landing. Mr. Wentworth ordered the boys into her
stern, and then got in himself to give Mr. Jackson a hand. He then
went forward to the wheel and the boatswain stood by the engine.
When they returned to the ship, it was recreation hour, and the
music of the “ship’s band” greeted the newcomers across the water.
Half a dozen hands were stretched over the side to catch the painter
of the launch as she approached the side ladder. The Chairman and
the Captain were standing on deck ready to welcome Mr. Jackson.
Bob immediately took the mail below and sorted it out according to
regulations, while Mr. Wentworth went to the clothes locker to find a
suit of “whites”, and a blue working suit to fit the new apprentice
seaman, William Brown.
Billy’s “Station Billet” was made out within half an hour from the
time he arrived, and Bob was detailed to explain it to him.
Billy made out that he was Number 6 in the Second Division, Second
Section, which was in charge of Harold French, as “B. M.”, and Bob
offered to take him to French so that he could report at once.
French looked him over quietly and then said, “You know how to
swim, I suppose?”
“Yes,” said Billy, “I’ve done my fifty yards.”
“How about diving?” asked French.
Billy put his hand on his stomach with a smile and said with a
peculiar drawl, “I’ve had a good many slaps right here. I’m not much
good at that.”
“Probably you don’t swim under water, then,” replied the “B. M.”
“No,” said Billy, “and we never went beyond our depth where I
learned swimming.”
“Well,” said French, “we’ll have to go with you under water the first
chance we get, and I guess we can teach you a thing or two.”
Billy answered these questions with a deliberate air and without any
evidence of being ill at ease. He evidently was in the habit of
thinking before he spoke, and carefully listening to what was said;
and, although his carriage was poor and his physique not particularly
strong, French liked the clear, steady look in his eyes.
As Billy was turning away, the “B. M.” called after him, “Say, young
fellow, have you put your things into your locker and got them in
order yet?”
“Yes,” replied Brown.
“Well,” said the “B. M.”, looking at his watch, “we’ve got twenty-five
minutes before prayers, and I guess we can go forward and have
ten minutes’ drill.”
Then he turned for a minute to Dick Gray who had just returned on
board, and said in an undertone: “You just keep an eye on him,
Dick, and show him the ropes.”
At that moment there happened to be nobody on the forward deck,
so that French and Billy and Dick had it all to themselves. French
gave his commands in a comparatively subdued voice, so as not to
attract a crowd, and then suggested to Brown a few simple ideas
about the attitude of respectful attention and the significance of it in
the daily life of a scout, while Dick took the position to illustrate
what French meant.
Billy was rather awkward, and his large round eyeglasses and
prominent ears gave him a queer look of somehow being out of
place in his new surroundings; but French noticed that he seemed to
enjoy the drill and to take a genuine interest in what he was being
taught.
“Heels together, little finger on the seam of your trousers, chest up,
stomach in,—don’t bend backwards, balance on the instep—neck
straight, chin in. There, sonny, that’s the way you want to hold
yourself at quarters, or any time ‘at attention.’ But you can’t expect
to do it easily without teaching your body to be strong in the right
places. You seem kind of soft now, and here’s a little medicine that
will help harden you a bit.”
Then he and Dick showed him an exercise in deep breathing and
stretching. “Do that six times, two or three times a day, and I guess
it will give you a lift,” said French.
Billy thought of thanking French, but somehow or other felt that his
gratitude would not be appreciated,—or else that he had better
show it by making progress rather than by words,—so he drew
himself up straight and waited to see what was going to happen
next. Just then the boatswain’s call sounded out sharp for prayers,
and they all went down the companionway to the berth deck.
Billy’s locker and bunk were not far from Dick’s, and after “tattoo”
they undressed together and talked about a number of things that
had aroused Billy’s curiosity. They were interrupted by the sounding
of “taps”; and a minute after Bertie Young, the master-at-arms,
made his rounds to see that all were in their bunks and that
everything was quiet.
“He knows mighty little about a vessel,” thought Dick, as he turned
over in his bunk, “but he’s a great one for asking questions, and I
shouldn’t wonder if he knew the whole thing before long.”
In the next bunk but one, Billy lay thinking about the farm and the
home folks. “I wonder,” thought he, “whether Roger has
remembered the special feed for the calf.” Then, in another minute,
he was thinking of the ship again, and of his present surroundings,
and wondered how he could have come into so entirely different a
world within only fifty miles of the world he had left behind him.
“There are lots of things on board this ship,” thought he, “that I
never heard of in my life; but I’ll do my best to learn every one of
them, and won’t the boys at home be surprised!”
CHAPTER XIV
The Key of the Keelson

The next morning, much to every one’s disappointment, the ship’s


departure was delayed by a dead calm, and so the “routine at
anchor” continued uninterrupted. Meantime Mr. Jackson was making
the most of his opportunities for observing the life on board. After
following the instruction of the four sections from one o’clock to half-
past two, he promised himself to have a good talk with the
Chairman and the Captain when the boys should have gone ashore
for their liberty and everything on board would be comparatively
quiet.
Billy had his first experience of going over the masthead that
morning; and, as he was rather a sedentary boy, the thought of it
made him feel a little queer. He did not like climbing or high places
particularly, but he simply used his common sense, and told himself
that the thing had to be done and that there was no use thinking
any more about it. Dick managed to get up just behind him so that
he could watch him and give him a word of encouragement, if
necessary, and things went on all right until they got nearly to the
crosstrees. Then Dick noticed that Billy hesitated, and he reached his
own arm well up beside him and whispered to him just where to put
his hands and feet. The sound of Dick’s voice was all that little Bill
needed, and he rallied immediately and climbed to the top, and then
stepped across and began going down the other side.
In the afternoon Mr. Jackson watched the string of white hats and
jumpers disappear down the cabin companionway, as the boys went
to get some of their money before going ashore; and then the
inspection of the same string drawn up in line on deck before they
got into the boats at three o’clock. He watched them row away from
the ship with the “long boat” a little in advance,—their white hats
and jumpers like so many dots, and their oar blades sparkling in the
afternoon sun.
As he turned his head away from this cheerful scene, he noticed the
Chairman standing by his side and looking in the same direction.
“They’re not a bad lot of youngsters,” said the latter, “and there’s
just mischief enough in them to keep us busy.”
“Have they all gone ashore?” asked Mr. Jackson.
“All but a few,” replied the Chairman, and he blew his whistle.
“Dick,” said he, turning to the messenger, who had just run up in
answer to the signal, “find out how many boys are left on board.”
In a few minutes Dick returned and gave the result of his search.
“There are five, sir: the Chief ‘B. M.’, reading a book in the jib
netting, sir; two forward, scrubbing sails for extra duty; Tom
Sheffield and the new boy,—Brown, sir, I think is his name.”
“That makes six, counting you,” said the Chairman.
“Yes, sir, I’m on duty as messenger until the liberty party returns.”
“That’s well! Now go and ask Brown to come up and speak to me.”
Billy clambered up the berth deck ladder, and, as he stood at salute
before the Chairman and Mr. Jackson, the latter thought he already
saw a change for the better in his bearing.
“Sit down, Brown. This is a slack time, and I thought perhaps you’d
like to have a little visit with your uncle.”
“Yes, sir,” answered Billy, and he still spoke with the old-fashioned
deliberation that Mr. Wentworth had noticed the evening before at
the landing.
As he sat down, Mr. Jackson asked: “Well, how do you like it, Bill, as
far as you have got?”
“I like it first rate, uncle; seems to me I’m seeing something new
every minute, and I like it, too! I find I get twisted with so many
new names of things, but I guess that will get all straightened out in
a few days.”
“What do you find hardest, Bill?”
“Well, I guess it’s hardest for me to remember to stand up straight;
but that ‘B. M.’ of mine—what’s his name—he’s a pretty good sort of
a fellow, and gives me a dig in the ribs now and then; and then
there’s Dick—I don’t know his last name—he’s a good ’un! Oh! That
reminds me,” exclaimed Billy suddenly, “I had almost forgotten
something!”
Then turning to the Chairman:
“Will you please tell me, sir, where I can find the key of the
keelson?”
A faint smile played over the Chairman’s face as he asked the boy
why he wanted it.
“Well, the quartermaster, I think they call him, told me to get it,
because the ‘B. M.’ asked him for it, and he said he had not seen it
for some time; so I went and asked the messenger, and he didn’t
know where it was, and sent me to the boatswain; and the
boatswain, he didn’t know where it was, and sent me to the mate,
and the mate sent me to the Officer of the Day, and he said I’d
better ask the Chairman of the Committee. It’s a mighty funny thing
how a thing like that can get lost on board ship, for I haven’t noticed
many things lying around loose.”
The Chairman was silent for a few seconds, and then, “It is a funny
thing, Brown,” said he, “but I’ll tell you what you do. The Captain is
down in the cabin at this moment. You go and report to him from
me, tell him your story, and say that I am sure that he can help you
out.”
“Shall I go now, sir?” asked Billy.
“Yes,” said the Chairman, “unless Mr. Jackson has something else to
say to you.”
But Mr. Jackson had nothing more to say just then except this:
“When you have found out where the key of the keelson is, Bill, I
wish you would ask permission to show it to me, and then—bring
the keelson along, too, so that we can examine it carefully and see
whether the key really fits.”
There was the slightest possible twinkle in Mr. Jackson’s eyes as he
made this remark, and Bill looked at him earnestly, as if to fathom its
meaning. He then turned to the cabin companionway and
disappeared below.
Just then Mr. Jackson’s eye was arrested by a boy up on the
mainmast crosstrees, and he recognized Jack Perkins.
“Why, there’s your chief ‘B. M.’,” said he to the Chairman, “taking a
view of the ocean.”
“Yes,” was the reply, “that’s Jack, and he’s having his regular
constitutional. It is pretty good exercise for a boy who is as strong as
he is, but I should hate to do it myself.”
The Chairman then explained that Jack was in the habit of beginning
at the jib stay and climbing up to the foremast crosstrees hand over
hand, with only the slight support he could get from his knees and
feet. He then would proceed in the same manner over to the
mainmast crosstrees, a distance of twenty-seven feet, and finally
come down by the main topping lift to the quarter-deck.
“That means a pretty good head,” said Mr. Jackson, “besides good
muscle.”
Just then Billy reappeared, and with a broad grin on his face,
addressed the Chairman.
“I’m mighty sorry to have troubled you, sir, about that key. I guess
the works inside my thinker have run down, sir.” Then, turning to Mr.
Jackson, “Perhaps I’d better wait until after the cruise, uncle, and I’ll
bring you the keelson in my gripsack!”
“He seems to be the right stuff!” remarked the Chairman as Billy
walked off.
Having found out from the Captain the exact position and use of the
keelson—to bind the ship’s timbers to the keel—Bill lost no time in
spreading the information among the ship’s company after their
return on board. He was as much interested in sharing a new piece
of information as he was in acquiring it originally; and before supper
was over, the story of his investigation and its result had spread from
quarter-deck to forecastle.
That evening, after the riding lights were up, two choice spirits were
cleaning the sidelights, and, hoping to squeeze a little more fun out
of Bill, casually remarked:
“Look—Brown—hurry up, will you, and get me the green oil for the
starboard light!”
Billy, in his great desire to help, started toward the companionway,
then hesitated, and finally turned back, remarking dryly:
“I guess, Johnnie, if you want to go find it yourself, you’ll find the
green oil just alongside the red oil, and you’ll find them both—I
should say—in the same locker with the ‘key of the keelson’.”
Soon after this, the Triangle Club met in the jib netting.
“What do you think of the new fellow—Brown?” asked Dick.
“If he goes on asking questions at this rate,” replied Chippie, “his
cocoanut’ll bust before long.”
“Not much,” retorted Tom; “he’ll make a good master-at-arms one of
these days. He’s good stuff!”
CHAPTER XV
Seasickness

The following morning at quarters, as an easterly breeze had sprung


up during the night, it was announced that the ship would sail
immediately after “mast”, which would be at a quarter of ten. This
meant another opportunity for the “B. M.’s” to see how well they
could handle their sections in getting under way; and, as soon as
“mast” was over, Jack called Ellsworth and the two other boatswain’s
mates together to remind them of the details of their jobs. Then he
went over and talked with Bertie Young, the master-at-arms.
“Look, Bertie,” said he, “don’t let any lubbers loiter down below after
the call for getting under way has been sounded.”
“Sure, Jack,” said Bertie, “I’ll be on the job.”
Meantime the sky had become overcast, and the wind had
freshened, and it still blew from the northeast as it had done the day
before. While going over the masthead, the boys noticed quite heavy
clouds near the horizon. There was also more moisture in the air,
and everything looked as if a northeaster were making up its mind
to visit the coast. The wind, however, was not strong enough to
warrant taking in any reefs, and the Bright Wing sailed briskly away
from her anchorage with a motion that gradually increased as they
cleared the harbor. The lee scuppers were all under water by this
time and no mistake, and the vessel flew like a gull when the wind
catches its outspread wings and carries it along without resistance.
The tide was running out, also, and they slipped off so quickly that
an hour later, when the boys were looking back at the shore, the
familiar spires and headlands had almost disappeared in the
distance.
The manœuvre of getting under way had been carried out as
smoothly as the last time, and now the master-at-arms was busy
warning the smaller and greener boys off the lee rail.
“The weather’s too cold for a bath this morning, you lubbers; and,
besides, the Captain doesn’t want to stop her headway to pick you
up,” cried Bertie. “Gee, what a spanking breeze!”
Now and then a boy might be noticed lingering at the foot of the
foremast stays to leeward, looking intently at the water, and then
coming back with a wistful look upon his face. As time went on,
these little visits seemed to become more frequent; and then Mr.
Miller and Mr. Wentworth were observed by the Chairman tucking
boys away in different corners of the deck wrapped up in their
blankets; and these blanketed bundles of boys deposited in the more
sheltered nooks began to increase until there were half a dozen or
more scattered about in different places.
Just then Jones passed along and said to one of the boys with a
jeer: “Seasick, eh! I thought so!”
“No, sir!” answered Chip, “I was sick, but I’m feeling better. Next
time she goes about, I think I’ll take a hand with the rest of them.”
“Ready about! Hard-a-lee!” rang out across the deck.
Chippie jumped to his feet in an instant.
“Gee,” muttered he, “but that was a short tack,” and ran to report to
his “B. M.”
“That’s good, Chippie,” said Ellsworth, “you take hold right here,”
and in another minute the Bright Wing gave a spring and was off
again on the port tack.
Harold French and Randall Turner were the two “B. M.’s” of the
second division, and they both began cheering up their invalids and
pointing to Chippie, who was right as a trivet, though his face still
looked a little paler than usual.
At mess inspection there were only three boys missing; but some of
those who had begun to feel well as long as they were on deck,
asked suddenly to be excused before the end of dinner, and
scrambled up the ladder into the fresh air as fast as they could. In
the meantime Bertie Young, the master-at-arms, had given cups of
broth to the patients who had remained on deck, and was urging
them to chew some pieces of hard tack.
“Put something into your stomachs, boys, and that will make them
feel more homelike.”
They were all feeling better by this time, and Dick Gray called out,
lifting his head from underneath his blanket:
“Say, fellows, here’s a conundrum for you: Which would you rather
do,—feel all O.K. ashore, or sick as a boiled owl at sea?”
“Gee,” growled out a voice from another gray blanket, “I know what
answer I’d a’ given to that two hours ago, but it’s different now! So,
here goes, fellows,” and he jumped to his feet. “I call for three times
three cheers for the Bright Wing and ‘being sick at sea!’”
The other two also sprang to their feet; and, as the Captain and Mr.
Miller were coming up on deck, they heard, to their surprise, the
sounds of the cheering, “Rah, Rah, Rah—Rah, Rah, Rah—Rah, Rah,
Rah—Sea Scouts—Sea Scouts—Sea Scouts—Bright Wing, Bright
Wing, Bright Wing—S-i-c-k a-t S-e-a!”
A roar of laughter followed from the former patients of the hospital
ward, and it was so contagious that it reached way forward to the
galley where the mess cooks were washing up after dinner.
“See here, we’re not all through yet,” cried Dick, as a new blanketed
figure lay down. Chippie noticed the pale face of Jones.
“Can I get you anything, Jones?” asked Chippie innocently.
“No,” answered Jones, with a groan and sour face.
“He doesn’t feel as perky as he did,” thought Chippie to himself, with
a grin.
There was always a “band concert” of half an hour after dinner
when the ship was at sea; and, under the influence of the music
from the Victrola, the last remnants of squeamishness disappeared,
except in the case of poor old Jones.
“Pride comes before a fall,” said Dick to Chippie; “I guess he’d have
done better to own up before.”
Some of the huskies even tried dancing up and down on the
windward side of the deck. The wind also seemed to be backing
round to the westward, and the motion of the waves was not quite
so lively.
Mr. Wentworth was Officer of the Day; and, after the band concert
and dance were over, the Chairman and Mr. Miller went below. Mr.
Wilson, the mate, was at the wheel giving some of the older boys a
spell from time to time, and the Captain had already gone below to
write some letters.
After Turner, French, and Perkins each had been at the wheel for
about twenty minutes, the lookout shouted, “Light-ho!”
The mate immediately responded in a loud singing voice, “Wh-e-r-e
—away?”
“Three points off the port bow!” came the answer from the lookout.
“Can you make her out?” called the mate.
And the lookout sang back, “I think it’s Whale’s Back Light, sir.”
Just then the cry, “Messenger!” was heard coming up through the
cabin companionway, and Sidney Malloy, who was the messenger on
duty, came running aft to answer the summons.
“Messenger, sir,” said he, as he saluted, standing beside the
Chairman’s bunk.
“Oh! Sidney, is that you?” said the Chairman, “are there any more
sick boys on deck?”
“No, sir,” answered Sidney, “only one; but a few of ’em look a little
green-like, sir.”
“Who are they?”
“Well, sir, there’s Dickie Gray and Chip Smith—they don’t seem to be
quite first-rate yet, sir.”
“Send Gray down to me,” said the Chairman, who then got out of his
bunk and took his coat down from the bulkhead.
There was a sound of running feet on the deck, for the messenger
always did his errands on the run, and, in another minute, Dick was
in the cabin.
“You sent for me, sir?”
“Yes,” said the Chairman, “sit down there on the locker a minute.
How far are we from ‘Whale’s Back’?”
“The lookout has just reported ‘Whale’s Back’ in sight, sir.”
“Really,” said the Chairman, “that is very interesting; because now
you can tell me how far we are from Portsmouth Harbor.”
“How is that, sir?” asked Dick.
“Well, if you don’t know, I can tell you in a few minutes; and then
you can pass it on to some of the other boys. Go and ask the
yeoman to give you a plumb line and half-circle, and then bring it
here to me.”
Dick ran off very much interested and found the yeoman writing a
letter on the berth deck.
“Say, Bob,” said Dick, “what’s a half-circle and plumb line? Have you
got one?”
“Sure,” answered Bob, “what do you want it for?”
“The old man has just sent me down to get it. Hurry up there,
quick.”
Bob Brackett, as yeoman of the ship, besides being always
responsible for carrying the mail, had charge of the stationery,
postage stamps, games, and instruction material; and now he dove
down into one corner of his yeoman’s locker and pulled out a board
shaped like a half-circle with a straight edge or diameter of eight
inches.
“There’s the board,” said he, and tossed it on the table; “the plumb
line seems to have gone adrift.”
Then, after rummaging a little longer, he called out:
“Here it is,” and handed Dick a leaden sinker fastened to a string.
“Now wait a minute,” said Bob, “and I’ll tack the string on for you.
You see it’s got to be fastened just in the right place,—at the center
of the straight edge.”
He fastened the string down with a thumb tack, and then Bob took it
and ran aft again to the cabin.
“Have you had anything about angles and circles at school, Gray?”
asked the Chairman.
“Yes, sir, but I don’t remember much about ’em.”
“Well, I guess you’ll remember enough to understand what I am
going to tell you.”
Then he pointed out the degree marks that ran along the curved
edge of the half circular board.
“Now if you hold the board up with the straight edge on top and
horizontal, you’ll notice that the plumb line falls at right angles to the
horizontal line and, at the edge of the circumference, passes through
a point marked zero—but you must have seen a thing like this
before, haven’t you? It’s what they call a ‘protractor.’”
“I’ve seen something like it, I think, sir, but I never understood what
it was for.”
“Well,” said the Chairman, “you’ll see now how easy it is to
understand, when you put your mind to it! This point marked zero
that the plumb line passes through when the straight edge is
horizontal, is exactly in the middle of the curved edge of the
semicircumference. Now, from zero along the curve to the end of the
straight edge, on both sides of zero, are ninety little points marking
ninety degrees, making two halves of a semicircle, or in other words,
one-half of a whole circle of 360 degrees.
“If you hold the straight edge of the board up to your eye and move
the further end upward from the horizon, while keeping the center
steady, the plumb line moves away from zero toward your body, and
at the same time along the face of the semicircle, and registers a
certain number of degrees from zero. If you imagine yourself
standing in the middle of a circle, you can also imagine a lighthouse
or a church steeple at the circumference of the circle. This distance
to the circumference will be the radius. Then imagine that radius
held fast at your end, but swinging upward and pointing to the sky.
It will measure ninety degrees when it is pointing straight up from
where you are standing to a point in the heavens directly above your
head, and forty-five degrees when pointing to a place in the sky half-
way between the horizon and a point directly above, and less than
forty-five degrees when pointing to a place in the sky nearer the
horizon. The straight edge of this board represents the direction of
the sight line, or radius; and, as you tip the end up above the
horizon until it reaches the top of the tower or steeple you are
measuring, the same number of degrees will be registered by the
plumb line at the bottom of the board, counting from zero, as the
line appears to move toward yourself. Now, if you are holding your
board with your right hand, just put the finger of your left hand on
the plumb line to keep it in place while you look to see what it
registers,—this way,” and the Chairman went through the motion
with his hands.
“Thank you, sir,” said Dick, “I understand that very well, but I don’t
see how it gives the distance to the lighthouse.”
“No,” replied the Chairman, “of course not, and that is just where
the most interesting part of the operation comes in. I have shown
you how to measure the height of your lighthouse in degrees along
the edge of a circumference; and now we must find out how to
measure the distance of the lighthouse, or the radius of the circle,
from the center where we are standing, to the point in the
circumference where the lighthouse stands.”
Dick looked up with interest and said, “I don’t see, sir, how that can
be done, because we want the distance in miles or yards, and there
is nothing to show us the number of miles or yards in a degree.”
“That’s true,” said the Chairman, “but happily for us, some old fellow
a long time ago had a bright idea. He knew that, in every circle, the
length of the radius is always in the same proportion to the length of
the circumference, and so he laid out a line equal in length to the
radius along the circumference, and found that it was equal to a
little more than 57 degrees. Supposing, then, that you found your
lighthouse to be four degrees above the horizon, you could then
calculate what fraction of the length of the radius the height of your
tower is by finding the number of times that four is contained in 57.
Let me see,—that would be about 14 1/4, wouldn’t it?”
“I guess so, sir,” said Dick.
“Now,” continued the Chairman, “if your lighthouse is 300 feet high,
this 300 feet will be the same part of the distance as 4 degrees is of
57 degrees. But 4 goes into 57, 14 1/4 times; therefore, the length of
the distance will be 300 feet × 14 1/4, which is 4,275 feet, or about
four-fifths of a mile.”
“Oh, I see,” cried Dick, “but how do we find out the height of the
lighthouse?”
“That’s a good question,” said the Chairman; “we can’t find the
distance unless the height is given, and for that reason the heights
of lighthouses and other conspicuous objects are usually given on
the charts. But, if we knew our distance from the lighthouse, we
could easily find its height by similar reasoning.”
Just then the Captain, who had been writing at the cabin table,
looked up and said: “I’ll show you the chart, Gray—I don’t just
remember the height of ‘Whale’s Back’,” and he reached out for the
chart of Portsmouth Harbor, which was rolled up in its place on the
cabin bulkhead. When he had unrolled it he showed Dick the figures.
“Thank you, sir,” said Dick. “Mr. Chairman, is there any time when
you’d like me to report about this?”
“Come and speak to me, right after ‘colors’,” replied the Chairman.
Dick saluted and climbed up on deck as quickly as he could. He felt a
kind of new opening in his brain, and was keen on making his
observation and calculation as soon as possible. Every trace of
seasickness had vanished.
CHAPTER XVI
The Commandant’s Inspection

The next morning Billy Brown went through “washing clothes” and
“scrubbing decks”, “colors”, “bright work”, “quarters”, and “mast”
with his eyes and ears wide open; and, all the time, he was looking
out for an opportunity to practice the exercise which his “B. M.” had
given him as medicine for his “softness.” During morning instruction
he had “knots and splices” for one period, “boxing the compass” for
another, and “ship’s tackle” with the mate for the third. He came out
very well with the first two, but was of course quite ignorant when it
came to ship’s tackle. As was always the case, however, the more
ignorant he was, the more anxious he seemed to learn, and quite a
shower of questions fell about the ears of the mate before the
instruction period was over.
While drill was going on among the boys, the Captain and the
Chairman were making a regular inspection below to prepare for the
visit of the Commandant of the Portsmouth Navy Yard, who was
expected on board at 11:30 to inspect the ship and crew.
Several boys’ lockers were found in disorder and reported to their
respective “B. M.’s.” But the Chairman was delighted with the
excellent condition of the galley and the forecastle.
“Ketchy”, the cook’s assistant, also served as cabin boy on board,
and he was particularly careful that morning to see that the officers’
quarters were as well up to the mark, at least, as any other part of
the vessel.
At half-past ten Mr. Miller left in the launch to call for the
Commandant at the Navy Yard; and Dick Gray, who was one of the
regular watch, was detailed to keep a sharp lookout for the return of
the launch and report it at once to Mr. Wentworth, who was Officer
of the Day.
All the boys were in their “whites” in honor of the impending visit;
and they were just about finishing the third instruction period when
Dick noticed the launch, flying the “Jack” in her bow, turn the corner
of the fort at the entrance of Little Harbor. He immediately reported
to Mr. Wentworth, and the “B. M.” on watch blew his call and
sounded “Retreat from drill.”
“Side boys—stand by!” was the next command, and two “B. M.’s”
with their respective quartermasters took their positions at the
starboard side-ladder. Meantime the rest of the crew was ordered to
its regular stations as at “quarters”, and only the watch were left to
lend a hand as the launch drew up to the side.
The Commandant was rather a stout officer with a pleasant look. A
good-humored smile lit up his face as he stepped on deck between
the side boys standing at salute and noticed the trill of the
boatswain’s call. His hand was first raised to acknowledge the salute,
and he then shook hands with the Captain and the Chairman who
were waiting to receive him.
They introduced Mr. Jackson as their guest, and all three walked aft
to the quarter-deck and sat down under the awning.
The boys were then ordered to “stand at ease” until after the
inspection of the ship, and their visitor inspected her carefully from
stem to stern, looking into every corner, and asking to have the lid
removed from everything that was covered up.
While they were in the galley he examined carefully the back of the
range to see whether there was any danger from fire; and then,
pointing to a ring in the deck beneath their feet, “What’s that?”
asked he.
“A coal bunker,” answered the Captain; and, lifting the corner, which
was a little stiff, he showed the place between the deck and the
timbers below where the range coal was kept.
The Commandant then asked questions about the storage of
gasoline for the launch and where the kerosene for the lamps was
kept. It was explained to him that the only supply of gasoline was

You might also like