1. Trang chủ
  2. » Công Nghệ Thông Tin

IT training scientific computing with MATLAB and octave (3rd ed ) quarteroni, saleri gervasio 2010 06 29 1

379 216 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 379
Dung lượng 4,48 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

The reason is that any real number x is in principle truncated by the machine, giving rise to a new number called the floating-point number , denoted by f lx, which does not necessarily c

Trang 2

For further volumes:

Science and Engineering

Texts in Computational

2

http://www.springer.com/series/5151

Trang 3

Trang 5

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.

Printed on acid-free paper

or parts thereof is permitted only under the provisions of the German Copyright Law of September 9, 1965, are liable for prosecution under the German Copyright Law.

in its current version, and permissions for use must always be obtained from Springer-Verlag Violations reproduction on microfilm or in any other way, and storage in data banks Duplication of this publication

The use of general descriptive names, registered names, trademarks, etc in this publication does not

This work is subject to copyright All rights are reserved, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,

Springer is part of Springer Science + Business Media (www.springer.com)

Cover design: deblik, Berlin

Springer Heidelberg Dordrecht London New York

Ecole Polytechnique Fédérale

Mathematics Subject Classification (2000): 65-01, 68U01, 68N15

© Springer-Verlag Berlin Heidelberg 2003, 2006, 2010

gervasio@ing.unibs.it

Alfio Quarteroni Fausto Saleri

MOX-Politecnico di MilanoPiazza Leonardo da Vinci 32

Trang 6

To the memory of Fausto Saleri

Trang 7

Trang 8

Preface to the First Edition

This textbook is an introduction to Scientific Computing We willillustrate several numerical methods for the computer solution of cer-tain classes of mathematical problems that cannot be faced by paperand pencil We will show how to compute the zeros or the integrals

of continuous functions, solve linear systems, approximate functions bypolynomials and construct accurate approximations for the solution ofdifferential equations

With this aim, in Chapter 1 we will illustrate the rules of the gamethat computers adopt when storing and operating with real and complexnumbers, vectors and matrices

In order to make our presentation concrete and appealing we willadopt the programming environmentMATLAB  1 as a faithful com-panion We will gradually discover its principal commands, statementsand constructs We will show how to execute all the algorithms that weintroduce throughout the book This will enable us to furnish an im-mediate quantitative assessment of their theoretical properties such asstability, accuracy and complexity We will solve several problems thatwill be raised through exercises and examples, often stemming from spe-cific applications

Several graphical devices will be adopted in order to render the ing more pleasant We will report in the margin theMATLAB commandalong side the line where that command is being introduced for the firsttime The symbol will be used to indicate the presence of exercises,the symbol to indicate the presence of a MATLAB program, while1

read-MATLAB is a trademark of TheMathWorks Inc., 24 Prime Park Way, ick, MA 01760, Tel: 001+508-647-7000, Fax: 001+508-647-7001

Trang 9

Nat-VIII Preface

the symbol will be used when we want to attract the attention ofthe reader on a critical or surprising behavior of an algorithm or a pro-cedure The mathematical formulae of special relevance are put within aframe Finally, the symbol indicates the presence of a display panelsummarizing concepts and conclusions which have just been reportedand drawn

At the end of each chapter a specific section is devoted to mentioningthose subjects which have not been addressed and indicate the biblio-graphical references for a more comprehensive treatment of the materialthat we have carried out

Quite often we will refer to the textbook [QSS07] where many issuesfaced in this book are treated at a deeper level, and where theoretical re-sults are proven For a more thorough description ofMATLAB we refer

to [HH05] All the programs introduced in this text can be downloadedfrom the web address

mox.polimi.it/qs

No special prerequisite is demanded of the reader, with the exception

of an elementary course of Calculus

However, in the course of the first chapter, we recall the principal sults of Calculus and Geometry that will be used extensively throughoutthis text The less elementary subjects, those which are not so neces-sary for an introductory educational path, are highlighted by the specialsymbol

re-We express our thanks to Thanh-Ha Le Thi from Springer-VerlagHeidelberg, and to Francesca Bonadei and Marina Forlizzi from Springer-Italia for their friendly collaboration throughout this project We grate-fully thank Prof Eastham of Cardiff University for editing the language

of the whole manuscript and stimulating us to clarify many points of ourtext

Preface to the Second Edition

In this second edition we have enriched all the Chapters by ducing several new problems Moreover, we have added new methodsfor the numerical solution of linear and nonlinear systems, the eigen-value computation and the solution of initial-value problems Anotherrelevant improvement is that we also use the Octave programming en-vironment Octave is a reimplementation of part of MATLAB which

Trang 10

intro-Preface IXincludes many numerical facilities ofMATLAB and is freely distributedunder the GNU General Public License.

Throughout the book, we shall often make use of the expression

“MATLAB command”: in this case, MATLAB should be understood

as the language which is the common subset of both programsLAB and Octave We have striven to ensure a seamless usage of ourcodes and programs under bothMATLAB and Octave In the few caseswhere this does not apply, we shall write a short explanation notice atthe end of each corresponding section

MAT-For this second edition we would like to thank Paola Causin for ing proposed several problems, Christophe Prud´homme, John W Eatonand David Bateman for their help with Octave, and Silvia Quarteronifor the translation of the new sections Finally, we kindly acknowledgethe support of the Poseidon project of the Ecole Polytechnique F´ed´erale

hav-de Lausanne

Preface to the Third Edition

This third edition features a complete revisitation of the whole book,many improvements in style and content to all the chapters, as well as asubstantial new development of those chapters devoted to the numericalapproximation of boundary-value problems and initial-boundary-valueproblems We remind the reader that all the programs introduced inthis text can be downloaded from the web address

mox.polimi.it/qs

Lausanne, Milano and Brescia Alfio Quarteroni

Trang 11

