Transcript

K13161

Kharab • G

uentherA

n Introduction to Num

erical Methods

A M

ATLA

B®A

pproach

T H I R D E D I T I O N

An Introduction to Numerical Methods

A MATLAB®ApproachT H I R D E D I T I O N

An Introduction to Numerical Methods: A MATLAB® Approach, Third Edition continues to present a wide range of useful and important algorithms for scientific and engineering applications. The authors use MATLAB to illustrate each numerical method, providing full details of the computer results so that the main steps are easily visualized and interpreted. The accompanying CD-ROM contains simple MATLAB functions that help readers understand how the methods work.

New to the Third Edition• A chapter on the numerical solution of integral equations• A section on nonlinear partial differential equations (PDEs) in the last

chapter• Inclusion of MATLAB GUIs throughout the text

The book begins with simple theoretical and computational topics, including computer floating point arithmetic, errors, interval arithmetic, and the root of equations. After presenting direct and iterative methods for solving systems of linear equations, the authors discuss interpolation, spline functions, concepts of least-squares data fitting, and numerical optimization. They then focus on numerical differentiation and efficient integration techniques as well as a variety of numerical techniques for solving linear integral equations, ordinary differential equations, and boundary-value problems. The book concludes with numerical techniques for computing the eigenvalues and eigenvectors of a matrix and for solving PDEs.

Written in an easy-to-follow, simple style, this text improves readers’ ability to master the theoretical and practical elements of the methods. Through this book, they will be able to solve many numerical problems using MATLAB.

Mathematics

K13161_Cover.indd 1 10/14/11 11:11 AM

An Introduction to Numerical Methods

A MATLAB®Approach

T H I R D E D I T I O N

This page intentionally left blankThis page intentionally left blank

An Introduction to Numerical Methods

Abdelwahab KharabAbu Dhabi University

Ronald B. GuentherOregon State University

A MATLAB®Approach

T H I R D E D I T I O N

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.

CRC PressTaylor & Francis Group6000 Broken Sound Parkway NW, Suite 300Boca Raton, FL 33487-2742

© 2012 by Taylor & Francis Group, LLCCRC Press is an imprint of Taylor & Francis Group, an Informa business

No claim to original U.S. Government worksVersion Date: 2011909

International Standard Book Number-13: 978-1-4398-6900-0 (eBook - PDF)

This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the valid-ity 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 uti-lized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopy-ing, 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.

Visit the Taylor & Francis Web site athttp://www.taylorandfrancis.com

and the CRC Press Web site athttp://www.crcpress.com

“k” — 2011/11/22 — 10:14 — page 1 —�

Contents

1 Introduction 1

1.1 ABOUT MATLAB and MATLAB GUI (Graphical User Interface) 1

1.2 AN INTRODUCTION TO MATLAB . . . . . . . . . . . . . . . . . 2

1.2.1 Matrices and matrix computation . . . . . . . . . . . . . . . 2

1.2.2 Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.2.3 Output format . . . . . . . . . . . . . . . . . . . . . . . . . 7

1.2.4 Planar plots . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

1.2.5 3-D mesh plots . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.2.6 Function files . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.2.7 Defining functions . . . . . . . . . . . . . . . . . . . . . . . 10

1.2.8 Relations and loops . . . . . . . . . . . . . . . . . . . . . . . 12

1.3 TAYLOR SERIES . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

2 Number System and Errors 23

2.1 FLOATING-POINT ARITHMETIC . . . . . . . . . . . . . . . . . 23

2.2 ROUND-OFF ERRORS . . . . . . . . . . . . . . . . . . . . . . . . 28

2.3 TRUNCATION ERROR . . . . . . . . . . . . . . . . . . . . . . . . 32

2.4 INTERVAL ARITHMETIC . . . . . . . . . . . . . . . . . . . . . . 34

3 Roots of Equations 39

3.1 THE BISECTION METHOD . . . . . . . . . . . . . . . . . . . . . 40

3.2 THE METHOD OF FALSE POSITION . . . . . . . . . . . . . . . 48

3.3 FIXED POINT ITERATION . . . . . . . . . . . . . . . . . . . . . 53

3.4 THE SECANT METHOD . . . . . . . . . . . . . . . . . . . . . . . 60

3.5 NEWTON’S METHOD . . . . . . . . . . . . . . . . . . . . . . . . 65

3.6 CONVERGENCE OF THE NEWTON ANDSECANT METHODS . . . . . . . . . . . . . . . . . . . . . . . . . 74

3.7 MULTIPLE ROOTS AND THE MODIFIEDNEWTON METHOD . . . . . . . . . . . . . . . . . . . . . . . . . 77

3.8 NEWTON’S METHOD FOR NONLINEARSYSTEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

“k” — 2011/11/22 — 10:14 — page 2 —�

4 System of Linear Equations 95

4.1 MATRICES AND MATRIX OPERATIONS . . . . . . . . . . . . . 96

4.2 NAIVE GAUSSIAN ELIMINATION . . . . . . . . . . . . . . . . . 99

4.3 GAUSSIAN ELIMINATION WITH SCALED PARTIALPIVOTING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

4.4 LU DECOMPOSITION . . . . . . . . . . . . . . . . . . . . . . . . 119

4.4.1 Crout’s and Cholesky’s methods . . . . . . . . . . . . . . . . 120

4.4.2 Gaussian elimination method . . . . . . . . . . . . . . . . . 124

4.5 ITERATIVE METHODS . . . . . . . . . . . . . . . . . . . . . . . 133

4.5.1 Jacobi iterative method . . . . . . . . . . . . . . . . . . . . 133

4.5.2 Gauss-Seidel iterative method . . . . . . . . . . . . . . . . . 135

4.5.3 Convergence . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

5 Interpolation 153

5.1 POLYNOMIAL INTERPOLATION THEORY . . . . . . . . . . . 154

5.2 NEWTON’S DIVIDED-DIFFERENCE INTERPOLATINGPOLYNOMIAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

5.3 THE ERROR OF THE INTERPOLATINGPOLYNOMIAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

5.4 LAGRANGE INTERPOLATING POLYNOMIAL . . . . . . . . . 171

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

6 Interpolation with Spline Functions 181

6.1 PIECEWISE LINEAR INTERPOLATION . . . . . . . . . . . . . . 182

6.2 QUADRATIC SPLINE . . . . . . . . . . . . . . . . . . . . . . . . . 187

6.3 NATURAL CUBIC SPLINES . . . . . . . . . . . . . . . . . . . . . 192

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

7 The Method of least-squares 207

7.1 LINEAR least-squares . . . . . . . . . . . . . . . . . . . . . . . . . 208

7.2 LEAST-SQUARES POLYNOMIAL . . . . . . . . . . . . . . . . . . 214

7.3 NONLINEAR least-squares . . . . . . . . . . . . . . . . . . . . . . 221

7.3.1 Exponential form . . . . . . . . . . . . . . . . . . . . . . . . 222

7.3.2 Hyperbolic form . . . . . . . . . . . . . . . . . . . . . . . . . 223

7.4 TRIGONOMETRIC LEAST-SQUARES POLYNOMIAL . . . . . . 229

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

8 Numerical Optimization 235

8.1 ANALYSIS OF SINGLE-VARIABLE FUNCTIONS . . . . . . . . 235

8.2 LINE SEARCH METHODS . . . . . . . . . . . . . . . . . . . . . . 237

8.2.1 Bracketing the minimum . . . . . . . . . . . . . . . . . . . . 238

8.2.2 Golden section search . . . . . . . . . . . . . . . . . . . . . . 238

8.2.3 Fibonacci Search . . . . . . . . . . . . . . . . . . . . . . . . 241

8.2.4 Parabolic Interpolation . . . . . . . . . . . . . . . . . . . . . 243

“k” — 2011/11/22 — 10:14 — page 3 —�

� �

8.3 MINIMIZATION USING DERIVATIVES . . . . . . . . . . . . . . 251

