Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Difference Matrices for ODE and PDE: A

MATLAB® Companion John M.


Neuberger
Visit to download the full and correct content document:
https://ebookmass.com/product/difference-matrices-for-ode-and-pde-a-matlab-compa
nion-john-m-neuberger/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

America's Scientific Treasures: A Travel Companion 2nd


Edition Stephen M. Cohen

https://ebookmass.com/product/americas-scientific-treasures-a-
travel-companion-2nd-edition-stephen-m-cohen/

Destination C1 & C2 : grammar & vocabulary with answer


key 18 [printing]. Edition Malcolm Mann

https://ebookmass.com/product/destination-c1-c2-grammar-
vocabulary-with-answer-key-18-printing-edition-malcolm-mann/

(eTextbook PDF) for Privilege, Power, and Difference


3rd Edition

https://ebookmass.com/product/etextbook-pdf-for-privilege-power-
and-difference-3rd-edition/

Managing for Public Service Performance: How People and


Values Make a Difference Peter Leisink

https://ebookmass.com/product/managing-for-public-service-
performance-how-people-and-values-make-a-difference-peter-
leisink/
A Grande Gripe John M. Barry [Barry

https://ebookmass.com/product/a-grande-gripe-john-m-barry-barry/

Python for Scientists (3rd Edition) John M. Stewart

https://ebookmass.com/product/python-for-scientists-3rd-edition-
john-m-stewart/

MATLAB for Engineers 6th Edition Holly Moore

https://ebookmass.com/product/matlab-for-engineers-6th-edition-
holly-moore/

John Murtagh's General Practice Companion Handbook 7th


Edition John Murtagh

https://ebookmass.com/product/john-murtaghs-general-practice-
companion-handbook-7th-edition-john-murtagh/

Essential MATLAB for engineers and scientists 6th


edition Edition Hahn

https://ebookmass.com/product/essential-matlab-for-engineers-and-
scientists-6th-edition-edition-hahn/
John M. Neuberger

Difference Matrices
for ODE and PDE
A MATLAB® Companion
Difference Matrices for ODE and PDE
John M. Neuberger

Difference Matrices
for ODE and PDE
A MATLAB® Companion

123
John M. Neuberger
Department of Mathematics and Statistics
Northern Arizona University
Flagstaff, AZ, USA

ISBN 978-3-031-11999-6 ISBN 978-3-031-12000-8 (eBook)


https://doi.org/10.1007/978-3-031-12000-8
MATLAB is a registered trademark of The MathWorks, Inc., Natick, MA, USA

Mathematics Subject Classification: 65-01, 35-01, 34-01, 65L, 65M, 65M06, 65M20, 65N, 65N06,
65N25, 35F, 35G, 35A24, 35J20

© Springer Nature Switzerland AG 2023


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part
of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission
or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar
methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this
publication does not imply, even in the absence of a specific statement, that such names are exempt from
the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors, and the editors are safe to assume that the advice and information in this
book are believed to be true and accurate at the date of publication. Neither the publisher nor the
authors or the editors give a warranty, expressed or implied, with respect to the material contained
herein or for any errors or omissions that may have been made. The publisher remains neutral with regard
to jurisdictional claims in published maps and institutional affiliations.

This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
Preface

This text will be useful for the four different audiences listed below. It is expected
that all readers will have knowledge of basic calculus, linear algebra, and ordinary
differential equations, and that the successful student will either already know
elementary partial differential equations, or be concurrently learning that subject.
The material is intended to be accessible to those without expertise in MATLAB®,
although a little prior experience with programming is probably required.
1. This text serves as a supplement for the student in an introductory partial dif-
ferential equations course. A selection of the included exercises can be assigned
as projects throughout the semester. Through the use of this text, the student will
develop the skills to run simulations corresponding to the primarily theoretical
course material covered by the instructor.
2. These notes work well as a standalone graduate course text in introductory
scientific computing for partial differential equations. With prerequisite
knowledge of ordinary and partial differential equations and elementary
numerical analysis, most of the material can be covered and many of the
exercises assigned in a one-semester course. Some of the harder exercises make
substantial projects, and relate to topics from the other graduate mathematics
courses graduate students typically take, e.g., differential equations and topics in
nonlinear functional analysis.
3. Established researchers in theoretical partial differential equations may find
these notes useful as well, particularly as an introductory guide for their research
students. Those unfamiliar with MATLAB can use the included material as a
reference in quickly developing their own applications in that language.
A mathematician who is new to the practical implementation of methods for
scientific computation in general can with relative ease, by working through a
selection of exercises, learn how to implement and execute numerical simula-
tions of differential equations in MATLAB. These notes can serve as a practical
guide in independent study, undergraduate or graduate research experiences, or
for help simulating solutions to specific thesis- or dissertation-related

v
vi Preface

experiments. The author hopes that the ease and brevity with which the notes
provide solutions to fairly significant problems will serve as inspiration.
4. The text can serve as a supplement for the instructor teaching any course in
differential equations. Many of the examples can be easily implemented and the
resulting simulation demonstrated by the instructor. If the course has a
numerical component, a few exercises of suitable difficulty can be assigned as
student projects. Practical assistance in implementing algorithms in MATLAB
can be found in this text.
Scientist and engineers have valid motivations to become proficient at imple-
menting numerical algorithms for solving PDE. The text’s emphasis on enforcing
boundary conditions, eigenfunctions, and general regions will be useful as an
introduction to their advanced applications. For the mathematician, accomplished or
student, a more powerful benefit can be the tangible, visual realization of the objects
of calculus, differential equations, and linear algebra. The high-level programs are
developed by the reader from earlier programs and short fragments of relatable
code. The resulting simulations are demonstrations of the properties of the under-
lying mathematical objects, where vectors represent functions, matrix operations
represent differentiation and integration, and calculations such as solving linear
systems or finding eigenvalues are easily accomplished with one line of code. Even
without much prior knowledge of programming or MATLAB, by working through
a selection of exercises in this text, the reader will be able to create working
programs that simulate many of the classic problems from PDE, while gaining an
understanding of the underlying fundamental mathematical principles.
The approach of the text is to first review MATLAB and a small selection of
techniques from elementary numerical analysis, and then introduce difference
matrices in the context of ordinary differential equations. We then apply these ideas
to PDE, including topics from the heat, wave, and Laplace equations, eigenvalue
problems, and semilinear boundary value problems. We enforce boundary condi-
tions on regions including the interval, square, disk, and cube, and more general
domains. We push the general notion that linear problems can be expressed as a
single linear system, while many nonlinear problems can be solved via Newton’s
method.

Flagstaff, USA John M. Neuberger


Acknowledgements

I would first and foremost like to acknowledge my father J. W. Neuberger. He


taught me much of what I know about differential equations and the numerical
solution of them. His belief in my pedagogical approach and the practical useful-
ness of my course notes was my main motivation in publishing them.
I also would like to thank my Ph.D. advisor Alfonso Castro who introduced me
to nonlinear functional analysis, particularly as it pertains to semilinear elliptic
boundary value problems. Chapter 5 content of this text reflects a small slice of my
lifetime pursuits to understand and computationally simulate the equations of that
subject.
I have been extremely lucky in having a wonderfully supportive family. My wife
Dina has been the main source of inspiration over my career as a professor of
mathematics. My son Nick has an interest in mathematics as well, and has been
insistent that I refine the notes so that others could use them.
I would also like to thank my 20-plus years of undergraduate and graduate
partial differential equations and numerical analysis students. Their feedback was
invaluable in the process of developing and refining my notes. The much-improved
text contains numerous clarifications and hints as a direct result of their suggestions,
and the exercises are more robust and complete, thanks to their countless hours of
coding and grinding out project reports. I learned a few MATLAB® tricks from
them too. A special thanks goes to my former and current students Tyler Diggans,
Ryan Kelly, and Ian Williams, who each took a turn at proofreading, and made
some valuable editorial suggestions from the student perspective.

vii
Contents

1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 A Summary of the Differential Equations We Will Consider . . . . 1
1.2 The Use of MATLAB® and the Student Exercises . . . . . . . . . . . 2
1.2.1 Using MATLAB®’s Debugger . . . . . . . . . . . . . . . . . . . 3
1.2.2 Line Numbering in MATLAB® Examples . . . . . . . . . . . 4
1.2.3 Reproducing Codes and Exercises . . . . . . . . . . . . . . . . . 4
1.2.4 Guidelines to the Homework Exercises . . . . . . . . . . . . . 5
1.3 The Organization of This Text . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Review of Elementary Numerical Methods and MATLAB® . . . . . .. 11
2.1 Introduction to Basic MATLAB® at the Command Line . . . . . .. 12
2.1.1 MATLAB®: sum, prod, max, min, abs, norm, linspace,
for loop, eigs, and sort . . . . . . . . . . . . . . . . . . . . . . . .. 15
2.2 Runge–Kutta Method for Initial Value Problems . . . . . . . . . . .. 17
2.2.1 The Shooting Method for ODE BVP—an IVP
Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 19
2.2.2 Comparison of Approximate Solutions to Exact
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 22
2.2.3 MATLAB®: Ones, Zeros, the ‘:’ Iterator, the @
Syntax for Defining Inline Functions, Subfunctions . . .. 23
2.2.4 Rows Versus Columns Part I, Diagnosing Dimension
and Size Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 26
2.3 Numerical Differentiation and Integration . . . . . . . . . . . . . . . . .. 27
2.3.1 Higher Order Differences and Symbolic Computation . .. 32
2.3.2 MATLAB®: kron, spdiags, ‘backslash’ (mldivide), tic,
and toc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 34
2.4 Newton’s Method for Vector Fields . . . . . . . . . . . . . . . . . . . . .. 36
2.4.1 MATLAB®: if, else, while, fprintf, meshgrid, surf,
reshape, find, single indexing, and rows versus
columns Part II . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 37

ix
x Contents

2.5 Cubic Spline Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .... 43


2.5.1 Making Animations . . . . . . . . . . . . . . . . . . . . . . . .... 45
2.6 Theory: ODE, Systems, Newton’s Method, IVP Solvers,
and Difference Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . .... 48
2.6.1 Some ODE Theory and Techniques . . . . . . . . . . . .... 48
2.6.2 Convergence and Order of Newton’s Method . . . . .... 52
2.6.3 First-Order IVP Numerical Solvers: Euler’s
and Runge–Kutta’s . . . . . . . . . . . . . . . . . . . . . . . . .... 54
2.6.4 Difference Formulas and Orders of Approximation . .... 54
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .... 57
3 Ordinary Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.1 Second-Order Semilinear Elliptic Boundary Value Problems . . . . 64
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.2 Linear Ordinary Second-Order BVP . . . . . . . . . . . . . . . . . . . . . . 69
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.3 Eigenvalues of D2 and Fourier Series . . . . . . . . . . . . . . . . . . . 71
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.4 Enforcing Zero Dirichlet, Zero Neumann, and Periodic
Boundary Conditions Using Either Point or Cell Grid. . . . . . . . . . 77
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
3.5 First-Order Linear Solvers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
3.6 Systems of First-Order Linear Equations
for Second-Order IVP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
3.7 First-Order Nonlinear IVP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.8 A Practical Guide to Fourier Series . . . . . . . . . . . . . . . . . . . . . . 89
4 Partial Differential Equations . . . . . . . . . . . . ............... . . . 93
4.1 The Laplacian on the Unit Square . . . . . ............... . . . 94
4.2 Creating the Sparse Laplacian Matrix D2 and Eigenvalues . . . . . 95
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . ............... . . . 98
4.3 Semilinear Elliptic BVP on the Square . . ............... . . . 100
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . ............... . . . 101
4.4 Laplace’s Equation on the Square . . . . . ............... . . . 102
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . ............... . . . 106
4.5 The Heat Equation . . . . . . . . . . . . . . . . ............... . . . 107
4.5.1 Explicit Method . . . . . . . . . . . . ............... . . . 108
4.5.2 Implicit Method . . . . . . . . . . . . ............... . . . 110
4.5.3 Explicit–Implicit Method . . . . . ............... . . . 111
Contents xi

4.5.4 The Method of Lines . . . . . . . . . . . . . . . . . . . . . . . . . . 112


4.5.5 Fourier Expansion with Numerical Integration . . . . . . . . 113
4.5.6 Block Matrix Systems . . . . . . . . . . . . . . . . . . . . . . . . . 117
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
4.6 The Wave Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
4.6.1 The Method of Lines . . . . . . . . . . . . . . . . . . . . . . . . . . 124
4.6.2 A Good Explicit Method . . . . . . . . . . . . . . . . . . . . . . . 124
4.6.3 Block Matrix Systems and D’Alembert Matrices . . . . . . 126
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
4.7 Tricomi’s Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
4.8 General Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
4.8.1 The Laplacian on the Cube . . . . . . . . . . . . . . . . . . . . . . 140
4.8.2 The Laplacian on the Disk . . . . . . . . . . . . . . . . . . . . . . 141
4.8.3 Accurate Eigenvalues of the Laplacian on Disk,
Annulus, and Sections . . . . . . . . . . . . . . . . . . . . . . . . . 146
4.8.4 The Laplace–Beltrami Operator on a Spherical
Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
4.8.5 A General Region Code . . . . . . . . . . . . . . . . . . . . . . . . 151
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
4.9 First-Order PDE and the Method of Characteristics . . . . . . . . . . . 161
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
4.10 Theory: Separation of Variables for PDE on Rectangular
and Polar Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
4.10.1 Eigenfunctions of the Laplacian . . . . . . . . . . . . . . . . . . 164
4.10.2 Laplace’s Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
4.10.3 The Heat Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
4.10.4 The Wave Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
5 Advanced Topics in Semilinear Elliptic BVP . . . . . . . . . . . . . . . . . . 181
5.1 Branch Following and Bifurcation Detection . . . . . . . . . . . . . . . 181
5.1.1 The Tangent Newton Method for Branch Following . . . . 182
5.1.2 The Secant Method for Bifurcation Detection . . . . . . . . 183
5.1.3 Secondary Bifurcations and Branch Switching . . . . . . . . 186
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
5.2 Mountain Pass and Modified Mountain Pass Algorithms
for Semilinear BVP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
5.2.1 The MPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
5.2.2 The MMPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
5.3 The p-Laplacian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Acronyms

Some frequently used acronyms.

ODE Ordinary differential equation, ordinary differential equations.


PDE Partial differential equation, partial differential equations.
BC Boundary condition.
IVP Initial value problem.
BVP Boundary value problem.
0-D BC Zero Dirichlet boundary condition, i.e., u ¼ 0 on the boundary.
For ODE, this is yðaÞ ¼ 0 and/or yðbÞ ¼ 0.
0-N BC Zero Neumann boundary condition, i.e., @u @g ¼ 0 on the boundary, for the
outward unit normal vector g. For ODE, this is y0 ðaÞ ¼ 0 and/or
y0 ðbÞ ¼ 0.
MOL Method of lines. It refers to discretizing a PDE with differences in all but
one variable, usually time, and then solving the resulting system of ODE
by any convenient means.
RHS General term for the RHS of an equation, most often a known function or
vector defining a linear system.
LHS General term for the LHS of an equation.
RK Runge–Kutta, an OððDxÞ4 Þ method for solving first-order IVP.
SOV Separation of variables; it refers to two distinct techniques:
1. ODE, equation ! f ðyðxÞÞy0 ðxÞ ¼ gðxÞ, hence both sides can be
integrated with respect to x, yielding an implicit solution
FðyÞ ¼ GðxÞ þ c.
2. PDE, for linear equation the substitution uðx1 ; x2 Þ ¼ pðx1 Þqðx2 Þ gives
Fðpðx1 ÞÞ ¼ c ¼ Gðqðx2 ÞÞ, whence BC can be used to convert the
side with boundary values to an eigenvalue problem, and then solve
the other side as a family parameterized by the eigenvalues to get a
general solution.
HW Homework. A necessary part of the learning process.

xiii
xiv Acronyms

CCN Usually refers to the minimal-energy sign-changing exactly-once


solution to Du þ f ðuÞ ¼ 0, for a variety of assumptions on f including
super linearity. The letters refer to the author’s names in the article [24].
MPA Mountain pass algorithm, celebrated result by [25].
MMPA Modified mountain pass algorithm. It extends the MPA to find the CCN
solution.
Chapter 1
Introduction

Summary In this chapter, we summarize the key features and content of the text.
We explain the choice of MATLAB® as a programming language, and then briefly
emphasize the usefulness of the MATLAB debugger. We discuss the process of
converting code fragments and hints from the text into working programs, and give
some suggestions to instructors on the assignment of the homework problems which
require those working programs. We give more detail concerning each of the specific
included topics.

1.1 A Summary of the Differential Equations We Will


Consider

Differential equations is a vast subject. We concentrate here primarily on the most


fundamental equations encountered in introductory ODE and PDE courses. Many
of the included computational homework exercises correspond to typical analytical
homework exercises from those courses.
In particular, for ODE we present algorithms for approximating solutions to first-
and second-order linear and nonlinear equations and systems of equations, for initial
and boundary value problems. Homework exercises include examples requiring the
separation of variables, integrating factors, constant coefficient second-order theory,
and other standard techniques from an introductory course in ODE. We discuss the
enforcement of Dirichlet, Neumann, and periodic boundary conditions. Eigenvalue
problems and eigenfunction expansions are featured.
For PDE, we emphasize the eigenfunctions of the Laplacian, Laplace’s equation,
the heat and wave equations, and nonlinear elliptic problems. We consider the classic
regions of the square, disk, and annulus, as well as other more general regions, with
various IC/BC. By a variety of methods, we show how to numerically simulate the
separation of variable solutions to typical homework exercises from an introductory
PDE course. For completeness, we include an example code for first-order PDE
and the method of characteristics. Included are a small selection of more advanced
equations, intended primarily to demonstrate the wide range of possible applications

© Springer Nature Switzerland AG 2023 1


J. M. Neuberger, Difference Matrices for ODE and PDE,
https://doi.org/10.1007/978-3-031-12000-8_1
2 1 Introduction

for the text’s methods. In particular, we include some experiments with Tricomi’s
equation, bifurcation for semilinear elliptic BVP, and eigenvalues of the p-Laplacian.

1.2 The Use of MATLAB® and the Student Exercises

The goal of these notes is to make easily accessible the general ability of the reader
to use first- and second difference matrices to set up and solve linear and nonlinear
systems which approximate ordinary and partial differential equations. The scope
of differential equations is immense, and so this brief exposition will only give the
details for the most fundamental types of classic problems listed above. It is our
belief that the ease with which the included problems are solved will be encouraging
to the reader with more complicated applications in mind, and that the developed
arsenal of MATLAB commands and techniques will be useful in their attempts to
follow through with their own successful investigations.
The value of the presented methodology for solving the stated types of prob-
lems is independent of the choice of programming platform. Generally, we solve
linear differential equations with a single sparse matrix linear system solve, and we
solve nonlinear differential equations with Newton’s method. The included algo-
rithms could certainly be implemented in languages other than MATLAB, but why
would you want to? The developed MATLAB codes are short, easily related to the
underlying equations from mathematics, and the heavy lifting is generally done by
built-in compiled commands, e.g., the indispensable ‘\’, MATLAB’s built-in effi-
cient linear solver, eigs, its nearly unbeatable eigenvalue solver, and ode45, one of
its generic ordinary differential equation (ODE) system solvers. One would have to
work very hard to improve on the overall speed or any other facets of these built-in
functions’ implementations in another setting. The sparse-matrix capability of MAT-
LAB means that fairly huge problems can be attacked with a personal computer, and
that with not too much more sophistication (but beyond the scope of this document),
implementations on supercomputing clusters could be effectively used to attack the
most serious and computing-intensive of applications. In all fairness, it would not
be too hard to implement the algorithms presented here in Mathematica, and the
experienced C/C++ programmer would not find it difficult to build a library of tools,
for example from Netlib [21]. Mathematica is probably a better choice, in fact, for
intensive symbolic computations that might be needed for pre- and post-processing in
more complicated applications. The intermediate programmer would probably find
it not too difficult to translate the code included in this text to Sage. Python could
certainly be used, once some perhaps missing high-level mathematical functionality
built in to MATLAB was otherwise accessed via appropriate libraries of functions.
So, many languages would provide a fine platform for writing short, easy programs
to solve, by our methods, the example problems included in this document, but we
argue that none of these other choices easily yield significant performance increases
or are easier to understand or implement than MATLAB. As an educator who regu-
larly teaches sections of numerical analysis and other applied mathematics courses
1.2 The Use of MATLAB® and the Student Exercises 3

with a computational component to engineering and science majors, the author finds
that this student population is currently likely to already know MATLAB and to be
using it in their other courses, and those that don’t already know it seem to pick it
up pretty quickly.
Chapter 2 contains several entire MATLAB programs which demonstrate some
useful coding techniques on some familiar problems from calculus and ordinary dif-
ferential equations. The exercises ask the reader to modify and test these programs.
Chapter 3 starts with a complete program, cut into segments and presented in between
mathematical definitions and explanations of the algorithm and syntax, and Chaps. 4
and 5 extend the ideas to PDE. Each subsequent section includes enough MAT-
LAB code fragments such that it should be possible for the relatively inexperienced
programmer to put together working programs demonstrating the relevant methods.
Complete programs are only occasionally presented. Each section comes with a few
exercises for the reader to further their skills, suggesting to apply, generalize, or com-
bine presented ideas to solve related problems or perform different comparisons. A
little familiarity with introductory numerical analysis would certainly be helpful, but
the attempt has been made to make the information fairly self-contained in that brief
descriptions of some well-known and important elementary numerical methods are
included.

1.2.1 Using MATLAB® ’s Debugger

It is our opinion that the debugger is an essential tool for developing and understand-
ing programs, and that it should be used early and often by the beginner and expert
alike. Getting a red ‘dimension mismatch’ error? Click on the hyphen to the right of
a line number on the left side of the editor. If a red bullet appears, the program will
stop the first time it hits that line. Then inspect the variables in question. Placing your
cursor over them will at least reveal their dimension. Perhaps the matrix should be
transposed before the operation? If the data involved is not too huge, you can open
a variable editor and inspect each element. Right-clicking on the breakpoint is one
way to select the conditional option, e.g., i==23 will stop on the line if i = 23. The
user can descend into a subfunction for inspection of that function’s local variables,
or skip over the function to the next line in the parent function. Clear breakpoints,
set new ones, continue to the next, checking the dimension and if need be contents
of any variables whose value is in question.
You can use the debugger to learn MATLAB. For a small n value, run an example
program, stepping through the code line by line. Check some calculations by hand
and compare. When a loop gets tedious, put a breakpoint after it or use a conditional
breakpoint to skip ahead in the action. MATLAB’s way of generating graphics can
be confusing to the uninitiated. Put a breakpoint on some plot lines and inspect
the independent and dependent variables. Whenever a program is paused in the
debugger, the command line can be used to examine contents or perform small
checking calculations. The ‘execute selection’ feature can often be used at that time
4 1 Introduction

to make a smaller portion of the code run given previously stored values of relevant
variables and functions.
The first time one attempts to execute a program with a significant number of
complicated, new lines of code, one expects coding errors. A good approach is to
step through the program in the debugger either line by line, or section by section,
generally inspecting the inputs and outputs of each line or related group of lines,
e.g., a loop, verifying that dimensions and values appear consistent with that pre-
dicted. Minor mistakes can be corrected as they are encountered, while one may
need to pause to consider the best fix for deeper errors. Real learning can occur when
errors are encountered that force the coder to determine if the steps of the algorithm
and corresponding variables are properly representing the underlying mathematical
constructs.

1.2.2 Line Numbering in MATLAB® Examples

Where it makes sense, the author includes line numbers to the left of the included
lines of MATLAB code. In particular, when breaking a program or program fragment
into blocks, resuming consecutive line numbering is used. Many new examples only
require minor modifications of previously developed MATLAB programs. In that
case, the line numbering may start at some integer larger than one, indicating that
some lines are missing and presumed to be understood based on prior discussion. Of
course differing styles, and adding comments, blank lines, options, extra loops, or
more descriptive output will in practice change the line numbering and format of the
code. Our approach in this document is somewhat minimalistic, generally making an
effort to set up and solve the problems with a few extra bells and whistles. If cutting
and pasting from a pdf of this text, it may be necessary to delete the line numbers
where present; take the opportunity to reflect on the purpose of each line as you
remove each line number. If you are typing the example programs into the MATLAB
editor by hand, it is wise to do so a block of a few lines at a time, executing or better
yet stepping through via the debugger and verifying the resulting state of the partial
program before adding another small block.

1.2.3 Reproducing Codes and Exercises

If this document is being used as a textbook, the author hopes that the student will
write their own MATLAB programs to recreate the results from many of the included
examples. The entire program is included in the text for some of the examples, and
enough code fragments and hints are provided such that it should be possible in
subsequent sections to reproduce the results without external resources. The reader
might think of it as a bit of a game to see if they can do so with line numbering
that agrees with that provided in the included code fragments, i.e., can you initialize
1.2 The Use of MATLAB® and the Student Exercises 5

and define the various scalars, vectors, and sparse matrices in five lines as the author
did? The included exercises are for the most part intended to be fairly reasonable
extensions or combinations of the presented ideas, again doable without outside
resources.1 The progression of examples and exercises is designed to teach MATLAB
and demonstrate the utility of difference matrices, at each step moving the reader
a little closer toward being able to independently investigate differential equations,
perhaps equations of their own choosing which are more complicated and beyond
the scope of this text.
Some sections contain short formally stated examples, typically involving pencil-
and-paper calculations to approximate a solution for some reasonable discretization
or a small number of iterations, and to compute for comparison the corresponding
exact solution by some known technique. Some of the un-starred (non-programming)
exercises are similar to these examples, and may be useful in preparing for in-class
assessments. ;-)

