Instant Access to Programming with MATLAB for Scientists A Beginner s Introduction 1st Edition Mikhailov Eugeniy E ebook Full Chapters
Instant Access to Programming with MATLAB for Scientists A Beginner s Introduction 1st Edition Mikhailov Eugeniy E ebook Full Chapters
Instant Access to Programming with MATLAB for Scientists A Beginner s Introduction 1st Edition Mikhailov Eugeniy E ebook Full Chapters
com
https://ebookmeta.com/product/programming-with-matlab-for-
scientists-a-beginner-s-introduction-1st-edition-mikhailov-
eugeniy-e/
OR CLICK BUTTON
DOWNLOAD NOW
https://ebookmeta.com/product/introduction-to-r-for-social-scientists-
a-tidy-programming-approach-1st-edition-ryan-kennedy/
ebookmeta.com
https://ebookmeta.com/product/applied-numerical-methods-with-matlab-
for-engineers-and-scientists-5th-edition-steven-chapra/
ebookmeta.com
https://ebookmeta.com/product/programming-with-openscad-a-beginner-s-
guide-to-coding-3d-printable-objects-1st-edition-justin-gohde/
ebookmeta.com
https://ebookmeta.com/product/energy-law-an-introduction-2nd-edition-
raphael-j-heffron/
ebookmeta.com
Encryption in SAS 9 4 Sixth Edition Sas Institute Inc
https://ebookmeta.com/product/encryption-in-sas-9-4-sixth-edition-sas-
institute-inc/
ebookmeta.com
https://ebookmeta.com/product/a-structuralist-theory-of-economics-1st-
edition-adolfo-garcia-de-la-sienra/
ebookmeta.com
https://ebookmeta.com/product/dirty-love-1st-edition-ainsley-booth/
ebookmeta.com
Bette Davis Black and White 1st Edition Julia A. Stern
https://ebookmeta.com/product/bette-davis-black-and-white-1st-edition-
julia-a-stern/
ebookmeta.com
Programming with
MATLAB for Scientists
A Beginner’s Introduction
Programming with
MATLAB for Scientists
A Beginner’s Introduction
Eugeniy E. Mikhailov
MATLAB® and Simulink® are trademarks of the MathWorks, Inc. and are used with permission. The
MathWorks does not warrant the accuracy of the text or exercises in this book. This book’s use or discussion of
MATLAB® and Simulink® software or related products does not constitute endorsement or sponsorship by the
MathWorks of a particular pedagogical approach or particular use of the MATLAB® and Simulink® software.
10 9 8 7 6 5 4 3 2 1
This book contains information obtained from authentic and highly regarded sources. Reprinted material is
quoted with permission, and sources are indicated. A wide variety of references are listed. Reasonable efforts
have been made to publish reliable data and information, but the author and the publisher cannot assume
responsibility for the validity of all materials or for the consequences of their use.
No part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic,
mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and
recording, or in any information storage or retrieval system, without written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com
(http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive,
Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration
for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate
system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only
for identification and explanation without intent to infringe.
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
I Computing Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 MATLAB Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1 MATLAB’s Graphical User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 MATLAB as a Powerful Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.1 MATLAB’s variable types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.2 Some built-in functions and operators . . . . . . . . . . . . . . . . . . . . . 15
2.2.3 Operator precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2.4 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.3 Efficient Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4 Using Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.5.1 Creating and accessing matrix elements . . . . . . . . . . . . . . . . . . . 19
2.5.2 Native matrix operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.5.3 Strings as matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.6 Colon (:) Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.6.1 Slicing matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.7 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.7.1 Saving plots to files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.8 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
v
vi Contents
7 Numerical Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
7.1 Estimate of the Derivative via the Forward Difference . . . . . . . . . . . . 87
7.2 Algorithmic Error Estimate for Numerical Derivative . . . . . . . . . . . . . 88
7.3 Estimate of the Derivative via the Central Difference . . . . . . . . . . . . . 89
7.4 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Preface
Intended Audience
This book is intended for anyone who wants to learn how to program with
MATLAB and seeks a concise and accessible introduction to programming,
MATLAB, and numerical methods. The author hopes that readers will find here
all necessary materials for handling their everyday computational and program-
ming problems. Even more seasoned readers may find useful insights on familiar
methods or explanations for puzzling issues they might encounter.
We will start with simple concepts and build up a skill set suitable to model,
simulate, and analyze real-life systems. Additionally, this book provides a broad
overview of the numerical methods necessary for successful scientific or engineer-
ing work. We will get familiar with a “lore” of computing, so you will know what
to look for when you decide to move to more advanced techniques.
The book is based on material of the one semester “Practical Computing for
Scientists” class taught at the College of William & Mary for students who have
not yet declared a major or students majoring in physics, neuroscience, biology,
computer science, applied math and statistics, or chemistry. The students who
successfully took this class were at all levels of their academic careers; some were
freshmen, some where seniors, and some were somewhere in between.
Why MATLAB?
A couple words about MATLAB, as it is our programming language of choice.
MATLAB has a good balance of already implemented features, which are impor-
tant for scientists and for ease of learning. MATLAB hides a lot of low-level details
from users: you do not need to think about variable types, compilation processes,
and so on. It just works. You can also do a calculation on a whole array of data
without tracking every element of the array. This part is deep inside of MATLAB.
From an instructor’s point of view, you do not need to worry about the instal-
lation of MATLAB for your class. It is easy and students are capable of doing it
alone. More importantly, it looks and works the same on variety of operational
systems, such as Windows, Mac, and Linux. MATLAB produces exactly the same
result on all computers.
From a student’s point of view, MATLAB is probably the most frequently
required programming language for an engineering or scientific position. There-
fore, if you learn MATLAB now, you likely will not need to retrain yourself to
another industry standard programming language.
xi
xii Preface
Such are the words with which Cumæ’s Sibyl from her
cell shrills forth awful mysteries and booms again from
the cavern, robing her truth in darkness—such the violence 10
with which Apollo shakes the bridle in her frenzied mouth
and plies her bosom with his goad. Soon as her frenzy
abated and the madness of her lips grew calm, Æneas the
hero began: “No feature, awful maiden, that suffering can
show rises on my sight new or unlooked-for—I have 15
foreseen all and scanned all in fancy already. I have
but one prayer to make: since here it is that Fame tells of
the gate of the infernal monarch, and the murky pool of
Acheron’s overflow, grant me to pass to the sight, to
the presence of my loved father—teach the way, and unlock 20
the sacred doors. Him I bore away through flames
and a driving tempest of darts on these my shoulders and
rescued him from the midst of the foe: he was the companion
of my journey, and encountered with me all the
waves of ocean, all the terrors of sea and sky in his own 25
feeble frame, beyond the strength and the day of old age.
Nay more—that I would kneel to thee and approach thy
dwelling—this was his charge, his oft-repeated prayer.
Oh, of thy grace, pity the son and the sire; for thou art
all-powerful, nor is it for nought that Hecate has set thee 30
over the groves of Avernus. If Orpheus had the power to
fetch back the shade of his wife, by the help of his Thracian
lyre and its sounding strings—if Pollux redeemed
his brother by dying in turn with him, and went and returned
on the path those many times—why talk of Theseus, 35
why of great Alcides[210]? my line, like theirs, is from
Jove most high.”
Such were his prayers, while his hands clasped the altar,
when thus the prophetess began: “Heir of the blood of
gods, son of Anchises of Troy, easy is the going down to
Avernus—all night and all day the gate of gloomy
Pluto stands unbarred; but to retrace your footsteps, and
win your way back to the upper air, that is the labour, that 5
the task. There have been a few, favourites of gracious
Jove, or exalted to heaven by the blaze of inborn worth,
themselves sprung from the gods, who have had the power.
The whole intervening space is possessed by woods,
and lapped round by the black windings of Cocytus’[211] 10
stream. And now, if your heart’s yearning is so great,
your passion so strong, twice to stem the Stygian pool,
twice to gaze on the night of Tartarus—if it be your joy
to give scope to a madman’s striving—hear what must
first be done. Deep in the shade of a tree lurks a branch, all 15
of gold, foliage alike and limber twig, dedicated to the
service of the Juno of the shades; it is shrouded by the
whole labyrinth of the forest, closed in by the boskage that
darkens the glens. Yet none may pierce the subterranean
mystery, till a man have gathered from the tree that leafy 20
sprout of gold, for this it is that fair Proserpine has ordained
to be brought her as her own proper tribute. Pluck
off one, another is there unfailingly, of gold as pure, a twig
burgeoning with as fine an ore. Let then your eye be
keen to explore it, your hand quick to pluck it when duly 25
found, for it will follow the touch with willingness and
ease, if you have a call from Fate; if not, no strength of
yours will overcome it, no force of steel tear it away.
But, besides this, you have the breathless corpse of a
friend lying unburied—alas! you know it not—tainting 30
your whole fleet with the air of death, while you are asking
Heaven’s will, and lingering on this our threshold. Him
first consign to his proper place, and hide him in the grave.
Lead black cattle to the altar: be this the expiation to
pave your way. Thus at last you shall look on the groves 35
of Styx and the realms untrodden of the living.” She
said, and closed her lips in silence.