Trang 12

1 What can’t be ignored 1

1.1 TheMATLAB and Octave environments 1

1.2 Real numbers 3

1.2.1 How we represent them 3

1.2.2 How we operate with floating-point numbers 6

1.3 Complex numbers 8

1.4 Matrices 10

1.4.1 Vectors 14

1.5 Real functions 16

1.5.1 The zeros 18

1.5.2 Polynomials 20

1.5.3 Integration and differentiation 22

1.6 To err is not only human 25

1.6.1 Talking about costs 29

1.7 TheMATLAB language 30

1.7.1 MATLAB statements 32

1.7.2 Programming in MATLAB 34

1.7.3 Examples of differences betweenMATLAB and Octave languages 37

1.8 What we haven’t told you 38

1.9 Exercises 38

2 Nonlinear equations 41

2.1 Some representative problems 41

2.2 The bisection method 43

2.3 The Newton method 47

2.3.1 How to terminate Newton’s iterations 49

2.3.2 The Newton method for systems of nonlinear equations 51

2.4 Fixed point iterations 54

2.4.1 How to terminate fixed point iterations 60

Trang 13

XII Contents

2.5 Acceleration using Aitken’s method 60

2.6 Algebraic polynomials 65

2.6.1 H¨orner’s algorithm 66

2.6.2 The Newton-H¨orner method 68

2.7 What we haven’t told you 70

2.8 Exercises 72

3 Approximation of functions and data 75

3.1 Some representative problems 75

3.2 Approximation by Taylor’s polynomials 77

3.3 Interpolation 78

3.3.1 Lagrangian polynomial interpolation 79

3.3.2 Stability of polynomial interpolation 84

3.3.3 Interpolation at Chebyshev nodes 86

3.3.4 Trigonometric interpolation and FFT 88

3.4 Piecewise linear interpolation 93

3.5 Approximation by spline functions 94

3.6 The least-squares method 99

3.7 What we haven’t told you 103

3.8 Exercises 105

4 Numerical differentiation and integration 107

4.1 Some representative problems 107

4.2 Approximation of function derivatives 109

4.3 Numerical integration 111

4.3.1 Midpoint formula 112

4.3.2 Trapezoidal formula 114

4.3.3 Simpson formula 115

4.4 Interpolatory quadratures 117

4.5 Simpson adaptive formula 121

4.6 What we haven’t told you 125

4.7 Exercises 126

5 Linear systems 129

5.1 Some representative problems 129

5.2 Linear system and complexity 134

5.3 The LU factorization method 135

5.4 The pivoting technique 144

5.5 How accurate is the solution of a linear system? 147

5.6 How to solve a tridiagonal system 150

5.7 Overdetermined systems 152

5.8 What is hidden behind theMATLAB command \ 154

5.9 Iterative methods 157

5.9.1 How to construct an iterative method 158

Trang 14

Contents XIII

5.10 Richardson and gradient methods 162

5.11 The conjugate gradient method 166

5.12 When should an iterative method be stopped? 169

5.13 To wrap-up: direct or iterative? 171

5.14 What we haven’t told you 177

5.15 Exercises 177

6 Eigenvalues and eigenvectors 181

6.1 Some representative problems 182

6.2 The power method 184

6.2.1 Convergence analysis 187

6.3 Generalization of the power method 188

6.4 How to compute the shift 190

6.5 Computation of all the eigenvalues 193

6.6 What we haven’t told you 197

6.7 Exercises 197

7 Ordinary differential equations 201

7.1 Some representative problems 201

7.2 The Cauchy problem 204

7.3 Euler methods 205

7.3.1 Convergence analysis 208

7.4 The Crank-Nicolson method 212

7.5 Zero-stability 214

7.6 Stability on unbounded intervals 216

7.6.1 The region of absolute stability 219

7.6.2 Absolute stability controls perturbations 220

7.7 High order methods 228

7.8 The predictor-corrector methods 234

7.9 Systems of differential equations 236

7.10 Some examples 242

7.10.1 The spherical pendulum 242

7.10.2 The three-body problem 246

7.10.3 Some stiff problems 248

7.11 What we haven’t told you 252

7.12 Exercises 252

8 Numerical approximation of boundary-value problems 255 8.1 Some representative problems 256

8.2 Approximation of boundary-value problems 258

8.2.1 Finite difference approximation of the one-dimensional Poisson problem 259

8.2.2 Finite difference approximation of a convection-dominated problem 262

Trang 15

XIV Contents

8.2.3 Finite element approximation of the

one-dimensional Poisson problem 263

8.2.4 Finite difference approximation of the two-dimensional Poisson problem 267

8.2.5 Consistency and convergence of finite difference discretization of the Poisson problem 272

8.2.6 Finite difference approximation of the one-dimensional heat equation 274

8.2.7 Finite element approximation of the one-dimensional heat equation 278

8.3 Hyperbolic equations: a scalar pure advection problem 281

8.3.1 Finite difference discretization of the scalar transport equation 283

8.3.2 Finite difference analysis for the scalar transport equation 285

8.3.3 Finite element space discretization of the scalar advection equation 292

8.4 The wave equation 293

8.4.1 Finite difference approximation of the wave equation 295

8.5 What we haven’t told you 299

8.6 Exercises 300

9 Solutions of the exercises 303

9.1 Chapter 1 303

9.2 Chapter 2 306

9.3 Chapter 3 312

9.4 Chapter 4 315

9.5 Chapter 5 320

9.6 Chapter 6 327

9.7 Chapter 7 330

9.8 Chapter 8 339

References 347

Index 353

Trang 16

Index of MATLAB and Octave programs

All the programs introduced in this text can be downloaded from

mox.polimi.it/qs

2.1 bisection: bisection method 45

2.2 newton: Newton method 51

2.3 newtonsys: Newton method for nonlinear systems 52