8.3.1 Newton’s method . . . . . . . . . . . . . . . . . . . . . . . . 251

8.3.2 Secant method . . . . . . . . . . . . . . . . . . . . . . . . . 251

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

9 Numerical Differentiation 257

9.1 NUMERICAL DIFFERENTIATION . . . . . . . . . . . . . . . . . 257

9.2 RICHARDSON’S FORMULA . . . . . . . . . . . . . . . . . . . . . 264

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

10 Numerical Integration 273

10.1 TRAPEZOIDAL RULE . . . . . . . . . . . . . . . . . . . . . . . . 274

10.2 SIMPSON’S RULE . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

10.3 ROMBERG ALGORITHM . . . . . . . . . . . . . . . . . . . . . . 293

10.4 GAUSSIAN QUADRATURE . . . . . . . . . . . . . . . . . . . . . 300

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

11 Numerical Methods for Linear Integral Equations 317

11.1 INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

11.2 QUADRATURE RULES . . . . . . . . . . . . . . . . . . . . . . . . 320

11.2.1 Trapezoidal rule . . . . . . . . . . . . . . . . . . . . . . . . . 321

11.2.2 The Gauss-Nystrom method . . . . . . . . . . . . . . . . . . 323

11.3 THE SUCCESSIVE APPROXIMATION METHOD . . . . . . . . 330

11.4 SCHMIDT’s METHOD . . . . . . . . . . . . . . . . . . . . . . . . 332

11.5 VOLTERRA-TYPE INTEGRAL EQUATIONS . . . . . . . . . . . 334

11.5.1 Euler’s method . . . . . . . . . . . . . . . . . . . . . . . . . 335

11.5.2 Heun’s method . . . . . . . . . . . . . . . . . . . . . . . . . 336

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340

12 Numerical Methods for Differential Equations 343

12.1 EULER’S METHOD . . . . . . . . . . . . . . . . . . . . . . . . . . 344

12.2 ERROR ANALYSIS . . . . . . . . . . . . . . . . . . . . . . . . . . 350

12.3 HIGHER-ORDER TAYLOR SERIES METHODS . . . . . . . . . . 355

12.4 RUNGE-KUTTA METHODS . . . . . . . . . . . . . . . . . . . . . 358

12.5 MULTISTEP METHODS . . . . . . . . . . . . . . . . . . . . . . . 372

12.6 ADAMS-BASHFORTH METHODS . . . . . . . . . . . . . . . . . 372

12.7 PREDICTOR-CORRECTOR METHODS . . . . . . . . . . . . . . 381

12.8 ADAMS-MOULTON METHODS . . . . . . . . . . . . . . . . . . . 382

12.9 NUMERICAL STABILITY . . . . . . . . . . . . . . . . . . . . . . 390

12.10 HIGHER-ORDER EQUATIONS AND SYSTEMSOF DIFFERENTIAL EQUATIONS AND SYSTEMS . . . . . . . . 393

12.11 IMPLICIT METHODS AND STIFF SYSTEMS . . . . . . . . . . 400

12.12 PHASE PLANE ANALYSIS: CHAOTIC DIFFERENTIALEQUATIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

“k” — 2011/11/22 — 10:14 — page 4 —�

13 Boundary-Value Problems 415

13.1 FINITE-DIFFERENCE METHODS . . . . . . . . . . . . . . . . . 416

13.2 SHOOTING METHODS . . . . . . . . . . . . . . . . . . . . . . . . 423

13.2.1 The nonlinear case . . . . . . . . . . . . . . . . . . . . . . . 423

13.2.2 The linear case . . . . . . . . . . . . . . . . . . . . . . . . . 428

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435

14 Eigenvalues and Eigenvectors 439

14.1 BASIC THEORY . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439

14.2 THE POWER METHOD . . . . . . . . . . . . . . . . . . . . . . . 443

14.3 THE QUADRATIC METHOD . . . . . . . . . . . . . . . . . . . . 447

14.4 EIGENVALUES FOR BOUNDARY-VALUE PROBLEMS . . . . . 456

14.5 BIFURCATIONS IN DIFFERENTIAL EQUATIONS . . . . . . . 458

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463

15 Partial Differential Equations 465

15.1 PARABOLIC EQUATIONS . . . . . . . . . . . . . . . . . . . . . . 466

15.1.1 Explicit methods . . . . . . . . . . . . . . . . . . . . . . . . 466

15.1.2 Implicit methods . . . . . . . . . . . . . . . . . . . . . . . . 470

15.2 HYPERBOLIC EQUATIONS . . . . . . . . . . . . . . . . . . . . . 477

15.3 ELLIPTIC EQUATIONS . . . . . . . . . . . . . . . . . . . . . . . . 482

15.4 NONLINEAR PARTIAL DIFFERENTIAL EQUATIONS . . . . . 489

15.4.1 Burger’s equation . . . . . . . . . . . . . . . . . . . . . . . . 489

15.4.2 Reaction-diffusion equation . . . . . . . . . . . . . . . . . . 491

15.4.3 Porous media equation . . . . . . . . . . . . . . . . . . . . . 493

15.4.4 Hamilton-Jacobi-Bellman equation . . . . . . . . . . . . . . 494

15.5 INTRODUCTION TO FINITE-ELEMENT METHOD . . . . . . . 496

15.5.1 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497

15.5.2 The Finite-Element Method . . . . . . . . . . . . . . . . . . 503

APPLIED PROBLEMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508

Bibliography and References 510

Appendix 515

A Calculus Review 515

A.1 Limits and continuity . . . . . . . . . . . . . . . . . . . . . . . . . . 515

A.2 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516

A.3 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516

B MATLAB Built-in Functions 519

C Text MATLAB Functions 523

“k” — 2011/11/22 — 10:14 — page 5 —�

D MATLAB GUI 525D.1 Roots of Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . 525D.2 System of Linear Equations . . . . . . . . . . . . . . . . . . . . . . 527D.3 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528D.4 The Method of Least Squares . . . . . . . . . . . . . . . . . . . . . 528D.5 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529D.6 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530D.7 Numerical Methods for Differential Equations . . . . . . . . . . . . 530D.8 Boundary-Value Problems . . . . . . . . . . . . . . . . . . . . . . . 531D.9 Numerical Methods for PDEs . . . . . . . . . . . . . . . . . . . . . 532

Answers to Selected Exercises 535

Index 565

This page intentionally left blankThis page intentionally left blank

“k” — 2011/11/22 — 10:14 — page 7 —�

Preface

This is a textbook designed for an introductory course in numerical methods. Itdeals with the theory and application of the most commonly used numerical meth-ods for solving numerical problems on microcomputers. It is intended for studentsin mathematics, science, and engineering who have completed the introductory cal-culus sequence. In addition, the reader is assumed to have taken a structuredprogramming course. The thrust of this text is to assist the students to becomefamiliar with the most common numerical methods encountered in science and en-gineering. The content material of this book has been designed to be compatiblewith any introductory numerical textbook that exists in the market. Students willbe able to examine and solve many numerical problems, using MATLAB R©1 in ashort period of time.

Due to the rapid advancement of computer technology and software developments,we have used MATLAB as the computing environment throughout all the chaptersof the book. Each numerical method discussed in this book is demonstrated throughthe use of MATLAB which is easy to use and has many features such as:

1. Powerful matrix structure,

2. Powerful two- and three-dimensional graphing facilities,

3. A vast number of powerful built-in functions,

4. MATLAB’s structured programming style that resembles FORTRAN and BA-SIC.

The goal of this present third edition is the same as the previous one. The bookintroduces students to a wide range of useful and important algorithms. Computerresults are presented with full details so that the main steps of the algorithm of each

1MATLABR© is a registered trademark of the MathWorks, Inc.For product information, please contact:

The MathWorks, Inc.3 Apple Hill DriveNatick, MA 01760-2098 USA

Tel: 508-647-7000 Fax: 508-647-7001E-mail: [email protected] Web: www.mathworks.com