1.2.4 Guidelines to the Homework Exercises

Un-starred problems, problems which are not indicated by one or more ‘*’, generally
do not require programming. These problems are not intended for programming
HW assignments. They may be useful in the preparation for in-class assessments.
Typically, these problems require a small pencil-and-paper calculation, e.g., for three
divisions and one iteration, or involve a theoretical derivation. If worked out by hand
prior to the writing and subsequent debugging of MATLAB code, they may serve to
aid in the successful implementation and understanding of the underlying algorithms.
Starred problems typically require writing MATLAB code in order to answer the
questions. Problems indicated with a single ‘*’ are generally the easiest and have
shorter expected student solution times. Some of these problems only require small
modifications to provided listings of MATLAB code. Problems which have been
marked with two or three ‘*’ generally have increased complexity, require more
student programming, and have a correspondingly greater anticipated required time
spent in solving them. They may make suitable projects for the beginning student.
The author uses the following system for assigning and assessing HW. All HW
will be submitted electronically via the course management system, e.g., BBLearn
or Moodle. Each HW will first and foremost be a short report, stating the problem
solved, giving a brief description of the algorithms and methods used to solve the
problems, containing graphical or tabular summaries of output, and with a brief but
convincing analysis of the results. Where possible, comparisons between known and
approximate solutions should be included. Each report should be in .pdf format. The
upload should be accompanied by one or several .m MATLAB files, capable of only