2.4 aitken: Aitken method 63

2.5 horner: synthetic division algorithm 67

2.6 newtonhorner: Newton-H¨orner method 69

3.1 cubicspline: interpolating cubic spline 96

4.1 midpointc: composite midpoint quadrature formula 114

4.2 simpsonc: composite Simpson quadrature formula 116

4.3 simpadpt: adaptive Simpson formula 124

5.1 lugauss: Gauss factorization 141

5.2 itermeth: general iterative method 160

6.1 eigpower: power method 185

6.2 invshift: inverse power method with shift 189

6.3 gershcircles: Gershgorin circles 191

6.4 qrbasic: method of QR iterations 194

7.1 feuler: forward Euler method 206

7.2 beuler: backward Euler method 207

7.3 cranknic: Crank-Nicolson method 213

7.4 predcor: predictor-corrector method 235

7.5 feonestep: one step of the forward Euler method 236

7.6 beonestep: one step of the backward Euler method 236

7.7 cnonestep: one step of the Crank-Nicolson method 236

7.8 newmark: Newmark method 241

7.9 fvinc: forcing term for the spherical pendulum problem 245 7.10 threebody: forcing term for the simplified three body system 247

Trang 17

XVI Index of MATLAB and Octave programs

8.1 bvp: approximation of a two-point

diffusion-convection-reaction problem by the finite difference method 2618.2 poissonfd: approximation of the Poisson problem with

Dirichlet boundary data by the five-point finite difference

method 2708.3 heattheta: θ-method for the one-dimensional heat equation 276

8.4 newmarkwave: Newmark method for the wave equation 295

9.1 gausslegendre: Gauss-Legendre composite quadrature

formula, with n = 1 317

9.2 rk2: Heun (or RK2) method 333

9.3 rk3: explicit Runge-Kutta method of order 3 334

9.4 neumann: numerical solution of a Neumann boundary-value

problem 3419.5 hyper: Lax-Friedrichs, Lax-Wendroff and upwind schemes 344

Trang 18

What can’t be ignored

In this book we will systematically use elementary mathematical cepts which the reader should know already, yet he or she might notrecall them immediately

We will therefore use this chapter to refresh them and we will dense notions which are typical of courses in Calculus, Linear Algebraand Geometry, yet rephrasing them in a way that is suitable for use inScientific Computing At the same time we will introduce new conceptswhich pertain to the field of Scientific Computing and we will begin to ex-plore their meaning and usefulness with the help ofMATLAB (MATrixLABoratory), an integrated environment for programming and visualiza-tion We shall also use GNU Octave (in short, Octave), an interpreterfor a high-level language mostly compatible with MATLAB which isdistributed under the terms of the GNU GPL free-software license andwhich reproduces a large part of the numerical facilities ofMATLAB

con-In Section 1.1 we will give a quick introduction to MATLAB andOctave, while we will present the elements of programming in Section 1.7.However, we refer the interested readers to the manuals [HH05, Pal08]for a description of theMATLAB language and to the manual [EBH08]for a description of Octave

MATLAB and Octave are integrated environments for Scientific puting and visualization They are written mostly in C and C++ lan-guages

Com-MATLAB is distributed by The MathWorks (see the website www

mathworks.com) The name stands for MATrix LABoratory since

origi-nally it was developed for matrix computation

Octave, also known as GNU Octave (see the website www.octave.org), is a freely redistributable software It can be redistributed and/or

Trang 19

2 1 What can’t be ignored

modified under the terms of the GNU General Public License (GPL) aspublished by the Free Software Foundation

There are differences betweenMATLAB and Octave environments,languages and toolboxes (i.e a collection of special-purposeMATLABfunctions) However, there is a level of compatibility that allows us towrite most programs of this book and run them seamlessly both inMAT-LAB and Octave When this is not possible, either because some com-mands are spelt differently, or because they operate in a different way,

or merely because they are just not implemented, a note will be written

at the end of each section to provide an explanation and indicate whatcould be done

Through the book, we shall often make use of the expression “LAB command”: in this case, MATLAB should be understood as the

MAT-language which is the common subset of both programsMATLAB andOctave

Just asMATLAB has its toolboxes, Octave has a richful set of tions available through a project called Octave-forge (see the websiteoctave.sourceforge.net) This function repository grows steadily inmany different areas Some functions we use in this book don’t belong

func-to the Octave core, nevertheless they can be downloaded by the websiteoctave.sourceforge.net

Once installed, the execution ofMATLAB or Octave yield the access

to a working environment characterized by the prompt>>oroctave:1>,

>>

octave:1> respectively For instance, when executing MATLAB on our personal

computer, the following message is generated:

< M A T L A B (R) >

Copyright 1984-2009 The MathWorks, Inc

Version 7.9.0.529 (R2009b) 64-bit (glnxa64)

GNU Octave, version 3.2.3

Copyright (C) 2009 John W Eaton and others

This is free software; see the source code for copying

conditions There is ABSOLUTELY NO WARRANTY; not even

for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE

For details, type ‘warranty’

Octave was configured for "x86_64-unknown-linux-gnu"

Additional information about Octave is available at

http://www.octave.org

Trang 20

1.2 Real numbers 3

Please contribute if you find this software useful

For more information, visit

http://www.octave.org/help-wanted.html

Report bugs to <bug@octave.org> (but first, please read

http://www.octave.org/bugs.html to learn how to write a

helpful report)

For information about changes from previous versions,

type ‘news’

octave:1>

In this chapter we will use the prompt >>, however, from Chapter 2

on the prompt will be always neglected in order to simplify notations.

1.2 Real numbers

While the setR of real numbers is known to everyone, the way in whichcomputers treat them is perhaps less well known On one hand, sincemachines have limited resources, only a subsetF of finite dimension of

R can be represented The numbers in this subset are called

floating-point numbers On the other hand, as we shall see in Section 1.2.2,F

is characterized by properties that are different from those of R The

reason is that any real number x is in principle truncated by the machine, giving rise to a new number (called the floating-point number ), denoted

by f l(x), which does not necessarily coincide with the original number

x.

1.2.1 How we represent them

To become acquainted with the differences betweenR and F, let us make

a few experiments which illustrate the way that a computer deals withreal numbers Note that whether we use MATLAB or Octave ratherthan another language is just a matter of convenience The results ofour calculation, indeed, depend primarily on the manner in which thecomputer works, and only to a lesser degree on the programming lan-

guage Let us consider the rational number x = 1/7, whose decimal representation is 0.142857 This is an infinite representation, since the

number of decimal digits is infinite To get its computer representation,

let us introduce after the prompt the ratio 1/7 and obtain

>> 1/7

ans =

0 1 4 2 9

Trang 21

4 1 What can’t be ignored

which is a number with only four decimal digits, the last being differentfrom the fourth digit of the original number

Should we now consider 1/3 we would find 0.3333, so the fourth

dec-imal digit would now be exact This behavior is due to the fact that real

numbers are rounded on the computer This means, first of all, that only

an a priori fixed number of decimal digits are returned, and moreoverthe last decimal digit which appears is increased by unity whenever thefirst disregarded decimal digit is greater than or equal to 5

The first remark to make is that using only four decimal digits torepresent real numbers is questionable Indeed, the internal representa-tion of the number is made of as many as 16 decimal digits, and what wehave seen is simply one of several possible MATLAB output formats.The same number can take different expressions depending upon thespecific format declaration that is made For instance, for the number

1/7, some possible output formats are avalibale inMATLAB:

format

format short yields 0.1429,

format short e ” 1.4286e − 01,

format short yields 0.14286,

format short e ” 1.4286e − 01,

x = ( −1) s · (0.a1a2 a t)· β e= (−1) s · m · β e −t , a

1= 0 (1.1)

where s is either 0 or 1, β (a positive integer larger than or equal to 2)

is the basis adopted by the specific computer at hand, m is an integer called the mantissa whose length t is the maximum number of digits ai

(with 0≤ a i ≤ β − 1) that are stored, and e is an integral number called

the exponent The format long e is the one which most resembles this

Trang 22

1.2 Real numbers 5representation, and e stands for exponent; its digits, preceded by the

sign, are reported to the right of the character e The numbers whose

form is given in (1.1) are called floating-point numbers, since the position

of the decimal point is not fixed The digits a1a2 a p (with p ≤ t) are

often called the p first significant digits of x.

The condition a1 = 0 ensures that a number cannot have multiple

representations For instance, without this restriction the number 1/10

could be represented (in the decimal basis) as 0.1 · 100, but also as 0.01 ·

101, etc

The setF is therefore fully characterized by the basis β, the number

of significant digits t and the range (L, U ) (with L < 0 and U > 0) of

variation of the index e Thus it is denoted as F(β, t, L, U) For instance,

in MATLAB we have F = F(2, 53, −1021, 1024) (indeed, 53 significant

digits in basis 2 correspond to the 15 significant digits that are shown

byMATLAB in basis 10 with the format long)

Fortunately, the roundoff error that is inevitably generated whenever

a real number x = 0 is replaced by its representative fl(x) in F, is small,

where M = β 1−tprovides the distance between 1 and its closest

floating-point number greater than 1 Note that M depends on β and t For

instance, inMATLAB M can be obtained through the commandeps, eps

and we obtain M = 2−52  2.22·10 −16 Let us point out that in (1.2) we

estimate the relative error on x, which is undoubtedly more meaningful

than the absolute error |x−fl(x)| As a matter of fact, the latter doesn’t

account for the order of magnitude of x whereas the former does.

The number u = 1

2 M is the maximum relative error that the puter can make while representing a real number by finite arithmetic

com-For this reason, it is sometimes named roundoff unity.

Number 0 does not belong toF, as in that case we would have a1= 0

in (1.1): it is therefore handled separately Moreover, L and U being

finite, one cannot represent numbers whose absolute value is either

arbi-trarily large or arbiarbi-trarily small Precisely, the smallest and the largest

positive real numbers ofF are given respectively by

x min = β L −1 , x

max = β U(1− β −t ).

In MATLAB these values can be obtained through the commands

realmax

x min = 2.225073858507201 · 10 −308 ,

x max = 1.797693134862316 · 10+308.

Trang 23

6 1 What can’t be ignored

A positive number smaller than xmin produces a message of flow and is treated either as 0 or in a special way (see, e.g., [QSS07],

under-Chapter 2) A positive number greater than xmax yields instead a sage of overflow and is stored in the variableInf(which is the computerInf

mes-representation of +∞).

The elements in F are more dense near xmin, and less dense while approaching x max As a matter of fact, the number inF nearest to xmax (to its left) and the one nearest to x min (to its right) are, respectively

x − max = 1.797693134862315 · 10+308,

x+min = 2.225073858507202 · 10 −308 . Thus x+min − x min  10 −323 , while x

max − x −

max  10292 (!) However,the relative distance is small in both cases, as we can infer from (1.2)

1.2.2 How we operate with floating-point numbers

Since F is a proper subset of R, elementary algebraic operations onfloating-point numbers do not enjoy all the properties of analogous op-erations onR Precisely, commutativity still holds for addition (that is

f l(x + y) = f l(y + x)) as well as for multiplication (f l(xy) = f l(yx)),

but other properties such as associativity and distributivity are violated.Moreover, 0 is no longer unique Indeed, let us assign the variable a thevalue 1, and execute the following instructions:

>> a = 1; b =1; w h i l e a + b ~= a ; b = b /2; end

The variable b is halved at every step as long as the sum of a and bremains different (~=) from a Should we operate on real numbers, thisprogram would never end, whereas in our case it ends after a finitenumber of steps and returns the following value for b: 1.1102e-16=

 M /2 There exists therefore at least one number b different from 0 such

that a+b=a This is possible sinceF is made up of isolated numbers; when

adding two numbers a and b with b<a and b less than M, we alwaysobtain that a+b is equal to a TheMATLAB number a+eps(a) is thesmallest number inF larger than a Thus the sum a+b will return a forall b < eps(a)

Associativity is violated whenever a situation of overflow or underflowoccurs Take for instance a=1.0e+308, b=1.1e+308 and c=-1.001e+308,and carry out the sum in two different ways We find that

a + (b + c) = 1.0990e + 308, (a + b) + c = Inf.

This is a particular instance of what occurs when one adds two bers with opposite sign but similar absolute value In this case the result

num-may be quite inexact and the situation is referred to as loss, or

cancel-lation, of significant digits For instance, let us compute ((1 + x) − 1)/x

Trang 24

1.2 Real numbers 7

−1

−0.5 0 0.5 1 1.5x 10

f (x) = x7− 7x6+ 21x5− 35x4+ 35x3− 21x2+ 7x − 1 (1.3)

at 401 equispaced points with abscissa in [1− 2 · 10 −8 , 1 + 2 · 10 −8] We

obtain the chaotic graph reported in Figure 1.1 (the real behavior is that

of (x −1)7, which is substantially constant and equal to the null function

in such a tiny neighborhood of x = 1) TheMATLAB commands thathave generated this graph will be illustrated in Section 1.5

Finally, it is interesting to notice that in F there is no place for

indeterminate forms such as 0/0 or ∞/∞ Their presence produces what

is called not a number (NaNin MATLAB or in Octave), for which the NaNnormal rules of calculus do not apply

Remark 1.1 Whereas it is true that roundoff errors are usually small, whenrepeated within long and complex algorithms, they may give rise to catas-trophic effects Two outstanding cases concern the explosion of the Arianemissile on June 4, 1996, engendered by an overflow in the computer on board,and the failure of the mission of an American Patriot missile, during the GulfWar in 1991, because of a roundoff error in the computation of its trajectory

An example with less catastrophic (but still troublesome) consequences isprovided by the sequence

z2= 2, z n+1= 2n−1/2

1− √1− 4 1−n z2

n , n = 2, 3, (1.4)

Trang 25

8 1 What can’t be ignored

Fig 1.2 Relative error|π − z n |/π versus n

which converges to π when n tends to infinity When MATLAB is used to

compute z n , the relative error found between π and z n decreases for the 16first iterations, then grows because of roundoff errors (as shown in Figure 1.2)

−1 is the imaginary unit (that is i2=−1), while x = Re(z)

and y = Im(z) are the real and imaginary part of z, respectively They

are generally represented on the computer as pairs of real numbers.Unless redefined otherwise,MATLAB variables i as well as j denotethe imaginary unit To introduce a complex number with real part x andimaginary part y, one can just write x+i*y; as an alternative, one canuse the command complex(x,y) Let us also mention the exponentialcomplex

and the trigonometric representations of a complex number z, that are equivalent thanks to the Euler formula

x2+ y2is the modulus of the complex number (it can be obtained

by settingabs(z)) while θ is its argument, that is the angle between the

abs

x axis and the straight line issuing from the origin and passing from the

point of coordinate x, y in the complex plane θ can be found by typing

angle(z) The representation (1.5) is therefore:

angle

abs ( z )*( cos ( a n g l e ( z ))+ i * sin ( a n g l e ( z )))

The graphical polar representation of one or more complex numberscan be obtained through the command compass(z), where z is eithercompass

a single complex number or a vector whose components are complexnumbers For instance, by typing

Trang 26

1.3 Complex numbers 9

1 2 3 4 5

one obtains the graph reported in Figure 1.3

For any given complex number z, one can extract its real part with

the commandreal(z)and its imaginary part withimag(z) Finally, the real imagcomplex conjugate ¯z = x − iy of z, can be obtained by simply writing

In MATLAB all operations are carried out by implicitly assuming

that the operands as well as the result are complex We may therefore

find some apparently surprising results For instance, if we compute the

cube root of−5 with the MATLAB command (-5)^(1/3), instead of

−1.7100 we obtain the complex number 0.8550 + 1.4809i (We

antic-ipate the use of the symbol ^for the power exponent.) As a matter of ^

fact, all numbers of the form ρe i(θ+2kπ) , with k an integer, are

indistin-guishable from z = ρe iθ By computing the complex roots of z of order

MATLAB will select the one that is encountered by spanning the

com-plex plane counterclockwise beginning from the real axis Since the polar

representation of z = −5 is ρe iθ with ρ = 5 and θ = π, the three roots

are (see Figure 1.4 for their representation in the Gauss plane)

Trang 27

10 1 What can’t be ignored

Fig 1.4 Representation in the complex plane of the three complex cube roots

of the real number−5

The first root is the one which is selected

Let n and m be positive integers A matrix with m rows and n columns

is a set of m ×n elements a ij , with i = 1, , m, j = 1, , n, represented

by the following table:

a single row is a row vector.

In order to introduce a matrix in MATLAB one has to write theelements from the first to the last row, introducing the character ; toseparate the different rows For instance, the command

>> A = [ 1 2 3; 4 5 6]

produces

Trang 28

1.4 Matrices 11

A =

that is, a 2× 3 matrix whose elements are indicated above The m × n

matrix zeros(m,n) has all null entries, eye(m,n) has all null entries zeros

eye

unless a ii , i = 1, , min(m, n), on the diagonal that are all equal to 1.

The n × n identity matrix is obtained with the command eye(n) (which

is an abridged version of eye(n,n)): its elements are δij = 1 if i = j,

0 otherwise, for i, j = 1, , n Finally, by the commandA=[ ]we can [ ]initialize an empty matrix

We recall the following matrix operations:

1 if A = (aij) and B = (bij) are m × n matrices, the sum of A and B

is the matrix A + B = (aij + bij);

2 the product of a matrix A by a real or complex number λ is the

matrix λA = (λaij);

3 the product of two matrices is possible only for compatible sizes,

precisely if A is m × p and B is p × n, for some positive integer p In

that case C = AB is an m × n matrix whose elements are

Note that MATLAB returns a diagnostic message when one tries to

carry out operations on matrices with incompatible dimensions For

Trang 29

12 1 What can’t be ignored

If A is a square matrix of dimension n, its inverse (provided it exists)

is a square matrix of dimension n, denoted by A −1, which satisfies the

matrix relation AA−1 = A−1A = I We can obtain A−1 through the

command inv(A) The inverse of A exists iff the determinant of A, a

j=1

Δ ij a ij , for n > 1, ∀i = 1, , n,

(1.8)

where Δij = (−1) i+jdet(Aij) and Aij is the matrix obtained by

elim-inating the i-th row and j-th column from matrix A (The result is independent of the row index i.) In particular, if A ∈ R 2×2 one has

det(A) = a11a22− a12a21,

while if A∈ R 3×3 we obtain

det(A) = a11a22a33+ a31a12a23+ a21a13a32

−a11a23a32− a21a12a33− a31a13a22.

We recall that if A = BC, then det(A) = det(B)det(C)

To invert a 2×2 matrix and compute its determinant we can proceed

For special classes of square matrices, the computation of inverses and

determinants is rather simple In particular, if A is a diagonal matrix, i.e.

Trang 30

1.4 Matrices 13

one for which only the diagonal elements akk, k = 1, , n, are non-zero, its determinant is given by det(A) = a11a22· · · a nn In particular, A is non-singular iff akk = 0 for all k In such a case the inverse of A is still

a diagonal matrix with elements a −1

kk.Let v be a vector of dimension n The command diag(v) produces diag

a diagonal matrix whose elements are the components of vector v Themore general command diag(v,m) yields a square matrix of dimension

n+abs(m) whose m-th upper diagonal (i.e the diagonal made of elements with indices i, i + m) has elements equal to the components of v, while

the remaining elements are null Note that this extension is valid alsowhen m is negative, in which case the only affected elements are those oflower diagonals

For instance if v = [1 2 3] then:

triangular if all elements above (respectively, below) the main diagonal

are zero Its determinant is simply the product of the diagonal elements.Through the commandstril(A)andtriu(A), one can extract from tril

triuthe matrix A of dimension n its lower and upper triangular part Theirextensions tril(A,m) or triu(A,m), with m ranging from -n and n,allow the extraction of the triangular part augmented by, or deprived of,extradiagonals

For instance, given the matrix A =[3 1 2; -1 3 4; -2 -1 3], by thecommand L1=tril(A) we obtain

AT the matrix A is called symmetric Finally,A’denotes the transpose A’

of A if A is real, or its conjugate transpose (that is, AH) if A is complex Asquare complex matrix that coincides with its conjugate transpose AH

is called hermitian.

Trang 31

14 1 What can’t be ignored

to carry out operations on matrices having non-compatible dimensions

If we repeat the previousMATLAB examples we obtain:

Vectors will be indicated in boldface; precisely, v will denote a column

vector whose i-th component is denoted by v i When all components are

real numbers we can write v∈ R n

In MATLAB, vectors are regarded as particular cases of matrices

To introduce a column vector one has to insert between square bracketsthe values of its components separated by semi-colons, whereas for a rowvector it suffices to write the component values separated by blanks orcommas For instance, through the instructions v = [1;2;3] and w =

[1 2 3] we initialize the column vector v and the row vector w, both

of dimension 3 The command zeros(n,1)(respectively, zeros(1,n))produces a column (respectively, row) vector of dimension n with null

elements, which we will denote by 0 Similarly, the commandones(n,1)ones

generates the column vector, denoted with 1, whose components are all

equal to 1

Trang 32

1.4 Matrices 15

A system of vectors{y1, , y m } is linearly independent if the

rela-tion

α1y1+ + αmym= 0

implies that all coefficients α1, , α mare null A systemB = {y1, ,

yn } of n linearly independent vectors in R n (orCn ) is a basis forRn(or

Cn), that is, any vector w inRn can be written as a linear combination

for a unique possible choice of the coefficients{w k } The latter are called

the components of w with respect to the basis B For instance, the

canon-ical basis ofRn is the set of vectors{e1, , e n }, where e i has its i-th

component equal to 1, and all other components equal to 0 and is the

one which is normally used

The scalar product of two vectors v, w ∈ R n is defined as

{v k } and {w k } being the components of v and w, respectively The

corresponding command is w’*v or elsedot(v,w), where now the apex dot

denotes transposition of the vector For a vector v with complex

com-ponents, v’ denotes its conjugate transpose vH, that is a row-vector v’

whose components are the complex conjugate ¯v k of vk The length (or

modulus) of a vector v is given by

v =(v, v) =



 n k=1

v2k

and can be computed through the command norm(v); v is also said norm

euclidean norm of the vector v.

The vector product between two vectors v, w ∈ R3, v× w or v ∧ w,

is the vector u ∈ R3 orthogonal to both v and w whose modulus is

|u| = |v| |w| sin(α), where α is the smaller angle formed by v and w It

can be obtained by the commandcross(v,w) crossThe visualization of a vector can be obtained by theMATLAB com-

quiver3.* / ^

The MATLAB command x.*y, x./y or x.^2 indicates that these

operations should be carried out component by component For instance

if we define the vectors

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

Trang 33

16 1 What can’t be ignored

18