“k” — 2011/11/22 — 10:14 — page 8 —�

numerical method are visualized and interpreted. For this reason, a supplementaryCD-ROM, attached at the back of the book, has been developed for students’ usewith MATLAB. The CD-ROM contains simple MATLAB functions that give a clearstep-by-step explanation of the mechanism behind the algorithm of each numericalmethod covered. Emphasis is placed on understanding how the methods work.These functions guide the student through the calculations necessary to understandthe algorithm(s). The main feature of this book, beside the use of MATLAB as itscomputing environment, is that the style of the book is easy, direct, and simple.This is likely to boost students’ confidence in their ability to master the elementsof the subject.

The book is organized in a fairly standard manner. Topics that are simpler, boththeoretically and computationally, come first; for example, the root of equationsis covered in Chapter 3. Chapter 2 contains an introduction to computer floatingpoint arithmetic, errors, and interval arithmetic.

Both direct and iterative methods are presented in Chapter 4 for solving systemsof linear equations.

Interpolation, spline functions, concepts of least squares data fitting, and numer-ical optimization are the subjects of Chapters 5, 6, 7, and 8. Interpolation formsthe theoretical basis for much of numerical analysis.

Chapters 9 and 10 are devoted to numerical differentiation and integration. Sev-eral efficient integration techniques are presented.

In Chapters 11 and 12 a wide variety of numerical techniques is presented for solv-ing linear integral equations and ordinary differential equations. An introductionfor solving boundary value problems is presented in Chapter 13. Chapter 14 is de-voted to some numerical techniques for computing the eigenvalues and eigenvectorsof a matrix.

The last Chapter 15 provides a basic introduction to numerical techniques forsolving partial differential equations.

In each chapter we have attempted to present clear examples in every sectionfollowed by a good number of related exercises at the end of each section withanswers to some exercises.

It is the purpose of this book to implement various important numerical methodson a personal computer and to provide not only a source of theoretical informationon the methods covered, but also to allow the student to easily interact with themicrocomputer and the algorithm for each method using MATLAB.

This text should provide an excellent tool suitable for teaching numerical methodcourses at colleges and universities. It is also suitable for self-study purposes.

In this third edition all suggestions from the expert reviewers were addressed.They all deserve our sincere thanks and appreciation.

Features in the Third Edition

There have been some minor changes in some sections. Major new features areas follows:

• A new chapter on numerical solution of integral equations.

“k” — 2011/11/22 — 10:14 — page 9 —�

• A new section on nonlinear PDEs has been added in Chapter 15.

• MATLAB GUI has been incorporated as an integral part of the text.

Acknowledgments

We wish to thank the many persons who have helped us in the creation of thisbook. They are: R. Baker Kearfoot of the University of Southwestern Louisiana,Rachid Kharab of the University of Rouen, A. Laradji of the King Fahd Universityof Petroleum & Minerals, A.R. Kashani of the Sahand University of Technology,Z. Khiari of King Fahd University of Petroleum & Minerals, and H. Cheded of theKing Fahd University of Petroleum & Minerals, who encouraged us to submit abook proposal.

Special thanks is due to Dr. B. Khoshandam of Semnan University for his keeninterest and suggestions in this edition and careful work on the use of MATLABGUI. His work was extraordinarily detailed and helpful to us.

We also thank the Anonymous Reviewers who made useful recommendations forthe third edition.

The authors remain very grateful to the editorial and production staff of Chap-man & Hall/CRC who have been helpful and available at all stages. Among themare Sarah Morris, Project Editor, who was in communication with us during thepreparation of the final version of this third edition, and Sunil Nair, mathematicsand statistics publisher, for his assistance and encouragement.

Finally, the authors are grateful for the financial and facilities support providedby Mr. H. Hassnaoui, General Director of the GFP in Muscat.

Suggestions for improvements to the book are always welcome and can be madeby e-mail at: [email protected].

Abdelwahab KharabRonald B. Guenther

“k” — 2011/11/22 — 10:14 — page 1 —�

Chapter 1

Introduction

The Taylor Series is one of the most important tools in numerical analysis. Itconstitutes the foundation of numerical methods and will be used in most of thechapters of this text. From the Taylor Series, we can derive the formulas and errorestimates of the many numerical techniques used. This chapter contains a reviewof the Taylor Series, and a brief introduction to MATLABR©.

1.1 ABOUT MATLAB and MATLAB GUI (Graphical User Inter-face)

MATLAB (MATrix LABoratory) is a powerful interactive system for matrix-based computation designed for scientific and engineering use. It is good for manyforms of numeric computation and visualization. MATLAB language is a high-level matrix/array language with control flow statements, functions, data structures,input/output, and object-oriented programming features. To fully use the powerand computing capabilities of this software program in classrooms and laboratoriesby teachers and students in science and engineering, part of this text is intended tointroduce the computational power of MATLAB to modern numerical methods.

MATLAB has several advantages. There are three major elements that havecontributed to its immense popularity. First, it is extremely easy to use sincedata can be easily entered, especially for algorithms that are adaptable to a tableformat. This is an important feature because it allows students to experimentwith many numerical problems in a short period of time. Second, it includes high-level commands for two-dimensional and three-dimensional data visualization, andpresentation graphics. Plots are easily obtained from within a script or in commandmode. Third, the most evident power of a MATLAB is its speed of calculation.The program gives instantaneous feedback. Because of their popularity, MATLABand other software such as MAPLE and Mathematica are now available in mostuniversity microcomputer laboratories.

One of the primary objectives of this text is to give students a clear step-by-stepexplanation of the algorithm corresponding to each numerical method used. To

1

“k” — 2011/11/22 — 10:14 — page 2 —�

2 INTRODUCTION

accomplish this objective, we have developed MATLAB M-functions contained ina supplementary CD-ROM at the back of the book. These M-files can be used forthe application or illustration of all numerical methods discussed in this text. EachM-function will enable students to solve numerical problems simply by entering dataand executing the M-functions.

It is well known that the best way to learn computer programming is to writecomputer programs. Therefore, we believe that by understanding the basic theoryunderlying each numerical method and the algorithm behind it, students will havethe necessary tools to write their own programs in a high-level computer languagesuch as C or FORTRAN.

Another future of MATLAB is that it provides the Graphical User Interface(GUI). It is a pictorial interface to a program intended to provide students witha familiar environment in which to work. This environment contains push buttons,toggle buttons, lists, menus, text boxes, and so forth, all of which are already famil-iar to the user, so that they can concentrate on using the application rather than onthe mechanics involved in doing things. They do not have to type commands at thecommand line to run the MATLAB functions. For this reason, we introduced GUIin this edition to make it easier for the students to run the M-functions of the book.A readme file named ”SUN Package readme contained in the directory NMETH”of the CD attached at the back cover of the book, explains in details the steps tofollow to run the MATLAB functions using GUI. In addition, Appendix D showsthe use of GUI for solving several examples.

1.2 AN INTRODUCTION TO MATLAB

In this section we give to the reader a brief tutorial introduction to MATLAB.For additional information we urge the reader to use the reference and user’s guidesof MATLAB.

1.2.1 Matrices and matrix computation

MATLAB treats all variables as matrices. They are assigned to expressions byusing an equal sign and their names are case-sensitive. For example,

>> A = [4 -2 5; 6 1 7; -1 0 6]

A =4 −2 56 1 7−1 0 6

New rows may be indicated by a new line or by a semicolon. A column vectormay be given as

>> x = [2; 8; 9] or x = [2 8 9]’

x =

“k” — 2011/11/22 — 10:14 — page 3 —�

AN INTRODUCTION TO MATLAB 3

289

Elements of the matrix can be a number or an expression, like

>> x = [2 1+2 12/4 2^3]

x =2 3 3 8

One can define an array with a particular structure by using the command

x = a : step : b

As an example

>> y = [0: 0.2 : 1]y =

0 0.2000 0.4000 0.6000 0.8000 1.0000

>> y = [0: pi/3 : pi]y =

0 1.0472 2.0944 3.1416

>> y = [20: -5 : 2]

y =20 15 10 5

MATLAB has a number of special matrices that can be generated by built-infunctions

>> ones(2)

ans =1 11 1

a matrix of all 1’s.

>> zeros(2,4)ans =

0 0 0 00 0 0 0

a 2× 4 matrix of zeros.

>> rand(2,4)

ans =0.9501 0.6068 0.8913 0.45650.2311 0.4860 0.7621 0.0185

“k” — 2011/11/22 — 10:14 — page 4 —�

4 INTRODUCTION

a 2× 4 random matrix with uniformly distributed random elements.

>> eyes(3)

ans =1 0 00 1 00 0 1

the 3× 3 identity matrix.

The diag function either creates a matrix with specified values on a diagonal orextracts the diagonal entries. For example,

>> v = [3 2 5];

>> D=diag(v)

D =3 0 00 2 00 0 5

a 3× 3 diagonal matrix with v on the main diagonal.

To extract the diagonal entries of an existing matrix, the diag function is used:

>> C=[2 -4 7; 3 1 8; -1 5 6];

>> u=diag(C)

u =216

The linspace function generates row vectors with equally spaced elements. Theform of the function is

linspace(firstValue, lastValue, numValues)

where firstValue and lastValue are the starting and ending values in the sequenceof elements and NumValues is the number of elements to be created. For example,

>> evens = linspace(0,10,6)

evens =

0 2 4 6 8 10

The most useful matrix functions are

“k” — 2011/11/22 — 10:14 — page 5 —�

AN INTRODUCTION TO MATLAB 5

eig eigenvalues and eigenvectors

inv inverse

lu LU decomposition

qr QR factorization

rank rank

A component-by-component addition of two vectors of the same dimensions isindicated as

>> x = [1 5 8];>> y = [2 4 3];>> x + y

ans =

3 9 11

Multiplying a vector or matrix by a scalar will scale each element of the vectoror matrix by the value of the scalar.

>> C = 2*[1 3; 4 2]

C =2 68 4

>> v = 3*[1 4 -5 7]

v =

3 12 − 15 21

The component-by-component multiplication of the vectors x and y is indicated as

>> x. * y

ans =

2 20 24

The inner, or dot, product of two vectors is a scaler and can be obtained by mul-tiplying a row vector and a column vector. For example,

>> u=[5 7 -1 2]; v=[2; 3; 10; 5];>> u*v

ans =

31

“k” — 2011/11/22 — 10:14 — page 6 —�

6 INTRODUCTION

The transpose of a matrix or a vector is denoted by a prime symbol. For example

>> x’

ans =158

The matrix operations of multiplication, power, and division are indicated as

>> B = [1 3; 4 2];

>> A = [2 5; 0 6];

>> A*B

ans =22 1624 12

>> A^2

ans =4 400 36

>> A/B

ans =1.6000 0.10002.4000 −0.6000

>> A.*B

ans =2 150 12

Note that the three operations of multiplication, division, and power can operateelementwise by preceding them with a period.

1.2.2 Polynomials

MATLAB provides a number of useful built-in functions that operates on poly-nomials. The coefficients of a polynomial are specified by the entries of a vector.For example, the polynomial p(x) = 2x3 − 5x2 + 8 is specified by [2 -5 0 8]. Toevaluate the polynomial at a given x we use the polyval function.

>> c = [2 -5 0 8];

>> polyval(c,2)

ans =

“k” — 2011/11/22 — 10:14 — page 7 —�

AN INTRODUCTION TO MATLAB 7

4evaluates the polynomials p(x) = 2x3 − 5x2 + 8 at x = 2.

The function roots finds the n roots of a polynomial of degree n. For example, tofind the roots of the polynomial p(x) = x3 − 5x2 − 17x+ 21, enter

>> a = [1 -5 -17 21];

>> roots(a)

ans =7.0000−3.0000

1.0000

1.2.3 Output format

While all computations in MATLAB are performed in double precision, the formatof the displayed output can be controlled by the following commands:

format short fixed point with 4 decimal places (the default)

format long fixed point with 14 decimal places

format short e scientific notation with 4 decimal places

format long e scientific notation with 15 decimal places

format rat approximation by ratio of small integers

Once invoked, the chosen format remains in effect until changed.

It is possible to make the printed output from a MATLAB function look good byusing the disp and fprintf functions.

disp(X) displays the array X, without printing the array name. If X is a string,the text is displayed. For example,

disp( ’The input matrix A is’)

will display the text placed in single quotes. The function disp also allows us toprint numerical values placed in text strings. For example,

disp([’Newton’s method converges after ’,num2str(iter),’ iterations’])

will write on the screen

Newton’s method converges after 9 iterations.

Here the function num2str(iter) converts a number to a string.

The other function fprintf is more flexible and has the form

“k” — 2011/11/22 — 10:14 — page 8 —�

8 INTRODUCTION

fprintf(’filename’,’format’,list)

where

filename is optional; if it is omitted, output goes to the screen.

format is a format control string containing conversion specifications or any optionaltext. The conversion specifications control the output of array elements.

Common conversion specifications include:

%P.Qe for exponential%P.Qf fixed point%P.Qg to automatically select the shorter of %P.Qe or %P.Qf

where P and Q are integers that set the field width and the number of decimalplaces, respectively.

list is a list of variable names separated by commas, and the special format \n pro-duces a new line.

For example, the statements

>> x = pi ; y = 4.679 ; z = 9 ;>> fprintf(’\n x = %8.7f\n y = %4.3f\n z = %1.0f\n’,x,y,z)

prints on the screen

x = 3.1415927y = 4.679z = 9

1.2.4 Planar plots

Plotting functions is very easy in MATLAB. Graphs of two-dimensional functionscan be generated by using the MATLAB command plot. You can, for example, plotthe function y = x2 + 1 over the interval [−1, 2] with the following commands:

>> x = [-1: 0.02: 2];>> y = x.ˆ2 +1;>> plot(x,y)

The plot is shown in Figure 1.1. The command plot(x,y,’r+:’) will result in r=redwith + for points and dotted line. Use help plot for many options.

To plot a function f1 defined by an M-file (see Subsection 1.2.7) in an interval[a, b], we use the command

>> fplot(’f1’,[a,b]).

“k” — 2011/11/22 — 10:14 — page 9 —�

AN INTRODUCTION TO MATLAB 9

−1 −0.5 0 0.5 1 1.5 21

1.5

2

2.5

3

3.5

4

4.5

5

FIGURE 1.1Plot of the function f(x) = x2 + 1 in [−1, 2].

Other features can be added to a given graph using commands such as grid, xlabel,ylabel, title, etc.... For example, the command grid on will create grid lines on thegraph.

Plots of parametrically defined curves can also be made. Try, for example,

t=[0: 0.01: 4*pi]; x=cos(2*t); y=sin(3*t); plot(x,y).

Multiple plots on a single graph are illustrated by

>> x=[0:0.1:2*pi];>> y1=cos(x); y2=sin(x); y3=cos(2*x);>> plot(x,y1,x,y2,x,y3)

1.2.5 3-D mesh plots

Three-dimensional or 3-D mesh surface plots are drawn with the function mesh.The command mesh(z) creates a three-dimensional perspective plot of the elementsof the matrix z.

The following example will generate the plot shown in Figure 1.2 of the functionz = e−x2−y2 over the square [−4, 4]× [−4, 4].

>> [x y]=meshgrid(-4.0:0.2:4.0,-4.0:0.2:4.0);>> z=exp(-x.^2-y.^2);>> mesh(x,y,z)

The first command creates a matrix whose entries are the points of a grid in

“k” — 2011/11/22 — 10:14 — page 10 —�

10 INTRODUCTION

−4

−2

0

2

4

−4

−2

0

2

40

0.2

0.4

0.6

0.8

1

FIGURE 1.2Three-dimensional surface.