1Some course instructors may consider that extensive sharing of code or student downloading of
existing code from the Internet accomplishes little toward course goals, and in some cases may
consider such actions to be unethical.
6 1 Introduction

minor editing of generating the valid results contained in the report. The report should
avoid the inclusion of large quantities of tedious data, graphics, code listings, etc.
Summarize!
For rating the point value of individual homework exercises, the author generally
uses a scale of 10, 20, and 40 points for single-, double-, and triple-starred problems,
respectively. To allow for some student choice and independent investigation of
several aspects of a given topic, the author often makes assignments of the form
submit x out of y possible points worth of starred problem solutions from Sections W
and Z, e.g., 200 out of 340 possible points worth of starred problems from Chap. 2,
as the first assignment.

1.3 The Organization of This Text

The text is organized as follows.

Chapter 2 contains a brief introduction to MATLAB, with a few relevant elemen-


tary examples and a fairly extensive list of warm-up exercises. In particular, we
discuss Runge–Kutta for systems of ODE, numerical differentiation and integra-
tion, and Newton’s method applied to vector fields. Throughout this chapter, we
distribute a discussion of the MATLAB commands needed in all subsequent mate-
rial, including linspace, kron, spdiags, meshgrid, reshape, eigs, and
the powerful linear system solver ‘\’. From the author’s experiences with students
while developing these notes, we attempt in this chapter to address many of their
‘frequently asked questions’ concerning common pitfalls in programming and
MATLAB syntax. A subsection on cubic splines demonstrates the uses of most of
the above commands in a single application, in particular by building a complicated
sparse matrix enforcing the various constraints in order to solve the correspond-
ing system with a single linear solve command. The final section contains a brief
overview of some theories for the existence and uniqueness of solutions to ODE,
ODE systems, convergence of Newton’s method, order of approximation of IVP
solvers, and difference formulas.

