Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

MATLAB Recipes: a problem-solution

approach 2nd Edition Michael Paluszek


Visit to download the full and correct content document:
https://textbookfull.com/product/matlab-recipes-a-problem-solution-approach-2nd-editi
on-michael-paluszek/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

MATLAB Machine Learning Recipes: A Problem-Solution


Approach 2nd Edition Michael Paluszek

https://textbookfull.com/product/matlab-machine-learning-recipes-
a-problem-solution-approach-2nd-edition-michael-paluszek/

MATLAB Machine Learning Recipes: A Problem-Solution


Approach 3rd Edition Michael Paluszek

https://textbookfull.com/product/matlab-machine-learning-recipes-
a-problem-solution-approach-3rd-edition-michael-paluszek/

Android Recipes A Problem-Solution Approach Dave Smith

https://textbookfull.com/product/android-recipes-a-problem-
solution-approach-dave-smith/

Raku Recipes: A Problem-Solution Approach J.J. Merelo

https://textbookfull.com/product/raku-recipes-a-problem-solution-
approach-j-j-merelo/
C Recipes - A Problem-Solution Approach 1st Edition
Shirish Chavan

https://textbookfull.com/product/c-recipes-a-problem-solution-
approach-1st-edition-shirish-chavan/

JavaScript Recipes: A Problem-Solution Approach 1st


Edition Russ Ferguson

https://textbookfull.com/product/javascript-recipes-a-problem-
solution-approach-1st-edition-russ-ferguson/

wxPython Recipes: A Problem - Solution Approach 1st


Edition Mike Driscoll

https://textbookfull.com/product/wxpython-recipes-a-problem-
solution-approach-1st-edition-mike-driscoll/

Java 9 Recipes: A Problem-Solution Approach 3rd Edition


Josh Juneau

https://textbookfull.com/product/java-9-recipes-a-problem-
solution-approach-3rd-edition-josh-juneau/

Spring 5 Recipes: A Problem-Solution Approach 4th


Edition Marten Deinum

https://textbookfull.com/product/spring-5-recipes-a-problem-
solution-approach-4th-edition-marten-deinum/
MATLAB
Recipes
A Problem-Solution Approach

Second Edition

Michael Paluszek
Stephanie Thomas
MATLAB Recipes
A Problem-Solution Approach

Second Edition

Michael Paluszek
Stephanie Thomas
MATLAB Recipes: A Problem-Solution Approach

Michael Paluszek Stephanie Thomas


Princeton, NJ Princeton Junction, NJ
USA USA

ISBN-13 (pbk): 978-1-4842-6123-1 ISBN-13 (electronic): 978-1-4842-6124-8


https://doi.org/10.1007/978-1-4842-6124-8

Copyright © 2020 by Michael Paluszek and Stephanie Thomas

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is
concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on
microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, com-
puter software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occur-
rence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the
benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as
such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the
authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made.
The publisher makes no warranty, express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Coordinating Editor: MarkPowers

Cover designed by eStudioCalamar