the square −4 ≤ x ≤ 4, −4 ≤ y ≤ 4 with grid 0.2 units wide and 0.2 units tall.The second command creates a matrix whose entries are the values of the functionz(x, y) at the grid points. The third command uses this information to constructthe graph.

Other commands related to 3-D graphics are plot3 and surf.

1.2.6 Function files

MATLAB contains a vast collection of built-in functions from elementary func-tions like sine and cosine, to more sophisticated functions like matrix inversions,matrix eigenvalues, and fast Fourier transforms. Table 1.1 contains a small se-lection of elementary mathematical MATLAB functions and Appendix B containsadditional lists of functions grouped by subject area.

Note that all these function names must be in lower cases. MATLAB functionsmay have single or multiple arguments. For example,

[U,D]=eig(A)

produces a matrix U whose columns are the eigenvectors of A and a diagonal matrixD with the eigenvalues of A on its diagonal.

1.2.7 Defining functions

MATLAB allows users to define their own functions by constructing an M-file inthe M-file Editor/Debugger. The first line of a function has the form

“k” — 2011/11/22 — 10:14 — page 11 —�

AN INTRODUCTION TO MATLAB 11

cos(x) cosine of xsin(x) sine of xtan(x) tangent of xsqrt(x) square root of xabs(x) absolute value of xexp(x) exponential of xlog(x) log to the base e of xlog10(x) log to the base 10 of xcosh(x) hyperbolic cosine of xtanh(x) hyperbolic tangent of xasin(x) inverse sine function of xexpm(A) exponential of matrix A

Table 1.1 Elementary mathematical MATLAB functions.

function y = function name(input arguments)

As an example, let us define the function f1(x) = 2x3−3x+1 as an m.file. Usingthe Editor/Debugger or any text editor like emacs, or vi, we enter

function y=f1(x)y=2*x.ˆ3-3*x+1;

Once this function is saved as an M-file named f1.m, we can use the MATLABcommand window to compute, for example,

>> f1(2)ans =

11

User defined functions can themselves have functions as input parameters. Theycan be evaluated internally by using the function feval. For example,

>> x1=fzero(’f1’,0)x1=

0.3949gives the zero of f1 closest to 0.

We can pass one function f1.m to another function f2.m in MATLAB by usingthe @ symbol:

>> f2(@f1)

For example, the built-in function quad(f,a,b) in MATLAB approximately inte-grates the function y = f(x) from a to b. To use this built-in function to approx-imately integrate y = cos(πx) + 2 from x = −1 to x = 1, we begin by writing a

“k” — 2011/11/22 — 10:14 — page 12 —�

12 INTRODUCTION

MATLAB function called f1.m:

function y=f1(x)

y=cos(pi*x)+2;

If we are in the correct directory, we type in MATLAB:

>> quad(@f1,-1,1)

ans =

4.00000001667477

1.2.8 Relations and loops

Like most computer languages, MATLAB offers a variety of flow control state-ments like for, while, and if. The statements, that we use to control the flow arecalled relations.

The Relational operators in MATLAB are

== equal

<= less than or equal

>= greater than or equal

˜ = not equal

< less than

> greater than

Note that “=” is used in an assignment statement while “==” is used in a relation.

Logical operators

& and

| or

˜ not

if, for, while statements

The general form of an if statement is

if <if expression><statements>

elseif <if expression>

<statements>

...

else

“k” — 2011/11/22 — 10:14 — page 13 —�

AN INTRODUCTION TO MATLAB 13

<statements>end

Here is a simple example. If i, j = 1, ..., 10, then

if i==jA(i,j)=1;

elseA(i,j)=0;

end

will result in a diagonal matrix A with 1’s in its diagonal.

The general form of a for loop is

for <loop variable= loop expression><statements>

end

For example,

S=0;for i=1:100

S=S+1;endS

results in the output

S =100

You can also nest for statements. The following statements create an m × nHilbert matrix, H.

for i=1:mfor j=1:n

H(i,j)=1/(i+j-1);end

end

The switch command is ideal for decision making. The general form of the switchcommand is

switch switch expressioncase case-expression

commands

“k” — 2011/11/22 — 10:14 — page 14 —�

14 INTRODUCTION

. . . .

otherwise

commands

end

Here is a code that finds the number of days in a month. For simplicity we willignore the effect of leap years on the number of days in February and assume thatthere are always 28 days in February. The switch command is ideal for this kind ofcode. We implement it as follows:

switch month

case {1,3,5,7,8,10,12}days = 31;

case {4,6,9,11}days = 30;

case 2

days = 28;

end

The variable month carries the number of the month, and depending on its valuethe first case, second case, or third case will be executed depending on the outcomeof comparing the switch expression (month) to the members of the various caseexpressions. The same result could also have been accomplished with an if-structure,but in this case the switch-command resulted in a more compact and elegant code.

Finally, MATLAB also has its own version of the while loop, which repeatedlyexecutes a group of statements while a condition remains true. Its form is

while < while expression >

< condition statements>

end

Given a positive number n the following statements compute and display the evenpowers of 3 less than n.

k=0;while 3^k < n

if rem(k,2)==03^kk=k+1;

elsek=k+1;

endend

“k” — 2011/11/22 — 10:14 — page 15 —�

TAYLOR SERIES 15

1.3 TAYLOR SERIES

THEOREM 1.1 (Taylor)

Suppose f has n+ 1 continuous derivatives on [a, b]. Let x, x0 ∈ [a, b]. Then

f(x) =n∑

k=0

f (k)(x0)

k!(x− x0)k +Rn+1(x) (1.1)

where

Rn+1(x) =f (n+1)(ξ)

(n+ 1)!(x− x0)n+1 (1.2)

for some ξ = ξ(x) between x0 and x.

Eqn. (1.1) is called the Taylor series expansion of f(x) about x0 and Rn+1(x)is called the remainder or the truncation error. Since in practical computationwith the Taylor series only a finite number of terms can be carried out, the termtruncation error generally refers to the error involved in using a finite summationto approximate the sum of an infinite series.

EXAMPLE 1.1

Derive the Taylor series for f(x) = ex near x0 = 0.

Since f (k)(0) = e0 = 1 for k = 0, 1, . . . , then from Eqn. (1.1) we have

ex = 1 + x+x2

2!+x3

3!+ · · ·+ xn

n!+

(n+ 1)!xn+1

=n∑

k=0

xk

k!+

(n+ 1)!xn+1. (1.3)

EXAMPLE 1.2

Using Example 1.1, approximate e with n = 3.

Set x = 1 and n = 3 in (1.3) to get

e ≈ 1 +1

1!+

1

2!+

1

3!= 2.66667.

Now using (1.2), one can get an estimate of the truncation error. We have the result

“k” — 2011/11/22 — 10:14 — page 16 —�

16 INTRODUCTION

R4 =eξ

4!with 0 < ξ < 1.

Since∣∣∣eξ∣∣∣ ≤ e ≤ 2.8, the error is no greater in magnitude than

(1/4!)(2.8) = 0.11667.

If we let x0 = 0 in Eqn. (1.1), we obtain the series known as Maclaurin series

f(x) =n∑

k=0

fk(0)

k!xk +

f (n+1)(ξ)

(n+ 1)!xn+1 (1.4)

with ξ(x) between 0 and x.

EXAMPLE 1.3Use the Taylor series to approximate e to three decimal places.

Using (1.1) we need to choose n so large that

(n+ 1)!<

2.8

(n+ 1)!< 5× 10−4

which yields n = 7 for the smallest n that approximates e to three decimal places.Setting n = 7 in the Taylor series we obtain

e ≈ 1 +1

1!+

1

2!+

1

3!+

1

4!+

1

5!+

1

6!+

1

7!= 2.718253968254.

The calculator value is 2.718281828459.

EXAMPLE 1.4Derive the Maclaurin series for f(x) = sinx.

The derivatives of sin x are

f ′(x) = cos x, f ′(0) = 1,f ′′(x) = − sinx, f ′′(0) = 0,f ′′′(x) = − cos x, f ′′′(0) = −1,· · · = · · · · · · = · · · .

Therefore,

sinx =n∑

k=0

(−1)kx2k+1

(2k + 1)!+ (−1)n

x2n+3

(2n + 3)!cos(ξ).

“k” — 2011/11/22 — 10:14 — page 17 —�

TAYLOR SERIES 17

Another useful form of the Taylor series is obtained by replacing in Eqn. (1.1) x byx+ h and x0 by x. The result is

f(x+ h) =n∑

k=0

f (k)(x)

k!hk +

f (n+1)(ξ)

(n+ 1)!hn+1 (1.5)

with ξ between x and x+ h.Here we introduce a commonly used notation for the error term. We say that

F (h) is of the order of hn and write

F (h) = O(hn)

if there exists a real constant C > 0 and a positive integer n such that

|F (h)| ≤ C |hn| for sufficiently small h.

EXAMPLE 1.5In Example 1.4 we found that for n = 2.

sinx = x− 1

3!x3 +

1

5!x5 − 1

7!x7 cos(ξ), 0 < ξ < x.

Since ∣∣∣∣∣sinx− x+ 1

3!x3 − 1

5!x5

x7

∣∣∣∣∣ =

∣∣∣∣1

7!cos(ξ)

∣∣∣∣ ≤1

7!= C

then

sinx− x+1

3!x3 − 1

5!x5 = O(x7).

The following theorem is a special case of Taylor’s Theorem (n = 0).

THEOREM 1.2 (Mean Value Theorem)If f is continuous on [a, b] and is differentiable on (a, b), then there exists a number

c ∈ (a, b) such that (see Figure 1.3)

f(b)− f(a) = f ′(c)(b − a).

The next theorem can be found in any calculus text and is an important tool fordetermining the existence of the solution of certain problems.

THEOREM 1.3 (Intermediate Value Theorem)Suppose f is continuous on a closed interval [a, b]. If w is any number between f(a) and

f(b), then there exists at least one c in (a, b) such that f(c) = w (See Figure 1.4).

“k” — 2011/11/22 — 10:14 — page 18 —�

18 INTRODUCTION

(a,f(a))

(b,f(b))

ba c x

yf

FIGURE 1.3Mean Value Theorem.

x

y

f(b)

w

c b

f

a

f(a)

FIGURE 1.4Intermediate Value Theorem.

EXAMPLE 1.6

Show that the equation x5 − 5x4 − 7x3 − x+ 1 = 0 has a solution in the interval [0, 1].

Consider the function f(x) = x5 − 5x4 − 7x3 − x+ 1. It is easy to see that f iscontinuous on [0, 1] and f(0) = 1, f(1) = −11. Since f(1) < 0 < f(0), then, fromthe intermediate value theorem (IVT), there exists x in (0, 1) such that

x5 + 5x4 − 7x3 − x+ 1 = 0.

EXERCISE SET 1.3

1. Find the Maclaurin series for f(x) = x2ex.

“k” — 2011/11/22 — 10:14 — page 19 —�

TAYLOR SERIES 19

2. Determine whether the following functions satisfy the hypotheses of the MeanValue Theorem. If so, find a number c satisfying the conclusion of the MeanValue Theorem.

(a) f(x) = x1/3, on [−1, 2],

(b) f(x) = x1/3, on [0, 2],

(c) f(x) = |x|, on [−2, 2].

3. Find all numbers c in [1, 3] that satisfy the conclusion of the Mean ValueTheorem for

f(x) = 2x+3

x.

4. Does the Mean Value Theorem tell us anything about the function

f(x) =∣∣∣x2 − 1

∣∣∣ in [0, 2]?

5. Show that f(x) = 2e2x + 4x− 3 has at least one zero in [0, 1].

6. Find the Taylor series expansion of f(x) = e−x about x0 = 0 and approximatef(−0.88) with n = 3.

7. Find the Taylor series expansion for f(x) = ln (1− x) about x0 = 0.

8. Approximate f(x) = ex sinx at x = 0.2 by using the Taylor series.

9. Find the Taylor series expansion of cos x about x = π/3.

10. What is the third-order Taylor polynomial for f(x) =√x+ 1 about x0?

11. Use a Taylor polynomial to find an approximate value for√e.

12. Use Taylor’s Theorem to show that

1− cos x

x=

1

2x+O(x2)

for x is sufficiently small.

13. Use Taylor’s Theorem to show that

(1 + x)−1 = 1− x+ x2 +O(x3)

for x is sufficiently small.

14. What is the error in the Taylor polynomial of degree 5 for f(x) = 1/x, usingx0 = 3/4 for x ∈ [1/2, 1]?

15. How many terms must be taken in the previous exercise to get an error of lessthan 10−2? 10−4?

“k” — 2011/11/22 — 10:14 — page 20 —�

20 INTRODUCTION

16. Construct a Taylor polynomial approximation to f(x) = e−x with x0 = 0 thatis accurate to within 10−3 on the interval [0, 1]. Find a value M such that|f(x1)− f(x2)| ≤M |x1 − x2| for all x1, x2 on the interval.

17. Find the Taylor polynomial of order 2 around x0 = 1 for f(x) = x3 + x.

18. Find intervals containing solutions to the following equations

(a) x− 3−x = 0,

(b) 4x2 − ex = 0.

19. Use the identity

cos2(x) =1 + cos(2x)

2

to find the Taylor series representation of cos2(x) about 0.

20. Use the previous exercise and the identity

sin2(x) = 1− cos2(x)

to find the Taylor series representation of sin2(x) about 0.

21. Evaluate to five decimal places the integrals

(a)∫ 10

sinxx dx,

(b)∫ 10

1−cos xx dx,

(c)∫ 10

ex−1x dx,

(d)∫ 21

ex

x dx

and prove your answers are correct by estimating the error.

22. Find to four decimal places the length of

(a) the ellipse (x4 )2 + (y2 )2 = 1,

(b) the parabola y = x2 for −2 ≤ x ≤ 2,

(c) the hyperbola y2 = x2 + 1 for y > 0 and −1 ≤ x ≤ 1.

COMPUTER PROBLEM SET 1.3

1. Consider the so-called cosine integral

Cin(x) =

∫ x

0

1− cos t

tdt.

“k” — 2011/11/22 — 10:14 — page 21 —�

� �

TAYLOR SERIES 21

(a) Find the degree 2n Taylor polynomial with remainder R(x) about a = 0.Give an estimate for |R(x)| that still depends on x.

(b) Write a MATLAB function Cin(x, tol, ca, n) with input parameters x, tolcomputing an approximate value ca for Cin(X) with an error less thantol. The program should use the Taylor polynomial of degree n to com-pute ca. n should be determined by the program as small as possible,depending on x and tol (use the results of part (a)).

Test your program for x = 1 and tol = 0.25, 0.06, 10−4. The exact valueis Cin(1) = 0.239811742. Print in every case the error ca−Cin(x), andn. Check if the actual errors are really ≤ tol. Print your results ca andthe value of n for x = 0.5, tol = 10−4.

2. The following MATLAB algorithm computes e−x using a Taylor series expan-sion:

s=1;t=1;i=1;while (abs(t) > eps*abs(s))

.... stop iterating when adding t to s does not change st=-t*x/i;s=s+t;i=i+1;

endresult=s

Run this algorithm for x=1:20.

This page intentionally left blankThis page intentionally left blank

References

Contents

Appendix 515

A Calculus Review 515

A.1 Limits and continuity . . . . . . . . . . . . . . . . .. . . . . . . . . 515

A.2 Differentiation . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . 516

A.3 Integration . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . 516

B MATLAB Built-in Functions 519

D MATLAB GUI 525

D.1 Roots of Equations . . . . . . . . . . . . . . . . . .. . . . . . . . . 525

D.2 System of Linear Equations . . . . . . . . . . . . . .. . . . . . . . 527

D.3 Interpolation . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . 528

