Get MATLAB Recipes: a problem-solution approach 2nd Edition Michael Paluszek PDF ebook with Full Chapters Now
Get MATLAB Recipes: a problem-solution approach 2nd Edition Michael Paluszek PDF ebook with Full Chapters Now
com
https://textbookfull.com/product/matlab-recipes-a-problem-
solution-approach-2nd-edition-michael-paluszek/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/matlab-machine-learning-recipes-a-
problem-solution-approach-2nd-edition-michael-paluszek/
textboxfull.com
https://textbookfull.com/product/matlab-machine-learning-recipes-a-
problem-solution-approach-3rd-edition-michael-paluszek/
textboxfull.com
https://textbookfull.com/product/android-recipes-a-problem-solution-
approach-dave-smith/
textboxfull.com
https://textbookfull.com/product/raku-recipes-a-problem-solution-
approach-j-j-merelo/
textboxfull.com
C Recipes - A Problem-Solution Approach 1st Edition
Shirish Chavan
https://textbookfull.com/product/c-recipes-a-problem-solution-
approach-1st-edition-shirish-chavan/
textboxfull.com
https://textbookfull.com/product/javascript-recipes-a-problem-
solution-approach-1st-edition-russ-ferguson/
textboxfull.com
https://textbookfull.com/product/wxpython-recipes-a-problem-solution-
approach-1st-edition-mike-driscoll/
textboxfull.com
https://textbookfull.com/product/java-9-recipes-a-problem-solution-
approach-3rd-edition-josh-juneau/
textboxfull.com
https://textbookfull.com/product/spring-5-recipes-a-problem-solution-
approach-4th-edition-marten-deinum/
textboxfull.com
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
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
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
IV
CONTENTS
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
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
VIII
CONTENTS
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
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
XI
CONTENTS
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
9.4 Simulating the Controlled Motor . . . . . . . . . . . . . . . . . . . . . . . . 270
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
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
XIII
CONTENTS
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
XV
ABOUT THE AUTHORS
XVI
Acknowledgments
We would like to acknowledge Joseph Mueller for his expert editing of this book.
XVII
Introduction
Writing software has become part of the job description for nearly every professional engineer
and engineering student. While there are many excellent prebuilt software applications for
engineers, almost everyone can benefit by writing custom software for their own problems.
MATLAB had its origins for that very reason. Scientists that needed to do operations on
matrices used numerical software written in FORTRAN. At the time, using computer languages
required the user to go through the write-compile-link-execute process that was time-consuming
and error-prone. MATLAB presented the user with a scripting language that allowed the user to
solve many problems with a few lines of a script that executed instantaneously. MATLAB had
built-in visualization tools that helped the user better understand the results. Writing MATLAB
was a lot more productive and fun than writing FORTRAN.
MATLAB has grown greatly since those days. The power of the basic MATLAB software
has grown dramatically, and hundreds of MATLAB libraries are now available, both commer-
cially and as open source. MATLAB is so sophisticated that most new users only use a fraction
of its power.
The goal of MATLAB Recipes is to help all users harness the power of MATLAB. This book
has two parts. The first part, Chapters 1 through 5, gives a framework that you can use to write
high-quality MATLAB code that you, your colleagues, and possibly your customers can utilize.
We cover coding practices, graphics, debugging, and other topics in a problem-solution format.
You can read these sections from cover to cover or just look at the recipes that interest you and
use them in your latest MATLAB code.
The second part of the book, Chapters 6 through 12, shows complete MATLAB applications
revolving around the control of and simulation of dynamical systems. Each chapter provides
the technical background for the topic, ideas on how you can write a simple control system, and
an example of how you might simulate the system. Each system is implemented in a MATLAB
script supported by a number of MATLAB functions. Each chapter also highlights a general
MATLAB topic, like graphics or writing graphical user interfaces (GUIs). We have deliberately
made the control systems simple so that the reader won’t need a course in control theory to get
results. Control experts can easily take the script and implement their own ideas. We cover a
number of areas, ranging from chemical processes to satellites – and we apologize if we didn’t
write an example for your area of interest!
The book has something for everyone – from the MATLAB novice to the authors of com-
mercial MATLAB packages. We learned new things writing this book! We hope that you enjoy
the book and look forward to seeing your software that it inspires.
XIX
Part I
Coding in MATLAB
CHAPTER 1
Coding Handbook
The purpose of this chapter is to provide an overview of MATLAB syntax and programming,
highlighting features that may be underutilized by many users and noting important differences
between MATLAB and other programming languages and IDEs. You should also become
familiar with the very detailed documentation that is available from the MathWorks in the help
browser. The Language Fundamentals section describes entering commands, operators, and
data types.
MATLAB has matured a lot in the last two decades from its origins as a linear algebra pack-
age. Originally, all variables were double precision matrices. Today, MATLAB provides differ-
ent variable types such as integers, data structures, object-oriented programming and classes,
and integration with Java. The MATLAB application is a full IDE with an integrated editor,
debugger, command history, and code analyzer and report capabilities. Engineers who have
been working with MATLAB for many years may find that they are not taking advantage of the
full range of capabilities now offered, and in this text we hope to highlight the more useful new
features.
The first part of this chapter provides an overview of the most commonly used MATLAB
types and constructs. We’ll then provide some recipes that make use of these constructs to show
you some practical applications of modern MATLAB.
The MATLAB application is a rich development environment for the MATLAB language.
It provides an editor, command terminal, debugger, plotting capabilities, creation of graphical
user interfaces, and more recently the ability to install third-party apps. MATLAB can interface
with other languages including FORTRAN, C, C++, Java, and Python. A code analyzer and
profiler are built-in. Extensive online communities provide forums for sharing code and asking
questions.
The main components of the MATLAB application are
Command Window – Terminal for entering commands and operating on variables in the base
workspace. The MATLAB prompt is >>.
Workspace display – List of the variables and their values in the current workspace (applica-
tion memory). Variables remain in the memory once created until you explicitly clear
them or close MATLAB.
Current Folder – File browser displaying contents of the current folder and providing file
system navigation. Recent versions of MATLAB can also display SVN status on config-
uration managed files.
File details – Panel displaying information on the file selected in the Current Folder panel.
Editor – Editor for m-files with syntax coloring and a built-in debugger. This can also display
any type of text file and will recognize and appropriately color other languages including
Java, C/C++, and XML/HTML.
Help browser – Searchable help documentation on all MATLAB products and third-party
products you have installed.
These components can be docked in various configurations. The default layout of the main
application window or desktop contains the first five components listed earlier and is shown in
Figure 1.1. The Command Window is in the center. The upper-left panel shows a file browser
with the contents of the Current Folder. Under this is a file information display. On the right-
hand side is the Workspace display and the Command History panel. The base workspace is all
the variables currently in the application memory. Commands from the history can be dragged
onto the command line to be executed, or double-clicked. The extensive toolbar includes but-
tons for running the code analyzer and opening the code profiler and the help window, as well as
typical file and data operations. Note the PLOTS and APPS tabs above the toolbar. The PLOTS
4
CHAPTER 1 CODING HANDBOOK
tab allows the graphical creation and management of plots from data selected in the workspace
browser. The APPS tab allows you to access and manage third-party apps that you install.
You can rearrange the components in the application window, moving, resizing, or hiding
them, and save your own layouts. You can “undock” any component, moving it to its own
window. You can also revert back to the default layout at any time or choose from several
other available configurations. You can also hide the toolstrip to get more real estate for your
windows. There are new capabilities to customize your interface with each version, so explore
what’s new!
The editor with the default syntax coloring is shown in Figure 1.2, with a file from this
chapter shown. The horizontal lines show the division of the code into “cells” using a double-
percent sign, which can be used for sequential execution of code and for creating sections of text
when publishing. The cell titles are bolded in the editor. MATLAB keywords are highlighted
in blue, comments in green, and strings in pink. The toolbar includes buttons for commenting
code, indenting, and running or debugging the code. The “Go To” pop-up menu gives access
to subfunctions within a large file (see Section 1.10). Note the PUBLISH and VIEW tabs with
additional features on publishing, covered in the next chapter, and options for the editor view.
The last window we will show is the help browser in Figure 1.3. MATLAB has extensive
help including examples and links to online videos and tutorials. Third-party toolboxes can
also install help into this browser. Like any browser, you can have open multiple tabs, there is
5
CHAPTER 1 CODING HANDBOOK
a search utility, and you can mark favorite topics. We will refer to topics available in the help
browser throughout this book.
Everything Is a Matrix
By default, all variables in MATLAB are double precision matrices. You do not need to declare
a type for these variables. Matrices can be multidimensional and are accessed using one-based
indices via parentheses. You can address elements of a matrix using a single index, taken
column-wise, or one index per dimension. Use square brackets to enclose the matrix data and
semicolons to mark the end of rows. Use a final semicolon to end the line, or leave it off to print
the result to the command line. To create a matrix variable, simply assign a value to it, like this
2x2 matrix a and 2x1 matrix b:
>> a = [1 2; 3 4];
>> a(1,1)
6
CHAPTER 1 CODING HANDBOOK
1
>> a(3)
2
>> b = [5; 6]
b =
5
6
You can simply add, subtract, multiply, and divide matrices with no special syntax. The
matrices must be the correct size for the linear algebra operation requested. A transpose is
indicated using a single quote suffix, A’, and the matrix power uses the operator ˆ.
>> b = a'*a;
>> c = aˆ2;
>> d = b + c;
By default, every variable is a numerical variable. You can initialize matrices to a given size
using the zeros, ones, eye, or rand functions, which produce zeros, ones, identity matri-
ces (ones on the diagonal), and random numbers, respectively. Use isnumeric to identify
numeric variables. Table 1.1 shows key matrix functions.
7
CHAPTER 1 CODING HANDBOOK
Function Purpose
zeros Initialize a matrix to zeros
ones Initialize a matrix to ones
eye Initialize an identity matrix
rand, randn, randi Initialize a matrix of random numbers
isnumeric Identify a matrix or scalar numeric value
isscalar Identify a scalar value (a 1 x 1 matrix)
size Return the size of the matrix
Use ischar to identify character variables. Also note that isempty returns TRUE for an
empty array, that is, ''.
Since R2016b, MATLAB has also provided a string type defined using regular quotes. Some
newer functions are designed to operate specifically on strings, but most work on both text types.
If you concatenate strings using square brackets, they are maintained as separate elements in an
array rather than combined as character arrays are. To append strings, use the “+” operator (see
Recipe 1.5). isempty returns FALSE for an empty string, that is, ``''; this creates a 1-by-1
string with no characters rather than an empty string.
>> str = "";
>> isempty(str)
ans =
logical
0
>> s = "Hello World";
>> msg = [s "additional string"]
msg =
8
CHAPTER 1 CODING HANDBOOK
For a description of string syntax, type help strings at the MATLAB command line,
and for a comprehensive list of string and character functions, type help strfun. Table 1.2
shows a selection of key string functions.
Function Purpose
ischar Identify a character array
isstring Identify a string
char Convert integer codes or cell array to character array
sprintf Write formatted data to a string
strcmp, strncmp Compare strings
strfind Find one string within another
num2str, mat2str Convert a number or matrix to a string
lower Convert a string to lowercase
contains Search for patterns in string arrays
split Split strings at whitespace
with
d = struct;
d.fieldName = 0;
In fact, we have found it is generally a good idea to create a special function to initialize
larger structures that are used throughout a set of functions. This is similar to creating a class
definition. Generating your data structure from a function, instead of typing out the fields in
a script, means you always start with the correct fields. Having an initialization function also
allows you to specify the types of variables and provide sample or default data. Remember,
since MATLAB does not require you to declare variable types, doing so yourself with default
data makes your code that much clearer.
9
CHAPTER 1 CODING HANDBOOK
You make a data structure into an array simply by assigning an additional copy. The fields
must be in the same order, which is yet another reason to use a function to initialize your
structure. You can nest data structures with no limit on depth.
1 d = MyStruct;
2 d(2) = MyStruct;
3
4 function d = MyStruct
5
6 d = struct;
7 d.a = 1.0;
8 d.b = 'string';
MATLAB now allows for dynamic field names using variables, that is, structName.
(dynamicExpression). This provides improved performance over getfield, where the
field name is passed as a string. This allows for all sorts of inventive structure programming.
Take our data structure array in the previous code snippet, and let’s get the values of field a
using a dynamic field name; the values are returned in a cell array.
>> field = 'a';
>> values = {d.(field)}
values =
[1] [1]
Use isstruct to identify structure variables and isfield to check for the existence of
fields. Note that isempty will return false for a struct initialized with struct, even if it has
no fields. Table 1.3 lists some key functions for interacting with structs.
Function Purpose
struct Initialize a structure with or without fields
isstruct Identify a structure
isfield Determine if a field exists in a structure
fieldnames Get the fields of a structure in a cell array
rmfield Remove a field from a structure
deal Set fields in a structure to a value
10
CHAPTER 1 CODING HANDBOOK
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.
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
Random documents with unrelated
content Scribd suggests to you:
quarter, days. A day is one revolution of Terra. From Mars, say, a
Terran year is something else entirely. Mars, of course, is not too
good an example for its sidereal day is very close to Terra's. But your
Venus, with its eighteen hour day—eighteen Terran hours—sees
Terra's year as four hundred eighty-six, plus, days. On Ertene, we
have no year. We had one, once. It was composed of four hundred
twelve point seven zero four two two nine three one days, sidereal.
Now, our day is different, since the length of the solar day depends
upon the progression of the planet about its luminary. Our luminary
behaves as a moon with a high ecliptic-angle as I have explained.
No, Guy, I have been mentally converting my year to your year, by
crude approximation."
The next panel was an ornate painting of the Ertinian system,
showing—out of scale for artistic purpose—the planets and sun, with
Ertene drawing away in a long spiral.
"For many years we pursued that spiral, withdrawing from the sun by
slow degrees. Then we broke free." Charalas indicated the panel
which showed Ertene in the foreground while the clustered system
was far behind.
They passed from panel to panel, all of which were interesting to Guy
Maynard. There was a series of the first star contacted by Ertene. It
was a small system, cold and forbidding, or hot and equally
forbidding. The outer planets were in the grip of frozen air, and the
inner planets bubbled in moltenness "This system was too far out of
line to turn. It was our first star, and we might have stayed in
youthfulness. Now, we know better."
The next panel showed a dimly-lighted landscape; a portrayal of
Ertene without its synthetic sun. The luminous sky was beautiful in a
nocturnal sort of way; to Guy it was slightly nostalgic for some
unknown reason, at any rate it was the soul of sadness, that
landscape.
Charalas shook his head and then smiled. He led Guy to the next
panel, and there was a portrait of an elderly man, quite a bit older
than Charalas though the neuro-surgeon was no young man.
"Timalas," said Charalas proudly. "He gave us the next panel."
The following panel was a similar scene to the dismal one, but now
the same trees and buildings and hills and sky were illuminated by a
sun. It was a cheerful, uplifting scene compared to the soul-clouding
darkness.
Maynard turned to Charalas and saw that the elderly doctor had been
watching him intently. Before he could speak, the Ertinian said: "It is a
hard nut to crack, lad. Many have tried but none have succeeded.
Like most things that are best for people, they are the least exciting
and the most formal, and people do not react cheerfully to a formal
diet."
Maynard shook his head. "But unlike a man with ulcers, I cannot
prescribe a diet of milk lest he die. Ertene will go on living no matter
whether I speak and sway them or whether I never say another word.
I am asked to convince an entire world against their will. I can not tell
them that it is the slightest bit dangerous to go on as they have. In
fact, it may be dangerous for them to remain. In all honesty, I must
admit that Terra is not without her battle scars."
Charalas said, thoughtfully: "Who knows what is best for civilization?
We do not, for we are civilization. We do as we think best, and if it is
not best, we die and another civilization replaces us in Nature's long-
time program to find the real survivor."
He faced the panel and said, partly to himself and partly to Guy:
"Is it best for Ertene to go on through time experimenting? Gathering
the fruits of a million civilizations bound forever to their stellar homes
because of the awful abyss between the stars? For the planets all to
become wanderers would be chaos.
"Therefore is it Nature's plan that Ertene be the one planet to gather
unto herself the fruit of all knowledge and ultimately lie barren
because of the sterility of her culture? Are we to be the sponge for all
thought? If so, where must it end? What good is it? Is this some great
master plan? Will we, after a million galactic years, reach a state
where we may disseminate the knowledge we have gained, or are we
merely greedy, taking all and giving nothing?
"What are we learning? And, above all, are we certain that Ertene's
culture is best for civilization? How may we tell? The strong and best
adapted survive, and since we are no longer striving against the
lesser forces of Nature on our planet, and indeed, are no longer
striving against those of antisocial thought among our own people—
against whom or what do we fight?
"Guy Maynard, you are young and intelligent. Perhaps by some
whimsy of fate you may be the deciding factor in Ertene's
aimlessness. We are here, Guy. We are at the gates to the future. My
real reason for bringing you to the Center of Ertene is to have you
present your case to the Council."
He took Guy's arm and led him through the door at the end of the
corridor. They went into the gilt-and-ivory room with the vast
hemispherical dome and as the door slowly closed behind them, Guy
Maynard, Terran, and Charalas, Ertinian, stood facing a quarter-circle
of ornate desks behind which sat the Council.
Obviously, they had been waiting.
IV.
Guy Maynard looked reproachfully at Charalas. He felt that he had
been tricked, that Charalas had kicked the bottom out of his argument
and then had forced him into the debate with but an impromptu
defense. He wondered how this discussion was to be conducted, and
while he was striving to collect a lucid story, part of his mind heard
Charalas going through the usual procedure for recording purposes.
"Who is this man?"
"He is Junior Executive Guy Maynard of the Terran Space Patrol."
"Explain his title."
"It is a rank of official service. It denotes certain abilities and
responsibilities."
"Can you explain the position of his rank with respect to other ratings
of more or less responsibility?"
Charalas counted off on his fingers. "From the lowest rank upward,
the following titles are used: Junior Aide, Senior Aide, Junior
Executive, Senior Executive, Sector Commander, Patrol Marshal,
Sector Marshal, and Space Marshal."
"These are the commissioned officers? Are there other ratings?"
"Yes, shall I name them?"
"Prepare them for the record. There is no need of recounting the
noncommissioned officials."
"I understand."
"How did Guy Maynard come to Ertene?"
"Maynard was rescued from a derelict spaceship."
"By whom?"
"Thomakein."
"Am I to assume that Thomakein brought him to Ertene for study?"
"That assumption is correct."
"The knowledge of the system of Sol is complete?"
"Between the information furnished by Guy Maynard and the
observations made by Thomakein, the knowledge of Sol's planets is
sufficient. More may be learned before Ertene loses contact, but for
the time, it is adequate."
"And Guy Maynard is present for the purpose of explaining the Terran
wishes in the question of whether Ertene is to remain here?"
"Correct."
The councilor who sat in the center of the group smiled at Guy and
said: "Guy Maynard, this is an informal meeting. You are to rest
assured we will not attempt to goad you into saying something you do
not mean. If you are unprepared to answer a given question, ask for
time to think. We will understand. However, we ask that you do not try
to shade your answers in such a manner as to convey erring
impressions. This is not a court of law; procedure is not important.
Speak when and as you desire and understand that you will not be
called to account for slight breaches of etiquette, since we all know
that formality is a deterrent to the real point in argument."
Charalas added: "Absolute formality in argument usually ends in the
decision going to the best orator. This is not desirable, since some of
the more learned men are poor orators, while some of the best
orators must rely upon the information furnished them by the
learned."
The center councilor arose and called the other six councilors by
name in introduction. This was slightly redundant since their names
were all present in little bronze signs on the desks. It was a
pleasantry aimed at putting the Terran at ease and offering him the
right to call them by name.
"Now," said Terokar, the center one, "we shall begin. Everything we
have said has been recorded for the records. But, Guy, we will
remove anything from the record that would be detrimental to the
integrity of any of us. We will play it back before you leave and you
may censor it."
"Thank you," said Guy. "Knowing that records are to be kept as
spoken will often deter honest expression."
"Quite true. That is why we permit censoring. Now, Guy, your wishes
concerning Ertene's alliance with Sol."
"I invite Ertene to join the Solar System."
"Your invitation is appreciated. Please understand that the
acceptance of such an invitation will change Ertene's social structure
forever, and that it is not to be taken lightly."
"I realize that the invitation is not one to accept lightly. It is a large
decision."
"Then what has Sol to offer?"
"A stable existence. The commerce of an entire system and the
friendship of another world of similar type in almost every respect.
The opportunity to partake in a veritable twinship between Ertene and
Sol, with all the ramifications that such a brotherhood would offer."
"Ertene's existence is stable, Guy. Let us consider that point first."
"How can any wandering program be considered stable?"
"We are born, we live, and we die. Whether we are fated to spend our
lives on a nomad planet or ultimately become the very center of the
universe about which everything revolves, making Ertene the most
stable planet of them all, Ertinians will continue living. When
nomadism includes the entire resources of a planet, it can not be
instable."
"Granted. But do you hope to go on forever?"
"How old is your history, Guy?"
"From the earliest of established dates, taken from the stones of
Assyria and the artifacts of Maya, some seven thousand years."
Charalas added a lengthy discussion setting the length of a Terran
year.
"Ertinian history is perhaps a bit longer," said Terokar. "And so who
can say 'forever'?"
"No comment," said Guy with a slight laugh. "But my statements
concerning stability are not to be construed as the same type of
instability suffered by an itinerant human. He has no roots, and few
friends, and he gains nothing nor does he offer anything to society.
No, I am wrong. It is the same thing. Ertene goes on through the eons
of wandering. She has no friends and no roots and while she may
gain experience and knowledge of the universe just as the tramp will,
her ultimate gain is poor and her offering to civilization is zero."
"I dispute that. Ertene's life has become better for the experience she
has gained and the knowledge, too."
"Perhaps. But her offering to civilization?"
"We are not a dead world. Perhaps some day we may be able to offer
the storehouses of our knowledge to some system that will need it.
Perhaps we are destined to become the nucleus of a great, galactic
civilization."
"Such a civilization will never work as long as men are restrained as
to speed of transportation. Could any pact be sustained between
planets a hundred light-years apart? Indeed, could any pact be
agreed upon?"
"I cannot answer that save to agree. However, somewhere there may
be some means of faster-than-light travel and communication. If this
is found, galactic-wide civilization will not only be possible but a
definite expectation."
"You realize that you are asking for Ertene a destiny that sounds
definitely egotistic?"
"And why not? Are you not sold on the fact that Terra is the best
planet in the Solar System?"
"Naturally."
"Also," smiled Charalas, "the Martians admit that Mars is the best
planet."
"Granted then that Ertene is stable. Even granting for the moment
that Ertene is someday to become the nucleus of the galaxy. I still
claim that Ertene is missing one item." Guy waited for a moment and
then added: "Ertene is missing the contact and commerce with other
races. Ertene is self-sufficient and as such is stagnant as far as new
life goes. Life on Ertene has reached the ultimate—for Ertene.
Similarly, life on Terra had reached that point prior to the opening of
space. Life must struggle against something, and when the struggle
is no longer possible—when all possible obstruction has been
circumvented—then life decays."
"You see us as decadent?"
"Not yet. The visiting of system after system has kept you from total
decadence. It is but a stasis, however. Unless one has the samples of
right and wrong from which to choose, how may he know his own
course?"
"Of what difference is it?" asked the councilor named Baranon. "If
there is no dissenting voice, if life thrives, if knowledge and science
advance, what difference does it make whether we live under one
social order or any other? If thievery and wrongdoing, for instance,
could support a system of social importance, and the entire
population lives under that code and thrives, of what necessity is it to
change?"
"Any social order will pyramid," said Guy. "Either up or down."
"Granted. But if all are prepared to withstand the ravages of their
neighbors, and are eternally prepared to live under constant strife, no
man will have his rights trod upon."
"But what good is this eternal wandering? This everlasting eye upon
the constantly receding horizon? This never ending search for the
proper place to stop in order that this theoretical galactic civilization
may start? At Ertene's state of progress, one place will be as good as
any other," said Guy.
"Precisely, except that some places are definitely less desirable.
Recall, Guy, that Ertene needs nothing."
"I dispute that. Ertene needs the contact with the outside worlds."
"No."
"You are in the position of a recluse who loves his seclusion."
"Certainly."
"Then you are in no position to appreciate any other form of social
order."
"We care for no other social order."
"I mentioned to Charalas that in my eyes, you are wrong. That I am
being asked to prescribe for a patient who will not die for lack of my
prescription. I can not even say that the patient will benefit directly.
My belief is as good as yours. I believe that Ertene is suffering
because of her seclusion and that her peoples will advance more
swiftly with commerce between the planets—and once again in
interstellar space, Ertene will have no planets with which to conduct
trade."
"And Sol, like complex society, will never miss the recluse. Let the
hermit live in his cave, he is neither hindering nor helping civilization."
"Indirectly, the hermit hinders. He excites curiosity and the wonder if a
hermit's existence might not be desirable and thus diverts other
thinkers to seclusion."
"But if the hermit withdraws alone and unnoticed, no one will know of
the hermitage, and then no one will wonder."
"But I know, and though no one else in the Solar System knows, I am
trying to bring you into our society. I have the desire of brotherhood,
the gregarious instinct that wants to be friend with all men. It annoys
me—as it annoys all men—to see one of us alone and unloved by his
fellows. I have a burning desire to have Ertene as a twin world with
Terra."
"But Ertene likes her itinerant existence. The fires that burn beyond
the horizon are interesting. Also," smiled Terokar, "the grass is
greener over there."
"One day you will come to the end of the block," said Guy, "and find
that the grass is no greener anywhere, with the exception that you
now have no more grass to look at, plus the sorry fact that you cannot
return. A million galactic years from now, Ertene will have passed
through the galaxy and will find herself looking at intergalactic space.
Then what?"
"Then our children will learn to live in a starless sky for a hundred
thousand generations. Solarians live in a sky of constant placement;
Ertene's sky is ever changing and all sky maps are obsolete in thirty
or forty years. You must remember that to us, wandering is the
normal way of life. Some of us believe that we may eventually return
to our parent sun. We may. But all of us believe that we would find
our parent sun no more interesting than others. No Guy, I doubt that
we will stop there either."
"You are assuming that you will not remain at Sol?"
"We are a shy planet. We do not like to change our way of life. You
are asking us to give up our life and to accept yours. It is similar to a
man asking a woman to marry. But a woman is not completely
reversed in her life when she marries. Here you are asking us to
cleave unto you forever—and there is no bond of love to soften the
hard spots."
"I did mention the bond of brotherhood," said Guy.
"Brotherhood with what?" asked Terokar. "You ask us to enter a bond
of twinship with a planet that is the center of strife. You ask us in the
name of similarity to join you—and help you gain mastery over the
Solar System."
"And why not?"
"Which of you is right? Is the Terran combine more righteous than the
Martian alliance?"
"Certainly."
"Why?"
Guy asked for a moment to think. The room was silent for a moment
and then he said, slowly and painfully: "I can think of no other reason
than the trite and no-answer reason: 'We're right because we're right!'
The Martian combine fights us to gain the land and the commerce
that we have taken because of superiority in space."
"A superiority given merely because of sheer size," said Baranon.
"The Martians, raised under a gravity of less than one third of Terra's
find it difficult to keep pace with the Terrans, who can live under three
times as much acceleration. Battle under such conditions is unfair,
and the fact that the Martians have been able to survive indicates that
their code is not entirely wrong."
Charalas nodded. "Any code that is entirely in error will not be able to
survive."
"So," said Terokar, "you ask us to join your belligerent system. You
ask us to emerge from our pleasure and join you in a struggle for
existence. You ask that we give up the peace that has survived for a
thousand years, and in doing so you ask that we come willingly and
permit our cities to be war-scarred and our men killed. You ask that
we join in battle against a smaller, less adapted race that still is able
to survive in spite of its ill-adaption to the rigors of space."
Guy was silent.
"Is that the way of life? Must we fight for our life? Strife is deplorable,
Guy Maynard, and I am saying that to you, who come of a planet
steeped in strife. You wear a uniform—or did—that is dedicated to the
job of doing a better job of fighting than the enemy. Continual warlike
activity has no place on Ertene.
"Plus one other thing, Guy Maynard. You are honorable and your
intent is clear. But your fellows are none too like you. Ertene would
become the playground of the Solar System. There would be
continual battles over Ertene, and Ertene with her inexperience in
warfare would be forced to accept the protection of Terra. That
protection would break down into the same sort of protection that is
offered the Plutonians by a handful of Terrans. In exchange for
'protection' against enemies that would possibly be no better or
worse, the Plutonians are stripped of their metal. They are not
accorded the privilege of schooling because they are too ignorant to
enter even the most elementary of schools. Besides, schooling would
make them aware of their position and they might rebel against the
system that robs them of their substance under the name of
'protection.' Protection? May the Highest Law protect me from my
protectors!" Terokar's lips curled slightly. "Am I not correct? Have not
the Plutonians the right to life, liberty, and the pursuit of happiness? It
would be a heavy blow to Terra if the third planet were forced to pay
value for the substance that comes from Pluto."
"After all," said Guy, "if Terra hadn't got there first, Mars would be
doing the same thing."
"Granted," said Baranon. "Absolutely correct. But two wrongs do not
make a right. Terra is no worse than Mars. But that does not excuse
either of them. They are both wrong!"
"Are you asking Terra to change its way of life?" demanded Guy.
"You are asking Ertene to change. We have the same privilege."
"Obviously in a system such as ours a completely altruistic society
would be wiped out."
"Obviously," said Baranon.
"Then—"
"Then Ertene will change its way of life—providing Terra changes
hers."
"Mars—"
"Mars will have to change hers, too. Can you not live in harmony?"
"Knowing what the Martians did to me—can you expect me to greet
one of them with open arms?"
"Knowing what you have done to them, I wouldn't expect either one of
you to change your greetings. No, Guy, I fear that Ertene will continue
on her path until such a time as we meet a system that is less
belligerent and more adapted to our way of life."
"Then I have failed?"
"Do not feel badly. You have failed, but you were fighting a huge,
overwhelming force. You fought the inheritance of a hundred
generations of wanderers. You fought the will of an integrated people
who deplore strife. You fought the desire of everyone on Ertene, and
since no Ertinian could change Solar society, we cannot expect a
Terran to change Ertinian ideals. You failed, but it is no disgrace to fail
against such an overwhelming defense."
Guy smiled weakly. "I presume that I was fighting against a
determined front?"
"You were trying to do the most difficult job of all. In order to have
succeeded, you would first have had to unsell us on our firm
convictions, and then sell us the desirability of yours. A double job,
both uphill."
"Then I am to consider the matter closed?"
"Yes. We have decided not to remain."
"You decided that before I came in," said Guy bitterly.
"We decided that a thousand years before you were born, so do not
feel bitter."
"I presume that a change in your plans is out of the question even
though further information on Sol's planets proves you wrong?"
"It will never be brought up again."
"I see," said Guy unhappily. "Part of my desire to convince you was
the hope of seeing my home again."
"Oh, but you will," said Charalas.
Guy was dumfounded. He could hardly believe his ears. He asked for
a repeat, and got it. It was still amazing. To Guy, it was outright
foolishness. He wouldn't have trusted anyone with such a secret. To
permit him to return to Terra with the knowledge he had—
"Charalas, what would prevent me from bringing my people to
Ertene? I could bring the forces of Terra down about your very ears."
"But you will not. We have a strict, value-even trade to offer you."
"But it would be so easy to keep me here."
"We could not restrain you without force. And if we must rely upon
your honor, we'd be equally reliant whether you be here or on Terra."
"Here," said Guy dryly, "I'd be away from temptation. If I were tempted
to tell, there'd be no one to tell it to."
"We must comply with an ancient rule," explained Terokar. "It says
specifically that no man without Ertinian blood may remain on Ertene.
It was made to keep the race pure when we were still about our
parent sun and has never been revoked. We wouldn't revoke it for
you alone."
"But permitting me to go free would be sheer madness."
"Not quite. We are mutually indebted to one another, Guy. There is
the matter of knowledge. You gave freely of yours, we gave you ours.
We have gained some points that were missing in our science, you
have a number of points that will make you rich, famous, and
remembered. Use them as your own, only do it logically in order that
they seem to be discoveries of your own. You admit the worth of
them?"
"Oh, but yes," said Guy eagerly. "Wonderful—"
"Then there is no debt for knowledge?"
"If any, I am in your debt."
"We'll call it even," said Baranon, dryly.
"Then there is the matter of life," said Terokar. "You know how you
were found?"
Guy shook his head in wonder. "I had been through the Martian idea
of how to get information out of a reluctant man," he said slowly. "I
know that their methods result in a terrible mindless state which to my
own belief is worse than death itself. I know that as I lost
consciousness, I prayed for death to come, even though I knew that
they would not permit it."
"We found you that way. You know. And we brought you back to life.
You owe us that."
"Indeed I do."
"Then for your life, we demand our life in return."
"I do not understand."
"Your life is yours. We ask that you say nothing of us—for we feel that
we will die if we are found. At least, the integrity of Ertene is at stake.
In any event, we will not be taken, you may as well know that. And
when I say die, I mean that Ertene will not go on living in the way we
want her to live. Therefore you will disclose nothing that will point our
way to anyone."
"And you are willing that I should return to Terra with such an oath?
What of my oath to Terra?"
"Do you feel that your presence on Ertene will benefit Terra in some
small way?" asked Charalas.
"Now that you have given me the things we spoke of before, I do."
"Then," said Charalas, "consider this point. You may not return unless
you swear to keep us secret. You may not give Terra the benefit of
your knowledge unless you deprive them of Ertene. Is that clear?"
"If I may not return to Terra, and may not remain on Ertene, I can
guess the other alternative and will admit that I do not like it. On the
returning angle, about all I can do is to justify myself in my own mind
that I have done all that I can by bringing these scientific items back
with me. Since doing the best I can for Terra includes keeping your
secret, I can do that also. But tell me, how do you hope to cover the
fact that I've been missing for almost a year? That will take more than
mere explanation."
"The process is easy," said Charalas. "We have one of the lifeships
from the derelict. It was slightly damaged in the blast. It is
maneuverable, but unwieldy. Evidence has been painstakingly
forged. Apparently you will have broken your straps under the shock
of the blast—and before the torture reached its height—and you
found yourself in a derelict with no one left alive but yourself. You
were hurt, mentally, and didn't grasp the situation clearly. There was
no way to signal your plight in secrecy, and open signaling would
have been dangerous since you were too close to Mars.
"You found the lifeship and waited until you could safely take off. The
derelict took a crazy course, according to the recorded log in your
own handwriting, and headed for interstellar space. You took off at
the safe time and have been floating free in the damaged lifeship.
You've been on a free orbit for the best part of a year."
"Sounds convincing enough."
"The evidence includes empty air cans, your own fingerprints on
everything imaginable, a dulled can opener and the remnants of can
labels that have fallen into nooks and crannies of the ship. The water-
recovery device has been under constant operation and examination
will show about a year's accumulation of residual matter. A scratch-
mark calendar has been kept on the wall of the lifeship, and daily it
has been added to. That is important since the wall will show more
oxidation in the scratches made a year ago than the ones made
recently. The accumulators of the ship have been run down as if in
service while you were forcing the little ship into its orbit, and the
demand recorder shows how the drain was used. The lights in the
ship have been burned, and the deposits of fluorescent material in
the tubes have been used about the calculated number of hours.
Books have been nearly worn out from re-reading and they were
used with fingerprint gloves though they were studied by us.
Instruments and gadgets are strewn about the ship in profusion,
indicating the attempts of an intelligent man trying to kill time. Also
you will find the initial findings on the energy collector we used in
conjunction with the light-shield.
"Now, yourself. Into your body we will inject the hormones that occur
with fear and worry. You will not enjoy a bit of atmosphobia, but
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com