Immediate download Matlab a practical introduction to programming and problem solving 3e edition Edition Attaway ebooks 2024
Immediate download Matlab a practical introduction to programming and problem solving 3e edition Edition Attaway ebooks 2024
com
https://ebookname.com/product/matlab-a-practical-
introduction-to-programming-and-problem-solving-3e-edition-
edition-attaway/
OR CLICK BUTTON
DOWNLOAD EBOOK
https://ebookname.com/product/java-an-introduction-to-problem-solving-
and-programming-6th-edition-walter-savitch/
ebookname.com
https://ebookname.com/product/programming-problem-solving-abstraction-
with-c-moffat/
ebookname.com
https://ebookname.com/product/problem-solving-and-programming-
concepts-9th-edition-maureen-sprankle/
ebookname.com
https://ebookname.com/product/marx-s-inferno-the-political-theory-of-
capital-third-printing-and-first-paperback-printing-edition-roberts/
ebookname.com
Breakthrough Business Results With MVT A Fast Cost Free
Secret Weapon for Boosting Sales Cutting Expenses and
Improving Any Business Process 1st Edition Charles W.
Holland
https://ebookname.com/product/breakthrough-business-results-with-mvt-
a-fast-cost-free-secret-weapon-for-boosting-sales-cutting-expenses-
and-improving-any-business-process-1st-edition-charles-w-holland/
ebookname.com
https://ebookname.com/product/purity-body-and-self-in-early-rabbinic-
literature-1st-edition-mira-balberg/
ebookname.com
https://ebookname.com/product/ancestors-of-worthy-life-plantation-
slavery-and-black-heritage-at-mount-clare-1st-edition-teresa-s-moyer/
ebookname.com
Additive Combinatorics 1st Edition Terence Tao
https://ebookname.com/product/additive-combinatorics-1st-edition-
terence-tao/
ebookname.com
MATLABÒ
A Practical Introduction to Programming
and Problem Solving
This page intentionally left blank
MATLAB Ò
A Practical Introduction to Programming
and Problem Solving
Third Edition
Stormy Attaway
Department of Mechanical Engineering,
Boston University
PREFACE ...................................................................................................... xi
ACKNOWLEDGMENTS............................................................................. xxi
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 understand basic programming concepts. That person would not be able
to then learn a language such as C++ or Java without taking another intro-
ductory 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 beginning students is to give them both: the
programming concepts and the efficient built-in functions. As MATLAB is very
easy to use, it is a perfect platform for this approach to teaching programming
and problem solving.
xi
xii Preface
matrices
n plot functions, including those that use logarithmic scales
some functions and operators on vectors and matrices, and prepares for
vectorizing code
n matrix multiplication covered much earlier (in Chapter 2)
n vectorized code covered in the loop chapter in order to compare the use
KEY FEATURES
Side-by-Side Programming Concepts and Built-in
Functions
The most important, and unique, feature of this book is that it teaches
programming concepts and the use of the built-in functions in MATLAB side-
by-side. It starts with basic programming concepts, such as variables,
Preface xiii
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
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
looping to calculate a sum, counting in a conditional loop, and error-checking,
are not found in many texts, but are 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, also some of the lower-level
file input/output functions. As file input and output are 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.
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
beginner programmers. Therefore, these concepts are introduced gradually.
First, arguably the easiest type 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.
covered. Sorting, searching, and indexing are also addressed. All of these are
again approached systematically; for example, cell arrays are covered before
they are used in file input functions and as labels on pie charts.
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.
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.
Techniques 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.
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 mathematical
techniques to solve systems of linear algebraic equations, fit curves to data, and
perform 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,
Preface xv
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.
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.
xviii Preface
“Explore Other Interesting Features”: this section has been added to the end of
every chapter in this third edition. 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.
When some problems are introduced, they are solved using both “The
Programming Concept” and “The Efficient Method”. This facilitates under-
standing the built-in functions and operators in MATLAB, as well as the
underlying programming 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 underline
n key important terms are shown in bold and italic.
The end-of-chapter “Summary” contains, where applicable, several sections:
Common Pitfalls: a list of common mistakes that are made, and how to
avoid them.
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.
Key Terms: a list of the key terms covered in the chapter, in sequence.
MATLAB Reserved Words: a list of the reserved key words in MATLAB.
Throughout the text, these are shown in bold, underlined type.
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.
MATLAB Operators: a list of the MATLAB operators covered in the chapter,
in the order covered.
Exercises: a comprehensive set of exercises, ranging from the rote to more
engaging applications.
xxi
xxii Acknowledgments
Introduction to Programming
Using MATLAB
Random documents with unrelated
content Scribd suggests to you:
According to this estimate the cat is a domestic comfortable
creature, usually found curled up like the ammonite, and in a state
of semi-torpor; it is essentially selfish and essentially cruel, but apart
from these two drawbacks, essentially feminine. “The cat is selfish,
and the dog is faithful.” This sums up a judgment founded on wilful
ignorance and gross egotism.
In respect to what is the dog faithful and the cat selfish? Simply in
this regard, that the dog takes the vainest man on something better
than his own estimate, while of the cat’s life and world the human
being forms but a little part.
Here plainly Greek meets Greek, and we had better let the
accusation of egotism alone. But apart from this point, the above
summary of the cat’s nature is about as true as the following
summary of the sportsman’s nature from the cat’s point of view.
“The sportsman is a quiet, domestic creature, fond of his comforts
and his meals; he is generally found smoking in an armchair before
the fire. The only thing which interferes with his domesticity is his
tendency to absent himself from the house for hours together; this
appears to be the result of a curious mania quite foreign to his
nature; and it will cause him even to miss his meals. If you come
upon him at such times he is engaged in a prosaic kind of wholesale
slaughter; he has no exciting chases after his prey, no display of
ability, no well-planned ambushes; but he kills at a distance through
an unpleasantly noisy instrument. The sportsman, too, is absolutely
dangerous to life at such time, and I have known cats fall victims to
his rage; whereas, if you meet him in his normal condition, he is
usually quite tame; you can safely leave kittens in the room with
him, and I have never known him kill a caged bird. The keeper is a
very dangerous sort of sportsman, and must be regarded as radically
unsafe. The difference between sportsmen and keepers is much the
same as that between capricious bulls and mad bulls.”
The fact is, that the usual judgment of cats rests on a total
misapprehension of the scope of a cat’s life; and the root of the
misunderstanding goes wider and deeper than this. The average
human being takes account only of those qualities of animals which
have some practical bearing on human life; even the animal lover is
wont to take account only of animal qualities, physical, mental, and,
at a stretch, moral; whereas that which is the pivot of human life
and human relations; that which, rudimentary as it is in animals, is
still the pivot of animal qualities—namely, the force of personality—is
altogether left out of account.
No judgment of animals can be adequate, or in any sense true,
which does not take account of personality, more or less developed,
and of the scope of the creature’s life as determined by it.
The more intimately one knows animals, the more one is struck by
their individuality, and the varying force of their personality.
Persis had the most intense personality of any animal I have ever
known. Mentu’s, less vivid, was still as individual and distinct; Ra had
a little narrow nature, Alexander was undeveloped, and the tabby is
frankly common; but all are as distinct from one another, as
essentially personal, as five human beings.
And it is greatly through this personality that the scope of an
animal’s life, as of the life of the human being, is determined; we are
all more or less at the mercy of what we, in our blindness, call “blind
forces;” but in all of us there is something which out of the
“manifold” of the world seeks and selects a consistent experience,
some principle which determines the scope of life.
Out of the many chemicals of the soil each plant draws those which
are appropriate to its own life, each plant transforms them into a
living thing, a definite beauty of leaf and bud.
And the alchemy of the higher creature does not only transform the
material particles of the world, now into the ashen silky hair and
yellow eyes of Mentu, now into the curly grizzled coat of Taffy; but
through the intelligence and sensibilities, through the desire for
approbation and of admiration, through the protective love of the
offspring, and the pure straining after the affection of the human
being, dimly understood, these dawning consciousnesses gather
from the world of sensation, of intelligence, of emotion, such
material as they can assimilate and transform, defining it into a life
and world of their own.
If we cannot from the point of more developed moral consciousness,
and higher intelligence, even seek to understand the dawnings in the
lower creatures of that which makes us what we are, then to us
animals are mere playthings or mere slaves, and we can have no
least perception of what is meant by that earnest, if unrealised,
“expectation of the creature.”
II
III
“But when she came back the dog was laughing.”
Counterbalancing the rudimentary powers of æsthetic pleasure in
the cat, we find in the dog a more facile intelligence, and a far more
adaptable nature. Some boast that they have taught tricks to a cat;
but the fact shows not so much that the cat was intelligent and
docile as that its owners were; for their ability has been usually to
seize on some natural movement of the cat, in jumping or in sitting
up, and gradually to induce the animal to exaggerate it. But the
tricks we teach a dog are against his nature, and it needs not only
intelligence but docility to take a savoury bite and abstain from
swallowing until the precisely right word is pronounced.
A cat walks about with a great purpose dimly imagined in its brain,
but a dog plans; he is “the low man adding one to one,” but his
sums are the most correct, for he is of a practical nature. He does
not have to pretend that a stick is alive before he can glean pleasure
from playing with it.
How far a dog, or indeed how far any animal is capable of using an
instrument for effecting its purposes is an undecided question; but I
have heard on near authority of a dog scraping a mat up against a
swing door through which he had to pass so that the door was kept
open. To use an instrument involves a complicated mental process,
in which not only association but reflection on the nature of the
thing is required. Taffy associated his muzzle with his walk, and
fetched it with pleasure when the association was established; but
reflection did not sufficiently come into the process to prevent him
from fetching a clothes brush or a Bible instead if convenient.
One clear point of superiority in the dog is his rudimentary sense of
humour. Almost any good-tempered dog, when well treated, will try
from time to time to laugh off a scolding. If he is encouraged, the
fooling is repeated again and again with growing exaggeration as he
rolls over with wide mouth and absurd contortions, or flies at one’s
face to lick it. He appreciates humour in others at his own expense,
a thing which not every human being is capable of doing; if he is
teased laughingly, he too will play the fool; if he is teased cruelly he
is cross or wretched. No dog likes one to blow in his face or ear, but
Taffy, though not wholly good-tempered, will allow the bellows to be
placed even in his mouth if he is assured that it is a game. When the
puff of air comes he darts up, jumps at and licks the person who is
teasing him, and barks with a wagging tail. If he is really bored or
tired he licks the nozzle of the bellows, or the hand that holds them,
deprecatingly; he declines the game, but in perfect good humour.
Now a cat has no sense of humour at all. Its very comedies are
serious; and to tease it is to outrage its dignity. The better bred a cat
is the more easily it takes offence. But after all the “sense of the
ridiculous” is a gross quality, and the humour of one age or of one
class seems vulgarity to another a little in advance. A cat is never
vulgar.
IV
If the scope of life and the qualities of intelligence differ from race to
race of animals, the strictly moral qualities appear to differ from
individual to individual.
Cats are called “selfish”; but even on the undiscriminating view such
qualities differ from cat to cat. Ra was certainly self-absorbed, but I
attribute this greatly to unhappy family circumstances when he was
young. Persis and Mentu were not selfish in this sense at all. Again
and again they have been found in the room with food untouched.
When one came in there was a greeting and short display of
affection, and not till then would the cat go to its food, and eat with
good appetite. Few people think of accusing a straightforward genial
collie of selfishness; yet if I left Taffy alone with his dinner, or even
with some one else’s dinner, there is a strong presumption that I
should find the plate clean and shining on return.
What people usually mean by this assertion is that the cat does not,
like the dog, depend entirely on human companionship; there are no
touching stories of faithfulness to a departed master; there is no
overwhelming interest in the human race. A cat has more of what
the average Briton calls “self-respect,” a quality he likes far better in
himself than in others.
On the other hand, a cat has more interest in other races of beings
than a dog. The only creatures in which most dogs show
spontaneous interest, unsolicited and untaught, are horses; and
even here the interest rests on association. But we have all known
cases of cats which deliberately set themselves to woo dogs; Ra and
his grandmother, unlike in all else, adored the same fox-terrier. I
have indeed seen a dog which had lost her puppies nurse a half-
grown cat, but the cat seemed to take the initiative. On the other
hand, a Manx cat, in a house where I was staying, allowed a beloved
terrier to take food out of her mouth. A cat has been known to bring
up squirrels; a tom-cat of our own fondled and protected chickens;
finally, a cat has been known to bring a half-starved friend to share
its dinner.
So-called “animal instincts” cannot account for the greater part of
these cases, which involve rather definite sacrifice. Dog friendships,
on the other hand, rarely involve sacrifice except for the sake of
man.
This instinct of benevolence may be noticed among birds. I have
heard on good authority of an Uncle canary bringing up a deserted
brood, and even with apparent embarrassment taking his place on
the nest; of sparrows bringing up young starlings, which, taken from
their own nest and placed on a window-ledge, sought refuge in the
sparrows’ nest; and finally, of a sparrow helping a wagtail to feed a
young cuckoo. Unless birds absolutely enjoy filling each other’s
mouths, such operations involve sacrifice; but in any case there is a
large social instinct shown; and when, as I sit in the garden, the
bean poles and seed sticks near me begin to blossom into robins, I
find I am suddenly the centre towards which such social instincts are
directed.
Temper differs in the same way from individual to individual, in
extent and quality. Ra had a cross temper; it irritated him if one took
liberties, and he struck without warning; but with regard to other
animals cowardice kept his temper in check. Mentu had the
occasional irritability of a nervous temperament, whether animal or
human; he often kept a bold front upon danger, when fear made
him afterwards positively sick and unable to eat for some time.
Persis was a very fiend to other animals, but had an utterly sweet
and grateful temper towards human beings unless jealousy came
into play.
Dogs are more often misjudged in respect to temper than cats,
probably because their ill-temper is more formidable; and the
nervous excitability of the collie is often mistaken for bad temper. I
have known a bad-tempered collie, but the clergyman who owned
him did not keep him long, as it was apt to make difficulties in the
parish if the congregation of the mission church was kept at bay on
a dark, windy evening.
Pugnacity is perhaps a different thing from ill-temper, and appears to
be a very wide-spread quality in bird-life. A great robin-tamer told
me that no robin could support his position unless he was very
pugnacious. Those who have tried to tame wild birds, or even those
who feed birds in the winter, will notice the extraordinary displays of
temper among them; how the blackbird loses half his meal through
trying to chase other birds away; how the tits play with him,
reckoning on this pugnacity; how the robin after he has made a
hearty meal lies in wait for late comers. Barn-door cocks are too
universally condemned in respect of temper; my patriarch has been
several times reported to me as having placed himself between two
young combatants; and he lives on excellent terms with a younger
replica of himself, the only point of quarrel being the distance to
which the young cock may chase a hen of the other’s harem which
has strayed into his own yard. Pugnacity is indeed apt to develop
into ill-temper with caged birds, but gentle handling in taming and
increased freedom would probably go far to obviate this.
I have spoken of moral qualities, but the centre of all these is the
question of conscience. It is impossible to deny that at any rate the
higher animals have conscience, if conscience means the recognition
of a law or principle higher than the immediate personal desire and
sometimes antagonistic to it.
Even if we allow that the sense of duty in human beings is based on
the “sanctions” of pleasures and pain, this makes no difference to
the quality of the sense once evolved; neither can it make any
difference in the quality of the sense in animals whether this is
produced by the “sanction” of nature or of the human race.
The more intelligent domestic creatures accept to some extent a
standard given by the power above them. The human standard is to
them in a sense as the law written on stone to us; and all know the
law has gone forth against the indulgence of ill-temper. Joey
recognises this law, and it is a moral effort he makes (very seldom)
to refrain from biting; he, too, has a conscience, though a singularly
bad one. Taffy with the nozzle of the bellows in his mouth can
choose whether to accept the situation cheerfully or crossly.
But the dog accepts his moral code more entirely from the human
being than the cat does. In this respect the cat is as the Gentile,
without the law, but a law unto himself. There is sacrifice of the
lower desires to the higher when the cat brings a friend to share her
dinner; when she lets a dog take food out of her mouth; when she
carries on towards her kittens, after the immediate needs and
desires of motherhood have ceased, a course of conduct more or
less consistently educative. A cat, the Egyptians said, reasoned like a
man, and this is true in that she determines, like a man, her own
ends and purposes in life. It is not approbation but admiration that
the cat demands from man; the dog accepts the purposes of life as
given from above. But he recognises, as clearly as he recognises the
sanction of the ginger-bread and the whip, the sanction of moral
appreciation or disapproval. He claims applause when he has done
well, and when the whip has been endured he still clings with
renewed trust to his diviner friend, and seeks by affection to win
back approval.
Such animals have wills essentially free as our own, but with dimmer
intelligence these wills are more at the mercy of their passions; and
the blinder intelligence leaves them, too, more at the mercy of
spiritual influences which flow out from us to them. There is a quick
response, as with children, not only to our treatment, but to the
spirit of our treatment, for they reward our trust with trust, and
answer our cheerfulness with heart and courage. And we, too, war
with principalities and powers, and are helped in the high and
hidden places by influences unseen. We call these creatures blind
and unconscious, but our consciousness, too, is dim, and our eyes
blinder to things divine than theirs to things human; we both move
gropingly and feebly in a great world and battle against the Will that
made us and has mercy on us—“so many men that know not their
right hand from their left, and also much cattle.”
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.