Preface 2012 Matlab
Preface 2012 Matlab
Motivation
The purpose of this book is to teach fundamentals of programming concepts and
skills needed for basic problem solving, all using MATLABW as the vehicle.
MATLAB is a powerful software package that has built-in functions to accomplish
a diverse range of tasks, from mathematical operations to three-dimensional
imaging. Additionally, MATLAB has a complete set of programming constructs
that allows users to customize programs to their own specifications.
The many books that introduce MATLAB come in two basic flavors: those that
demonstrate the use of the built-in functions in MATLAB, with a chapter or two
on some programming concepts; and those that cover only the programming
constructs without mentioning many of the built-in functions that make
MATLAB efficient to use. Someone who learns just the built-in functions will
be well-prepared to use MATLAB, but would not understand basic program-
ming concepts. That person would not be able to then learn a language such
as Cþþ or Java without taking another introductory course or reading another
book on the programming concepts. Conversely, anyone who learns only pro-
gramming concepts first (using any language) would tend to write highly inef-
ficient code using control statements to solve problems, not realizing that in
many cases these are not necessary in MATLAB.
This book instead takes a hybrid approach, introducing both the programming
and efficient uses. The challenge for students is that it is nearly impossible to
predict whether they will in fact need to know programming concepts later
or whether a software package such as MATLAB will suffice for their careers.
Therefore, the best approach for beginning students is to give them both: the
programming concepts and the efficient built-in functions. Since MATLAB is
very easy to use, it is a perfect platform for this approach to teaching program-
ming and problem solving.
Since programming concepts are critically important to this book, emphasis is
not placed on the time-saving features that evolve with every new MATLAB re-
lease. For example, in current versions of MATLAB, statistics on variables are xi
xii Preface
available readily in the Workspace Window. This is not shown in any detail in
the book, since whether this feature is available depends on the software ver-
sion, and because of the desire to explain the concepts in the book.
Key Features
Side-by-Side Programming Concepts and Built-in Functions
The most important, and unique, feature of this book is that it teaches program-
ming concepts and the use of the built-in functions in MATLAB side by side. It
starts with basic programming concepts such as variables, assignments, input/
output, selection, and loop statements. Then throughout the rest of the book,
many times a problem will be introduced and then solved using the “program-
ming concept” and also using the “efficient method.” This will not be done in
every case to the point that it becomes tedious, but just enough to get the ideas
across.
Systematic Approach
Another key feature is that the book takes a very systematic, step-by-step ap-
proach, building on concepts throughout the book. It is very tempting in a
MATLAB text to show built-in functions or features early on with a note that
says “we’ll do this later.” This does not happen in this edition; all functions
are covered before they are used in examples. Additionally, basic programming
Preface xiii
concepts will be explained carefully and systematically. Very basic concepts, such
as looping to calculate a sum, counting in a conditional loop, and error-checking,
are not found in many texts but will be covered here.
File Input/Output
Many applications in engineering and the sciences involve manipulating large
data sets that are stored in external files. Most MATLAB texts at least mention the
save and load functions, and in some cases selected lower-level file input/out-
put functions as well. Since file input and output is so fundamental to so many
applications, this book will cover several low-level file input/output functions,
as well as reading from and writing to spreadsheet files. Later chapters will also
deal with audio and image files. These file input/output concepts are intro-
duced gradually: first load and save in Chapter 2, then lower-level functions
in Chapter 9, and finally sound and images in Chapter 14.
User-Defined Functions
User-defined functions are a very important programming concept, and yet
many times the nuances and differences among concepts such as types of func-
tions and function calls versus function headers can be very confusing to begin-
ning programmers. Therefore, these concepts are introduced gradually. First,
functions that calculate and return one single value—arguably the easiest type
of functions to understand—are demonstrated in Chapter 2. Later, functions
that return no values and functions that return multiple values are introduced
in Chapter 6. Finally, advanced function features are shown in Chapter 10.
Problem-Solving Tools
In addition to the programming concepts, some basic mathematics necessary
for solving many problems will be introduced. These will include statistical
functions, solving sets of linear algebraic equations, and fitting curves to data.
The use of complex numbers and some calculus (integration and differentia-
tion) will also be introduced. The basic math will be explained and the
built-in functions in MATLAB to perform these tasks will be described.
xiv Preface
Vectorized Code
Efficient uses of the capabilities of the built-in operators and functions in
MATLAB are demonstrated throughout the book. However, to emphasize the im-
portance of using MATLAB efficiently, vectorized code is treated in a separate
chapter. Techniques, such as preallocating vectors and using logical vectors,
are featured, as well as methods of determining how efficient the code is.
Layout of Text
The book consists of two parts. The first part covers programming constructs
and demonstrates the programming method versus efficient use of built-in
functions to solve problems. The second part covers tools that are used for basic
problem solving, including plotting, image processing, and mathematical tech-
niques to solve systems of linear algebraic equations, fit curves to data, and per-
form basic statistical analyses. The first six chapters cover the very basics in
MATLAB and in programming, and are all prerequisites for the rest of the book.
After that, many chapters in the problem-solving section can be introduced
when desired, to produce a customized flow of topics in the book. This is true
to an extent, although the order of the chapters has been chosen carefully to
ensure that the coverage is systematic.
The individual chapters are described here, as well as which topics are required
for each chapter. Part I, Introduction to Programming Using MATLAB, includes
the following chapters.
Chapter 1: Introduction to MATLAB covers expressions, operators, char-
acters, variables, and assignment statements. Scalars, vectors, and matrices
are all introduced as are many built-in functions that manipulate them.
Chapter 2: Introduction to MATLAB Programming introduces the idea of
algorithms and scripts. This includes simple input and output, and com-
menting. Scripts are then used to create and customize simple plots, and to
do file input and output. Finally, the concept of a user-defined function is
introduced with only the type of function that calculates and returns a
single value.
Preface xv
files and also .mat files that store MATLAB variables are introduced. Cell
arrays and string functions are used extensively in this chapter.
Chapter 10: Advanced Functions covers more advanced features of and
types of functions such as anonymous functions, nested functions, and
recursive functions. Function handles and their use both with anonymous
functions and function functions are introduced. The concept of having a
variable number of input and/or output arguments to a function is intro-
duced; this is implemented using cell arrays. String functions are also used
in several examples in this chapter. The section on recursive functions is at
the end and may be omitted.
Part II, Advanced Topics for Problem Solving with MATLAB, contains the fol-
lowing chapters.
Chapter 11: Advanced Plotting Techniques continues with more on the
plot functions introduced in Chapter 2. Various two-dimensional plot
types, such as pie charts and histograms, are introduced, as is customizing
plots using cell arrays and string functions. Three-dimensional plot func-
tions as well as selected functions that create the coordinates for specified
objects are demonstrated. The notion of Handle GraphicsW is covered, and
selected graphics properties, such as line width and color, are introduced.
Applications that involve reading data from files and then plotting use both
cell arrays and string functions.
Chapter 12: Matrix Representation of Linear Algebraic Equations intro-
duces a basic method that can be used in MATLAB to solve systems of equa-
tions using a matrix representation. First, matrix and vector operations and
matrix definitions are described. This section can be covered at any point after
Chapter 5. Then, matrix solutions using the Gauss-Jordan and Gauss-Jordan
elimination methods are described. This section includes mathematical
techniques and also the MATLAB functions that implement them.
Chapter 13: Basic Statistics, Sets, Sorting, and Indexing starts with some
of the built-in statistical and set operations in MATLAB. Since some of these
require a sorted data set, methods of sorting are described. Finally, the
concepts of indexing into a vector and searching a vector are introduced.
Sorting a vector of structures and indexing into a vector of structures are
described, but these sections can be omitted. A recursive binary search
function is in the end and may be omitted.
Chapter 14: Sights and Sounds briefly discusses sound files and introduces
image processing. An introduction to programming graphical user interfaces
is also given, including the creation of a button group. Nested functions are
used in the GUI examples. A patch function example uses a structure.
Chapter 15: Advanced Mathematics covers three basic topics: curve fitting,
complex numbers, and integration and differentiation in calculus. Finally,
Preface xvii
some of the Symbolic Math Toolbox functions are shown, including those
that solve equations. This method returns a structure as a result.
Pedagogical Features
There are several pedagogical tools that are used throughout this book that are
intended to make it easier to learn the material. A list of Key Terms covered in
each chapter, in sequence, is on the first page.
First, the book takes a conversational tone with sections called Quick Ques-
tion! These are designed to stimulate thought about the material that has just
been covered. A question is posed, and then the answer is given. It will be most
beneficial to the reader to try to think about the question before reading the
answer! In any case, these sections should not be skipped over as the answers
often contain very useful information.
Practice problems are given throughout the chapters. These are very simple
problems that serve as drills of the material just covered.
When certain problems are introduced, they are solved both using The Pro-
gramming Concept and The Efficient Method. This facilitates understanding
the built-in functions and operators in MATLAB as well as the underlying pro-
gramming concepts. The Efficient Method boxes highlight methods that will
save time for the programmer, and in many cases are faster to execute in
MATLAB, as well.
Additionally, to aid the reader:
n Identifier names (variables and user-defined functions) are shown in italics
(as are filenames and file extensions).
n MATLAB function names are shown in bold.
n Reserved words are shown in bold and underline.
n Key important terms are shown in bold and italic.
The end-of-chapter summary contains, where applicable, several sections:
n Common Pitfalls: A list of common mistakes that are made, and how to
avoid them.
n Programming Style Guidelines: To encourage the creation of “good”
programs that others can actually understand, the programming chapters
have guidelines that make programs easier to read and understand, and
therefore easier to work with and to modify.
n MATLAB Reserved Words: A list of the reserved key words in MATLAB.
Throughout the text, these are shown in bold, underlined type.
xviii Preface