(Ebook) MATLAB Programming for Engineers by Stephen J. Chapman ISBN 9780357030394, 0357030397 2024 scribd download
(Ebook) MATLAB Programming for Engineers by Stephen J. Chapman ISBN 9780357030394, 0357030397 2024 scribd download
com
https://ebooknice.com/product/matlab-programming-for-
engineers-34563734
OR CLICK HERE
DOWLOAD EBOOK
https://ebooknice.com/product/matlab-programming-for-engineers-1901944
ebooknice.com
https://ebooknice.com/product/matlab-programming-with-applications-
for-engineers-4705226
ebooknice.com
https://ebooknice.com/product/fortran-for-scientists-and-
engineers-5759150
ebooknice.com
ebooknice.com
(Ebook) MATLAB ■■ : ■■■ / MATLAB bian cheng by S. J.
Chapman ISBN 9787030111395, 7030111397
https://ebooknice.com/product/matlab-matlab-bian-cheng-6818770
ebooknice.com
ebooknice.com
https://ebooknice.com/product/matlab-for-engineers-55471902
ebooknice.com
https://ebooknice.com/product/matlab-for-engineers-33941214
ebooknice.com
ebooknice.com
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.
Discovering Diverse Content Through
Random Scribd Documents
Shelmore istui tuijottaen vuorotellen kuvaan ja vieraaseensa. Hän
kääntyi äkkiä Palsfordin puoleen ja kysyi tiukasti:
"Ehkä sen vuoksi", sanoi hän hiljaisella äänellä, "ehkä sen vuoksi,
että hän tiesi toisen James Arradeanen vielä elävän ja uhkasi estää
sen, mistä olisi tullut kaksinnaiminen. Se siitä, Shelmore. Mutta
kuulkaahan nyt — tämä juttu alkaa näyttää paljon vakavammalta
kuin osasin aavistaakaan. Päättäen siitä, mitä olen teille kertonut ja
te minulle, pitää tätä asiaa tutkia perinpohjin. Mutta ensin minun
pitää saada ehdoton ja nimenomainen varmuus siitä, etten ole
erehtynyt tuosta naisesta. Olen kyllä siinä varmassa uskossa, että
teidän rouva Champernownenne on juuri sama rouva Arradeane,
joka poistui Normansholtista noin parikymmentä vuotta sitten, mutta
jos saisin nähdä hänet…"
"Se käy päinsä viidessä minuutissa", keskeytti Shelmore katsoen
kelloaan. "Kaupungin valtuustolla on juuri istuntonsa tässä
läheisyydessä. Tulkaa mukaani, niin poikkeamme sinne."
Bartlettin takkamatto
"No niin, minulla ei ole mitään sitä vastaan, kiitoksia vain", vastasi
Mellapont. Hän tuli lähemmäksi emännän poistuttua huoneesta ja
katsahti Bartlettiin merkitsevästi. "Hyvä on, poikaseni!" mutisi hän.
"Haluan hieman jutella kanssanne, siinä kaikki. Teillä on mukavat
olot täällä ja niin kodikkaat huoneetkin, vai mitä?" jatkoi hän
emännän palattua pari lasia ja karahvi soodavettä mukanaan. "Te
olette täällä epäilemättä kuin kotonanne?"
"Herra Mellapont", sanoi hän äkkiä, "jos kerron teille kaikki tietoni,
saanko luottaa vaikenemiseenne?"
"Saatte niin paljon kuin oikeus myöntää", vastasi Mellapont.
"Varmasti!"
"Sen kyllä voisin tehdä", sanoi Mellapont. "Minä suon neiti Prettyn
rahat teille yhtä mielelläni kuin jollekin toiselle. Hän on jossakin
määrin hölmö, minun mielestäni. No, Bartlett, näittekö hänet ja
puhuttelitteko häntä?"
Pahassa pinteessä
Bartlett näytti olevan sitä mieltä, että John Hackdale toimi vain
välittäjänä tai apurina hänen ja rouva Champernownen kesken.
Mutta Mellapont ei ollut siitä lainkaan varma. Hänestä näytti siltä
kuin hän pätevistä syistä voisi epäillä rouva Champernownen ja John
Hackdalen jakavan keskenään synkemmän salaisuuden. Hän tiesi
erään seikan, joka tunkeutui esille yhä uudestaan. Ashenhurst-
huvila, sen läheiset kujat ja hylätty, pensaitten valtaama
hiekkakuoppa kuuluivat kaikki siihen piiriin, jota Hackdale oli
ylimääräisenä poliisina vartioinut. Voiko mikään olla uskottavampaa
kuin että hän tullessaan sinne murhailtana sattui salaa näkemään
rikoksen toimeenpanon ja tunsi rouva Champernownen, jolla
varmasti oli revolveri ja joka Mellapontin mielestä oli juuri sellainen
nainen, että hän voi sitä käyttää tehokkaasti? Niinpä saattoi ja
kieltämättä olikin John Hackdalen etujen mukaista suojella sitä
naista, josta hänen toimeentulonsa riippui ja joka voi edistää hänen
etujaan.
Kuta enemmän Mellapont tätä ajatteli, sitä varmempi hän oli siitä,
että hän nyt oli oikeilla jäljillä — että rouva Champernowne murhasi
Deanen, itsekkäistä syistä, ja että John Hackdale sen tiesi. Ja hän oli
näkevinään kuinka se oli tapahtunut. Rouva oli ehkä tarkastellut
aluettaan ja ulkohuoneitaan revolveri taskussaan (hän oli näet
saanut selville eräältä apulaiseltaan, että rouva Champernowne
pyytäessään revolverin kantolupaa oli ilmoittanut sen johtuvan siitä,
että hänen talonsa oli niin yksinäisellä paikalla ja kaikenlaisten
kulkurien oli tapana tunkeutua hänen vajoihinsa ja säilytyssuojiinsa)
ja tällöin tavannut Deanen. Hän oli hyvinkin helposti viekoitellut
Deanen hiekkakuoppaan ja ampunut hänet siellä, luottaen siihen,
että epäluulot kohdistuisivat kulkureita vastaan, joita aina jäi jäljelle
kaupunkiin markkinapäivinä. Ja John Hackdale oli ehkä ollut
läheisyydessä ja kuultuaan laukauksen mennyt paikalle, mistä ääni
oli kuulunut, ja tavannut siellä rouva Champernownen. Tämä kaikki
oli mahdollista, ja Mellapontin mielestä John Hackdale oli juuri
sellainen mies, joka hyvinkin voi liittyä salaliittoon vaietakseen
asiasta.
Oli kuinka tahansa, saatuaan nämä uudet tiedot hänen piti mennä
tapaamaan ja puhuttelemaan John Hackdalea ja rouva
Champernownea. Hackdalea kuitenkin ensin, ehdottomasti kahden
kesken, koska oli kysymyksiä — hyvin tärkeitä kysymyksiä — joihin
hänen oli heti saatava vastaukset. Sitten vasta nainen. Rouva
Champernownen täytyi alistua kuulusteltavaksi, vaikka olikin
pormestari, hieno ja rikas.
"Se ei olekaan siellä!" sanoi hän. "Mutta minä olen ihan varma
siitä, että pistin sen sinne, minä kun säilytän sitä aina täällä. Kuka…"
"Teidän olisi parasta ajatella asiaa sen valossa, mitä teille kerroin",
sanoi hän vakavasti. "Ehkä huomaatte olevan muutakin minulle
ilmaistavaa, ja jos niin käy, tiedätte kyllä, mistä minut löydätte."
"Minä sanoin hänelle, että hän ilmoittaisi vain minut", selitti hän.
"Herra Palsford, seisokaa tässä juuri ovea vastapäätä. Minä haluan,
että rouva Champernowne näkisi teidät ensin…"
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.
ebooknice.com