D.4 The Method of Least Squares . . . . . . . . . . . . . .. . . . . . . 528

D.5 Integration . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . 529

D.6 Differentiation . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . 530

D.7 Numerical Methods for Differential Equations . . . . . .. . . . . . 530

D.8 Boundary-Value Problems . . . . . . . . . . . . . . . .. . . . . . . 531

D.9 Numerical Methods for PDEs . . . . . . . . . . . . . .. . . . . . . 532

Answers to Selected Exercises 535

Index 565 This page intentionally left blank

Preface

This is a textbook designed for an introductory course innumerical methods. It

deals with the theory and application of the most commonlyused numerical meth

ods for solving numerical problems on microcomputers. It isintended for students

in mathematics, science, and engineering who have completedthe introductory cal

culus sequence. In addition, the reader is assumed to havetaken a structured

programming course. The thrust of this text is to assistthe students to become

familiar with the most common numerical methods encounteredin science and en

gineering. The content material of this book has beendesigned to be compatible

with any introductory numerical textbook that exists in themarket. Students will

be able to examine and solve many numerical problems, usingMATLAB R ©1 in a

short period of time.

Due to the rapid advancement of computer technology andsoftware developments,

we have used MATLAB as the computing environment throughoutall the chapters

of the book. Each numerical method discussed in this bookis demonstrated through

the use of MATLAB which is easy to use and has manyfeatures such as:

1. Powerful matrix structure,

2. Powerful twoand three-dimensional graphing facilities,

3. A vast number of powerful built-in functions,

4. MATLAB’s structured programming style that resemblesFORTRAN and BASIC.

The goal of this present third edition is the same as theprevious one. The book

introduces students to a wide range of useful and importantalgorithms. Computer

results are presented with full details so that the mainsteps of the algorithm of each

1 MATLAB R © is a registered trademark of the MathWorks,Inc.

For product information, please contact:

The MathWorks, Inc.

3 Apple Hill Drive

Natick, MA 01760-2098 USA

Tel: 508-647-7000 Fax: 508-647-7001

numerical method are visualized and interpreted. For thisreason, a supplementary

CD-ROM, attached at the back of the book, has beendeveloped for students’ use

with MATLAB. The CD-ROM contains simple MATLAB functionsthat give a clear

step-by-step explanation of the mechanism behind thealgorithm of each numerical

method covered. Emphasis is placed on understanding how themethods work.

These functions guide the student through the calculationsnecessary to understand

the algorithm(s). The main feature of this book, beside theuse of MATLAB as its

computing environment, is that the style of the book iseasy, direct, and simple.

This is likely to boost students’ confidence in theirability to master the elements

of the subject.

The book is organized in a fairly standard manner. Topicsthat are simpler, both

theoretically and computationally, come first; for example,the root of equations

is covered in Chapter 3. Chapter 2 contains an introductionto computer floating

point arithmetic, errors, and interval arithmetic.

Both direct and iterative methods are presented in Chapter4 for solving systems

of linear equations.

Interpolation, spline functions, concepts of least squaresdata fitting, and numer

ical optimization are the subjects of Chapters 5, 6, 7, and8. Interpolation forms

the theoretical basis for much of numerical analysis.

Chapters 9 and 10 are devoted to numerical differentiationand integration. Sev

eral efficient integration techniques are presented.

In Chapters 11 and 12 a wide variety of numericaltechniques is presented for solv

ing linear integral equations and ordinary differentialequations. An introduction

for solving boundary value problems is presented in Chapter13. Chapter 14 is de

voted to some numerical techniques for computing theeigenvalues and eigenvectors

of a matrix.

The last Chapter 15 provides a basic introduction tonumerical techniques for

solving partial differential equations.

In each chapter we have attempted to present clear examplesin every section

followed by a good number of related exercises at the endof each section with

answers to some exercises.

It is the purpose of this book to implement variousimportant numerical methods

on a personal computer and to provide not only a source oftheoretical information

on the methods covered, but also to allow the student toeasily interact with the

microcomputer and the algorithm for each method usingMATLAB.

This text should provide an excellent tool suitable forteaching numerical method

courses at colleges and universities. It is also suitablefor self-study purposes.

In this third edition all suggestions from the expertreviewers were addressed.

They all deserve our sincere thanks and appreciation.

Features in the Third Edition

There have been some minor changes in some sections. Majornew features are

as follows:

• A new section on nonlinear PDEs has been added in Chapter15.

• MATLAB GUI has been incorporated as an integral part ofthe text.

Acknowledgments

We wish to thank the many persons who have helped us in thecreation of this

book. They are: R. Baker Kearfoot of the University ofSouthwestern Louisiana,

Rachid Kharab of the University of Rouen, A. Laradji of theKing Fahd University

of Petroleum & Minerals, A.R. Kashani of the SahandUniversity of Technology,

Z. Khiari of King Fahd University of Petroleum & Minerals,and H. Cheded of the

King Fahd University of Petroleum & Minerals, whoencouraged us to submit a

book proposal.

Special thanks is due to Dr. B. Khoshandam of SemnanUniversity for his keen

interest and suggestions in this edition and careful workon the use of MATLAB

GUI. His work was extraordinarily detailed and helpful tous.

We also thank the Anonymous Reviewers who made usefulrecommendations for

the third edition.

The authors remain very grateful to the editorial andproduction staff of Chap

man & Hall/CRC who have been helpful and available at allstages. Among them

are Sarah Morris, Project Editor, who was in communicationwith us during the

preparation of the final version of this third edition, andSunil Nair, mathematics

and statistics publisher, for his assistance andencouragement.

Finally, the authors are grateful for the financial andfacilities support provided

by Mr. H. Hassnaoui, General Director of the GFP in Muscat.

Suggestions for improvements to the book are always welcomeand can be made

by e-mail at: [email protected]. Abdelwahab Kharab RonaldB. Guenther

“k” — 2011/11/22 — 10:14 — page 509 —�

� �

Bibliography and References

[1] AKAI, T.J., Applied Numerical Methods for Engineers, John Wiley & Sons,New York, 1993.

[2] ALEFRED, G., and J. HERZBERGER, Introduction to Interval Computa-tions, Academic Press, New York, 1983.

[3] AYYUB, B.M., and R.H. McCUEN, Numerical Methods for Engineers,Prentice-Hall, Upper Saddle River, NJ, 1996.

[4] BIRKHOFF, G., and G.C. ROTA, Ordinary Differential Equations, JohnWiley & Sons, New York, 1978.

[5] BRACEWELL, R.N., The Fourier Transform and Its Applications,McGraw-Hill, New York, 1978.

[6] BRENT, R.P., Algorithms for Minimization without Derivatives, Prentice-Hall, Englewood Cliffs, NJ, 1973.

[7] BURDEN, R.L., and J.D. FAIRS, Numerical Analysis, Fourth Ed. PWS-KENT Publishing Comp., Boston, MA., 1988.

[8] CARNAHAN, B., H.A. LUTHER, and J.O. WILKES, Applied NumericalMethods, John Wiley & Sons, New York, 1969.

[9] CHAPRA, S.C., Numerical Methods for Engineers: With Personal Com-puter Applications, McGraw-Hill, New York, 1985.

[10] CHENEY, W., and D. KINCAID, Numerical Mathematics and Computing,4th ed. Brooks/Cole Publishing Comp., New York, 1999.

[11] DE BOOR, C., and S.D. CONTE, Elementary Numerical Analysis,McGraw-Hill, New York, 1980.

[12] DAHLQUIST, G., and A. BJORCK, Numerical Methods, Prentice-Hall,Englewood Cliffs, NJ, 1974.

[13] DATTA, B.N., Numerical Linear Algebra and Applications, Brooks/Cole,Pacific Grove, CA, 1995.

509

“k” — 2011/11/22 — 10:14 — page 510 —�

510 Bibliography and References

[14] DAVIS, P.J., and P. RABINOWITZ, Methods of Numerical Integration,Academic Press, New York, 2nd ed., 1984.

