0% found this document useful (0 votes)
13 views40 pages

Buy ebook MATLAB Mathematics 1st Edition Mathworks cheap price

The document provides information about the MATLAB Mathematics 1st Edition by Mathworks, including links to download the ebook and other related digital products. It also contains details on various mathematical concepts covered in the book, such as matrices, linear algebra, and factorization. Additionally, it includes contact information for MathWorks and a revision history of the document.

Uploaded by

slawusreedah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views40 pages

Buy ebook MATLAB Mathematics 1st Edition Mathworks cheap price

The document provides information about the MATLAB Mathematics 1st Edition by Mathworks, including links to download the ebook and other related digital products. It also contains details on various mathematical concepts covered in the book, such as matrices, linear algebra, and factorization. Additionally, it includes contact information for MathWorks and a revision history of the document.

Uploaded by

slawusreedah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Get the full ebook with Bonus Features for a Better Reading Experience on ebookmeta.

com

MATLAB Mathematics 1st Edition Mathworks

https://ebookmeta.com/product/matlab-mathematics-1st-
edition-mathworks/

OR CLICK HERE

DOWLOAD NOW

Download more ebook instantly today at https://ebookmeta.com


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

Audio Toolbox User s Guide MATLAB Simulink 1st Edition


Mathworks

https://ebookmeta.com/product/audio-toolbox-user-s-guide-matlab-
simulink-1st-edition-mathworks/

ebookmeta.com

Deep Learning Toolbox Getting Started Guide MATLAB The


Mathworks

https://ebookmeta.com/product/deep-learning-toolbox-getting-started-
guide-matlab-the-mathworks/

ebookmeta.com

MATLAB Symbolic Math Toolbox User s Guide 2023 The


Mathworks

https://ebookmeta.com/product/matlab-symbolic-math-toolbox-user-s-
guide-2023-the-mathworks/

ebookmeta.com

The Feminine Mesquite The Complete Series 1st Edition


Sable Sylvan

https://ebookmeta.com/product/the-feminine-mesquite-the-complete-
series-1st-edition-sable-sylvan/

ebookmeta.com
The Routledge Handbook of State Owned Enterprises 1st
Edition Luc Bernier Massimo Florio Philippe Bance Eds

https://ebookmeta.com/product/the-routledge-handbook-of-state-owned-
enterprises-1st-edition-luc-bernier-massimo-florio-philippe-bance-eds/

ebookmeta.com

Edexcel International GCSE 9 1 History The Origins and


Course of the First World War 1905 18 Student Book 1st
Edition Rosemary Rees
https://ebookmeta.com/product/edexcel-international-gcse-9-1-history-
the-origins-and-course-of-the-first-world-war-1905-18-student-
book-1st-edition-rosemary-rees/
ebookmeta.com

One Night with the Mountain Man 1st Edition Eve London

https://ebookmeta.com/product/one-night-with-the-mountain-man-1st-
edition-eve-london/

ebookmeta.com

Pokemon HeartGold SoulSilver The Official Pokemon Johto


Guide Johto Pokedex Official Strategy Guide Prima Official
Game Guide The Pokemon Company
https://ebookmeta.com/product/pokemon-heartgold-soulsilver-the-
official-pokemon-johto-guide-johto-pokedex-official-strategy-guide-
prima-official-game-guide-the-pokemon-company/
ebookmeta.com

Fundamentals of Structural Dynamics: Theory and


Computation Hjelmstad

https://ebookmeta.com/product/fundamentals-of-structural-dynamics-
theory-and-computation-hjelmstad/

ebookmeta.com
Mathematics Mathematics HL Option Discrete Mathematics 1st
Edition Catherine Quinn Peter Blythe Chris Sangwin Robert
Haese Michael Haese
https://ebookmeta.com/product/mathematics-mathematics-hl-option-
discrete-mathematics-1st-edition-catherine-quinn-peter-blythe-chris-
sangwin-robert-haese-michael-haese/
ebookmeta.com
MATLAB®
Mathematics

R2023a
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
MATLAB® Mathematics
© COPYRIGHT 1984–2023 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
June 2004 First printing New for MATLAB 7.0 (Release 14), formerly part of
Using MATLAB
October 2004 Online only Revised for MATLAB 7.0.1 (Release 14SP1)
March 2005 Online only Revised for MATLAB 7.0.4 (Release 14SP2)
June 2005 Second printing Minor revision for MATLAB 7.0.4
September 2005 Second printing Revised for MATLAB 7.1 (Release 14SP3)
March 2006 Second printing Revised for MATLAB 7.2 (Release 2006a)
September 2006 Second printing Revised for MATLAB 7.3 (Release 2006b)
September 2007 Online only Revised for MATLAB 7.5 (Release 2007b)
March 2008 Online only Revised for MATLAB 7.6 (Release 2008a)
October 2008 Online only Revised for MATLAB 7.7 (Release 2008b)
March 2009 Online only Revised for MATLAB 7.8 (Release 2009a)
September 2009 Online only Revised for MATLAB 7.9 (Release 2009b)
March 2010 Online only Revised for MATLAB 7.10 (Release 2010a)
September 2010 Online only Revised for MATLAB 7.11 (Release 2010b)
April 2011 Online only Revised for MATLAB 7.12 (Release 2011a)
September 2011 Online only Revised for MATLAB 7.13 (Release 2011b)
March 2012 Online only Revised for MATLAB 7.14 (Release 2012a)
September 2012 Online only Revised for MATLAB 8.0 (Release 2012b)
March 2013 Online only Revised for MATLAB 8.1 (Release 2013a)
September 2013 Online only Revised for MATLAB 8.2 (Release 2013b)
March 2014 Online only Revised for MATLAB 8.3 (Release 2014a)
October 2014 Online only Revised for MATLAB 8.4 (Release 2014b)
March 2015 Online only Revised for MATLAB 8.5 (Release 2015a)
September 2015 Online only Revised for MATLAB 8.6 (Release 2015b)
October 2015 Online only Rereleased for MATLAB 8.5.1 (Release 2015aSP1)
March 2016 Online only Revised for MATLAB 9.0 (Release 2016a)
September 2016 Online only Revised for MATLAB 9.1 (Release 2016b)
March 2017 Online only Revised for MATLAB 9.2 (Release 2017a)
September 2017 Online only Revised for MATLAB 9.3 (Release 2017b)
March 2018 Online only Revised for MATLAB 9.4 (Release 2018a)
September 2018 Online only Revised for MATLAB 9.5 (Release 2018b)
March 2019 Online only Revised for MATLAB 9.6 (Release 2019a)
September 2019 Online only Revised for MATLAB 9.7 (Release 2019b)
March 2020 Online only Revised for MATLAB 9.8 (Release 2020a)
September 2020 Online only Revised for MATLAB 9.9 (Release 2020b)
March 2021 Online only Revised for MATLAB 9.10 (Release 2021a)
September 2021 Online only Revised for MATLAB 9.11 (Release 2021b)
March 2022 Online only Revised for MATLAB 9.12 (Release 2022a)
September 2022 Online only Revised for MATLAB 9.13 (Release 2022b)
March 2023 Online only Revised for MATLAB 9.14 (Release 2023a)
Contents