returns a vector whose i-th component is equal to xi y i.

Finally, we recall that a vector v∈ C n, with v= 0, is an eigenvector

of a matrix A∈ C n ×n associated with the complex number λ if

Av = λv.

The complex number λ is called eigenvalue of A In general, the

com-putation of eigenvalues is quite difficult Exceptions are represented bydiagonal and triangular matrices, whose eigenvalues are their diagonalelements

See the Exercises 1.3-1.6

1.5 Real functions

This section deals with manipulation of real functions More particularly,

for a given function f defined on an interval (a, b), we aim at computing

its zeros, its integral and its derivative, as well as drawing its graph Thecommandfplot(fun,lims)plots the graph of the function fun (whichfplot

is stored as a string of characters) on the interval (lims(1), lims(2))

For instance, to represent f (x) = 1/(1 + x2) on the interval (−5, 5), we

can write

>> fun =’1/(1+x^2)’; lims=[-5,5]; fplot(fun,lims);

or, more directly,

>> fplot(’1/(1+x^2)’,[-5 5]);

In MATLAB the graph is obtained by sampling the function on a

set of non-equispaced abscissae and reproduces the true graph of f with

a tolerance of 0.2% To improve the accuracy we could use the command

>> fplot(fun,lims,tol,n,LineSpec)

where tol indicates the desired tolerance and the parameter n(≥ 1)

ensures that the function will be plotted with a minimum of n + 1 points

Trang 34

1.5 Real functions 17LineSpec is a string specifying the style or the color of the line used for

plotting the graph For example, LineSpec=’ ’ is used for a dashed

line, LineSpec=’r-.’ for a red dashed-dotted line, etc To use default

values for tol, n or LineSpec one can pass empty matrices ([ ])

By writing grid on after the command fplot, we can obtain the gridbackground-grid as that in Figure 1.1

The function f (x) = 1/(1 + x2) can be defined in several different ways:

by the instruction fun=’1/(1+x^2)’ seen before;

by the commandinlinewith the instruction inline

The inline command, whose common syntax is

fun=inline(expr, arg1, arg2, , argn),

defines a function fun depending on the ordered set of variables arg1,

arg2, , argn The string expr contains the expression of fun

For example, fun=inline(’sin(x)*(1+cos(t))’, ’x’,’t’) defines the

function f un(x, t) = sin(x)(1+cos(t)) The brief form fun=inline(expr)

implicitely supposes that expr depends on all the variables which appear

in the definition of the function itself, by following alphabetical order

For example, by the command fun=inline(’sin(x) *(1+cos(t))’) we

define the function f un(t, x) = sin(x)(1 + cos(t)), whose first variable is

t, while the second one is x (by following lexicographical order).

The common syntax of an anonymous function reads

fun=@(arg1, arg2, ,argn)[expr]

In order to evaluate the function fun at a point x (or at a set of

points, stored in the vector x) we can make use of the commandseval, eval

feval

orfeval, otherwise we can simply evaluate the function consistently with

the command used to define the function itself Even if they produce the

same result, the commands eval and feval have a different syntax eval

has only one input parameter (the name of the mathematical function

to be evaluated) and evaluates the function fun at the point stored in

the variable which appears inside the definition of fun (i.e., x in the

above definitions) On the contrary, the function feval has at least two

parameters; the former is the name fun of the mathematical function to

be evaluated, the latter contains the inputs to the function fun

We report in Table 1.1 the various ways for defining, evaluating and

plotting a mathematical function In the following, we will use one of

Trang 35

18 1 What can’t be ignored

fplot(’fun’,[-2,2])fun=inline(’1/(1+x^2)’) y=fun(x) fplot(fun,[-2,2])

y=feval(fun,x) fplot(’fun’,[-2,2])y=feval(’fun’,x)

y=feval(fun,x) fplot(’fun’,[-2,2])y=feval(’fun’,x)

Table 1.1 How to define, evaluate and plot a mathematical function

the definitions of Table 1.1 and proceed coherently However, the readercould make different choices

If the variable x is an array, the operations /, * and ^ acting on

ar-rays have to be replaced by the corresponding dot operations /, *

and ^ which operate component-wise For instance, the instructionfun=@(x)[1/(1+x ^2)] is replaced by fun=@(x)[1./(1+x.^2)].The command plot can be used as alternative to fplot, providedplot

that the mathematical function has been evaluated on a set of abscissa.The following instructions

generates a row array of n equispaced points from a to b, while the

com-mand plot(x,y,’c’,’Linewidth’,2) creates a linear piecewise curve

connecting the points (xi , y i) (for i = 1, , n) with a cyan line width of

2 points

1.5.1 The zeros

We recall that if f (α) = 0, α is called zero of f or root of the equation

f (x) = 0 A zero is simple if f  (α) = 0, multiple otherwise.

From the graph of a function one can infer (within a certain tolerance)which are its real zeros The direct computation of all zeros of a givenfunction is not always possible For functions which are polynomials with

real coefficients of degree n, that is, of the form

Trang 36

we can obtain the only zero α = −a0/a1, when n = 1 (i.e p1 represents

a straight line), or the two zeros, α+ and α − , when n = 2 (this time p2

represents a parabola) α ±= (−a1±a2− 4a0a2)/(2a2).

However, there are no explicit formulae for the zeros of an arbitrary

polynomial p n when n ≥ 5.

In what follows we will denote with Pn the space of polynomials of

degree less than or equal to n,

where the a k are given coefficients, real or complex

Also the number of zeros of a function cannot in general be

deter-mined a priori An exception is provided by polynomials, for which the

number of zeros (real or complex) coincides with the polynomial degree

Moreover, should α = x + iy with y = 0 be a zero of a polynomial

with degree n ≥ 2, if a k are real coefficients, then its complex conjugate

¯

α = x − iy is also a zero.

