Table Of ContentAn Introduction to
Scientific Computing
®
with MATLAB and
Python Tutorials
An Introduction to
Scientific Computing
®
with MATLAB and
Python Tutorials
Sheng Xu
MATLAB® is a trademark of The MathWorks, Inc. and is 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® software or related products does not constitute endorsement or sponsorship by The
MathWorks of a particular pedagogical approach or particular use of the MATLAB® software.
First edition published 2022
by CRC Press
6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742
and by CRC Press
4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN
© 2022 Sheng Xu
CRC Press is an imprint of Taylor & Francis Group, LLC
Reasonable efforts have been made to publish reliable data and information, but the author and pub-
lisher cannot assume responsibility for the validity of all materials or the consequences of their use.
The authors and publishers have attempted to trace the copyright holders of all material reproduced
in this publication and apologize to copyright holders if permission to publish in this form has not
been obtained. If any copyright material has not been acknowledged please write and let us know so
we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, 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, access www.copyright.
com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA
01923, 978-750-8400. For works that are not available on CCC please contact mpkbookspermis-
sions@tandf.co.uk
Trademark notice: Product or corporate names may be trademarks or registered trademarks and are
used only for identification and explanation without intent to infringe.
ISBN: 978-1-032-06315-7 (hbk)
ISBN: 978-1-032-06318-8 (pbk)
ISBN: 978-1-003-20169-4 (ebk)
DOI: 10.1201/9781003201694
Typeset in Nimbus Roman
by KnowledgeWorks Global Ltd.
Publisher’s note: This book has been prepared from camera-ready copy provided by the authors
To my family and friends.
Contents
Preface xiii
Author xv
1 AnOverviewofScientificComputing 1
1.1 WhatIsScientificComputing? . . . . . . . . . . . . . . . . . . . . 1
1.2 ErrorsinScientificComputing . . . . . . . . . . . . . . . . . . . . 3
1.2.1 AbsoluteandRelativeErrors . . . . . . . . . . . . . . . . . 3
1.2.2 UpperBounds . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.3 SourcesofErrors . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 AlgorithmProperties . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Taylor’sTheorem 9
2.1 Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1.1 PolynomialEvaluation . . . . . . . . . . . . . . . . . . . . 10
2.2 Taylor’sTheorem . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2.1 TaylorPolynomials . . . . . . . . . . . . . . . . . . . . . . 12
2.2.2 TaylorSeries . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2.3 Taylor’sTheorem . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 AlternatingSeriesTheorem . . . . . . . . . . . . . . . . . . . . . 19
2.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.5 ProgrammingProblems . . . . . . . . . . . . . . . . . . . . . . . 22
3 RoundoffErrorsandErrorPropagation 25
3.1 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.1.1 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.2 Floating-PointNumbers . . . . . . . . . . . . . . . . . . . . . . . 26
3.2.1 ScientificNotationandRounding . . . . . . . . . . . . . . 27
3.2.2 DPFloating-PointRepresentation . . . . . . . . . . . . . . 29
3.3 ErrorPropagation . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.3.1 CatastrophicCancellation . . . . . . . . . . . . . . . . . . 32
3.3.2 AlgorithmStability . . . . . . . . . . . . . . . . . . . . . . 33
3.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.5 ProgrammingProblems . . . . . . . . . . . . . . . . . . . . . . . 37
vii
viii Contents
4 DirectMethodsforLinearSystems 43
4.1 MatricesandVectors . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.2 TriangularSystems . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.3 GEandA=LU . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.3.1 ElementaryMatrices . . . . . . . . . . . . . . . . . . . . . 51
4.3.2 A=LU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.3.3 SolvingAx=bbyA=LU . . . . . . . . . . . . . . . . . . 55
4.4 GEPPandPA=LU . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.4.1 GEPP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.4.2 PA=LU . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4.4.3 SolvingAx=bbyPA=LU . . . . . . . . . . . . . . . . . . 65
4.5 TridiagonalSystems . . . . . . . . . . . . . . . . . . . . . . . . . 67
4.6 ConditioningofLinearSystems . . . . . . . . . . . . . . . . . . . 69
4.6.1 VectorandMatrixNorms. . . . . . . . . . . . . . . . . . . 72
4.6.2 ConditionNumbers . . . . . . . . . . . . . . . . . . . . . . 76
4.6.3 ErrorandResidualVectors . . . . . . . . . . . . . . . . . . 77
4.7 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
4.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
4.9 ProgrammingProblems . . . . . . . . . . . . . . . . . . . . . . . 82
5 RootFindingforNonlinearEquations 85
5.1 RootsandFixedPoints . . . . . . . . . . . . . . . . . . . . . . . . 86
5.2 TheBisectionMethod . . . . . . . . . . . . . . . . . . . . . . . . 90
5.3 Newton’sMethod . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.3.1 ConvergenceAnalysisofNewton’sMethod . . . . . . . . . 96
5.3.2 PracticalIssuesofNewton’sMethod . . . . . . . . . . . . . 99
5.4 SecantMethod . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
5.5 Fixed-PointIteration . . . . . . . . . . . . . . . . . . . . . . . . . 105
5.6 Newton’sMethodforSystemsofNonlinearEquations . . . . . . . 109
5.6.1 Taylor’sTheoremforMultivariateFunctions . . . . . . . . 110
5.6.2 Newton’sMethodforNonlinearSystems . . . . . . . . . . 114
5.7 UnconstrainedOptimization . . . . . . . . . . . . . . . . . . . . . 117
5.8 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
5.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
5.10 ProgrammingProblems . . . . . . . . . . . . . . . . . . . . . . . 127
6 Interpolation 131
6.1 TerminologyofInterpolation . . . . . . . . . . . . . . . . . . . . . 132
6.2 PolynomialSpace . . . . . . . . . . . . . . . . . . . . . . . . . . 133
6.2.1 ChebyshevBasis . . . . . . . . . . . . . . . . . . . . . . . 135
6.2.2 LegendreBasis . . . . . . . . . . . . . . . . . . . . . . . . 138
6.3 MonomialInterpolation . . . . . . . . . . . . . . . . . . . . . . . 141
6.4 LagrangeInterpolation . . . . . . . . . . . . . . . . . . . . . . . . 143
6.5 Newton’sInterpolation . . . . . . . . . . . . . . . . . . . . . . . . 145
6.6 InterpolationError . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Contents ix
6.6.1 ErrorinPolynomialInterpolation . . . . . . . . . . . . . . 150
6.6.2 BehaviorofInterpolationError . . . . . . . . . . . . . . . 153
6.6.2.1 Equally-SpacedNodes. . . . . . . . . . . . . . . 153
6.6.2.2 ChebyshevNodes . . . . . . . . . . . . . . . . . 156
6.7 SplineInterpolation . . . . . . . . . . . . . . . . . . . . . . . . . 159
6.7.1 PiecewiseLinearInterpolation . . . . . . . . . . . . . . . . 159
6.7.2 CubicSpline . . . . . . . . . . . . . . . . . . . . . . . . . 161
6.7.3 CubicSplineInterpolation . . . . . . . . . . . . . . . . . . 163
6.8 DiscreteFourierTransform(DFT) . . . . . . . . . . . . . . . . . . 167
6.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
6.10 ProgrammingProblems . . . . . . . . . . . . . . . . . . . . . . . 177
7 NumericalIntegration 183
7.1 DefiniteIntegrals . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
7.2 NumericalIntegration . . . . . . . . . . . . . . . . . . . . . . . . 187
7.2.1 ChangeofIntervals . . . . . . . . . . . . . . . . . . . . . . 189
7.3 TheMidpointRule . . . . . . . . . . . . . . . . . . . . . . . . . . 191
7.3.1 DegreeofPrecision(DOP) . . . . . . . . . . . . . . . . . . 192
7.3.2 ErroroftheMidpointRule . . . . . . . . . . . . . . . . . . 195
7.4 TheTrapezoidalRule . . . . . . . . . . . . . . . . . . . . . . . . . 200
7.5 Simpson’sRule . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
7.6 Newton-CotesRules . . . . . . . . . . . . . . . . . . . . . . . . . 209
7.7 GaussianQuadratureRules . . . . . . . . . . . . . . . . . . . . . . 209
7.8 OtherNumericalIntegrationTechniques . . . . . . . . . . . . . . . 216
7.8.1 IntegrationwithSingularities. . . . . . . . . . . . . . . . . 216
7.8.2 AdaptiveIntegration . . . . . . . . . . . . . . . . . . . . . 217
7.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
7.10 ProgrammingProblems . . . . . . . . . . . . . . . . . . . . . . . 221
8 NumericalDifferentiation 225
8.1 DifferentiationUsingTaylor’sTheorem . . . . . . . . . . . . . . . 225
8.1.1 TheMethodofUndeterminedCoefficients . . . . . . . . . 227
8.2 DifferentiationUsingInterpolation . . . . . . . . . . . . . . . . . 229
8.2.1 DifferentiationUsingDFT . . . . . . . . . . . . . . . . . . 231
8.3 RichardsonExtrapolation . . . . . . . . . . . . . . . . . . . . . . 232
8.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
8.5 ProgrammingProblems . . . . . . . . . . . . . . . . . . . . . . . 235
9 InitialValueProblemsandBoundaryValueProblems 237
9.1 InitialValueProblems(IVPs) . . . . . . . . . . . . . . . . . . . . 238
9.1.1 Euler’sMethod . . . . . . . . . . . . . . . . . . . . . . . . 239
9.1.1.1 LocalTruncationErrorandGlobalError . . . . . 242
9.1.1.2 Consistency,ConvergenceandStability . . . . . . 244
9.1.1.3 ExplicitandImplicitMethods . . . . . . . . . . . 247
9.1.2 TaylorSeriesMethods . . . . . . . . . . . . . . . . . . . . 248