Matrices and Arrays


1
Creating, Concatenating, and Expanding Matrices . . . . . . . . . . . . . . . . . . 1-2

Removing Rows or Columns from a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . 1-8

Reshaping and Rearranging Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9

Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-14

Array Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-21

Linear Algebra
2
Matrices in the MATLAB Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Creating Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Adding and Subtracting Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Vector Products and Transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Multiplying Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
Identity Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
Matrix Inverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
Kronecker Tensor Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
Vector and Matrix Norms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
Using Multithreaded Computation with Linear Algebra Functions . . . . . . . 2-9

Systems of Linear Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-10


Computational Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-10
General Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-11
Square Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-11
Overdetermined Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
Underdetermined Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15
Solving for Several Right-Hand Sides . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17
Iterative Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-18
Multithreaded Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-18

Factorizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20
Cholesky Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20
LU Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-21
QR Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-22
Using Multithreaded Computation for Factorization . . . . . . . . . . . . . . . . 2-25

v
Powers and Exponentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-26

Eigenvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-30
Eigenvalue Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-30
Multiple Eigenvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-31
Schur Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-31

Singular Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-33


Batched SVD Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-35
Low-Rank SVD Approximations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-35

LAPACK in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-37


A Brief History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-37

Matrix Exponentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-38

Graphical Comparison of Exponential Functions . . . . . . . . . . . . . . . . . . . 2-42

Basic Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-46

Determine Whether Matrix Is Symmetric Positive Definite . . . . . . . . . . . 2-54

Image Compression with Low-Rank SVD . . . . . . . . . . . . . . . . . . . . . . . . . . 2-56

Random Numbers
3
Why Do Random Numbers Repeat After Startup? . . . . . . . . . . . . . . . . . . . 3-2

Create Arrays of Random Numbers ............................... 3-3


Random Number Functions . . . . ............................... 3-3
Random Number Generators . . . ............................... 3-4
Random Number Data Types . . . ............................... 3-4

Random Numbers Within a Specific Range . . . . . . . . . . . . . . . . . . . . . . . . . 3-6

Random Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7

Random Numbers from Normal Distribution with Specific Mean and


Variance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8

Random Numbers Within a Sphere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-9

Generate Random Numbers That Are Repeatable . . . . . . . . . . . . . . . . . . 3-11


Specify the Seed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11
Save and Restore the Generator Settings . . . . . . . . . . . . . . . . . . . . . . . . 3-12

Generate Random Numbers That Are Different . . . . . . . . . . . . . . . . . . . . 3-14

Managing the Global Stream Using RandStream . . . . . . . . . . . . . . . . . . . 3-16

vi Contents
Creating and Controlling a Random Number Stream . . . . . . . . . . . . . . . 3-19
Substreams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-19
Choosing a Random Number Generator . . . . . . . . . . . . . . . . . . . . . . . . . 3-21
Configuring a Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24
Restore State of Random Number Generator to Reproduce Output . . . . . 3-25

Multiple Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-28

Replace Discouraged Syntaxes of rand and randn . . . . . . . . . . . . . . . . . . 3-32


Description of the Discouraged Syntaxes . . . . . . . . . . . . . . . . . . . . . . . . 3-32
Description of Replacement Syntaxes . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-32
Replacement Syntaxes for Initializing the Generator with an Integer Seed
..................................................... 3-33
Replacement Syntaxes for Initializing the Generator with a State Vector
..................................................... 3-34
If You Are Unable to Upgrade from Discouraged Syntax . . . . . . . . . . . . . 3-34

Controlling Random Number Generation . . . . . . . . . . . . . . . . . . . . . . . . . 3-36

Sparse Matrices
4
Computational Advantages of Sparse Matrices . . . . . . . . . . . . . . . . . . . . . 4-2
Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Computational Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2

Constructing Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4


Creating Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4
Importing Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7

Accessing Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8


Nonzero Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8
Indices and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9
Indexing in Sparse Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9
Visualizing Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-12

Sparse Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14


Efficiency of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
Permutations and Reordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
Factoring Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-17
Eigenvalues and Singular Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-24
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26

Finite Difference Laplacian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-28

Graphical Representation of Sparse Matrices . . . . . . . . . . . . . . . . . . . . . 4-32

Graphs and Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-38

Sparse Matrix Reordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-44