Chapter 3 contains ODE material, but with an eye toward easy modification for
the subsequent application to PDE. The homework exercises assume familiarity
with integrating factors, separation of variables, converting second-order prob-
lems to first-order systems, constant coefficient linear theory, some basic existence
and uniqueness theory for IVP, eigenvalues, and other topics typically covered
in an introductory undergraduate ODE course. It is also assumed that the most
elementary numerical topics are understood, e.g., difference approximations for
derivatives, Riemann sums, Euler’s and Runge–Kutta’s methods for IVP, Newton’s
method for a function of one or several variables, and Taylor’s and Lagrange’s poly-
nomial approximation theorems with error terms. For a brief overview of these
1.3 The Organization of This Text 7

topics, see Sect. 2.6. In the last section of Chap. 3, we include a brief practical
guide for the construction of the Fourier series, which are used heavily in Chap. 4.

• Section 3.1 starts off with an example: how to solve a nonlinear second-order
ordinary elliptic boundary value problem (BVP) using Newton’s method. We
use this example as a place to introduce and explain several essential MAT-
LAB commands and techniques which will be constantly reused throughout
the document. The second difference matrix D2 on the point grid is introduced
in this first application and then used in many subsequent applications. This
Newton’s method approach is more efficient compared to implementing the
shooting method, an alternative ODE-only method briefly discussed in Chap. 2.
In Sect. 4.3, only small modifications to the ODE Newton’s method code will
be required to solve a semilinear elliptic BVP on the square.

