Professional Documents
Culture Documents
Essentials Of Matlab Programming 3Rd Edition full chapter pdf docx
Essentials Of Matlab Programming 3Rd Edition full chapter pdf docx
https://ebookmass.com/product/essentials-of-torts-3rd-edition-
ebook-pdf-version/
https://ebookmass.com/product/matlab-programming-for-
engineers-6th-edition-stephen-j-chapman/
https://ebookmass.com/product/corrections-the-essentials-3rd-
edition-ebook-pdf/
https://ebookmass.com/product/ebook-pdf-essentials-of-
sociology-3rd-edition-by-george-ritzer/
The ASAM Essentials of Addiction Medicine 3rd Edition,
(Ebook PDF)
https://ebookmass.com/product/the-asam-essentials-of-addiction-
medicine-3rd-edition-ebook-pdf/
https://ebookmass.com/product/essentials-of-public-health-
essential-public-health-3rd-edition-ebook-pdf/
https://ebookmass.com/product/aacn-essentials-of-progressive-
care-nursing-third-edition-chulay-aacn-essentials-of-progressive-
care-nursing-3rd-edition-ebook-pdf/
https://ebookmass.com/product/matlab-a-practical-introduction-to-
programming-and-problem-solving-4th-edition-stormy-attaway/
https://ebookmass.com/product/the-essentials-of-risk-
management-3rd-edition-michel-crouhy/
ESSENTIALS OF ESSENTIALS OF
CHAPMAN
MATLAB
PROGRAMMING
MATLAB
PROGRAMMING
ESSENTIALS OF
STEPHEN J. CHAPMAN STEPHEN J. CHAPMAN
THIRD EDITION
MATLAB
PROGRAMMING
To register or access your online learning solution or purchase materials
for your course, visit www.cengagebrain.com.
3E
THIRD EDITION
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
Preface | vii
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
viii | Preface
4. Device-independent plotting.
Unlike 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 a programmer to interactively con-
struct a graphical user interface (GUI) for his or her program. With
this capability, the programmer can design sophisticated data analysis
programs that can be operated by relatively inexperienced users.
Pedagogical Features
This book is specifically designed to be used in a first-year “Introduction to
Programming/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, Chapters 8 and 9
may be deleted, and the remaining material will still teach the fundamentals of
programming and using MATLAB to solve problems. This feature should ap-
peal to harassed engineering educators trying to cram ever more material into a
finite curriculum.
The book includes several features designed to aid student comprehension.
A total of 14 quizzes appear scattered throughout the chapters, with answers
to all questions included in Appendix C. These quizzes can serve as a useful
self-test of comprehension. In addition, there are approximately 150 end-of-
chapter exercises. Answers to all exercises are included in the Instructor’s Man-
ual. Good programming practices are highlighted in all chapters with special
Good Programming Practice boxes, and common errors are highlighted in Pro-
gramming Pitfalls boxes. End-of-chapter materials include Summaries of Good
Programming Practice and Summaries of MATLAB Commands and Functions.
Instructor Resources
A detailed Instructor’s Solutions Manual containing solutions to all end-of-
chapter exercises is available via the secure, password-protected Instructor
Resource Center at https://sso.cengage.com. The Instructor Resource Center
also contains helpful Lecture Note PowerPoint slides, the MATLAB source code
for all examples in the book, and the source code for all of the solutions in the
Instructor’s Solutions Manual.
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
Preface | ix
Acknowledgments
I would like to thank these reviewers who offered their helpful suggestion for
this edition:
David Eromom Georgia Southern University
Arlene Guest Naval Postgraduate School
Mary M. Hofle Idaho State University
Mark Hutchenreuther California Polytechnic State
University
Mani Mini Iowa State University
In addition I would like to acknowledge and thank my Global Engineering team
at Cengage Learning for their dedication to this edition:
Timothy Anderson, Product Director; Mona Zeftel, Senior Content Developer;
D. Jean Buttrom, Content Project Manager; Kristin Stine, Marketing Manager;
Elizabeth Murphy and Brittany Burden, Learning Solutions Specialists; Ashley
Kaupert, Associate Media Content Developer; Teresa Versaggi and Alexander
Sham, Product Assistants; and Rose Kernan of RPK Editorial Services, Inc.
They have skillfully guided every aspect of this text’s development and produc-
tion to successful completion.
In addition, I would like to thank my wife Rosa for her help and encourage-
ment over the more than 40 years we have spent together.
Stephen J. Chapman
Melbourne, Australia
November 8, 2015
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
MindTap Online Course
Index 479
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
Chapter 1
Introduction to MATLAB
1
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
2 | Chapter 1 Introduction to MATLAB
the scientist or engineer how to use MATLAB’s own tools to locate the right func-
tion for a specific purpose from the enormous list 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) including an editor and debugger, and
an extremely rich set of functions to 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,
Basic, Fortran, and Pascal. However, the extremely rich list of predefined functions
and plotting tools makes it superior to these other languages for many engineering
analysis applications.
The second disadvantage is cost: a full copy of MATLAB is five to ten times
more expensive than a conventional C or Fortran compiler. This relatively high
cost is more than offset by the reduced time required for an engineer or scientist
to create a working program, so MATLAB is cost-effective for businesses. How-
ever, it is too expensive for most individuals to consider purchasing. Fortunately,
there is also an inexpensive student edition of MATLAB, which is a great tool
for students wishing to learn the language. The student edition of MATLAB is
essentially identical to the full edition.1
1
There are also some free software programs that are largely compatible with MATLAB, such as
GNU Octave and FreeMat.
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
1.3 The MATLAB Environment | 5
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:
■ Command Window
■ Toolstrip
■ Documents Window, including the Editor/Debugger and Array Editor
■ Figure Windows
■ Workspace Browser
■ Current Folder Browser, with the Details Window
■ Help Browser
■ Path Browser
■ Popup Command History Window
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
6 | Chapter 1 Introduction to MATLAB
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.
Users enter commands and see responses here.
Figure 1.3 The Toolstrip, which allows a user to select from a wide variety of
MATLAB tools and commands.
Figure 1.3 are 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-hand corner of the Toolstrip contains the Quick
Access Toolbar, which is a place where the user can customize the interface and
display the most commonly used commands and functions at all times. To cus-
tomize the functions displayed there, right-click on the toolbar and select the
Customize option from the popup menu.
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 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. On screen, 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. [See color insert.]
After an M-file is saved, it may be executed by typing its name in the Com-
mand 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.
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
10 | Chapter 1 Introduction to MATLAB
(b)
(a)
(c)
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
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 popup menu. (c) The MATLAB
Editor, docked to the MATLAB desktop. (d) The MATLAB Editor, displayed as an
independent window. [See color insert.]
Figure 1.6 MA
MATLAB plot of sin x versus x.
Figure 1.7 Select the Undock option from the menu displayed after clicking the
small down arrow in the upper-right corner of a pane.
Window (and all script files executed from the Command Window) share a com-
mon workspace, so they can all share variables. As we will see later, MATLAB func-
tions 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 executed, the workspace contains the following variables.
» whos
Name Size Bytes Class Attributes
area 1x1 8 double
radius 1x1 8 double
string 1x32 64 char
x 1x61 488 double
y 1x61 488 double
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 ap-
propriate 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
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
14 | Chapter 1 Introduction to MATLAB
A variable can be deleted from the workspace with the clear command.
The clear command takes the form
clear var1 var2 ...
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 a user to change
the values contained in a variable or array.
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203
1.3 The MATLAB Environment | 15
Selecting the
current folder
Current Folder
Browser
Het is eene zeer belangrijke en, naar mijne meening, ook zeer
aangename studie, die Fransche plaatsnamen van vreemden,
onfranschen, onromaanschen oorsprong na te sporen en in hunne
oorspronkelijke beteekenis en vorm te leeren kennen. Maar het
zoude ons veel te verre afleiden en wegvoeren, wilden wij deze
stoffe in het algemeen hier nader ontvouwen, al ware ’t dan ook
maar zeer beknoptelijk en oppervlakkig. In bescheidenheid willen wij
ons bepalen tot de Germaansche namen die in het Noord-Westen
van Frankrijk voorkomen. Maar ook dan nog kunnen wij niet anders
als in zeer algemeenen zin deze zaak behandelen, en slechts
weinige namen uit de honderdtallen die daar zijn, slechts eenigen
van de bijzondersten en opmerkelijksten hier den Lezer voorstellen.
Moge dit sommigen tot eenen spoorslag strekken om aan dezen tak
der aardrijks-, taal- en volkenkundige wetenschap in het bijzonder
hunne aandacht te wijden. Vooral voor de Vlamingen, Brabanders en
Limburgers, voor alle Dietsche Belgen, goede en echte Germanen
als zij van oorsprongs wegen zijn, en in hunne neigingen en
gevoelens moeten wezen, vooral voor dezen is het zeer nuttig te
zien hoe diep het Germanendom in de Noordwestelijke gouwen van
Frankrijk is doorgedrongen.
Natuurlijk zijn daar in het Fransch, gelijk het heden ten dage in
Artesië gesproken wordt, nog talrijke sporen van het Vlaamsch
overgebleven. Maar vooral de plaatsnamen in dat gewest zijn nog
heden ten dage de levende getuigen van het Vlaamsche, het
Dietsche, Germaansche wezen des volks, dat deze namen eens
gegeven heeft, van den Germaanschen taaltak, waaruit zij
voortgekomen zijn. Zie hier eenigen van die namen, die echter in
hunne hedendaagsche geijkte schrijfwijze min of meer de teekenen
vertoonen van den Franschen invloed, waar aan zij onderworpen
zijn.
1. R i c m a n i n g h e n , A u d i n g h e n , H a r d i n g h e n ,
Maninghen, Bazinghen, Hervelinghen,
Ta r d i n g h e n , W a c q u i n g h e n , L e u b r i n g h e n .
2. B o n n i n g u e s , P e u p l i n g u e , B e s s i n g u e . [97]
3. L o t t i n g h e m , T r e l i n g h e m , H e r b i n g h e m ,
H o c q u i n g h e m , B e r t i n g h e m , Ta t i n g h e m ,
Ruminghem, Elinghem, Spanghem.
Het zoude zekerlijk te veel gevergd zijn van het geduld des
vriendelijken lezers van dit opstel, en ongetwijfeld te veel ruimte
innemen van de plaats die hier beschikbaar is, indien ik alle
bovengenoemde patronymicale plaatsnamen van Artesië hier nader
wilde ontleden. Slechts van enkelen zij mij dit vergund.
Als een bewijs hoe zeer de oude beteekenis van het woordeken tun
in Noord-Nederland verloren gegaan is, moge ’t volgende dienen.
Daar is een overoud volksliedje, dat in de middeleeuwen in alle
Nederlandsche gewesten, in alle Nederduitsche landen en gouwen,
van Duinkerke en nog verder westelijk, tot Hamburg en nog verder
oostelijk bekend en in veelvuldig gebruik was. Dat begint alzoo:
Heden ten dage is dat liedeken bijna volkomen vergeten en uit der
lieden harte en mond verdwenen. Slechts in weinige plaatsen van
het bovengenoemde groote taalgebied kent het volk het nog. Eene
van die plaatsen is de stad Bolsward in Friesland. Maar, het woord
tuun, waar een vogel op zoude kunnen zitten, niet meer verstaande,
zoo heeft men dit woord veranderd, en men zingt daar nu: