(Download PDF) Matlab Programming For Engineers 6Th Edition Stephen J Chapman Full Chapter PDF
(Download PDF) Matlab Programming For Engineers 6Th Edition Stephen J Chapman Full Chapter PDF
(Download PDF) Matlab Programming For Engineers 6Th Edition Stephen J Chapman Full Chapter PDF
https://ebookmass.com/product/fortran-for-scientists-and-
engineers-4th-edition-stephen-j-chapman/
https://ebookmass.com/product/matlab-for-engineers-6th-edition-
holly-moore/
https://ebookmass.com/product/essential-matlab-for-engineers-and-
scientists-6th-edition-edition-hahn/
https://ebookmass.com/product/essential-matlab-for-engineers-and-
scientists-sixth-edition-edition-hahn/
Essential MATLAB for engineers and scientists 7th
Edition Edition Hahn
https://ebookmass.com/product/essential-matlab-for-engineers-and-
scientists-7th-edition-edition-hahn/
https://ebookmass.com/product/essential-matlab-for-engineers-and-
scientists-7th-edition-hahn-b-d/
https://ebookmass.com/product/essential-matlab-for-engineers-and-
scientists-brian-d-hahn-daniel-t-valentine/
https://ebookmass.com/product/ebook-pdf-essentials-of-matlab-
programming-3rd-edition/
https://ebookmass.com/product/applied-numerical-methods-with-
matlab-for-engineers-and-scientists-5th-edition-steven-c-chapra/
MATLAB®
Programming
for Engineers
MATLAB®
Programming
for Engineers
Sixth Edition
Stephen J. Chapman
BAE Systems Australia
Product Director, Global Engineering: ALL RIGHTS RESERVED. No part of this work covered by the
Timothy L. Anderson copyright herein may be reproduced or distributed in any form
Senior Product Assistant: Alexander or by any means, except as permitted by U.S. copyright law,
Cengage
20 Channel Center Street
Boston, MA 02210
USA
The most significant of these changes include the introduction of the App
Designer, which includes a whole new paradigm for creating MATLAB apps; a
new family of plotting functions; and strings. There have also been many smaller
improvements throughout the program. The book has been revised to reflect
these changes.
The major changes in this edition of the book include:
■■ An increase in the number of MATLAB applications featured in the chapters,
with more end-of-chapter exercises using them.
■■ More extensive coverage of plots in Chapter 3 and Chapter 8. The discussion
character arrays.
■■ Coverage of the time data types: dateTime, duration, and
calendarDuration.
■■ Coverage of table arrays.
■■ A completely rewritten Chapter 14 featuring the new App Designer and class-
based GUIs.
■■ An extra on-line Chapter 15 featuring the older GUIDE-based GUIs; this
Programming Pitfalls
Make sure that your variable names are unique in the first 31 characters. Otherwise,
MATLAB will not be able to tell the difference between them.
Pedagogical Features
The first eight chapters of this book are specifically designed to be used in a fresh-
man “Introduction to Program/Problem Solving” course. It should be possible to
cover this material comfortably in a 9-week, 3-hour-per-week course. If there is
insufficient time to cover all of the material in a particular Engineering program,
Chapter 8 may be omitted, and the remaining material will still teach the fundamen-
tals of programming and using MATLAB to solve problems. This feature should
appeal to harassed engineering educators trying to cram ever more material into a
finite curriculum.
The remaining chapters cover advanced material that will be useful to the
engineer and engineering students as they progress in their careers. This material
includes advanced I/O, object-oriented programming, and the design of GUIs for
programs.
The book includes several features designed to aid student comprehension. A
total of 20 quizzes appear scattered throughout the chapters, with answers to all
questions included in Appendix B. These quizzes can serve as a useful self-test of
comprehension. In addition, there are approximately 230 end-of-chapter exercises.
Answers to all exercises are included in the Instructor’s Solutions Manual. Good
programming practices are highlighted in all chapters with special Good Program-
ming Practice boxes, and common errors are highlighted in Programming Pitfalls
boxes. End-of-chapter materials include Summaries of Good Programming Practice
and Summaries of MATLAB Commands and Functions.
The book is accompanied by an Instructor’s Solutions Manual, which contains
the solutions to all end-of-chapter exercises. The source code for all examples in
xii | Preface
the book is available from the book’s website at https://login.cengage.com, and the
source code for all solutions in the Instructor’s Manual is available separately to
instructors.
Acknowledgments
I would like to thank all my friends at Cengage Learning for the support they have
given me in getting this book to market.
In addition, I would like to thank my wife Rosa, and our children Avi, David,
Rachel, Aaron, Sarah, Naomi, Shira, and Devorah for their help and encouragement.
Stephen J. Chapman
Melbourne, Australia
Digital Resources
xiii
xiv | Digital Resources
enter units, use a specific number of significant digits, use a specific number of
decimal places, respond with a computed answer, or answer within a different
tolerance value than the default.
Visit https://www.webassign.com/instructors/features/ to learn more. To create an account,
instructors can go directly to the signup page at http://www.webassign.net/signup.html.
MindTap Reader
Available via WebAssign and our digital subscription service, Cengage Unlimited,
MindTap Reader is Cengage’s next-generation eBook for engineering students.
The MindTap Reader provides more than just text learning for the student. It
offers a variety of tools to help our future engineers learn chapter concepts in a way
that resonates with their workflow and learning styles.
■■ Personalize their experience
Within the MindTap Reader, students can h ighlight key concepts, add notes, and
bookmark pages. These are collected in My Notes, ensuring they will have their own
study guide when it comes time to study for exams.
Digital Resources | xv
Index 807
1
2 | Chapter 1 Introduction to MATLAB
the scientist or engineer how to use MATLAB’s own tools to locate the right
function for a specific purpose from the enormous variety of choices available.
In addition, it teaches how to use MATLAB to solve many practical engineering
problems, such as vector and matrix algebra, curve fitting, differential equations,
and data plotting.
The MATLAB program is a combination of a procedural programming language, an
integrated development environment (IDE) that includes an editor and debugger, and
an extremely rich set of functions that perform many types of technical calculations.
The MATLAB language is a procedural programming language, meaning that the
engineer writes procedures, which are effectively mathematical recipes for solving a
problem. This makes MATLAB very similar to other procedural languages such as
C or Fortran. However, the extremely rich list of predefined functions and plot-
ting tools makes it superior to these other languages for many engineering analysis
applications.
In addition, the MATLAB language includes object-oriented extensions that
allow engineers to write object-oriented programs. These extensions are similar to
other object-oriented languages such as C++ or Java.
statistics associated with an input data set. In most languages, you would
need to write your own subroutines or functions to implement calculations
such as the arithmetic mean, standard deviation, median, and so forth. These
and hundreds of other functions are built right into the MATLAB language,
making your job much easier.
In addition to the large library of functions built into the basic MATLAB
language, there are many special-purpose toolboxes available to help solve
complex problems in specific areas. For example, you can buy standard tool-
boxes to solve problems in signal processing, control systems, communica-
tions, image processing, and neural networks, among many others. There is
also an extensive collection of free user-contributed MATLAB programs that
are shared through the MATLAB website.
4. Device-Independent Plotting
Unlike most other computer languages, MATLAB has many integral plot-
ting and imaging commands. The plots and images can be displayed on any
graphical output device supported by the computer on which MATLAB is
running. This capability makes MATLAB an outstanding tool for visualizing
technical data.
5. Graphical User Interface
MATLAB includes tools that allow an engineer to interactively construct a
graphical user interface (GUI) for his or her program, and also to produce
Web apps. With this capability, an engineer can design sophisticated data
analysis programs that can be operated by relatively inexperienced users.
6. MATLAB Compilers
MATLAB’s flexibility and platform independence is achieved by compiling
MATLAB programs into a device-independent p-code, and then interpreting
the p-code instructions at run-time. This approach is similar to that used by
Microsoft’s Visual Basic language or by Java. Unfortunately, the resulting
programs sometimes executed slowly because the MATLAB code is inter-
preted rather than compiled. Newer versions of MATLAB have partially
overcome this problem by introducing just-in-time (JIT) compiler technol-
ogy. The JIT compiler compiles portions of the MATLAB code as it is exe-
cuted to increase overall speed.
A separate MATLAB Coder is also available. The MATLAB Coder gen-
erates portable and readable C and C++ code from MATLAB code. This
converted code can then be compiled and included in programs written in
other languages. In addition, legacy code written in other languages can be
compiled and used within MATLAB.
Current Folder
This control allow
Browser
a user to view Launch the MATLAB
shows a list
or change the Help Browser Editor
of the files in the
current directory
current directory
Details Window
displays the Workspace Browser
MATLAB Command
properties of a file shows variables defined
Window
selected in the in workspace
Current Folder Browser
Figure 1.1 The default MATLAB desktop. The exact appearance of the
desktop may differ slightly on different types of computers.
The major tools within or accessible from the MATLAB desktop are:
■■ The Command Window
■■ The Toolstrip
■■ The Documents Window, including the Editor/Debugger and Array Editor
■■ Figure Windows
The functions of these tools are summarized in Table 1.1. We will discuss them in
later sections of this chapter.
Result is added
to the workspace
User input
Result of
calculation
Figure 1.2 The Command Window appears in the center of the desktop.
You enter commands and see responses here.
displayed in the Command Window as shown in Figure 1.2, and the variable can be
used in further calculations. (Note that p is predefined in MATLAB, so we can just
use pi without first declaring it to be 3.141592 … ).
If a statement is too long to type on a single line, it may be continued on succes-
sive lines by typing an ellipsis (...) at the end of the first line and then continuing
on the next line. For example, the following two statements are identical.
x1 = 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6
and
x1 = 1 + 1/2 + 1/3 + 1/4 ...
+ 1/5 + 1/6
Instead of typing commands directly in the Command Window, a series of com-
mands can be placed into a file, and the entire file can be executed by typing its
name in the Command Window. Such files are called script files. Script files (and
functions, which we will see later) are also known as M-files because they have a file
extension of “.m”.
Figure 1.3 The Toolstrip, which allows you to select from a wide variety
of MATLAB tools and commands.
“Home”, “Plots”, “Apps”, “Editor”, and so forth. When one of the tabs is selected,
a series of controls grouped into sections is displayed. In the Home tab, the sections
are “File”, “Variable”, “Code”, and so forth. With practice, the logical grouping of
commands helps the user to quickly locate any desired function.
In addition, the upper-right corner of the Toolstrip contains the Quick Access
Toolbar, which is where you can customize the interface and display the most com-
monly used commands and functions at all times. To customize the functions dis-
played there, right-click on the toolbar and select the Customize option from the
popup menu.
M-file with the “New Script” command from the “File” group on the Toolstrip
(Figure 1.5a), or by clicking the New icon and selecting Script from the popup
menu (Figure 1.5b). You can open an existing M-file file with the Open command
from the “File” section on the Toolstrip.
An Edit/Debug Window displaying a simple M-file called calc_area.m
is shown in Figure 1.5. This file calculates the area of a circle given its radius
and displays the result. By default, the Edit Window is docked to the desktop, as
shown in Figure 1.5c. The Edit Window can also be undocked from the MATLAB
desktop. In that case, it appears within a container called the Documents Window,
as shown in Figure 1.5d. We will learn how to dock and undock a window later in
this chapter.
The Edit Window is essentially a programming text editor, with the MATLAB
language’s features highlighted in different colors. Comments in an M-file file appear
in green, variables and numbers appear in black, complete character strings appear in
magenta, incomplete character strings appear in red, and language keywords appear
in blue.
After an M-file is saved, it may be executed by typing its name in the Command
Window. For the M-file in Figure 1.5, the results are:
» calc_area
The area of the circle is 19.635
The Edit Window also doubles as a debugger, as we shall see in Chapter 2.
10 | Chapter 1 Introduction to MATLAB
(a) (b)
(c)
1.3 The MATLAB Environment | 11
(d)
Figure 1.5 (a) Creating a new M-file with the “New Script” command. (b) Creating
a new M-file with the “New >> Script” selection from the Toolbar. (c) The MATLAB
Editor, docked to the MATLAB desktop. (See color insert.) (d) The MATLAB Editor,
displayed as an independent window.
(a)
(b)
Figure 1.7 (a) Selecting the “Undock” option from the menu displayed after clicking the
small down arrow in the upper-right corner of a pane. (b) Selecting the “Dock” option
after clicking the small down arrow in the upper-right corner of an undocked window.
script files executed from the Command Window) share a common workspace, so
they can all share variables. As we will see later, MATLAB functions differ from
script files in that each function has its own separate workspace.
A list of the variables and arrays in the current workspace can be generated with
the whos command. For example, after M-files calc_area and sin_x are exe-
cuted, the workspace contains the following variables.
» whos
Name Size Bytes Class Attributes
Script file calc_area created variables area, radius, and string, while
script file sin_x created variables x and y. Note that all of the variables are in the
same workspace, so if two script files are executed in succession, the second script
file can use variables created by the first script file.
The contents of any variable or array may be determined by typing the appro-
priate name in the Command Window. For example, the contents of string can be
found as follows:
» string
string =
The area of the circle is 19.635
A variable can be deleted from the workspace with the clear command. The
clear command takes the form
where var1 and var2 are the names of the variables to be deleted. The com-
mand clear variables or simply clear deletes all variables from the current
workspace.
Figure 1.8 The Workspace Browser and Array Editor. The Array Editor
is invoked by double-clicking a variable in the Workspace Browser. It allows
you to change the values contained in a variable or array.
Selecting the
current folder
Current Folder
Browser
“help inverse” will produce nothing. On the other hand, the command “look-
for inverse” will produce the following results:
» lookfor inverse
ifft - Inverse discrete Fourier transform.
ifft2 - Two-dimensional inverse discrete Fourier transform.
ifftn - N-dimensional inverse discrete Fourier transform.
ifftshift - Inverse FFT shift.
acos - Inverse cosine, result in radians.
acosd - Inverse cosine, result in degrees.
acosh - Inverse hyperbolic cosine.
acot - Inverse cotangent, result in radian.
acotd - Inverse cotangent, result in degrees.
acoth - Inverse hyperbolic cotangent.
Another random document with
no related content on Scribd:
than as something antecedent to the first progress towards
systematic knowledge.
15 Ἀριστοτλίζειν.
22 Sprengel, i. 136.
26 Ib. i. 207.
27 Ib. i. 211.
29 Ib. i. 239.
30 Ib. i. 241.
31 Ib. ib.
We shall see, in the next chapter, that when the intellect of Europe
began really to apply itself to the observation of nature, the progress
towards genuine science soon began to be visible, in this as in other
subjects; but before this tendency could operate freely, the history of
botany was destined to show, in another instance, how much more
grateful to man, even when roused to intelligence and activity, is the
study of tradition than the study of nature. When the scholars of
Europe had become acquainted with the genuine works of the
ancients in the original languages, the pleasure and admiration
which they felt, led them to the most zealous endeavors to illustrate
and apply what they read. They fell into the error of supposing that
the plants described by Theophrastus, Dioscorides, Pliny, must be
those which grew in their own fields. And thus Ruellius, 33 a French
physician, who only travelled in the environs of Paris and Picardy,
imagined that he found there the plants of Italy and Greece. The
originators of genuine botany in Germany, Brunfels and Tragus
(Bock), committed the same mistake; and hence arose the
misapplication of classical names to many genera. The labors of
many other learned men took the same direction, of treating the
ancient writers as if they alone were the sources of knowledge and
truth.
33 De Natura Stirpium, 1536.
But the philosophical spirit of Europe was already too vigorous to
allow this superstitious erudition to exercise a lasting sway.
Leonicenus, who taught at Ferrara till he was almost a hundred
years old, and died in 1524, 34 disputed, with great freedom, the
authority of the Arabian writers, and even of Pliny. He saw, and
showed by many examples, how little Pliny himself knew of nature,
and how many errors he had made or transmitted. The same
independence of thought with regard to other ancient writers, was
manifested by other scholars. Yet the power of ancient authority
melted away but gradually. Thus Antonius Brassavola, who
established on the banks of the Po the first botanical garden of
modern times, published in 1536, his Examen omnium Simplicium
Medicamentorum; and, as Cuvier says, 35 though he studied plants in
nature, his book (written in the 369 Platonic form of dialogue), has
still the character of a commentary on the ancients.
34 Sprengel, i. 252.
42 Cuvier, p. 198.
43 Dedicatio, a 2.
After certain preparatory speculations, he says, 44 “Let us now
endeavor to mark the kinds of plants by essential circumstances in
the fructification.” He then observes, “In the constitution of organs
three things are mainly important—the number, the position, the
figure.” And he then proceeds to exemplify this: “Some have under
one flower, one seed, as Amygdala, or one seed-receptacle, as
Rosa; or two seeds, as Ferularia, or two seed-receptacles, as
Nasturtium; or three, as the Tithymalum kind have three seeds, the
Bulbaceæ three receptacles; or four, as Marrubium, four seeds,
Siler four receptacles; or more, as Cicoraceæ, and Acanaceæ have
more seeds, Pinus, more receptacles.”
44 Lib. i. c. 13, 14.