vii
Iterative Methods for Linear Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-56
Direct vs. Iterative Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-56
Generic Iterative Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-57
Summary of Iterative Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-57
Choosing an Iterative Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-59
Preconditioners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-60
Equilibration and Reordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-62
Using Linear Operators Instead of Matrices . . . . . . . . . . . . . . . . . . . . . . 4-64
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-64

Graph and Network Algorithms


5
Directed and Undirected Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
What Is a Graph? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
Creating Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
Graph Node IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8
Modify or Query Existing Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8

Modify Nodes and Edges of Existing Graph . . . . . . . . . . . . . . . . . . . . . . . 5-10

Add Graph Node Names, Edge Weights, and Other Attributes . . . . . . . . 5-13

Graph Plotting and Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-17

Visualize Breadth-First and Depth-First Search . . . . . . . . . . . . . . . . . . . . 5-28

Partition Graph with Laplacian Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-33

Add Node Properties to Graph Plot Data Tips . . . . . . . . . . . . . . . . . . . . . . 5-36

Build Watts-Strogatz Small World Graph Model . . . . . . . . . . . . . . . . . . . . 5-41

Use PageRank Algorithm to Rank Websites . . . . . . . . . . . . . . . . . . . . . . . 5-48

Label Graph Nodes and Edges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-55

Functions of One Variable


6
Create and Evaluate Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2

Roots of Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4


Numeric Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
Roots Using Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
Roots in a Specific Interval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
Symbolic Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7

Integrate and Differentiate Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-9

viii Contents
Polynomial Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11

Predicting the US Population . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-13

Roots of Scalar Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-18

Computational Geometry
7
Triangulation Representations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2
2-D and 3-D Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2
Triangulation Matrix Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-3
Querying Triangulations Using the triangulation Class . . . . . . . . . . . . . . . 7-4

Working with Delaunay Triangulations . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-14


Definition of Delaunay Triangulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-14
Creating Delaunay Triangulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-15
Triangulation of Point Sets Containing Duplicate Locations . . . . . . . . . . . 7-35

Creating and Editing Delaunay Triangulations . . . . . . . . . . . . . . . . . . . . . 7-38

Spatial Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-53

Voronoi Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-58


Plot 2-D Voronoi Diagram and Delaunay Triangulation . . . . . . . . . . . . . . 7-58
Computing the Voronoi Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-61

Types of Region Boundaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-65


Convex Hulls vs. Nonconvex Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-65
Alpha Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-67

Computing the Convex Hull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-71


Computing the Convex Hull Using convhull and convhulln . . . . . . . . . . . 7-71
Convex Hull Computation Using the delaunayTriangulation Class . . . . . . 7-74
Convex Hull Computation Using alphaShape . . . . . . . . . . . . . . . . . . . . . 7-75

Interpolation
8
Gridded and Scattered Sample Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2
Interpolation versus Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2
Grid Approximation Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-3

Interpolating Gridded Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-5


MATLAB Gridded Interpolation Functions . . . . . . . . . . . . . . . . . . . . . . . . . 8-5
Grid Representations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-5
Example: Temperature Interpolation on 2-D Grid . . . . . . . . . . . . . . . . . . . 8-8
Gridded Interpolation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-10

ix
Interpolation of Multiple 1-D Value Sets . . . . . . . . . . . . . . . . . . . . . . . . . . 8-13

Interpolation of 2-D Selections in 3-D Grids . . . . . . . . . . . . . . . . . . . . . . . 8-15

Interpolating Scattered Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-17


Scattered Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-17
Interpolating Scattered Data Using griddata and griddatan . . . . . . . . . . 8-20
scatteredInterpolant Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-22
Interpolating Scattered Data Using the scatteredInterpolant Class . . . . . 8-23
Interpolation of Complex Scattered Data . . . . . . . . . . . . . . . . . . . . . . . . 8-30
Addressing Problems in Scattered Data Interpolation . . . . . . . . . . . . . . . 8-32

Interpolation Using a Specific Delaunay Triangulation . . . . . . . . . . . . . . 8-41


Nearest-Neighbor Interpolation Using a delaunayTriangulation Query . . 8-41
Linear Interpolation Using a delaunayTriangulation Query . . . . . . . . . . . 8-42

Extrapolating Scattered Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-44


Factors That Affect the Accuracy of Extrapolation . . . . . . . . . . . . . . . . . . 8-44
Compare Extrapolation of Coarsely and Finely Sampled Scattered Data
..................................................... 8-44
Extrapolation of 3-D Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-48

Normalize Data with Differing Magnitudes . . . . . . . . . . . . . . . . . . . . . . . . 8-50

Resample Image with Gridded Interpolation . . . . . . . . . . . . . . . . . . . . . . 8-53

Optimization
9
Optimizing Nonlinear Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Minimizing Functions of One Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Minimizing Functions of Several Variables . . . . . . . . . . . . . . . . . . . . . . . . 9-3
Maximizing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4
fminsearch Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4
Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-6

Curve Fitting via Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-7

Set Optimization Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-10


How to Set Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-10
Options Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-10
Tolerances and Stopping Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-11
Output Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-12

Optimization Solver Iterative Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-13

Optimization Solver Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-14


What Is an Output Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-14
Creating and Using an Output Function . . . . . . . . . . . . . . . . . . . . . . . . . 9-14
Structure of the Output Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-15
Example of a Nested Output Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-16
Fields in optimValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-17

x Contents
States of the Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-17
Stop Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-18

Optimization Solver Plot Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-20


What Is a Plot Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-20
Example: Plot Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-20

Optimize Live Editor Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-22


What Is the Optimize Live Editor Task? . . . . . . . . . . . . . . . . . . . . . . . . . . 9-22
Minimize a Nonlinear Function of Several Variables . . . . . . . . . . . . . . . . 9-23
Solve a Scalar Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-25

Optimization Troubleshooting and Tips . . . . . . . . . . . . . . . . . . . . . . . . . . 9-28