• Section 3.2 solves a linear ordinary second-order BVP. By our presented method,
not much change will be required in Sect. 4.4 in order to solve Laplace’s equa-
tion on a square domain in a similar fashion.

• Section 3.3 solves a classic ODE eigenvalue problem with one additional line
of MATLAB beyond that already coded in previous sections. We use this exam-
ple to introduce eigenfunction expansions. We include a proof of the exact
eigenvalues of a second difference matrix, which is useful in understanding the
convergence of algorithms that use this matrix. Only minor modifications to the
developed MATLAB codes will be required to solve a PDE eigenvalue problem
on the square in Sect. 4.2.

• Section 3.4 shows how to build the D2 matrix for both the point and cell grids,
for Dirichlet, Neumann, mixed, and periodic boundary conditions (BC).

• Section 3.5 solves a first-order linear initial value problem. We feature the first
difference matrix D1 .

• Section 3.6 applies our first-order method to a system of two first-order initial
value problems in order to solve a second-order initial value problem.

• Section 3.7 applies Newton’s method to solve a nonlinear first-order initial value
problem (IVP), similar to that in Sect. 3.1 but using D1 .

• Section 3.8 provides a brief overview of the theory and a practical guide for the
construction of the Fourier series.
Chapter 4 concerns PDE. The homework exercises assume some familiarity with
(or the concurrent learning of) elementary PDE techniques and theory, includ-
ing separation of variables, applications of Fourier series in PDE, eigenfunction
expansions, Laplace’s, Heat, and Wave equations, for classical regions such as
the interval, square, disk, and annular domains, with various boundary and initial
8 1 Introduction