To compute in MATLAB one zero of a function fun, near a given

value x0, either real or complex, the command fzero(fun,x0)can be fzeroused The result is an approximate value of the desired zero, and also the

interval in which the search was made Alternatively, using the command

fzero(fun,[x0 x1]), a zero of fun is searched for in the interval whose

endpoints are x0,x1, provided f changes sign between x0 and x1.

Let us consider, for instance, the function f (x) = x2−1+e x Looking

at its graph we see that there are two zeros in (−1, 1) To compute them

we need to execute the following commands:

Alternatively, after noticing from the function plot that one zero is

in the interval [−1, −0.2] and another in [−0.2, 1], we could have written

>> f z e r o ( fun ,[ -1 -0.2])

ans =

-0.7146

Trang 37

20 1 What can’t be ignored

The fzero syntax is the same if the function fun is defined either bythe command inline or by a string

Otherwise, if fun is defined by an M-file, we can choose one betweenthese two calls:

>> f z e r o ( ’ fun ’ , 1)

or

>> f z e r o ( @fun ,1)

mathemat-ical functions defined with either inline, anonymous function or M-file

1.5.2 Polynomials

Polynomials are very special functions and there is a specialMATLABtoolbox polyfun for their treatment The commandpolyval is apt topolyval

evaluate a polynomial at one or several points Its input arguments are

a vector p and a vector x, where the components of p are the polynomial

coefficients stored in decreasing order, from an down to a0, and thecomponents of x are the abscissae where the polynomial needs to beevaluated The result can be stored in a vector y by writing

>> y = p o l y v a l (p , x )

For instance, the values of p(x) = x7+3x2−1, at the equispaced abscissae

x k =−1+k/4 for k = 0, , 8, can be obtained by proceeding as follows:

-0.81244 -0.24219 0.82098 3.00000

Trang 38

1.5 Real functions 21Alternatively, one could use the command feval However, in such

case one should provide the entire analytic expression of the polynomial

in the input string, and not simply its coefficients

The programrootsprovides an approximation of the zeros of a poly- rootsnomial and requires only the input of the vector p For instance, we can

compute the zeros of p(x) = x3− 6x2+ 11x − 6 by writing

Unfortunately, the result is not always that accurate For instance,

for the polynomial p(x) = (x + 1)7, whose unique zero is α = −1 with

multiplicity 7, we find (quite surprisingly)

In fact, numerical methods for the computation of the polynomial

roots with multiplicity larger than one are particularly subject to

round-off errors (see Section 2.6.2)

The command p=conv(p1,p2)returns the coefficients of the poly- convnomial given by the product of two polynomials whose coefficients are

contained in the vectors p1 and p2

Similarly, the command[q,r]=deconv(p1,p2)provides the coefficients deconv

of the polynomials obtained on dividing p1 by p2, i.e.p1 = conv(p2,q)

+ r In other words, q and r are the quotient and the remainder of the

division

Let us consider for instance the product and the ratio between the

two polynomials p1(x) = x4− 1 and p2(x) = x3− 1 :

Trang 39

22 1 What can’t be ignored

p=conv(p1,p2) p = coefficients of the polynomial p1p2[q,r]=deconv(p1,p2) q = coefficients of q, r = coefficients of r

We therefore find the polynomials p(x) = p1(x)p2(x) = x7− x4− x3+ 1,

q(x) = x and r(x) = x − 1 such that p1(x) = q(x)p2(x) + r(x).

The commandspolyint(p)andpolyder(p)provide respectively thepolyint

polyder coefficients of the primitive (vanishing at x = 0) and those of the

deriva-tive of the polynomial whose coefficients are given by the components ofthe vector p

If x is a vector of abscissae and p (respectively, p1and p2) is a vector

containing the coefficients of a polynomial p (respectively, p1 and p2),the previous commands are summarized in Table 1.2

A further command,polyfit, allows the computation of the n + 1

poly-polyfit

nomial coefficients of a polynomial p of degree n once the values attained

by p at n + 1 distinct nodes are available (see Section 3.3.1).

1.5.3 Integration and differentiation

The following two results will often be invoked throughout this book:

1 the fundamental theorem of integration: if f is a continuous function

Trang 40

1.5 Real functions 23

2 the first mean-value theorem for integrals: if f is a continuous

func-tion in [a, b) and x1, x2∈ [a, b) with x1< x2, then∃ξ ∈ (x1, x2) such

Even when it does exist, a primitive might be either impossible to

determine or difficult to compute For instance, knowing that ln|x| is a

primitive of 1/x is irrelevant if one doesn’t know how to efficiently

com-pute the logarithms In Chapter 4 we will introduce several methods to

compute the integral of an arbitrary continuous function with a desired

accuracy, irrespectively of the knowledge of its primitive

We recall that a function f defined on an interval [a, b] is differentiable

in a point ¯x ∈ (a, b) if the following limit exists and is finite

We say that a function which is continuous together with its

deriva-tive at any point of [a, b] belongs to the space C1([a, b]) More generally,

a function with continuous derivatives up to the order p (a positive

in-teger) is said to belong to C p ([a, b]) In particular, C0([a, b]) denotes the

space of continuous functions in [a, b].

A result that will be often used is the mean-value theorem, according

to which, if f ∈ C1([a, b]), there exists ξ ∈ (a, b) such that

f  (ξ) = (f (b) − f(a))/(b − a).

Finally, it is worth recalling that a function that is continuous with

all its derivatives up to the order n in a neighborhood of x0, can be

approximated in such a neighborhood by the so-called Taylor polynomial

of degree n at the point x0:

derivative, the indefinite integral (i.e a primitive) and the Taylor

poly-nomial, respectively, of a given function In particular, having defined in

the string f the function on which we intend to operate, diff(f,n)

Ngày đăng: 05/11/2019, 14:58

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN