Programming with MATLAB for Scientists A Beginner s Introduction 1st Edition Mikhailov Eugeniy E instant download
Programming with MATLAB for Scientists A Beginner s Introduction 1st Edition Mikhailov Eugeniy E instant download
https://ebookmeta.com/product/programming-with-matlab-for-
scientists-a-beginner-s-introduction-1st-edition-mikhailov-
eugeniy-e/
https://ebookmeta.com/product/introduction-to-r-for-social-
scientists-a-tidy-programming-approach-1st-edition-ryan-kennedy/
https://ebookmeta.com/product/applied-numerical-methods-with-
matlab-for-engineers-and-scientists-5th-edition-steven-chapra/
https://ebookmeta.com/product/programming-with-openscad-a-
beginner-s-guide-to-coding-3d-printable-objects-1st-edition-
justin-gohde/
https://ebookmeta.com/product/energy-law-an-introduction-2nd-
edition-raphael-j-heffron/
Encryption in SAS 9 4 Sixth Edition Sas Institute Inc
https://ebookmeta.com/product/encryption-in-sas-9-4-sixth-
edition-sas-institute-inc/
https://ebookmeta.com/product/international-organizations-and-
member-state-responsibility-critical-perspectives-1st-edition-
ana-sofia-barros/
https://ebookmeta.com/product/a-structuralist-theory-of-
economics-1st-edition-adolfo-garcia-de-la-sienra/
https://ebookmeta.com/product/dirty-love-1st-edition-ainsley-
booth/
Bette Davis Black and White 1st Edition Julia A. Stern
https://ebookmeta.com/product/bette-davis-black-and-white-1st-
edition-julia-a-stern/
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