conditions. For the convenience of the reader, these topics are briefly summarized
in Sect. 4.10. The required numerical techniques are for the most part explained
in this text, with some review of classic methods, e.g., the explicit and implicit
methods for the heat equation.

• Section 4.1 introduces sparse block matrices for encoding the Laplacian. In
particular, we develop the D2 matrix on a square domain, enforcing Dirichlet
and/or Neumann BC on the individual four sides, using both point and cell grids.

• Section 4.2 verifies the numerically approximated eigenvalues and eigenvectors


of the Laplacian on the square and discusses eigenfunction expansions.

• Section 4.3 generalizes the method from Sect. 3.1 to solve a semilinear elliptic
BVP on the square.

• Section 4.4 applies the ideas from Sect. 3.2 to PDE in order to solve nonhomo-
geneous Laplace’s equation on the square with nonhomogeneous mixed BC.

• Section 4.5 contains seven methods for solving the heat equation on the interval
and square. We present the classic explicit, implicit, and Crank–Nicolson meth-
ods. We use this material as a place to introduce the ‘method of lines’ (MOL) as
a general and powerful PDE technique. We make use of the separation of vari-
able (SOV) eigenfunction expansion solution derivation, implementing Fourier
expansions in MATLAB to solve the heat equation, where the coefficients are
obtained by either exact formulas or numerical integration. We demonstrate that
it is possible to obtain reasonable approximations to solutions of the heat equa-
tion by solving one large sparse linear system with a single linear system solver
call.