Function Handles
10
Parameterizing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2
Parameterizing Using Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . 10-2
Parameterizing Using Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . 10-3

Ordinary Differential Equations (ODEs)


11
Choose an ODE Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Ordinary Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Types of ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Systems of ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-3
Higher-Order ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-3
Complex ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-4
Basic Solver Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-5
Summary of ODE Examples and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-7

Summary of ODE Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-10


Options Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-10
Compatibility of Options with Each Solver . . . . . . . . . . . . . . . . . . . . . . 11-10
Usage Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-12

ODE Event Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-13


What is Event Location? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-13
Writing an Event Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-13
Event Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-14
Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-14
Simple Event Location: A Bouncing Ball . . . . . . . . . . . . . . . . . . . . . . . . 11-14
Advanced Event Location: Restricted Three Body Problem . . . . . . . . . . 11-15

Solve Nonstiff ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-19

xi
Solve Stiff ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-23

Solve Differential Algebraic Equations (DAEs) . . . . . . . . . . . . . . . . . . . . 11-30


What is a Differential Algebraic Equation? . . . . . . . . . . . . . . . . . . . . . . 11-30
Consistent Initial Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-31
Differential Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-31
Imposing Nonnegativity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-32
Solve Robertson Problem as Semi-Explicit Differential Algebraic Equations
(DAEs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-32

Nonnegative ODE Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-35

Troubleshoot Common ODE Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-38


Error Tolerances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-38
Problem Scale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-39
Solution Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-40
Problem Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-42

Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-43

Solve Predator-Prey Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-52

Solve Equations of Motion for Baton Thrown into Air . . . . . . . . . . . . . . 11-56

Solve ODE with Strongly State-Dependent Mass Matrix . . . . . . . . . . . . 11-62

Solve Stiff Transistor Differential Algebraic Equation . . . . . . . . . . . . . . 11-75

Solve System of ODEs with Multiple Initial Conditions . . . . . . . . . . . . . 11-80

Solve Celestial Mechanics Problem with High-Order Solvers . . . . . . . . 11-88

Boundary Value Problems (BVPs)


12
Solving Boundary Value Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2
Boundary Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2
Initial Guess of Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-3
Finding Unknown Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-3
Singular BVPs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-4
BVP Solver Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-4
Evaluating the Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-5
BVP Examples and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-6

Solve BVP with Two Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-8

Solve BVP with Unknown Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-11

Solve BVP Using Continuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-15

Verify BVP Consistency Using Continuation . . . . . . . . . . . . . . . . . . . . . . 12-20

xii Contents
Solve BVP with Singular Term . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-25

Solve BVP with Multiple Boundary Conditions . . . . . . . . . . . . . . . . . . . . 12-29

Partial Differential Equations (PDEs)


13
Solving Partial Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
What Types of PDEs Can You Solve with MATLAB? . . . . . . . . . . . . . . . . . 13-2
Solving 1-D PDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
Example: The Heat Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-5
PDE Examples and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-8

Solve Single PDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-9

Solve PDE with Discontinuity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-16

Solve PDE and Compute Partial Derivatives . . . . . . . . . . . . . . . . . . . . . . 13-23

Solve System of PDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-32

Solve System of PDEs with Initial Condition Step Functions . . . . . . . . 13-39

Delay Differential Equations (DDEs)


14
Solving Delay Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2
Constant Delay DDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2
Time-Dependent and State-Dependent DDEs . . . . . . . . . . . . . . . . . . . . . 14-2
DDEs of Neutral Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2
Evaluating the Solution at Specific Points . . . . . . . . . . . . . . . . . . . . . . . . 14-3
History and Initial Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-3
Discontinuities in DDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-3
Propagation of Discontinuities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-4
DDE Examples and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-4

DDE with Constant Delays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-6

DDE with State-Dependent Delays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-10

Cardiovascular Model DDE with Discontinuities . . . . . . . . . . . . . . . . . . 14-14

DDE of Neutral Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-19

Initial Value DDE of Neutral Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-23

xiii
Numerical Integration
15
Integration to Find Arc Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-2

Complex Line Integrals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-3

Singularity on Interior of Integration Domain . . . . . . . . . . . . . . . . . . . . . 15-5

Analytic Solution to Integral of Polynomial . . . . . . . . . . . . . . . . . . . . . . . 15-7

Integration of Numeric Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-8

Calculate Tangent Plane to Surface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-12

Fourier Transforms
16
Fourier Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2

Basic Spectral Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-10


Spectral Analysis Quantities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-10
Noisy Signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-10
Audio Signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-12

Polynomial Interpolation Using FFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-16


FFT in Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-16
Interpolate Asteroid Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-16

2-D Fourier Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-19


Two-Dimensional Fourier Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-19
2-D Diffraction Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-19

Square Wave from Sine Waves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-23

Analyzing Cyclical Data with FFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-28

Gate-Based Quantum Algorithms


17
Introduction to Quantum Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-2
Qubit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-2
Quantum Gate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-9
Quantum Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-11
Helper Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-16

Types of Quantum Gates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-18


Creation Functions for SimpleGate Objects . . . . . . . . . . . . . . . . . . . . . . 17-18

xiv Contents
Creation Functions for CompositeGate Objects . . . . . . . . . . . . . . . . . . . 17-22

Local Quantum State Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-25


Create Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-25
Simulate Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-25
Display State Formula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-26
Plot Possible States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-27
Query Possible States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-27
Query Qubit State Probabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-27
Simulate Quantum State Measurement . . . . . . . . . . . . . . . . . . . . . . . . . 17-28

Run Quantum Circuit on Hardware Using AWS . . . . . . . . . . . . . . . . . . . 17-29


Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-29
Set Up Access to AWS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-29
Device Availability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-30
Connect to Quantum Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-31
Create Task to Run Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-31
Fetch Measurement Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-32

Graph Coloring with Grover's Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 17-34


Create State Oracle for Graph Coloring Problem . . . . . . . . . . . . . . . . . 17-34
Create Diffuser Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-38
Apply Grover's Algorithm to Graph Coloring Oracle . . . . . . . . . . . . . . . 17-39
Run Circuit on AWS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-42

Ground-State Protein Folding Using Variational Quantum Eigensolver


(VQE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-45
Model Protein Fold with Qubits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-45
Write Function to Calculate Energy of Folds . . . . . . . . . . . . . . . . . . . . . 17-46
Compute Minimum Energy for All Folds . . . . . . . . . . . . . . . . . . . . . . . . 17-48
Write CVaR-VQE Objective Function . . . . . . . . . . . . . . . . . . . . . . . . . . 17-48
Create Circuit Ansatz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-49
Simulate Iterations of CVaR-VQE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-50
Run Final Iteration on QPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-54

xv
Other documents randomly have
different content
CHAPTER X
WILLIAM WALLACE
THE next thing that Billy knew he was waking up, not wide awake,
but a little at a time.
The room seemed very white, and there was somebody in white
standing by his bed. No, it wasn’t Miss King, for this woman had
something white on her head.
Then he felt somebody holding his hand and saying, “Billy, little
Billy.”
He woke up a little further. He tried to say, “Aunt Mary,” but the
words wouldn’t come.
The woman in white took hold of Aunt Mary, and led her out of the
room.
Then he saw something large in the window. He wasn’t at all sure
that he wasn’t dreaming about mountains. But this mountain had a
round top and, while he watched it, it moved. Billy woke up enough
to see that it was somebody standing in the window.
Billy knew only one person who could fill up a window like that. He
tried his voice again. This time he made it go.
“That you, Mr. Prescott?” he said, his voice going up and up till it
ended in a funny little quaver.
Then the mountain came over to him. It was Mr. Prescott.
Billy, looking up, spoke again, very slowly:
“The dimes did roll into the river, Mr. Prescott.”
“Hang it!” said Mr. Prescott. “Of course they did!”
The nurse nodded. “He’s kept talking about that,” she said. “We
thought perhaps you’d know.”
Mr. Prescott started to go close to the bed.
The nurse put out her hand.
“Hang it!” said Mr. Prescott. “I was a brute. Can you ever forgive
me, Billy?”
“Sure, sir,” answered Billy.
His voice sounded so strong that the nurse told Mr. Prescott that
she was afraid he was exciting the patient.
Billy said, “Please stay.”
Then the nurse told Mr. Prescott that he might stay ten minutes if
he wouldn’t talk to the patient.
Billy tried to smile at Mr. Prescott, but he was so tired that he shut
his eyes instead.
Next time it was Uncle John who was holding his hand, but Uncle
John didn’t smile.
“Uncle John,” said Billy, “what’s the matter with me?”
“Just a few broken bones, Billy, my lad,” answered Uncle John.
“Which ones?” asked Billy.
“Just a left arm and a left leg.”
“That all?” asked Billy.
After that they wouldn’t let him see anybody. There were two
nurses instead of one, and three doctors—“specialists” Billy heard
his own nurse say.
After that there were two doctors every day: a doctor with white
hair, and a doctor with light brown hair, parted in the middle.
The doctor with the white hair seemed to think more about Billy
than he did about his bones, for he talked to Billy while he was
feeling around.
The young doctor seemed to think more about the bones. But Billy
liked him, too, for one day when they were hurting him terribly the
young doctor said:
“You’re a game sort of chap.”
Billy wasn’t quite sure what “game” meant, but he kept right on
gritting his teeth till they were through.
The first day that the young doctor began to come alone, he said:
“Nurse, how are the contusions getting along?”
“They are much lighter in color, doctor, this morning,” answered
the nurse.
“I don’t understand,” said the doctor, standing very straight and
putting his forefinger on his chin, “how a fall of the nature of this one,
practically on the left side, could have produced so many contusions
on the right.”
“What are contusions?” asked Billy.
The doctor began to talk about stasis of the circulation following
superficial injuries.
“Show me one,” said Billy.
When the nurse showed him one on his right arm, just below the
shoulder, Billy said:
“Oh, one of my black and blue spots! That must have been when I
was playing caged lion.”
That time the doctor and the nurse were the ones who didn’t
understand.
Then Billy laughed, a happy boyish laugh. He hadn’t laughed that
way since he and his father used to have frolics together.
The doctor looked at him a minute, then he said:
“Nurse, to-morrow this young chap may have company for half an
hour.”
“I’m glad to hear that, doctor,” said the nurse. “I’ll go right away to
tell Mr. Prescott. He’s fairly worn me out with telephoning to know
when we would let him come.”
At ten o’clock the next morning Mr. Prescott came.
After he had answered Billy’s questions about the fire, and had
told him that the new roof was almost finished, he took a newspaper
out of his pocket.
He folded it across, then down on both sides, and held it up in
front of Billy.
There it was, in big head-lines:
“Billy Bradford Saves Prescott Mill”
Then Mr. Prescott read him what the paper said. They had even
put in about finding him with the flowers in his buttonhole.
“Those,” interrupted Billy, “were Miss King’s flowers.”
“Yes,” said Mr. Prescott; “she cried, right in the office, when she
read that.”
Then Billy told Mr. Prescott about the closet, and all about the box,
and asked him to pull out the drawer in the little stand by his bed.
There lay his jack-knife. Somebody had shut up all that was left of
the blades, and there was so little left that they couldn’t be opened.
Mr. Prescott put the knife into Billy’s hand.
“That was a good knife,” said Billy, looking at it with affection.
“I think,” said Mr. Prescott, “that you really ought to let me have
that knife.”
Billy hesitated a moment, then he said:
“If you please, Mr. Prescott, I should like to keep that knife. It has
been a good friend to me.”
Mr. Prescott took the little white hand, knife and all, in his own
strong, firm fingers.
“I want it, Billy, because you have been a good friend to me.”
Billy’s face flushed so suddenly red that Mr. Prescott was afraid
that something was going to happen to Billy. He called, “Nurse!”
“I’m all right,” said Billy.
He grew red again as he said:
“Mr. Prescott, I want to tell you something.”
Mr. Prescott said: “Let me fix your pillows first.”
Of course he got them all mixed up, and the nurse had to come.
She looked at her watch, and then at Mr. Prescott, but she didn’t say
anything.
Then Mr. Prescott sat close by the bed with Billy’s hand lying in
his, and Billy told him about William Wallace.
Mr. Prescott looked a little surprised, then he said:
“William Wallace seems to know a good deal, doesn’t he?”
Billy, in honor, had to nod his head, but he grew very sober.
Perhaps, after all, Mr. Prescott would like William Wallace better than
he liked him.
“I don’t really approve,” said Mr. Prescott, “of his calling you a
coward, though that sometimes makes a boy try to be brave.
“One thing is sure, he can’t ever call you that again, can he?”
Billy shook his head.
“Personally,” continued Mr. Prescott, almost as if he were talking
business, “I had rather be saved by you than by William Wallace.
Can you guess why?”
Billy shook his head again, but this time he smiled.
“Because,” said Mr. Prescott, “you did it out of your heart. William
Wallace would have done it out of his head.”
Billy smiled serenely. Everything—broken jack-knife, broken arm,
broken leg—was exactly all right now.
“Really and truly,” Mr. Prescott went on, “there are two of
everybody, only most people don’t seem to know it: one is his heart,
and the other is his head.
“If I were you, I would be on good terms with William Wallace—it
generally takes both to decide. I’d take him as a sort of brother, but I
wouldn’t let him rule.”
“No,” said Billy.
Then Mr. Prescott saw the nurse coming, and he hurried off.
The next time that Uncle John came Billy asked him what had
become of the man—“the poor man,” Billy called him.
“That man,” said Uncle John, his mouth growing rather firm, “was
found out in his sin.
“He undertook a little too much when he set fire to one end of the
mill, and then tried to blow up the main office. That’s too much for
one man to do at one time, especially when he’s a man that leaves
things around.”
“Oh!” said Billy.
“Now,” said Uncle John, “he’s where he’s having his actions
regulated.”
“I hope,” said Billy, “that they’ll be good to him.”
“Billy,” said Uncle John, very decidedly, “all that you are called
upon to do about that man is to believe that he couldn’t think straight.
“But the way this world is made makes it necessary, when a man
can’t think straighter than to try to destroy the very mill where he’s
working, for some one else to do a part of his thinking for him.
“That’s what the men that make the laws are trying to do. They are
trying to help men to think straight.”
Billy was listening hard. It was a good while since he had heard
one of Uncle John’s lectures.
“You know, Billy, my lad, that there are a lot of things we have to
leave to God.”
“Yes, Uncle John.”
“There are a lot more that we have to leave to the law.
“The best thing for a boy like you and a man like me to do is to
leave things where they belong.”
“All right, Uncle John, I will,” said Billy, giving a little sigh of relief
as if he were glad to have that off his mind.
The next day when Mr. Prescott came, he told Billy that, the day
after that, he was to be moved to Mr. Prescott’s house on the hill.
Billy looked a little sober. He had been thinking a great deal about
home.
“I’m all alone in that big house,” said Mr. Prescott.
“Then,” said Billy, “I’ll come.”
CHAPTER XI
THE TREASURE ROOM
THEY took Billy to Mr. Prescott’s house in his machine. They had to
take a good many pillows and they planned to take an extra nurse,
but the young doctor said that he was going up that way, and could
just as well help.
Billy and the doctor were getting to be very good friends.
“He’s different,” Billy had confided to Uncle John, “but I like him a
lot.”
“Nice people often are different,” said Uncle John.
Billy was so much better that he had some fun, while they were
putting him into the auto, about his “stiff half,” as he called his left
side.
“You just wait till I get that arm and that leg to working,” he said.
“They’ll have to work over time.”
They put him in a large room with broad windows, where he could
look down on the river and across at the mountains. There was a
large brass bed in the room, but Mr. Prescott had had a hospital bed
sent up.
“You’d have hard work to find me in that bed,” said Billy to the
nurse, “wouldn’t you?”
It was a beautiful room. One of the maids told Billy that it had been
Mr. Prescott’s mother’s room, and that he had always kept it as she
had left it.
For the first week Billy feasted his eyes on color.
The walls of the room were soft brown; the paint was the color of
cream. There were two sets of curtains: one a soft old blue, and over
that another hanging of all sorts of colors. It took Billy a whole day to
pick out the pattern on those curtains.
There was a mahogany dressing table, and there was a wonderful
rug—soft shades of rose in the middle, and ever so many shades of
blue in the border.
There was a fireplace with a shining brass fender. And there were
—oh, so many things!
Then Billy spent almost another week on the pictures. But when
he wanted to rest his eyes he looked at his old friends, the
mountains, lying far across the river.
Mr. Prescott, too, liked the mountains. He came to sit by him in the
evening, and they had real friendly times together watching the
mountains fade away into the night, and seeing the electric lights
flash out, one after another, all along the river.
Finally the doctors took off the splints. They had a great time doing
it, testing his joints to see whether or not they would work.
Then Billy found that, as the young doctor said, there had been a
“tall lot of worrying done about those bones.”
This time the white-haired doctor paid more attention to his bones
than he did to Billy. He didn’t say anything till he went to put his
glasses back in the case. Then he straightened up, and said:
“I’m happy to tell you, young man, that those joints will work all
right after they get used to working again.”
The next day Billy went down the long flight of stairs, with Mr.
Prescott on one side, and the nurse on the other, to the great library,
right under the room where he had been.
“Feel pretty well, now that you’re down?” asked Mr. Prescott, after
the nurse had gone up-stairs.
“Sure, sir,” answered Billy.
“Then follow me,” said Mr. Prescott, opening a door at the end of
the library.
Billy followed, but he had hardly stepped in before he stepped
back.
“Why, Billy,” said Mr. Prescott, coming quickly back to him, “I didn’t
mean to frighten you. We’ll stay in the library.”
Now the doctor had told Mr. Prescott that Billy mustn’t be
frightened by anything if they could help it, for he’d been through
about all a boy’s nerves could stand. So Mr. Prescott drew Billy over
to the big sofa, fixed some pillows around him, and put a foot-rest
under his leg.
Then Mr. Prescott settled himself in a great chair as though he had
nothing in the world to do except to talk to Billy.
“That,” said Mr. Prescott, “is my treasure room. When I go in there,
I think of brave men, and of how they helped the world along. What
made you step back?”
“Because,” answered Billy, half ashamed, “I thought I saw a man
in the corner pointing something at me.”
“I ought,” said Mr. Prescott, “to have thought of that before I took
you into the room.
“I’ve been trying, for some time, to make that old suit of armor and
that spear look like a knight standing there, ready for action. I must
have, at last, succeeded, but I’m sorry that it startled you.
“You see I’m naturally interested in weapons of war because they
are all made of steel or iron.”
“Battle-ships, too,” said Billy.
“Yes,” said Mr. Prescott. “But you mustn’t forget the great naval
battles that were won with ships of wood.
“There’s one thing in that room,” Mr. Prescott went on, “that I am
sure you will like to see. It is my great-great-grandfather’s musket.”
“Oh,” said Billy, “I didn’t know that you had a great-great-
grandfather.”
“I did,” said Mr. Prescott, just as quietly as if Billy had been talking
sense. “He was a brave man, too. That is the musket that he had
when he was with General Washington at Valley Forge.”
“Oh!” exclaimed Billy again.
“Know about Valley Forge, do you?”
“A little,” answered Billy, very humbly.
“That’s enough to start on,” said Mr. Prescott.
Billy could almost imagine that Uncle John was talking. Billy spent
a great deal more time every day than anybody realized in thinking
about his Uncle John.
“Perhaps you don’t know, many people don’t,” said Mr. Prescott,
“that the first name of that place was Valley Creek. It was changed to
Valley Forge because a large forge plant was established there. It
was one of the first places in this state where they made iron and
steel.
“By the way, George Washington’s father was a maker of pig iron
down in Virginia.”
“Oh!” said Billy. “There seem to be a lot of things to know about
iron.”
“There’s really no end to them,” said Mr. Prescott. “They begin way
back in history. Did you ever read about Goliath the giant?”
“My father used to read those stories to me,” answered Billy, “out
of a great big Bible.”
“Was it like this one?” asked Mr. Prescott, getting up quickly and
bringing him, from the library table, a great Bible, covered with light
brown leather.
“That looks almost like ours,” answered Billy.
“This,” said Mr. Prescott, “is the one my mother used to read to
me. There’s a great deal about iron in it,” he added, as he put it away
carefully.
“To come back to Goliath,” said Mr. Prescott. “His spear had a
head of iron that weighed six hundred shekels.
“Then there was that iron bedstead of Og, king of Bashan. Ever
hear of him?”
“I don’t seem,” answered Billy, “to remember about him.”
“Perhaps I shouldn’t have remembered,” said Mr. Prescott, “if I
hadn’t been so interested in iron.”
“That,” said Billy, “was probably on account of your grandfather,
and your father,” he added quickly.
“There’s a great deal about iron in the Bible,” said Mr. Prescott.
“Only four or five pages over in Genesis there is a verse about a
man named Tubal-Cain, who was a master-worker in brass and iron.
“Then there are some things in mythology that you ought to know,
now that you’re interested in iron. One of them is that the old
Romans, who imagined all sorts of gods, said that iron was
discovered by Vulcan. They said, too, that he forged the thunderbolts
of Jupiter.
“Now, then, Billy, how about my treasure room?”
“Ready, sir,” answered Billy, working himself out from among his
pillows.
“Once,” said Mr. Prescott, walking close by Billy, “I went into a
room something like this, only it had many more things in it. The
room was in Sir Walter Scott’s house. He had one of Napoleon’s
pistols from Waterloo.
“He called his room an armory. I generally call mine my ‘treasure
room.’”
“I think I like armory better,” said Billy.
“Then,” said Mr. Prescott, “will you walk into my armory?”
“First of all,” said Billy, “I want to see that gun—musket.”
“Here it is,” said Mr. Prescott. “There,” he added, pointing to a
picture in an oval brass frame, “is my great-great-grandfather.”
“Oh!” said Billy.
Then Mr. Prescott knew that Billy had never before seen a
silhouette.
“That kind of picture,” he said, “does make a man look as black as
his own hat, though it is often a good profile. I used to make them
myself. Some night I’ll make one of you.
“Now that you’ve seen the musket, I think that you had better take
a look at this suit of armor that I have been trying to make stand up
here like a knight.
“This coat of mail is made of links, you see. Sometimes they were
made of scales of iron linked together.
“The work that those old smiths did is really wonderful, especially
when you remember that their only tools were hammer, pincers,
chisel, and tongs. It took both time and patience to weld every one of
those links together.”
“I don’t think I understand what weld means,” said Billy.
“When iron is heated to a white heat,” said Mr. Prescott, “it can be
hammered together into one piece. Most metals have to be soldered,
you know. The blacksmiths generally use a powder that will make
the iron weld more easily, because it makes the iron soften more
quickly, but iron is its own solder.
“You’d better sit down here while I explain a little about this suit of
armor; then you’ll know what you’re reading about when you come to
a knight.
“I suppose that every boy knows what a helmet and a vizor are;
they learn about that from seeing firemen.”
“And policemen,” said Billy.
“Only the helmets of the knights covered their faces and ended in
guards for their necks. I dare say that you don’t know what a gorget
is.”
“No,” said Billy, “I don’t.”
“That is the piece of armor that protected the throat. Here is the
cuirass or breast-plate, and the tassets that covered the thighs.
They’re hooked to the cuirass. And here are the greaves for the
shins. There are names for all the arm pieces, too, but we’ll let those
go just now.
“This shield, you see, is wood covered with iron, and part of the
handle inside is wood. A man must have weighed a great deal when
he had a full suit of armor on, and he must have been splendid to
look at and rather hard to kill.
“Those old smiths certainly made a fine art of their work in iron.
They got plenty of credit for it, too. In the Anglo-Saxon times they
were really treated as officers of rank.
“When a man was depending on his sword to protect his family, he
naturally respected a man who could make good swords. The smiths
sort of held society together.”
Billy, looking around the room, saw that one side had spears and
shields and helmets hung all over it; and on the wall at the end were
pistols, bows and arrows, and some dreadful knives.
“Did all those,” he asked, pointing at the end of the room, “kill
somebody?”
“Ask it the other way,” said Mr. Prescott; “did they all protect
somebody? Then I can safely say that they did, for any foe would
think twice before he attacked a man in mail. These things were all
made because they were needed.”
“What do you suppose put the armorers out of business?”
“I don’t know,” answered Billy.
“Gunpowder,” said Mr. Prescott. “A man could be blown up, armor
and all.”
“Then they had to make guns,” said Billy.
“And they’ve been at that ever since,” said Mr. Prescott.
“Come over to this cabinet, and I’ll show you my special treasure.
“Shut your eyes, Billy, and think of walls in a desert long enough
and high enough to shut in a whole city.”
Billy shut his eyes. “I see the walls,” he said.
“Now, just inside the wall, think a garden with great beds of roses.”
“Blush roses?” queried Billy.
“Damask,” replied Mr. Prescott; “pink, pretty good size.”
“That’s done!” said Billy.
“Now, in that garden, think an Arab chief, a sheik, mounted on a
beautiful Arabian horse, and—open your eyes!”
“Here is his sword!”
“I saw him clearly!” exclaimed Billy, his eyes flying wide open.
“HERE IS HIS SWORD”

“My!” he said, “but that’s a beauty!”


“It is,” said Mr. Prescott. “Look!”
Then he took the hilt in his right hand and the point in his left, and
began to bend the point toward the hilt.
“Don’t,” cried Billy. “You’ll break it!”
“The tip and the hilt of the best of the old swords were supposed to
come together,” said Mr. Prescott.
“See, this has an inscription in Arabic.”
“I have a genuine Toledo, too, but you’ve been in here long
enough. Let’s go back into the library. You may come in here
whenever you like. Mornings, I think, would be the best time.”
When Billy was comfortably settled among his pillows, with the
Damascus sword on the sofa by him, Mr. Prescott said:
“Men, in the olden time, thought so much of their swords that they
often named them, and had them baptized by the priest. The great
emperor Charlemagne had a sword named ‘Joyeuse.’
“Sometimes, too, the old bards sang about swords and their
makers.”
“Tell me,” said Billy, “how they made swords.”
“The people way over in the East understood the process of
converting iron into steel, but in those days they had plenty of gold
and very little steel, so swords were sometimes made of gold with
only an edge of steel.
“The steel swords were made by hammering little piles of steel
plates together. They were heated, hammered, and doubled over,
end to end, until the layers of steel in a single sword ran up into the
millions.
“Now, we’ll come back to the present time, and I’ll show you
something that I brought home yesterday to put in my treasure
room.”
Billy watched eagerly, while Mr. Prescott took a package from the
library table, and opened it.
Then, in delight, he exclaimed:
“The great iron key!”
“The same,” said Mr. Prescott, “and glad enough I am to have it
here.
“When I gave Tom the new key, he didn’t look altogether happy. I
think the fellow really has enjoyed having the care of this one.”
“I suppose,” said Billy, “that the new one is so small that he will be
afraid of losing it. They don’t make such large keys nowadays.”
“That statement may be true in general,” said Mr. Prescott, “but the
fact is that the new key is as large as this.”
Then Mr. Prescott stopped talking, but he looked right at Billy.
“You don’t mean,” said Billy, after thinking for a minute as hard as
he could, “that you have had a key made, do you?”
“That is the meaning that I intended to convey,” answered Mr.
Prescott. “But I’m not going to tease a fellow that is down-stairs for
the first time, so I’ll tell you, right away, that Mr. John Bradford made
the casting for the new key, and he used this for a pattern.”
“Oh!” said Billy, smiling.
“You didn’t like it very well, did you, Billy,” asked Mr. Prescott,
“when I put that key back in the door?”
“No,” answered Billy, “I didn’t.”
“Just at that time,” said Mr. Prescott, “a great many things had to
be considered. I decided that it was better to risk the key than to risk
letting the man know that we knew what had happened.
“You never knew either, did you, how many nights after that I spent
in the office?”
“Honest?” asked Billy, opening his eyes very wide.
“Running a mill, I’d have you understand, Billy Bradford,” said Mr.
Prescott, “is no easy job.”
“It doesn’t seem to be,” said Billy, just as earnestly as if he had
been a man.
“I must go,” said Mr. Prescott. “I had almost forgotten that I am one
of the modern workers in iron.

You might also like