Lecture Note 1:Introduction,calculus review and computer representation of numbers Xiaoqun Zhang Shanghai Jiao Tong University Last updated:September 17,2012
Lecture Note 1: Introduction, calculus review and computer representation of numbers Xiaoqun Zhang Shanghai Jiao Tong University Last updated: September 17, 2012
Lecture note 1 Numerical Analysis 1.1 Course descrioption What is numerical methods? Numerical methods is the study of algorithms for the problems of continuous math- ematics. 1.Topic covered in this course: Solutions of one variation nonlinear equations: Given f,find p such that f(p)=0. Computation error analysis Interpolation and curve fitting: given (xo,y0),(1,y),..,(In,yn),find a polynomial passing through these points. Numerical differentiation and integration: 张,,f)dr Solve linear equations: Solve for Ax=b with A being a large size matrix.Gaussian elimination etc. 2.Not covered in this course: Solutions of Partial/Ordinary differential equations ●Optimization ·Approximation Many more...(Eigenvalue problems,evaluation of matrix functions,etc.) Why numerical methods? Many problems have no analytical solution. Example:3x-e =0. Numerical methods can handle large scale problems. Example:System of linear equations Ax =b,where A is n x n and b,r are n x 1.Analytical solution (Cramer's rule).zi=det(Ai)/det(A).In the computation of det(A;),we need sums over all permutations of the numbers {1,2,...,n}.Operation n!.(factorial of n.)1GHz computer,n =60,we need more than60/10°/(3600*24*365)≈2.63×1064 years! However,using Gaussian elimination,O(n).Much much less than 1 seconds. The arithmetic in computer is finite-digital.Many numbers cannot be repre- sented EXACTLY in computer. Example:Arithmetic in mathematics(√②)2=2.Computer√2=l.4l4 (4-digits)and (1.414)22. 2
Lecture note 1 Numerical Analysis 1.1 Course descrioption What is numerical methods? Numerical methods is the study of algorithms for the problems of continuous mathematics. 1. Topic covered in this course: • Solutions of one variation nonlinear equations: Given f, find p such that f(p) = 0. • Computation error analysis • Interpolation and curve fitting: given (x0, y0),(x1, y1), · · · ,(xn, yn), find a polynomial passing through these points. • Numerical differentiation and integration: df dx, d 2 f dx2 , R b a f(x)dx • Solve linear equations: Solve for Ax = b with A being a large size matrix. Gaussian elimination etc. 2. Not covered in this course: • Solutions of Partial/Ordinary differential equations • Optimization • Approximation • Many more...(Eigenvalue problems, evaluation of matrix functions, etc.) Why numerical methods? • Many problems have no analytical solution. Example: 3x − e x = 0. • Numerical methods can handle large scale problems. Example: System of linear equations Ax = b, where A is n × n and b, x are n × 1. Analytical solution (Cramer’s rule). xi = det(Ai)/ det(A). In the computation of det(Ai), we need sums over all permutations of the numbers {1, 2, ..., n}. Operation > n!.(factorial of n.) 1GHz computer, n = 60, we need more than 60!/109/(3600 ∗ 24 ∗ 365) ≈ 2.63 × 1064 years! However, using Gaussian elimination, O(n 3 ). Much much less than 1 seconds. • The arithmetic in computer is finite-digital. Many numbers cannot be represented EXACTLY in computer. Example: Arithmetic in mathematics (√ 2)2 = 2. Computer √ 2 = 1.414 (4-digits) and (1.414)2 6= 2. 2
Lecture note 1 Numerical Analysis Computer cannot operate on functions directly. Example:What is the integral ofedt. 。Many more.… 1.2 Calculus Review In the following,we assume f is a function defined on a set X of real numbers. 1.2.1 Some definitions Definition 1 (Limit)f has the limit L at ro,written lim f(x)=L I-T0 if,given any real number e>0,there erists a real number o>0 such that lf(x)-L<e,whenever x∈Xand‖x-xol<6. Definition 2(Continuous)f is continuous at ro if lim f(x)=f(xo). Remarks: We say f is continuous on the set X if it is continuous at each number in X. The limit of a sequence is defined in a similar manner. f is continuous at to If {In}is any sequence in X converging to ro,then limn→of(xn)=f(xo. Definition 3(Derivative)If f is defined in an open interval containing ro.f is differentiable at ro if f'(xo)=lim f()-f(xo) x→x0工-T0 erists.The number f'(ro)is called the derivative of f at ro. Remarks: f is differentiable on X if f is differentiable at each number in X. Geometrically,the derivative of f at zo is the slope of the tangent line to the graph at f at (ro,f(ro)). If the function f is differentiable at zo,then f is continuous at xo
Lecture note 1 Numerical Analysis • Computer cannot operate on functions directly. Example: What is the integral of R x −∞ e −t 2 dt. • Many more... 1.2 Calculus Review In the following, we assume f is a function defined on a set X of real numbers. 1.2.1 Some definitions Definition 1 (Limit) f has the limit L at x0, written limx→x0 f(x) = L if, given any real number ǫ > 0, there exists a real number δ > 0 such that kf(x) − Lk < ǫ, whenever x ∈ X and kx − x0k < δ. Definition 2 (Continuous) f is continuous at x0 if limx→x0 f(x) = f(x0). Remarks: • We say f is continuous on the set X if it is continuous at each number in X. • The limit of a sequence is defined in a similar manner. • f is continuous at x0 ⇔ If {xn} is any sequence in X converging to x0, then limn→∞ f(xn) = f(x0). Definition 3 (Derivative) If f is defined in an open interval containing x0. f is differentiable at x0 if f ′ (x0) = limx→x0 f(x) − f(x0) x − x0 exists. The number f ′ (x0) is called the derivative of f at x0. Remarks: • f is differentiable on X if f is differentiable at each number in X. • Geometrically, the derivative of f at x0 is the slope of the tangent line to the graph at f at (x0, f(x0)). • If the function f is differentiable at x0, then f is continuous at x0. 3
Lecture note 1 Numerical Analysis Note:all the functions we will consider when discussing numerical methods will be assumed to be continuous since this is a minimal requirement for predictable be- havior.More sophisticated assumptions about a function generally lead to better approximation results.For example,smooth function,such as differentiable func- tions,will normally behave more predictable than on numerous jagged features. Notations: C(X)denotes the set of all functions that are continuous on X.For example C(a,b),Cla,b] Cn(X)denotes the set of all functions that have n continuous derivatives on X.For example C"(a,b),Cn[a,b].The set of functions that have derivatives of all orders on X is denoted C().Polynomial,rational,trigonometric, exponential,and logarithmic functions are in C(X). .Given a function f on X,f(X)denotes the set of all the number f(r)for xEX.For example f([a,b]). 1.2.2 Important theorems Theorem 1 (Rolle's Theorem)Suppose f Cla,b and f is differentiable on (a,b).If f(a)=f(b),then a number c in (a,b)erists with f'(c)=0. See Figure 1.1. '(c=0 v=f(r) f(a)=f(b) Figure 1.1:Rolle's Theorem Theorem 2 (Mean Value Theorem)Suppose f is differentiable on (a,B).Let a,b be two distinct points in (a,B).Then there exists a pointE(a,b),such that f')=f)-fa) b-a that is f(b)=f(a)+f'()(b-a). 4
Lecture note 1 Numerical Analysis Note: all the functions we will consider when discussing numerical methods will be assumed to be continuous since this is a minimal requirement for predictable behavior. More sophisticated assumptions about a function generally lead to better approximation results. For example, smooth function, such as differentiable functions, will normally behave more predictable than on numerous jagged features. Notations: • C(X) denotes the set of all functions that are continuous on X. For example C(a, b), C[a, b] • C n(X) denotes the set of all functions that have n continuous derivatives on X. For example C n(a, b), Cn[a, b]. The set of functions that have derivatives of all orders on X is denoted C∞(X). Polynomial, rational, trigonometric, exponential, and logarithmic functions are in C∞(X). • Given a function f on X, f(X) denotes the set of all the number f(x) for x ∈ X. For example f([a, b]). 1.2.2 Important theorems Theorem 1 (Rolle’s Theorem) Suppose f ∈ C[a, b] and f is differentiable on (a, b). If f(a) = f(b), then a number c in (a, b) exists with f ′ (c) = 0. See Figure 1.1. f ′ (c) = 0 b b f(a) = f(b) a c b y = f(x) x y Figure 1.1: Rolle’s Theorem Theorem 2 (Mean Value Theorem) Suppose f is differentiable on (α, β). Let a, b be two distinct points in (α, β). Then there exists a point ξ ∈ (a, b), such that f ′ (ξ) = f(b) − f(a) b − a that is f(b) = f(a) + f ′ (ξ)(b − a). 4
Lecture note 1 Numerical Analysis f(r) Slope) Slope Figure 1.2:Mean value Theorem Figure 1.3:Extreme value theorem See figure 1.2. Example:Let f(x)=xsin(x)-(x-2)Inx show that f'(x)=0 at least on [1,2]Since f(1)=0 and f(2)=0,thus there exists c such that f'(c)=0. Theorem 3(Extreme Value Theorem)Suppose f ECla,b],then there erists two points E,2∈[a,bl,such that f()≤f(x)≤f(ξ2)forx∈[a,.In addition, if f is differentiable on [a,b],then E,2 occur either at the endpoints of a,bl or where f'is zero. Theorem 4(Intermediate Value Theorem)Suppose f E Cla,b]and K is any number between f(a),f(b),then there exists a numbergE(a,b)for which f()=K. Example:Let f(x)=x-e-z For f(0)=-1,f(1)=1-1>0,then there exists c such that f(c)=0. Theorem 5(Taylor's theorem)Suppose fCn[a,b]that fn+1 erists on [a,b], and ro E [a,b],there erists a number E(r)between ro and r with f(r)=Pn(x)+
Lecture note 1 Numerical Analysis b b a c b y = f(x) b Slope f ′ (c) Slope f(b)−f(a) b−a x y Figure 1.2: Mean value Theorem b b a ξ2 b y = f(x) x y ξ1 Figure 1.3: Extreme value theorem See figure 1.2. Example: Let f(x) = x sin(πx) − (x − 2) ln x show that f ′ (x) = 0 at least on [1, 2] Since f(1) = 0 and f(2) = 0, thus there exists c such that f ′ (c) = 0. Theorem 3 (Extreme Value Theorem) Suppose f ∈ C[a, b], then there exists two points ξ1, ξ2 ∈ [a, b], such that f(ξ1) ≤ f(x) ≤ f(ξ2) for x ∈ [a, b]. In addition, if f is differentiable on [a, b], then ξ1, ξ2 occur either at the endpoints of [a, b] or where f ′ is zero. Theorem 4 (Intermediate Value Theorem) Suppose f ∈ C[a, b] and K is any number between f(a), f(b), then there exists a number ξ ∈ (a, b) for which f(ξ) = K. Example: Let f(x) = x − e −x For f(0) = −1, f(1) = 1 − 1 e > 0, then there exists c such that f(c) = 0. Theorem 5 (Taylor’s theorem) Suppose f ∈ C n[a, b] that f n+1 exists on [a, b], and x0 ∈ [a, b], there exists a number ξ(x) between x0 and x with f(x) = Pn(x) + 5
Lecture note 1 Numerical Analysis f6) f(a =f(r) Figure 1.4:Intermediate Value theorem Rn(r)where Pn(z)= fo+ole-0+"aolz-oP+…+严mle-or 2 n! ml红-o k=0 and R国=t1z-o+1 (n+1)! Example::1)e near0:e2≈R(a)=1+x+分+…+元.2)fo)=cos( near0,cos()≈乃(d)=1-号,See1.5. fa)=e2,P乃(m)=1+x+号+om【-2,2习 f(a)=cos(e).PB(e)=1-号on【-T,可 Figure 1.5:Two Taylor approximation examples
Lecture note 1 Numerical Analysis a ξ b y = f(x) x y f(b) f(a) K Figure 1.4: Intermediate Value theorem Rn(x) where Pn(x) = f(x0) + f ′ (x0)(x − x0) + f ′′(x0) 2! (x − x0) 2 + · · · + f n(x0) n! (x − x0) n = Xn k=0 f k (x0) k! (x − x0) k and Rn(x) = f n+1(ξ(x)) (n + 1)! (x − x0) n+1 Example:: 1) e x near 0: e x ≈ P3(x) = 1 + x + x 2 2! + · · · + x n n! . 2) f(x) = cos(x) near 0, cos(x) ≈ P2(x) = 1 − x 2 2 . See 1.5. −2 −1.5 −1 −0.5 0 0.5 1 1.5 2 −1 0 1 2 3 4 5 6 7 8 f(x)=exp(x) P3 (x) −3 −2 −1 0 1 2 3 −4 −3.5 −3 −2.5 −2 −1.5 −1 −0.5 0 0.5 1 f(x)=cos(x) P2 (x)=1−1/2 x2 f(x) = e x , P3(x) = 1 + x + x 2 2! + x 3 3! on [−2, 2] f(x) = cos(x), P2(x) = 1 − x 2 2 on [−π, π] Figure 1.5: Two Taylor approximation examples 6
Lecture note 1 Numerical Analysis 1.3 Round off errors and Computer arithmetic 1.3.1 IEEE floating point representation Binary(machine)representation of numbers (11011.01)2=1×24+1×22+0×22+1×21+1×20 +0×2-1+1×2-2 -16+8+2+1+分 =27.25 .Examples:Integers:(111...1)2 =2n-1 Fraction::(0.111.1)2=1-六 IEEE Floating Point Arithmetic Standard 754-1985.A machine number has three parts:Long real(64 bit),double precision 63|62 52|510 Exponent Mantissa -sign (1 bit):s -exponent (11 bit):c -mantissa(52 bit):f ·Conversion formula: x=(-1)2c-1023(1+f) ·Example: |0|100..0111011010.0 -s=0 -c=(100..011)2=210+2+1=1027 -f=(0.101101..02=立+3+六+高=0.703125 thsx=(-1)°21027-1023)(1.703125)=27.25 .What are the possible values for s,c and f. s={0,1}={+,-} c={0,1,2,3,211-1}={0,1,2,3,,2047}c-1023={-1023,.,1024 f=0壶0是1-} 123
Lecture note 1 Numerical Analysis 1.3 Round off errors and Computer arithmetic 1.3.1 IEEE floating point representation • Binary (machine) representation of numbers (11011.01)2 = 1 × 2 4 + 1 × 2 2 + 0 × 2 2 + 1 × 2 1 + 1 × 2 0 + 0 × 2 −1 + 1 × 2 −2 = 16 + 8 + 2 + 1 + 1 4 = 27.25 • Examples: Integers: (111 . . . 1)2 = 2n − 1 Fraction: (0.111 . . . 1)2 = 1 − 1 2n • IEEE Floating Point Arithmetic Standard 754-1985. A machine number has three parts: Long real (64 bit), double precision 63 62 52 51 0 S Exponent Mantissa – sign (1 bit): s – exponent (11 bit):c – mantissa (52 bit):f • Conversion formula: x = (−1)s 2 c−1023(1 + f) • Example: 0 100. . . 011 1011010.. . 0 – s = 0 – c = (100 . . . 011)2 = 210 + 2 + 1 = 1027 – f = (0.101101 . . . 0)2 = 1 2 + 1 8 + 1 16 + 1 64 = 0.703125 thus x = (−1)02 (1027 − 1023)(1.703125) = 27.25 • What are the possible values for s, c and f. s = {0, 1} = {+, −} c = {0, 1, 2, 3, . . ., 2 11−1} = {0, 1, 2, 3, . . ., 2047} c−1023 = {−1023, . . ., 1024} f = {0, 1 2 52 , 2 2 52 , 3 2 52 , . . . , 1 − 1 2 52 } 7
Lecture note 1 Numerical Analysis ·Why1+f? The first nonzero bit is always 1.To save bits,we don't store it. 101101..00 is to represent1.101101..00. underflow and overflow -Special numbers 土0 s=0or1,c=0,f=0 土00 8=0or1,c=2047,f=0 NaN (not a number)c=2047,f is anything but 0 -The Smallest positive number is s=0.c=1 and f=0 2021-1023(1+0)≈0.2225×10-307 Underflow if a number occurs with a magnitude less than it. The largest positive number is s=0,c=2046 and f=1- 22016-10232-2a)≈01797×109 1 Overflow if a number occurs with a magnitude greater than it. -Machine precision:女≈2.2204×10-16Type“eps”in Matlab,you will get this number. There are various kinds of errors that we encounter when using a computer for computation. Truncation Error:Caused by adding up to a finite number of terms,while we should add infinitely many terms to get the exact answer in theory. Errors depending on the numerical algorithms,step size,and so on. Overflow/Underflow:Caused by too large or too small numbers to be represented/stored properly in finite bits. Negligible Addition:Caused by adding two numbers of magnitudes differ- ing by over 52 bits,as can be seen in the last section. Round-off Error:Caused by representing/storing numeric data in finite bits. Loss of Significance:Caused by a bad subtraction,which means a subtrac- tion of a number from another one that is almost equal in value. Error Magnification:Caused and magnified/propagated by multiplying/dividing a number containing a small error by a large/small number. 8
Lecture note 1 Numerical Analysis • Why 1 + f? The first nonzero bit is always 1. To save bits, we don’t store it. 101101 . . . 00 is to represent 1.101101 . . .00. • underflow and overflow – Special numbers ±0 s = 0 or 1, c = 0, f = 0 ±∞ s = 0 or 1, c = 2047, f = 0 NaN (not a number) c = 2047, f is anything but 0 – The Smallest positive number is s = 0, c = 1 and f = 0 2 0 2 1−1023(1 + 0) ≈ 0.2225 × 10−307 Underflow if a number occurs with a magnitude less than it. – The largest positive number is s = 0, c = 2046 and f = 1 − 1 2 52 2 0 2 2046−1023(2 − 1 2 52 ) ≈ 0.17977 × 10309 Overflow if a number occurs with a magnitude greater than it. – Machine precision: 1 2 52 ≈ 2.2204× 10−16 Type “eps” in Matlab, you will get this number. There are various kinds of errors that we encounter when using a computer for computation. • Truncation Error: Caused by adding up to a finite number of terms, while we should add infinitely many terms to get the exact answer in theory. • Errors depending on the numerical algorithms, step size, and so on. • Overflow/Underflow: Caused by too large or too small numbers to be represented/ stored properly in finite bits. • Negligible Addition: Caused by adding two numbers of magnitudes differing by over 52 bits, as can be seen in the last section. • Round-off Error: Caused by representing/storing numeric data in finite bits. • Loss of Significance: Caused by a bad subtraction, which means a subtraction of a number from another one that is almost equal in value. • Error Magnification: Caused and magnified/propagated by multiplying/dividing a number containing a small error by a large/small number. 8
Lecture note 1 Numerical Analysis Round off errors The use of binary digits tends to conceal the computational difficulties.To examine this,for simplicity,we now assume k-digit decimal machine numbers: ±0.d1d2.dk×10,1≤d1≤9,0≤d:≤9. Any real number can be represented by y=0.d山d.…dkdk+1.×10m Obtain decimal machine number by 。Chopping:chop off the digits dk.+1,·, fl(y)=0.d1d2..dk×10m Example:(5-digit) 元=0.314159265..×101 f1(π)=0.31415×101 ·Rounding:dk+1≥5,add dk by 1 and chopping d+1<5 chopping. Example: fl(π)=0.31416×101 If p*is an approximation to p,the following definition describes two methods for measuring approximation errors: ·Absolute error lp-pl ·Relative error rf,provided that p≠0. Example:let p=0.3000 x 101 and p*=0.3100 x 101.Absolute error is 0.1,and the relative error is 0.3333 x 10-1. Significant digits The significant figures (also known as significant digits)of a number are those digits that carry meaning contributing to its precision. Precision of approximation:The number p*is said to approximate p to t significant digits(or figures)if t is the largest nonnegative integer for which ≤5xw- For a number y=0.dd2…dkdk+1…×10", -k decimal digits by chopping fl(y)=0.d1…dk ly-fl(y)l 0.dk+idx+2...1om-k 1 0.d边…d4×10m≤0×10-*=10-k-1) 9
Lecture note 1 Numerical Analysis Round off errors The use of binary digits tends to conceal the computational difficulties. To examine this, for simplicity, we now assume k-digit decimal machine numbers: ±0.d1d2 . . . dk × 10n , 1 ≤ d1 ≤ 9, 0 ≤ di ≤ 9. Any real number can be represented by y = 0.d1d2 . . . dkdk+1 . . . × 10n Obtain decimal machine number by • Chopping: chop off the digits dk+1, · · · , fl(y) = 0.d1d2 . . . dk × 10n Example: (5-digit) π = 0.314159265 . . .. × 101 fl(π) = 0.31415 × 101 • Rounding: dk+1 ≥ 5, add dk by 1 and chopping dk+1 < 5 chopping. Example: fl(π) = 0.31416 × 101 If p ∗ is an approximation to p, the following definition describes two methods for measuring approximation errors: • Absolute error |p − p ∗ | • Relative error |p−p ∗ | |p| , provided that p 6= 0. Example: let p = 0.3000 × 101 and p ∗ = 0.3100 × 101 . Absolute error is 0.1, and the relative error is 0.3333 × 10−1 . Significant digits The significant figures (also known as significant digits) of a number are those digits that carry meaning contributing to its precision. • Precision of approximation : The number p ∗ is said to approximate p to t significant digits (or figures) if t is the largest nonnegative integer for which |p − p ∗ | |p| ≤ 5 × 10−t For a number y = 0.d1d2 · · · dkdk+1 · · · × 10n, – k decimal digits by chopping fl(y) = 0.d1 · · · dk |y − fl(y)| |y| = 0.dk+1dk+2 · · · 10n−k 0.d1d2 · · · dk × 10n ≤ 1 0.1 × 10−k = 10−(k−1) 9
Lecture note 1 Numerical Analysis -k decimal digits by rounding y-f@1≤5×10-k Does not depend on the scale n!! Significant digits for a number:The number of significant digits in an answer to a calculation will depend on the number of significant digits in the given data How to recognize the significant digits? 1.If a number is expressed in decimal machine number,all digits are sig- nificant.For example for 1300,two significant representation 0.13 x 104; four significant representation 0.1300 x 104. 2.For a general number,non-zero digits are always significant.Thus,22 has two significant digits.and 22.3 has three significant digits.With zeroes,the situation is more complicated: -Zeroes placed before other digits are not significant;0.046 has two significant digits. Zeroes placed between other digits are always significant;4009 kg has four significant digits. Zeroes placed after other digits but behind a decimal point are sig- nificant:7.90 has three significant digits.Zeroes at the end of a number are significant only if they are behind a decimal point.Oth- erwise,it is impossible to tell if they are significant.For example,in the number 8200,it is not clear if the zeroes are significant or not. The number of significant digits in 8200 is at least two,but could be three or four.To avoid uncertainty,use decimal machine number to place significant zeroes behind a decimal point:0.8200 x 10 has four significant digits 0.820 x 104 has three significant digits 0.82 x 104 has two significant digits. Finite (significant)digit arithmetic .fl(z)is the float point representation of z. ·x⊕y=fl(fl(x)+fl(y) This the output of“x+y”by a computer ·x⑧y=fl(fl(x)×fl(y)and more. Loss of significant digit Cancelation of significant digits due to subtraction of nearly equal numbers. 10
Lecture note 1 Numerical Analysis – k decimal digits by rounding | y − fl(y) y | ≤ 5 × 10−k Does not depend on the scale n!! • Significant digits for a number: The number of significant digits in an answer to a calculation will depend on the number of significant digits in the given data. How to recognize the significant digits? 1. If a number is expressed in decimal machine number, all digits are significant. For example for 1300, two significant representation 0.13 × 104 ; four significant representation 0.1300 × 104 . 2. For a general number, non-zero digits are always significant. Thus, 22 has two significant digits, and 22.3 has three significant digits. With zeroes, the situation is more complicated: – Zeroes placed before other digits are not significant; 0.046 has two significant digits. – Zeroes placed between other digits are always significant; 4009 kg has four significant digits. – Zeroes placed after other digits but behind a decimal point are significant; 7.90 has three significant digits. Zeroes at the end of a number are significant only if they are behind a decimal point. Otherwise, it is impossible to tell if they are significant. For example, in the number 8200, it is not clear if the zeroes are significant or not. The number of significant digits in 8200 is at least two, but could be three or four. To avoid uncertainty, use decimal machine number to place significant zeroes behind a decimal point: 0.8200×104 has four significant digits 0.820 × 104 has three significant digits 0.82 × 104 has two significant digits. Finite (significant) digit arithmetic • fl(x) is the float point representation of x. • x ⊕ y = fl(fl(x) + fl(y)) This the output of “x + y” by a computer • x ⊗ y = fl(fl(x) × fl(y)) and more.. Loss of significant digit Cancelation of significant digits due to subtraction of nearly equal numbers. 10