Preface 2019 Matlab
Preface 2019 Matlab
MOTIVATION
The purpose of this book is to teach basic programming concepts and skills
needed for basic problem solving, all using MATLAB® 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.
There are many books that introduce MATLAB. There are two basic flavors of
these books: 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 under-
stand basic programming 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 programming concepts first (using any language)
would tend to write highly inefficient code using control statements to solve
problems, not realizing that in many cases these are not necessary in MATLAB.
Instead, this book takes a hybrid approach, introducing both the programming
and the 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 on
or whether a software package such as MATLAB will suffice for their careers.
Therefore, the best approach for beginners is to give them both: the program-
ming concepts and the efficient built-in functions. Since MATLAB is very easy to
use, it is a perfect platform for this approach of teaching programming and
problem solving.
As programming concepts are critically important to this book, emphasis is not
placed on the time-saving features that evolve with every new MATLAB release.
xi
xii Preface
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
approach, 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
Preface xiii
says “we’ll do this later”. This book does not do that; functions are covered
before they are used in examples. Additionally, basic programming concepts
will be explained carefully and systematically. Very basic concepts such as loop-
ing to calculate a sum, counting in a conditional loop, and error-checking are
not found in many texts, but are covered here.
Data Transfer
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, also some of the lower-level file
input/output functions. As 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 introduced
gradually: first load and save in Chapter 3, then lower-level functions in
Chapter 9, and finally sound and images in Chapter 13. A brief introduction to
RESTFUL web functions, which import data from websites is given in Chapter 9.
User-Defined Functions
User-defined functions are a very important programming concept, and yet,
many times the nuances and differences between concepts such as types of
functions and function calls versus function headers can be very confusing
to beginning programmers. Therefore, these concepts are introduced gradually.
First, arguably the easiest types of functions to understand, those that calculate
and return one single value, are demonstrated in Chapter 3. 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.
xiv Preface
The use of complex numbers and some calculus (integration and differentia-
tion) will also be introduced. The built-in functions in MATLAB to perform
these tasks will be described.
Vectorized Code
Efficient uses of the capabilities of the built-in operators and functions in
MATLAB are demonstrated throughout the book. In order to emphasize the
importance of using MATLAB efficiently, the concepts and built-in functions
necessary for writing vectorized code are treated very early in Chapter 2. Tech-
niques such as preallocating vectors and using logical vectors are then covered
in Chapter 5 as alternatives to selection statements and looping through vectors
and matrices. Methods of determining how efficient the code is are also covered.
Object-Oriented Programming
Creating objects and classes in MATLAB has been an option for some time, but
as of R2014b, all Graphics objects are truly objects. Thus, object-oriented
programming (OOP) is now a very important part of MATLAB programming.
Applications using App Designer reinforce the concepts.
LAYOUT OF TEXT
This text is divided into 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 techniques to solve
systems of linear algebraic equations, fit curves to data, and perform basic sta-
tistical 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
Preface xv
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.
Chapter 11: Introduction to Object-Oriented Programming and Graphics As
of version R2014b, all plot objects are actual objects. This chapter introduces
Object-Oriented Programming (OOP) concepts and terminology using plot
objects and then expands to how to write your own class definitions and create
your own objects.
Chapter 12: Advanced Plotting Techniques continues with more on the plot
functions introduced in Chapter 3. Different two-dimensional plot types, such
as logarithmic scale plots, pie charts, and histograms are introduced, as is cus-
tomizing plots using cell arrays and string functions. Three-dimensional plot
functions as well as some functions that create the coordinates for specified
objects are demonstrated. The notion of Graphics is covered, and some
graphics properties such as line width and color are introduced. Core graphics
objects and their use by higher-level plotting functions are demonstrated.
Applications that involve reading data from files and then plotting use both cell
arrays and string functions.
Chapter 13: Sights and Sounds briefly discusses sound files and introduces
image processing. An introduction to programming Graphical User Interfaces
(GUIs) is also given, including the creation of a button group and embedding
images in a GUI. Nested functions are used in the GUI examples. The App
Designer is introduced; it creates OOP code and builds on the concepts from
Chapter 11.
Chapter 14: Advanced Mathematics covers seven basic topics: it starts with
some of the built-in statistical and set operations in MATLAB, then curve fitting,
complex numbers, solving systems of linear algebraic equations, and integra-
tion and differentiation in calculus. Finally, 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.
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. The 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, they 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 drill the material just covered.
“Explore Other Interesting Features” This book is not intended to be a complete
reference book and cannot possibly cover all of the built-in functions and tools
available in MATLAB; however, in every chapter, there will be a list of functions
and/or commands that are related to the chapter topics, which readers may
wish to investigate.
Preface xix
When some problems are introduced, they are solved both using “The Program-
ming Concept” and also “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” highlights methods that will save
time for the programmer, and in many cases, are also faster to execute in
MATLAB.
Additionally, to aid the reader:
n Identifier names are shown in italic
n MATLAB function names are shown in bold
n Reserved words are shown in bold and underlined
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: In order to encourage “good” programs
that others can actually understand, the programming chapters will have
guidelines that will make programs easier to read and understand and
therefore easier to work with and modify.
n Key Terms: a list of the key terms covered in the chapter, in sequence.
n MATLAB Reserved Words: a list of the reserved keywords in MATLAB.
Throughout the text, these are shown in bold, underlined type.
n MATLAB Functions and Commands: a list of the MATLAB built-in
functions and commands covered in the chapter, in the order covered.
Throughout the text, these are shown in bold type.
n MATLAB Operators: a list of the MATLAB operators covered in the
chapter, in the order covered.
n Exercises: a comprehensive set of exercises, ranging from the rote to more
engaging applications.