[15] ELJINDI, M., and A. KHARAB, The Quadratic Method for Computing theEigenpairs of a Matrix, IJCM 73, 530, 2000.

[16] EVANS, G., Practical Numerical Analysis, John Wiley & Sons, Chichester,England, 1995.

[17] FAUSETT, L.V., Applied Numerical Analysis Using MATLAB, Prentice-Hall, Upper Saddle River, NJ, 1999.

[18] FORSYTHE, G.E., M.A. MALCOLM, and C.B. MOLER, Computer Meth-ods for Mathematical Computations, Prentice-Hall, Englewood Cliffs, NJ,1977.

[19] GERALD, C.F., and P.O. WHEATLEY, Applied Numerical Analysis,Addison-Wesley, Reading, MA, 1989.

[20] GILL, P.E., W. MURRAY, and M.H. WRIGHT, Numerical Linear Algebraand Optimization, volume 1, Addison-Wesley, Redwood City, CA, 1991.

[21] GOLUB, G., and C. VAN LOAN, Matrix Computations, Johns HopkinsPress, Baltimore, 1983.

[22] GOLUB, G., and J. M. ORTEGA, Scientific Computing and DifferentialEquations: An Introduction to Numerical Methods, Academic Press, Inc.,Boston, 1992.

[23] GREENSPAN, D., and V. CASULLI, Numerical Analysis for Applied Math-ematics, Science and Engineering, Addison-Wesley, New York, 1988.

[24] HAGEMAN, L.A., and D.M. YOUNG, Applied Iterative Methods, Aca-demic Press, New York, 1981.

[25] HAGER, W.W., Applied Numerical Linear Algebra Methods, Prentice-Hall,Englewood Cliffs, NJ, 1988.

[26] HANSELMAN, D., and B. Littlefield, Mastering MATLAB 5: A Com-prehensive Tutorial and Reference, Prentice-Hall, Upper Saddle River, NJ,1998.

[27] HOFFMAN, J.D., Numerical Methods for Engineering and Scientists,McGraw-Hill, New York, 1992.

[28] HEATH, M.T.,Scientific Computing: An Introductory Survey, McGraw-Hill, New York, 1997.

[29] HORN, R.A., and C.R. JOHNSON, Matrix Analysis, Cambridge UniversityPress, Cambridge, 1985.

[30] HULTQUIST, P.F., Numerical Methods for Engineers and Computer Sci-entists, Benjamin/Cummings Publishing Comp., CA, 1988.

“k” — 2011/11/22 — 10:14 — page 511 —�

Bibliography and References 511

[31] INCROPERA, F.P., and D.P. DeWITT, Introduction to Heat Transfer,Wiley, New York, 2nd ed., 1990.

[32] ISAACSON, E., and H.B. KELLER, Analysis of Numerical Methods, Wiley,New York, 2nd ed., 1990.

[33] JOHNSTON, R.L., Numerical Methods: A Software Approach, John Wiley& Sons, New York, 1982.

[34] KAHANER, D., C. MOLER, and S. NASH, Numerical Methods and Soft-ware, Prentice Hall, Englewood Cliffs, NJ, 1989.

[35] KERNIGHAN, B.W., and R. PIKE, The Practice of Programming,Addison-Wesley, Reading, MA, 1999.

[36] KINGS, J.T., Introduction to Numerical Computation, McGraw-Hill, NewYork, 1984.

[37] LAMBERT, J.D., Numerical Methods for Ordinary Differential Systems,John Wiley & Sons, Chichester, 1991.

[38] LAMBERT, J.D., The Initial Value Problem for Ordinary DifferentialEquations, The state of the art in numerical analysis, D. Jacobs, editor.Academic Press, New York, 1977.

[39] LAWSON, C. L., and R.J. HANSON, Solving Leclsi-Squares Problems,Prentice Hall, Englewood Cliffs, NJ, 1974.

[40] LINDFIELD, G.R., and J.E.T. PENNY, Microcomputers in NumericalAnalysis, Ellis Horwood, Chichester, 1989.

[41] MARCHAND, P., Graphics and GUIs with MATLAB, CRC Press, BocaRaton, FL, 3rd ed., 2008.

[42] MARON, M.J., and R.J. LOPEZ, Numerical Analysis: A Practical Ap-proach, 3rd ed. Wadsworth, Belmont, CA, 1991.

[43] MATHEWS, H., and K. D. FINK, Numerical Methods Using MATLAB,Prentice Hall, Upper Saddle River, NJ, 3rd ed., 1999.

[44] MATHEWS, J.H., Numerical Methods for Mathematics, Science, and En-gineering, 2nd ed. Prentice-Hall International, Englewood Cliffs NJ, 1992.

[45] McNEARY, S.S., Introduction to Computational Methods for Students ofCalculus, Prentice-Hall, Englewood Cliffs. NJ, 1973.

[46] MILLER, W., The Engineering of Numerical Software, Prentice Hall, En-glewood Cliffs, NJ, 1984.

[47] MILLER, W., A Software Tools Sampler, Prentice-Hall, Englewood Cliffs,NJ, 1987.

[48] MOOR, R.V., Interval Analysis, Prentice-Hall, Englewood Cliffs, NJ, 1966.

“k” — 2011/11/22 — 10:14 — page 512 —�

512 Bibliography and References

[49] MORRIS, J.L., Computational Methods in Elementary Theory and Appli-cation of Numerical Analysis, John Wiley & Sons, New York, 1983.

[50] ORTEGA, J.M., and W.G. POOLE, Jr., An Introduction to NumericalMethods for Differential Equations, Pitman Press, Marshfield, MA, 1981.

[51] POLKING, J.C., Ordinary Differential Equations Using MATLAB,Prentice-Hall, Englewood Cliffs, NJ, 1995.

[52] RAMIREZ, R.W., The FFT, Fundamentals and Concepts, Prentice-Hall,Englewood Cliffs, NJ, 1985.

[53] RATSCHEK, H., Uber Einige Intervallarithmetische Grundbegriffe, Com-puting 4, 43, 1969.

[54] RICE, J.R., Numerical Methods, Software, and Analysis, IMSL ReferenceEdition, McGraw-Hill, New York, 1983.

[55] SCHIAVONE P., C. COUTANDA, and A. MIODUCHAWSKI, IntegralMethods in Science and Engineering, Birkhauser, Boston, 2002.

[56] SCHWARTZ, H.R., Numerical Andlysis: A Comprehensive Introduction,Wiley, New York, 1989.

[57] SHAMPINE, L.F., R.C. ALLEN, and S. PRUESS Jr., Fundamentals ofNumerical Computing, John Wiley & Sons, New York, 1997.

[58] SHAMPINE, L., and M.W. REICHELT, The MATLAB ODE suite, SIAMJournal on Scientific Computing 18(1), pp. 1-22, Jan 1997.

[59] SHINGAREVA, I., and C. LIZARRAGA-CELAYA, Solving Nonlinear Par-tial Differential Equations with Maple and Mathematica, Springer, Wien,New York, 2011.

[60] STOER, J., and R. BULIRSCH, Introduction to Numerical Analysis,Springer-Verlag, New York, 1993.

[61] The MATHWORKS, Inc., Using MATLAB, The Mathworks, Inc., Natick,MA, 1996.

[62] The MATHWORKS, Inc., Using MATLAB Graphics, The Mathworks, Inc.,Natick, MA, 1996.

[63] The MATHWORKS, Inc., MATLAB Language Reference Manual, TheMathworks, Inc., Natick, MA, 1996.

[64] TOURIN, A., An Introduction to Finite Difference Methods for PDEs inFinance, The Fields Institute, Toronto, 2010.

[65] TRICOMI, F.G., and C.H.H. BAKER, Treatment of Inegral Equations byNumerical Methods, Birkhauser, Boston, 2002.

[66] VARGA, R.S., Matrix Iterative Analysis, Prentice-Hall, Englewood Cliffs,NJ, 1962.


Top Related