• Section 4.6 contains a brief discussion of the wave equation, with examples on
the interval and square. We show how to implement the method of lines. We
provide discussion and exercises showing that the obvious explicit and implicit
methods experience difficulties with dissipation and phase drift, and present a
better alternative explicit method. It is demonstrated that a linear problem such
as the wave equation can be effectively set up and solved with a single invoca-
tion of a linear solver.

• Section 4.7 considers Tricomi’s equation. We are able to solve these boundary
value problems by combining Laplace’s equation and wave equation single-
linear-system techniques.

• In Sect. 4.8, we develop techniques for the creation of D2 matrices for other
regions. We consider the cube, disk, annulus, angular sectors of disk and annu-
lus, and sphere. Based on MATLAB’s built-in delsq function, we provide an
Another random document with
no related content on Scribd:
1.F.6. INDEMNITY - You agree to indemnify and hold the
Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and distribution
of Project Gutenberg™ electronic works, harmless from all liability,
costs and expenses, including legal fees, that arise directly or
indirectly from any of the following which you do or cause to occur:
(a) distribution of this or any Project Gutenberg™ work, (b)
alteration, modification, or additions or deletions to any Project
Gutenberg™ work, and (c) any Defect you cause.

Section 2. Information about the Mission of


Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many small
donations ($1 to $5,000) are particularly important to maintaining tax
exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About Project


Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.

Project Gutenberg™ eBooks are often created from several printed


editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.

You might also like