Cover image by Christopher Burns on Unsplash (www.unsplash.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit
www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science +
Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio
rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also
available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.
com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via
the book’s product page, located at www.apress.com/9781484261231. For more detailed information, please visit http://
www.apress.com/source-code.
Contents

About the Authors XV

Acknowledgements XVII

Introduction XIX

I Coding in MATLAB 1

1 Coding Handbook 3
MATLAB Language Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Brief Introduction to MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . 3
Everything Is a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Strings Are Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Use Strict Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Cell Arrays Hold Anything and Everything . . . . . . . . . . . . . . . . . . 11
Optimize Your Code with Logical Arrays . . . . . . . . . . . . . . . . . . . 12
Use Persistent and Global Scope to Minimize Data Passing . . . . . . . . . . 12
Understanding Unique MATLAB Operators and Keywords . . . . . . . . . . 14
Harnessing the Power of Multiple Inputs and Outputs . . . . . . . . . . . . . 16
Use Function Handles for Efficiency . . . . . . . . . . . . . . . . . . . . . . 17
Numerics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Datastore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Tall Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Tables and Categoricals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Large MAT-files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Advanced Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.1 Initializing a Data Structure Using Parameters . . . . . . . . . . . . . . . . . 25
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

III
CONTENTS

1.2 Performing mapreduce on an Image Datastore . . . . . . . . . . . . . . . . . 29


Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.3 Creating a Table from a File . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.4 Processing Table Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.5 String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.6 Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.7 Non-English Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
1.8 Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
1.9 Using JSON-Formatted Strings . . . . . . . . . . . . . . . . . . . . . . . . . 39
1.10 Creating Function Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
1.11 Locating Directories for Data Storage . . . . . . . . . . . . . . . . . . . . . 42
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
1.12 Loading Binary Data from a File . . . . . . . . . . . . . . . . . . . . . . . . 43
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

IV
CONTENTS

1.13 Command-Line File Interaction . . . . . . . . . . . . . . . . . . . . . . . . 45


Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
1.14 Using a MEX File to Link to an External Library . . . . . . . . . . . . . . . 47
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
1.15 Protect Your IP with Parsed Files . . . . . . . . . . . . . . . . . . . . . . . . 50
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
1.16 Writing to a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
1.17 Using an Explicit Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
1.18 Using a Script Subfunction . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
1.19 Using Memoize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
1.20 Using Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
1.21 Creating Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
1.22 MATLAB Online . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

V
CONTENTS

2 MATLAB Style 69
2.1 Developing Your Own MATLAB Style Guidelines . . . . . . . . . . . . . . 69
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
2.2 Writing Good Function Help . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
2.3 Overloading Functions and Utilizing varargin . . . . . . . . . . . . . . . 77
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
2.4 Adding Built-in Inputs and Outputs to Functions . . . . . . . . . . . . . . . 78
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
2.5 Adding Argument Checking to Functions . . . . . . . . . . . . . . . . . . . 80
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
2.6 Adding Dot Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
2.7 Smart Structuring of Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
2.8 Implementing MATLAB Command-Line Help for Folders . . . . . . . . . . 85
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
2.9 Publishing Code into Technical Reports . . . . . . . . . . . . . . . . . . . . 88
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
2.10 Integrating Toolbox Documentation into the MATLAB Help System . . . . . 93
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

VI
CONTENTS

2.11 Structuring a Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98


Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

3 Visualization 101
3.1 Plotting Data Interactively from the MATLAB Desktop . . . . . . . . . . . . 102
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.2 Incrementally Annotate a Plot . . . . . . . . . . . . . . . . . . . . . . . . . 107
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
3.3 Create a Custom Plot Page with Subplot . . . . . . . . . . . . . . . . . . . . 109
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
3.4 Create a Heat Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
3.5 Create a Plot Page with Custom-Sized Axes . . . . . . . . . . . . . . . . . . 113
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
3.6 Plotting with Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
3.7 Generating a Color Distribution . . . . . . . . . . . . . . . . . . . . . . . . 118
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
3.8 Visualizing Data over 2D or 3D Grids . . . . . . . . . . . . . . . . . . . . . 120
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
3.9 Generate 3D Objects Using Patch . . . . . . . . . . . . . . . . . . . . . . . 123
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

VII
CONTENTS

3.10 Working with Light Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 125


Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
3.11 Programmatically Setting the Camera Properties . . . . . . . . . . . . . . . . 129
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
3.12 Display an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
3.13 Graph and Digraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
3.14 Adding a Watermark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

4 Interactive Graphics 143


4.1 Creating a Simple Animation . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
4.2 Playing Back an Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
4.3 Animate Line Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
4.4 Implementation of a uicontrol Button . . . . . . . . . . . . . . . . . . . 152
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
4.5 Display Status of a Running Simulation or Loop . . . . . . . . . . . . . . . . 155
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

VIII
CONTENTS

4.6 Create a Custom GUI with App Designer . . . . . . . . . . . . . . . . . . . 158


Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
4.7 Build a Data Acquisition GUI . . . . . . . . . . . . . . . . . . . . . . . . . 165
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

5 Testing and Debugging 175


5.1 Creating a Unit Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
5.2 Running a Test Suite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
5.3 Setting Verbosity Levels in Tests . . . . . . . . . . . . . . . . . . . . . . . . 186
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
5.4 Create a Logging Function to Display Data . . . . . . . . . . . . . . . . . . 189
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
5.5 Generating and Tracing MATLAB Errors and Warnings . . . . . . . . . . . . 191
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
5.6 Testing Custom Errors and Warnings . . . . . . . . . . . . . . . . . . . . . . 192
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
5.7 Testing Generation of Figures . . . . . . . . . . . . . . . . . . . . . . . . . 196
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

IX
CONTENTS

6 Classes 199
6.1 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . 199
6.2 State Space Systems Base Class . . . . . . . . . . . . . . . . . . . . . . . . 201
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
6.3 State Space Systems Discrete Class . . . . . . . . . . . . . . . . . . . . . . 206
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
6.4 Using the State Space Class . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
6.5 Using a Mocking Framework . . . . . . . . . . . . . . . . . . . . . . . . . 209
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

II Applications 213

7 The Double Integrator 215


7.1 Writing the Equations for the Double Integrator Model . . . . . . . . . . . . 215
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
7.2 Creating a Fixed-Step Numerical Integrator . . . . . . . . . . . . . . . . . . 216
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
7.3 Implement a Discrete Proportional-Derivative
Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
7.4 Double Integrator Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
7.5 Create Time Axes with Reasonable Time Units . . . . . . . . . . . . . . . . 229
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

X
CONTENTS

Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
7.6 Create Figures with Multiple Subplots . . . . . . . . . . . . . . . . . . . . . 230
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

8 Robotics 235
8.1 Creating a Dynamical Model of the SCARA Robot . . . . . . . . . . . . . . 235
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
8.2 Customize a Visualization Function for the Robot . . . . . . . . . . . . . . . 238
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
8.3 Using Numerical Search for Robot Inverse Kinematics . . . . . . . . . . . . 243
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
8.4 Developing a Control System for the Robot . . . . . . . . . . . . . . . . . . 246
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
8.5 Simulating the Controlled Robot . . . . . . . . . . . . . . . . . . . . . . . . 248
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

9 Electric Motors 253


9.1 Motor Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
9.2 Controlling the Motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
9.3 Pulsewidth Modulation of the Switches . . . . . . . . . . . . . . . . . . . . 262
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

XI
CONTENTS

Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
9.4 Simulating the Controlled Motor . . . . . . . . . . . . . . . . . . . . . . . . 270
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

10 Fault Detection 277


10.1 Modeling an Air Turbine . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
10.2 Building a Detection Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
10.3 Simulating the Fault Detection System . . . . . . . . . . . . . . . . . . . . . 284
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
10.4 Building a GUI for the Detection Filter Simulation . . . . . . . . . . . . . . 287
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

11 Chemical Processes 299


11.1 Modeling the Chemical Mixing Process . . . . . . . . . . . . . . . . . . . . 299
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
11.2 Sensing the pH of the Chemical Process . . . . . . . . . . . . . . . . . . . . 303
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
11.3 Controlling the Effluent pH . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
11.4 Simulating the Controlled pH Process . . . . . . . . . . . . . . . . . . . . . 312
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312

XII
CONTENTS

12 Aircraft 327
12.1 Creating a Dynamical Model of an Aircraft . . . . . . . . . . . . . . . . . . 327
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
12.2 Equilibrium Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
12.3 Designing a Control System for an Aircraft . . . . . . . . . . . . . . . . . . 340
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
12.4 Plotting a 3D Trajectory for an Aircraft . . . . . . . . . . . . . . . . . . . . 342
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
12.5 Simulating the Controlled Aircraft . . . . . . . . . . . . . . . . . . . . . . . 346
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
12.6 Draw an Aircraft . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

13 Spacecraft Attitude Control 359


13.1 Creating a Dynamical Model of the Spacecraft . . . . . . . . . . . . . . . . 359
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
13.2 Computing Angle Errors from Quaternions . . . . . . . . . . . . . . . . . . 364
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
13.3 Simulating the Controlled Spacecraft . . . . . . . . . . . . . . . . . . . . . . 366
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366

XIII
CONTENTS

13.4 Performing Batch Runs of a Simulation . . . . . . . . . . . . . . . . . . . . 373


Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

14 Automobiles 383
14.1 Automobile Dynamics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
14.2 Modeling the Automobile Radar . . . . . . . . . . . . . . . . . . . . . . . . 389
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
14.3 Automobile Autonomous Passing Control . . . . . . . . . . . . . . . . . . . 393
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
14.4 Automobile Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
14.5 Modeling an Automobile Suspension . . . . . . . . . . . . . . . . . . . . . 400
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400

Bibliography 407

Index 409

XIV
About the Authors

Michael Paluszek is President of Princeton Satellite Systems,


Inc. (PSS) in Plainsboro, New Jersey. Mr. Paluszek founded
PSS in 1992 to provide aerospace consulting services. He used
MATLAB to develop the control system and simulations for the
IndoStar-1 geosynchronous communications satellite. This led
to the launch of Princeton Satellite Systems’ first commercial
MATLAB toolbox, the Spacecraft Control Toolbox, in 1995.
Since then, he has developed toolboxes and software packages
for aircraft, submarines, robotics, and nuclear fusion propulsion,
resulting in Princeton Satellite Systems’ current extensive prod-
uct line. He is working with the Princeton Plasma Physics Laboratory on a compact nuclear
fusion reactor for energy generation and space propulsion.
Prior to founding PSS, Mr. Paluszek was an engineer at GE Astro Space in East Windsor,
NJ. At GE, he designed the Global Geospace Sciences Polar despun platform control system
and led the design of the GPS IIR attitude control system, the Inmarsat-3 attitude control sys-
tems, and the Mars Observer delta-V control system, leveraging MATLAB for control design.
Mr. Paluszek also worked on the attitude determination system for the DMSP meteorological
satellites. He flew communication satellites on over 12 satellite launches, including the GSTAR
III recovery, the first transfer of a satellite to an operational orbit using electric thrusters. At
Draper Laboratory, Mr. Paluszek worked on the Space Shuttle, Space Station, and submarine
navigation. His Space Station work included designing of Control Moment Gyro-based control
systems for attitude control.
Mr. Paluszek received his bachelor’s degree in Electrical Engineering and master’s and
Engineer’s degrees in Aeronautics and Astronautics from the Massachusetts Institute of Tech-
nology. He is author of numerous papers and has over a dozen US patents. Mr. Paluszek is
coauthor of MATLAB Recipes, MATLAB Machine Learning, MATLAB Machine Learning
Recipes: A Problem-Solution Approach, and Practical MATLAB Deep Learning, all published
by Apress.

XV
ABOUT THE AUTHORS

Stephanie Thomas is Vice President of Princeton Satellite Sys-


tems, Inc. in Plainsboro, New Jersey. She received her bache-
lor’s and master’s degrees in Aeronautics and Astronautics from
the Massachusetts Institute of Technology in 1999 and 2001.
Ms. Thomas was introduced to the PSS Spacecraft Control
Toolbox for MATLAB during a summer internship in 1996 and
has been using MATLAB for aerospace analysis ever since. In
her nearly 20 years of MATLAB experience, she has devel-
oped many software tools including the Solar Sail Module for
the Spacecraft Control Toolbox, a proximity satellite operations
toolbox for the Air Force, collision monitoring Simulink blocks
for the Prisma satellite mission, and launch vehicle analysis tools
in MATLAB and Java. She has developed novel methods for space situation assessment such as
a numeric approach to assessing the general rendezvous problem between any two satellites im-
plemented in both MATLAB and C++. Ms. Thomas has contributed to PSS’ Attitude and Orbit
Control textbook, featuring examples using the Spacecraft Control Toolbox, and written many
software user guides. She has conducted SCT training for engineers from diverse locales such
as Australia, Canada, Brazil, and Thailand and has performed MATLAB consulting for NASA,
the Air Force, and the European Space Agency. Ms. Thomas is coauthor of MATLAB Recipes,
MATLAB Machine Learning, MATLAB Machine Learning Recipes: A Problem-Solution Ap-
proach, and Practical MATLAB Deep Learning, published by Apress. In 2016, Ms. Thomas
was named a NASA NIAC Fellow for the project “Fusion-Enabled Pluto Orbiter and Lander.”

XVI
CHAPTER 1 CODING HANDBOOK

Cell Arrays Hold Anything and Everything


One variable type unique to MATLAB is the cell array. This is really a list container, and you
can store variables of any type in elements of a cell array. Cell arrays can be multidimensional,
just like matrices, and are useful in many contexts.
Cell arrays are indicated by curly braces, {}. They can be of any dimension and contain any
data, including string, structures, and objects. You can initialize them using the cell function,
recursively display the contents using celldisp, and access subsets using parentheses just
like for a matrix. The following is a short example.
>> c = cell(3,1);
>> c{1} = 'some text';
>> c{2} = false;
>> c{3} = [1 2; 3 4];
>> b = c(1:2);
>> celldisp(b)
b{1} =
string

b{2} =
0

Using curly braces for access gives you the element data as the underlying type. When you
access elements of a cell array using parentheses, the contents are returned as another cell array,
rather than the cell contents. MATLAB help has a special section called Comma-Separated
Lists which highlights the use of cell arrays as lists. The code analyzer will also suggest more
efficient ways to use cell arrays, for instance:
Replace
a = {b{:} c};
with
a = [b {c}];
Cell arrays are especially useful for sets of strings, with many of MATLAB’s string search
functions optimized for cell arrays, such as strcmp.
Use iscell to identify cell array variables. Use deal to manipulate structure array and
cell array contents. Table 1.4 shows a selection of key cell array functions.

Table 1.4: Key Functions for Cell Arrays

Function Purpose
cell Initialize a cell array
cellstr Create cell array from a character array
iscell Identify a cell array
iscellstr Identify a cell array containing only strings
celldisp Recursively display the contents of a cell array

11
Another random document with
no related content on Scribd:
15. by the words of the Lord] i.e. the king’s command was
recognised to be in accordance with the Divine will. Read by the
word; the plural words is probably only a textual error.

¹⁶And the priests went in unto the inner part of


the house of the Lord, to cleanse it, and
brought out all the uncleanness that they
found in the temple of the Lord into the court
of the house of the Lord. And the Levites took
it, to carry it out abroad to the brook Kidron.
16. the priests] The work was so divided between priests and
Levites that only the priests went into the house.

unto the inner part of the house] Render, within the house. The
reference is not to the Holy of Holies specially, but to the whole
interior of the house.

uncleanness] compare verse 5; Isaiah xxx. 22.

to the brook Kidron] The brook Kidron is the deep valley on the
east of Jerusalem separating it from the Mount of Olives; 2 Samuel
xv. 23; John xviii. 1. It was treated as an unclean spot, compare xv.
16.

¹⁷Now they began on the first day of the first


month to sanctify, and on the eighth day of the
month came they to the porch of the Lord;
and they sanctified the house of the Lord in
eight days: and on the sixteenth day of the
first month they made an end.
17. to sanctify ... and they sanctified] Two periods of eight days
each were spent in “sanctifying,” the courts apparently requiring
eight days and the house itself eight days.

¹⁸Then they went in to Hezekiah the king


within the palace, and said, We have cleansed
all the house of the Lord, and the altar of
burnt offering, with all the vessels thereof, and
the table of shewbread, with all the vessels
thereof.
18. the table of shewbread] Compare iv. 8 (note), 19; 1
Chronicles xxviii. 16—“the tables of shewbread.”

¹⁹Moreover all the vessels, which king Ahaz in


his reign did cast away when he trespassed,
have we prepared and sanctified; and, behold,
they are before the altar of the Lord.
19. cast away] According to xxviii. 24, “cut in pieces”; compare 2
Kings xvi. 17. The reference is probably to the “bases” and the “sea.”

have we prepared] Render, have we set up. Ahaz had taken


away the supports both of the laver and of the sea (2 Kings xvi. 17).

20‒24 (not in 2 Kings).


The Sevenfold Sacrifice for the Reconciliation of the
People.

The ritual of the sin offering is fully given in Leviticus iv. Ahaz had
broken the covenant, and Hezekiah’s sin offering was intended to
atone for the breach.

²⁰Then Hezekiah the king arose early, and


gathered the princes of the city, and went up
to the house of the Lord. ²¹And they brought
seven bullocks, and seven rams, and seven
lambs, and seven he-goats, for a sin offering
for the kingdom and for the sanctuary and for
Judah. And he commanded the priests the
sons of Aaron to offer them on the altar of the
Lord.
21. they brought] As the sacrifice was not for an individual but for
a whole people the offering on this occasion consisted of seven of
each of four different sacrificial animals, the bullocks, rams, and
lambs being used for the burnt offering (verses 22 and 24), and the
he-goats for the special sin offering (verse 23).

for the kingdom] i.e. for the kingly house.

for the sanctuary] i.e. for the Temple (compare Leviticus xvi. 16),
but probably inclusive of the personnel of the Temple, i.e. the priests
and Levites, since otherwise they would have been passed over in
the great sin offering.

on the altar of the Lord] Not on the altar of Ahaz (2 Kings xvi.
11).

²²So they killed the bullocks, and the priests


received the blood, and sprinkled it on the
altar: and they killed the rams, and sprinkled
the blood upon the altar: they killed also the
lambs, and sprinkled the blood upon the altar.
22. received the blood] In basons with which they dashed (not as
the text “sprinkled”) the blood against the altar. This dashing was
different from the sprinkling with the finger.
²³And they brought near the he-goats for the
sin offering before the king and the
congregation; and they laid their hands upon
them: ²⁴and the priests killed them, and they
made a sin offering with their blood upon the
altar, to make atonement for all Israel: for the
king commanded that the burnt offering and
the sin offering should be made for all Israel.
23. brought near] i.e. to the king and the people.

and they laid their hands] “they” = the representatives of the


people, for whom the sacrifice was to be offered, compare Leviticus
iv. 15.

25‒30.
The Levitical Service of Music.

²⁵And he set the Levites in the house of the


Lord with cymbals, with psalteries, and with
harps, according to the commandment of
David, and of Gad the king’s seer, and Nathan
the prophet: for the commandment was of the
Lord by his prophets.
25. and of Gad ... and Nathan] Neither of these prophets is
elsewhere said to have had a part in inciting David to the
organisation of the Temple music with which the Chronicler credits
him. Their names are introduced in order to emphasise the value of
the musicians of the Temple, whose service is thus declared to have
arisen through the inspiration of prophets; compare 1 Chronicles
xxviii. 19.
²⁶And the Levites stood with the instruments of
David, and the priests with the trumpets.
26. with the instruments] LXX. ἐν ὀργάνοις. Compare 1 Chronicles
xxiii. 5.

²⁷And Hezekiah commanded to offer the burnt


offering upon the altar. And when the burnt
offering began, the song of the Lord began
also, and the trumpets, together with the
instruments of David king of Israel. ²⁸And all
the congregation worshipped, and the singers
sang, and the trumpeters sounded; all this
continued until the burnt offering was finished.
27. and the trumpets] Compare 1 Chronicles xv. 24 (note).

together with the instruments of David] Render perhaps, even


according to the guidance of the instrument of David, i.e. led (or
“accompanied”) by them.

²⁹And when they had made an end of offering,


the king and all that were present with him
bowed themselves and worshipped.
29. bowed themselves and worshipped] i.e. first bowed down (on
their knees) and then completely prostrated themselves.

³⁰Moreover Hezekiah the king and the princes


commanded the Levites to sing praises unto
the Lord with the words of David, and of
Asaph the seer. And they sang praises with
gladness, and they bowed their heads and
worshipped.
30. to sing praises] Since (1) the Hebrew word for “Psalms”
means “Praises,” and (2) the words of David and Asaph are specially
mentioned in this verse, it is clear that the Chronicler by this phrase
means “to sing Psalms.”

31‒36 (not in Kings).


A Great Sacrifice of Burnt Offerings and Thank Offerings.

³¹Then Hezekiah answered and said, Now ye


have consecrated yourselves ¹ unto the Lord,
come near and bring sacrifices and thank
offerings into the house of the Lord. And the
congregation brought in sacrifices and thank
offerings; and as many as were of a willing
heart brought burnt offerings. ³²And the
number of the burnt offerings, which the
congregation brought, was threescore and ten
bullocks, an hundred rams, and two hundred
lambs: all these were for a burnt offering to the
Lord.
¹ Hebrew filled your hand.

31. answered and said] i.e. answered the thoughts or expectation


of the people as expressed by the Sacrifices and the Songs;
compare Job iii. 2 (Revised Version).

ye have consecrated yourselves] Hebrew “filled your hand”;


compare xiii. 9; Exodus xxviii. 41.
sacrifices and thank offerings] The phrase means simply the
special type of sacrifices which were termed “thank offerings.” The
fat of such offerings was burnt on the altar, the breast and right thigh
were reserved for the priests, but the remainder belonged to the
offerer and was used for a joyous meal (Leviticus vii. 12 ff.). The
burnt offering was entirely consumed on the altar, no portion being
kept by priests or offerer (Leviticus i. 1‒13): hence such sacrifices
represented a greater cost and are accordingly said to be given by
those who were “of willing heart,” i.e. conspicuously pious and
generous.

³³And the consecrated things were six


hundred oxen and three thousand sheep.
33. the consecrated things] The term was applied (1) to gold and
other valuables offered in the Temple; compare xv. 18; 1 Chronicles
xviii. 8‒11; (2) to those parts of the various sacrifices which were
assigned to be eaten by the priests; Leviticus xxi. 22 (“the holy
[bread]”), xxii. 2, 3, 15 (“the holy things”). Here the reference is more
general, i.e. to the thank offerings (verse 35) themselves.

³⁴But the priests were too few, so that they


could not flay all the burnt offerings: wherefore
their brethren the Levites did help them, till the
work was ended, and until the priests had
sanctified themselves: for the Levites were
more upright in heart to sanctify themselves
than the priests.
34. the priests] According to Leviticus i. 5 f. the task of flaying the
sacrifice was to be performed by the offerer. Either this passage
marks a later stage in the customary ritual, or perhaps this occasion
was regarded by the Chronicler as exceptional because the offerings
were brought on behalf of the “congregation” in general. The verse
presents other difficult features. Who were the priests who had not
yet sanctified themselves? What is the significance of the evident
contrast between the attitude of the priests and that of the Levites, to
the disadvantage of the former? It would seem that there were
priests who had deliberately or slackly failed to comply with
Hezekiah’s injunction (verses 4, 5) and were therefore still ritually
unclean from the pollution of the previous reign. In general we infer
that the Levites had either been less deeply involved in the idolatries
of Ahaz or at least were more zealous than the priests for the
restoration of the worship of Jehovah alone. Possibly this tradition
may truly represent the historical facts; or it may be an inference
derived from 2 Kings xvi. 16 where the subservience of the high-
priest Urijah to king Ahaz is mentioned (so Kittel). Less probable is
the view of Benzinger that this verse has been added by the
Chronicler to the midrashic source upon which he is here depending,
and that it represents merely the Chronicler’s personal predilection
for the Levites as distinct from the priests.

³⁵And also the burnt offerings were in


abundance, with the fat of the peace offerings,
and with the drink offerings for every burnt
offering. So the service of the house of the
Lord was set in order.
35. with the fat] Compare vii. 7; Leviticus iii. 3, 17.

the peace offerings] i.e. the thank offerings (verse 31).

drink offerings] compare Numbers xv. 5, 7, 10. The offering was


to be of wine, and the quantity used was to correspond with the size
of the animal sacrificed.

was set in order] i.e. was re-established.


³⁶And Hezekiah rejoiced, and all the people,
because of that which God had prepared for
the people: for the thing was done suddenly.
36. that which God had prepared for the people] It was God, not
Hezekiah, who had done it all.

suddenly] In the very first year of Hezekiah’s reign (verse 3).

Chapter XXX.
1‒12 (not in 2 Kings).
Hezekiah Invites all Israel to keep the Passover.

From verse 2 it appears that this Passover took place in the first
year of Hezekiah while the Northern Kingdom was still standing. The
invitation to share in it at Jerusalem which Hezekiah is here (verse 1)
said to have sent to north Israel is opposed to all historic probability.
The Chronicler, however, was little likely to be troubled by that
difficulty, even if he had observed it (see note, verse 5). Furthermore
it is a plausible suggestion that the references to Ephraim,
Manasseh, etc. in verses 1, 10, 11, 18 really reflect conditions of the
Chronicler’s own circumstances, regarding which see the note on xv.
9. It is therefore a mistake to suggest that the date may be wrong
and that the Passover really took place in the sixth year of Hezekiah
after the fall of Samaria on the ground that the invitation would then
be more credible.

¹And Hezekiah sent to all Israel and Judah,


and wrote letters also to Ephraim and
Manasseh, that they should come to the
house of the Lord at Jerusalem, to keep the
passover unto the Lord, the God of Israel.
²For the king had taken counsel, and his
princes, and all the congregation in
Jerusalem, to keep the passover in the
second month.
2. in the second month] The Law allowed such a postponement;
compare Numbers ix. 10, 11.

³For they could not keep it at that time,


because the priests had not sanctified
themselves in sufficient number, neither had
the people gathered themselves together to
Jerusalem. ⁴And the thing was right in the
eyes of the king and of all the congregation.
3. at that time] In the first month.

⁵So they established a decree to make


proclamation throughout all Israel, from Beer-
sheba even to Dan, that they should come to
keep the passover unto the Lord, the God of
Israel, at Jerusalem: for they had not kept it in
great numbers ¹ in such sort as it is written.
¹ Or, of a long time.

5. to make proclamation] A phrase characteristic of the


Chronicler.
from Beer-sheba even to Dan] i.e. the extreme points of the
undivided kingdom of David and Solomon. “The existence of the
North Kingdom is either ignored or more probably the writer
assumed that it had already fallen” (Curtis). On the origin of the
phrase and the order in Chronicles (Beer-sheba to Dan not Dan to
Beer-sheba, as in 2 Samuel xxiv. 2, etc.) see Hogg in the Expositor,
1898, pp. 411‒421.

they had not kept it in great numbers in such sort as it is written]


The statement applies to Israel, not to Judah; for the first time an
attempt is made to draw Israel en masse to a regular Passover at
Jerusalem.

⁶So the posts went with the letters from the


king and his princes throughout all Israel and
Judah, and according to the commandment of
the king, saying, Ye children of Israel, turn
again unto the Lord, the God of Abraham,
Isaac, and Israel, that he may return to the
remnant that are escaped of you out of the
hand of the kings of Assyria.
6. the posts] Literally “the runners.”

the remnant that are escaped of you out of the hand of the kings
of Assyria] The phrase applies most naturally to the final downfall of
Samaria through Shalmaneser and Sargon (722‒721 b.c.), but it is
possible of course to interpret it of the repeated disasters at the
hands of the Assyrians in the time of Tiglath-pileser some ten years
earlier.

⁷And be not ye like your fathers, and like your


brethren, which trespassed against the Lord,
the God of their fathers, so that he gave them
up to desolation ¹, as ye see.
¹ Or, to be an astonishment.

7. to desolation] Render, as margin, to be an astonishment;


compare xxix. 8 (same Hebrew word).

⁸Now be ye not stiffnecked, as your fathers


were; but yield yourselves ¹ unto the Lord, and
enter into his sanctuary, which he hath
sanctified for ever, and serve the Lord your
God, that his fierce anger may turn away from
you.
¹ Hebrew give the hand.

8. yield yourselves] Literally “give the hand”; compare 1


Chronicles xxix. 24 “submitted themselves”).

sanctified for ever] Compare vii. 16.

⁹For if ye turn again unto the Lord, your


brethren and your children shall find
compassion before them that led them
captive, and shall come again into this land:
for the Lord your God is gracious and
merciful, and will not turn away his face from
you, if ye return unto him. ¹⁰So the posts
passed from city to city through the country of
Ephraim and Manasseh, even unto Zebulun:
but they laughed them to scorn, and mocked
them.
9. shall find compassion] Compare Psalms cvi. 46 (a similar
phrase in Hebrew).

¹¹Nevertheless divers of Asher and Manasseh


and of Zebulun humbled themselves, and
came to Jerusalem.
11. of Asher] Asher is somewhat strange. The parallel with verse
10 alone suggests that we should read of Ephraim; and this is the
more probable if the real significance of the reference is for the
Chronicler’s period (see the head-note, and xv. 9). It is not likely that
Judaism at that time could claim many adherents in the old territory
of Asher (see Hölscher, Palästina, p. 32).

humbled themselves] So xxxiii. 12.

¹²Also in Judah was the hand of God to give


them one heart, to do the commandment of
the king and of the princes by the word of the
Lord.
12. Also in Judah was the hand of God] i.e. the mighty working of
God, which brought some penitents from far parts of Israel,
manifested itself in Judah also.

the commandment of the king ... by the word of the Lord] The
king’s command was according to God’s command in the Law.

13‒27 (not in 2 Kings).


Hezekiah’s Great Passover.
It seems clear that the story of Hezekiah’s Passover has been
composed by the Chronicler on the analogy of Josiah’s grand
celebration of that feast (see xxxv. 1‒19), which the present festival
even surpasses in some respects—viz. in its scope (for all Israel and
strangers, whereas Josiah’s was for Judeans only) and in its
duration (for two weeks, Josiah’s for one). Josiah’s Passover was
famous because of the account of it in Kings. Doubtless the
Chronicler felt that a celebration of that feast was incumbent upon a
great reforming monarch, and he has therefore credited Hezekiah
with observing it.

¹³And there assembled at Jerusalem much


people to keep the feast of unleavened bread
in the second month, a very great
congregation.
13. the feast of unleavened bread] In the “Passover” were united
two separate “feasts,” (1) the eating of the lamb on the fourteenth of
Nisan, (2) the eating of unleavened bread from the fourteenth to the
twenty-first of Nisan. The combined Feast was sometimes called “the
Passover” and sometimes (as here) “the feast of unleavened bread”;
compare Exodus xii. 1‒14 and 17‒20, and note that the intervening
verses, 15, 16, bind the two feasts into one celebration.

¹⁴And they arose and took away the altars that


were in Jerusalem, and all the altars for
incense ¹ took they away, and cast them into
the brook Kidron.
¹ Or, vessels.

14. the altars] Compare xxviii. 24.


¹⁵Then they killed the passover on the
fourteenth day of the second month: and the
priests and the Levites were ashamed, and
sanctified themselves, and brought burnt
offerings into the house of the Lord. ¹⁶And
they stood in their place after their order,
according to the law of Moses the man of
God: the priests sprinkled the blood, which
they received of the hand of the Levites.
15. the second month] Compare verses 2, 3.

were ashamed] Of their former backwardness; compare verse 3,


xxix. 34.

¹⁷For there were many in the congregation that


had not sanctified themselves: therefore the
Levites had the charge of killing the passovers
for every one that was not clean, to sanctify
them unto the Lord.
17. of killing the passovers] “Passovers” (plural rare) = “Paschal
victims”; compare verse 15, xxxv. 8 (“passover offerings”).

¹⁸For a multitude of the people, even many of


Ephraim and Manasseh, Issachar and
Zebulun, had not cleansed themselves, yet did
they eat the passover otherwise than it is
written. For Hezekiah had prayed for them,
saying, The good Lord pardon every one ¹
¹ Or, him that setteth his whole heart.

18. of Ephraim, etc.] The list of tribes given here does not agree
with the list in verse 11, but in both cases it may be that the
Chronicler merely wished by his list to designate men of the Northern
Kingdom as opposed to those of the Southern. He could not make
the distinction by using the term “Israel” here, for in Chronicles
“Israel” as a rule is not used in opposition to “Judah”; compare xi. 3
(note). (For a somewhat different view, see the head-note on verses
1‒12 and xv. 9.)

otherwise than it is written] i.e. they were allowed to partake of


the Passover meal, although not purified according to the regulations
of the Law.

¹⁹that setteth his heart to seek God, the Lord,


the God of his fathers, though he be not
cleansed according to the purification of the
sanctuary.
18, 19. The good Lord pardon every one that, etc.] In Hebrew
verse 18 ends abruptly with the word “pardon.” Probably the Revised
Version is correct in disregarding the Hebrew division. The phrase
“the good Lord” is not found elsewhere, and another suggestion is
to transpose the adjective and read (verse 18) ... “The Lord pardon
the good: (verse 19) even every one that,” etc. The LXX., however,
supports the order of the Hebrew text.

²⁰And the Lord hearkened to Hezekiah, and


healed the people.
20. healed the people] By prevention; no plague was allowed to
break out among them, although uncleanness in the sanctuary had
been threatened with death; Leviticus xv. 31.
²¹And the children of Israel that were present
at Jerusalem kept the feast of unleavened
bread seven days with great gladness: and
the Levites and the priests praised the Lord
day by day, singing with loud instruments unto
the Lord.
21. with loud instruments] Literally “with instruments of strength.”
It is better to read “with all their might” (as 1 Chronicles xiii. 8). The
change in Hebrew amounts only to the dropping of the smallest letter
(yōd).

²²And Hezekiah spake comfortably unto all the


Levites that were well skilled in the service of
the Lord. So they did eat throughout the feast
for the seven days, offering sacrifices of peace
offerings, and making confession ¹ to the Lord,
the God of their fathers.
¹ Or, giving thanks.

22. spake comfortably] i.e. with kindly and appreciative words.


For the phrase compare Isaiah xl. 2.

making confession] Or, as margin, “giving thanks.”

²³And the whole congregation took counsel to


keep other seven days: and they kept other
seven days with gladness.
23. other seven days] Compare vii. 9 (Solomon’s Dedication
Feast).
²⁴For Hezekiah king of Judah did give to the
congregation for offerings a thousand bullocks
and seven thousand sheep; and the princes
gave to the congregation a thousand bullocks
and ten thousand sheep: and a great number
of priests sanctified themselves.
24. sanctified themselves] Compare xxix. 34.

²⁵And all the congregation of Judah, with the


priests and the Levites, and all the
congregation that came out of Israel, and the
strangers that came out of the land of Israel,
and that dwelt in Judah, rejoiced.
25. the strangers] i.e. men of alien descent dwelling in Israel with
certain conceded, not inherited, rights, and with most of the
obligations of the native Israelite. LXX. οἱ προσήλυτοι Compare ii. 17;
1 Chronicles xxii. 2 for the unfavourable side of a “stranger’s”
position.

²⁶So there was great joy in Jerusalem: for


since the time of Solomon the son of David
king of Israel there was not the like in
Jerusalem.
26. since the time of Solomon] For Solomon’s great festival, see
v. 2 ff.

there was not the like] Compare what is said of Josiah’s


Passover; xxxv. 18 (note).
²⁷Then the priests the Levites arose and
blessed the people: and their voice was heard,
and their prayer came up to his holy
habitation, even unto heaven.
27. the priests the Levites] So in xxiii. 18, but only in these two
places in Chronicles The phrase is Deuteronomic, and implies that at
the stage of ritual development represented in Deuteronomy all
Levites were potentially priests. Such was not in any case the view
of the Chronicler, and perhaps we ought to read “the priests and the
Levites” both here and in xxiii. 18.
Chapter XXXI.
1 (compare 2 Kings xviii. 4).
Destruction of Idolatrous Symbols.

¹Now when all this was finished, all Israel


that were present went out to the cities of
Judah, and brake in pieces the pillars ¹, and
hewed down the Asherim, and brake down the
high places and the altars out of all Judah and
Benjamin, in Ephraim also and Manasseh,
until they had destroyed them all. Then all the
children of Israel returned, every man to his
possession, into their own cities.
¹ Or, obelisks.

1. the pillars ... the Asherim] Compare xiv. 3 (note).

in Ephraim also] It is obviously assumed that the Northern


Kingdom had come to an end; compare xxx. 6, 9.

2‒21 (not in 2 Kings).


Organisation of the Priests. Tithe.

If the Temple had been desecrated and closed by Ahaz, it would


follow that the organisation of its Priests and Levites had fallen into
confusion. The Chronicler therefore makes Hezekiah the restorer of

You might also like