* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Approximate Error
Survey
Document related concepts
Transcript
ΑΡΙΘΜΗΤΙΚΗ ΑΝΑΛΥΣΗ 2. Αναπαράσταση αριθμών στον υπολογιστή 3. Σφάλματα 1 Scientific computing is a discipline concerned with the development and study of numerical algorithms for solving mathematical problems that arise in various disciplines in science and engineering 2 Numerical methods are an essential part of an engineer’s life • They allow to solve a much wider range of physical/engineering problems than analytical methods do. • Numerical methods are however only one part of the solution: a good physical understanding of the problem is essential. • Numerical methods are the elementary pieces of more complex codes and software used for scientific simulation. • Studying numerical methods allows one to - understand how more complex codes work - be able to modify an existing code or create a new one - understand the errors/limits introduced by the numerical simulation 3 2. ΑΝΑΠΑΡΑΣΤΑΣΗ ΑΡΙΘΜΩΝ ΣΤΟΝ ΥΠΟΛΟΓΙΣΤΗ Base 10 1 257.76 2 10 5 10 7 10 7 10 6 10 2 1 0 2 Base 2 (1 23 0 2 2 1 21 1 20 ) (1011.0011) 2 1 2 3 4 ( 0 2 0 2 1 2 1 2 ) 10 11.1875 4 • Numbers that have a finite expansion in one numbering system may have an infinite expansion in another numbering system: (1.1)10 (1.000110011001100...)2 • You can never represent 1.1 exactly in binary system. 5 Convert Base 10 Integer to binary representation Converting a base-10 integer to binary representation. Quotient 11/2 5 5/2 2 2/2 1 1/2 0 Remainder 1 a0 1 a1 0 a2 1 a3 (11)10 (a3 a 2 a1a0 ) 2 (1011) 2 6 Fractional Decimal Number to Binary Converting a base-10 fraction to binary representation. 0.1875 2 0.375 2 0.75 2 0.5 2 Number Number after decimal 0.375 0.75 1.5 1.0 0.375 0.75 0.5 0.0 Number before decimal 0 a1 0 a2 1 a 3 1 a4 (0.1875)10 (a1a 2 a3a 4 ) 2 (0.0011) 2 7 Decimal Number to Binary 11.187510 ?.? 2 Since (11)10 (1011) 2 and (0.1875)10 (0.0011) 2 we have (11.1875)10 (1011.0011) 2 8 All Fractional Decimal Numbers Cannot be Represented Exactly Converting a base-10 fraction to approximate binary representation. Number 0.3 2 0.6 2 0.2 2 0.4 2 0.8 2 0.6 1.2 0.4 0.8 1.6 Number after decimal 0.6 0.2 0.4 0.8 0.6 Number before Decimal 0 a1 1 a2 0 a 3 0 a4 1 a 5 (0.3)10 (a1a2 a3a4 a5 ) 2 (0.01001) 2 0.28125 Floating Point Representation (decimal) • Floating Decimal Point : Scientific Form 256.78 is written as 2.5678 10 2 0.003678 is written as 3.678 10 3 256.78 is written as 2.5678 10 2 10 Floating-Point Arithmetic (cont.) • A computer number has three parts – the sign (+ or -) – the fraction part (called the mantissa) – the exponent part • There are three level of precision and these are the number of bits used for mantissa and exponent. Length Sign Mantissa Exponent Range Single 32 1 23 8 10±38 Double 64 1 52 11 10±308 Extended 80 1 64 15 10±4931 11 The form is exponent sign mantissa 10 or m 10e Example: For 2.5678 10 2 1 m 2.5678 e2 12 Floating Point Format for Binary Numbers y m 2e sign of number 0 for ve, 1 for - ve m mantissa 12 m 102 1 is not stored as it is always given to be 1. e integer exponent 13 Example 9 bit-hypothetical word the first bit is used for the sign of the number, the second bit for the sign of the exponent, the next four bits for the mantissa, and the next three bits for the exponent 54.7510 110110.112 1.10110112 25 1.10112 1012 We have the representation as 0 0 1 0 1 mantissa Sign of the number 1 1 0 1 exponent Sign of the exponent 14 Machine Epsilon Defined as the measure of accuracy and found by difference between 1 and the next number that can be represented Ten bit word Sign of number Sign of exponent Next four bits for exponent Next four bits for mantissa Next number 0 0 0 0 0 0 0 0 0 0 110 0 0 0 0 0 0 0 0 0 1 1.00012 1.062510 mach 1.0625 1 2 4 15 Relative Error and Machine Epsilon The absolute relative true error in representing a number will be less then the machine epsilon Example 0.0283210 1.11002 25 1.11002 20110 2 10 bit word (sign, sign of exponent, 4 for exponent, 4 for mantissa) 0 1 0 1 1 0 1 1 0 0 Sign of the Sign of the number exponent exponent mantissa 1.11002 20110 2 0.0274375 0.02832 0.0274375 a 0.02832 0.034472 2 4 0.0625 16 Subtracting two almost equal numbers 17 IEEE-754 Floating Point Standard • Standardizes representation of floating point numbers on different computers in single and double precision. • Standardizes representation of floating point operations on different computers. 18 IEEE-754 Format Single & Double Precision S Exponent8 Fraction23 32 bits for single precision 00000000000000000000000000000000 Sign Biased (s) Exponent (e’) Mantissa (m) . Value ( 1) 1 m 2 2 s Double precision S Exponent11 Fraction52 (continued) e ' 127 19 Example#1 11010001010100000000000000000000 Sign Biased (s) Exponent (e’) Mantissa (m) Value 1 1. m2 2 s e ' 127 1 1.101000002 2 1 1.625 2162127 1 1.625 235 5.5834 1010 1 (10100010) 2 127 20 Example#2 Represent -5.5834x1010 as a single precision floating point number. ? ??????????????????????????????? Sign Biased (s) Exponent (e’) Mantissa (m) 5.5834 10 1 1. ? 2 10 1 ? 21 Exponent for 32 Bit IEEE-754 8 bits would represent 0 e 255 Bias is 127; so subtract 127 from representation 127 e 128 22 Exponent for Special Cases Actual range of e 1 e 254 e 0 and e 255 are reserved for special numbers Actual range of e 126 e 127 23 Special Exponents and Numbers e 0 e 255 s 0 1 0 1 0 or 1 e all zeros all zeros all ones all ones all ones all zeros all ones m all zeros all zeros all zeros all zeros non-zero Represents 0 -0 NaN 24 IEEE-754 Format The largest number by magnitude 1.1........12 2 127 3.40 10 The smallest number by magnitude 1.00......02 2 126 38 2.18 10 38 Machine epsilon mach 2 23 1.19 10 7 25 Significant Digits • Significant digits are those digits that can be used with confidence. • Single-Precision: 7 Significant Digits 1.175494… × 10-38 to 3.402823… × 1038 • Double-Precision: 15 Significant Digits 2.2250738… × 10-308 to 1.7976931… × 10308 26 Using Floating Point Numbers • Beware of meaningless precision! – In 1424, Jamshid Masud al-Kashi published = 3.141 592 653 589 793 25… – …but noted that the error in computing the perimeter of a circle with a radius 600’000 times that of earth would be less than the thickness of a horse’s hair. • Donald E. Knuth : – Floating point arithmetic is by nature inexact, and it is not difficult to misuse it so that the computed answers consist almost entirely of “noise”. One of the principal problems of numerical analysis is to determine how accurate the results of certain numerical methods will be. 27 Using Floating Point Numbers • Never use equality between two floating point numbers !!!!!!!! • Use a special method to compare them!!!!! (define an acceptable error for the specific problem) 28 3. Σφάλματα (Errors) • Η λύση ενός προβλήματος με τη βοήθεια αριθμητικών μεθόδων διαφέρει πάντοτε από την ακριβή λύση λόγω της παρουσίας σφαλμάτων. Διάφορες πηγές μπορούν να προκαλέσουν σφάλματα στα αριθμητικά αποτελέσματα • Πηγές σφαλμάτων: – Μετρήσεις φυσικών ή χημικών συσκευών και μηχανισμών (δεδομένα που περιέχουν εγγενώς σφάλματα), ονομάζονται αρχικά σφάλματα. – Σφάλμα του μαθηματικού προβλήματος ή σφάλμα της μαθηματικής περιγραφής. Προέρχεται από τη μετατροπή του προβλήματος σε μαθηματικό πρόβλημα, περιέχοντας απλοποιήσεις ή και παραλείψεις. – Σφάλματα από προβλήματα κακής κατάστασης :αριθμητικά προβλήματα που είναι πολύ ευαίσθητα σε μικρές μεταβολές των δεδομένων. 29 Accuracy and Precision • Accuracy refers to how closely a computed or measured value agrees with the true value, while precision refers to how closely individual computed or measured values agree with each other (Μέτρο της ικανότητας διάκρισης μεταξύ σχεδόν ίσων τιμών). a) inaccurate and imprecise b) accurate and imprecise c) inaccurate and precise d) accurate and precise 30 Four possible sources of errors ➡ Measurement error ➡ Modeling error ➡ Truncation error ➡ Round-off error 31 A. Measurement error ➡ Any instrument has a limit on its precision, and an experimental result is always obtained with a tolerance: e.g. 20.2 ± 0.1 cm B. Modeling error ➡ Difference between the real system and the simplified description used. ➡ Example of the bridge: representing the elements as homogeneous or with a simplified geometry. 32 C. Truncation error ➡ This error arises due to the discrete or iterative nature of the numerical methods used. ➡ For example, an iterative scheme can be developed to obtain the physical quantity G. - If the scheme is well-designed then G(n) approaches the true value of G0 when n→∞. - However, we always have to stop at a finite value of n=N. The truncation error is the difference between G(N) and G(∞). ➡ A truncation error also arises when approximating a continuous quantity by a discrete form or a derivative by a discrete limit: 33 D. Round-off error • This error is intrinsic to the use of a computer. • A computer does not use the real number but finiteprecision numbers (e.g. some decimals are discarded.) • The propagation of rounding errors from one floating point operation to the next is the most frequent source of numerical instabilities. 34 Sources of Numerical Errors 1) Round off error (σφάλμα στρογγυλοποίησης) – Προκύπτει από την ανάγκη αναπαράστασης των αριθμών με πεπερασμένο πλήθος ψηφίων 2) Truncation error (σφάλμα αποκοπής) – Δημιουργείται από τον αλγόριθμο που χρησιμοποιείται και την προσέγγιση που επιλέγεται (κατά την αντικατάσταση μιας ακριβούς διαδικασίας υπολογισμού με μία προσεγγιστική) 35 Round off Error • Caused by representing a number approximately 1 0.333333 3 2 1.4142... 36 Rounding errors are random 37 Problems created by round off error • 28 Americans were killed on February 25, 1991 by an Iraqi Scud missile in Dhahran, Saudi Arabia. • The patriot defense system failed to track and intercept the Scud. Why? 38 Problem with Patriot missile • Clock cycle of 1/10 seconds was represented in 24-bit fixed point register created an error of 9.5 x 10-8 seconds. • The battery was on for 100 consecutive hours, thus causing an inaccuracy of 9.5 108 0.342s s 3600s 100hr 0.1s 1hr • The shift calculated in the ranging system of the missile was 687 meters. • The target was considered to be out of range at a distance greater than 137 meters. 39 Example: quadrature of a circle 40 41 42 A large collection of software bugs • Careless numerical computing does occasionally lead to disasters http://wwwzenger.informatik.tumuenchen.de/persons/huckle/bugse.html 43 Άσκηση (σε MATLAB) 44 • Why measure errors? 1) To determine the accuracy of numerical results. 2) To develop stopping criteria for iterative algorithms. 45 True Error (απόλυτο σφάλμα) • Defined as the difference between the true value in a calculation and the approximate value found using a numerical method etc. True Error = True Value – Approximate Value 46 Example—True Error The derivative, f (x ) of a function f (x) can be approximated by the equation, f ' ( x) If f ( x h) f ( x) h and h 0.3 a) Find the approximate value of f ' (2) b) True value of f ' (2) c) True error for part (a) f ( x) 7e 0.5 x 47 Example (cont.) Solution: a) For x 2 and h 0.3 f ( 2 0.3) f ( 2) 0.3 f (2.3) f (2) 0.3 f ' ( 2) 7e 0.5( 2.3) 7e 0.5( 2) 0.3 22.107 19.028 10.263 0.3 48 Example (cont.) Solution: b) The exact value of f ' (2) can be found by using our knowledge of differential calculus. f ( x) 7e 0.5 x f ' ( x ) 7 0.5 e0.5 x 3.5e 0.5 x So the true value of f ' (2) 3.5e 0.5( 2 ) 9.5140 f ' ( 2) is True error is calculated as Et True Value – Approximate Value 9.5140 10.263 0.722 49 Relative True Error (σχετικό απόλυτο σφάλμα) • Defined as the ratio between the true error, and the true value. Relative True Error ( t ) = True Error True Value 50 Example—Relative True Error Following from the previous example for true error, find the relative true error for f ( x) 7e 0.5 x at f ' (2) with h 0.3 From the previous example, Et 0.722 Relative True Error is defined as t True Error True Value 0.722 0.075888 9.5140 as a percentage, t 0.075888 100% 7.5888% 51 Approximate Error (προσεγγιστικό σφάλμα) • What can be done if true values are not known or are very difficult to obtain? • Approximate error is defined as the difference between the present approximation and the previous approximation. Approximate Error (Ea ) = Present Approximation – Previous Approximation 52 Example—Approximate Error For f ( x) 7e 0.5 x at x 2 find the following, a) f (2) using h 0.3 b) f (2) using h 0.15 c) approximate error for the value of f (2) for part b) Solution: a) For x 2 and h 0.3 f ( x h) f ( x) h f ( 2 0.3) f ( 2) f ' ( 2) 0.3 f ' ( x) 53 Example (cont.) Solution: (cont.) f (2.3) f (2) 0.3 7e 0.5( 2.3) 7e 0.5( 2) 0.3 22.107 19.028 10.263 0.3 b) For x 2 and h 0.15 f (2 0.15) f (2) 0.15 f (2.15) f (2) 0.15 f ' (2) 54 Example (cont.) Solution: (cont.) 7e 0.5( 2.15) 7e 0.5( 2) 0.15 20.50 19.028 9.8800 0.15 c) So the approximate error, Ea is Ea Present Approximation – Previous Approximation 9.8800 10.263 0.38300 55 Relative Approximate Error (σχετικό σφάλμα) • Defined as the ratio between the approximate error and the present approximation. Relative Approximate Error ( a ) = Approximate Error Present Approximation 56 Example—Relative Approximate Error For f ( x) 7e at x 2 , find the relative approximate error using values from h 0.3 and h 0.15 Solution: From Example 3, the approximate value of f (2) 10.263 using h 0.3 and f (2) 9.8800 using h 0.15 0.5 x Ea Present Approximation – Previous Approximation 9.8800 10.263 0.38300 57 Example (cont.) Solution: (cont.) a Approximate Error Present Approximation 0.38300 0.038765 9.8800 as a percentage, a 0.038765 100% 3.8765% Absolute relative approximate errors may also need to be calculated, a | 0.038765 | 0.038765 or 3.8765 % 58 How is Absolute Relative Error used as a stopping criterion? If |a | s where s is a pre-specified tolerance, then no further iterations are necessary and the process is stopped. If at least m significant digits are required to be correct in the final answer, then |a | 0.5 10 2m % 59 Table of Values For f ( x) 7e 0.5 x at x 2 with varying step size, h h f ( 2) a m 0.3 10.263 N/A 0 0.15 9.8800 3.877% 1 0.10 9.7558 1.273% 1 0.01 9.5378 2.285% 1 0.001 9.5164 0.2249% 2 60 Θεώρημα If a positive number x has n correct digits in the narrow sense, the relative error ER of this number does not exceed 1 𝑛−1 10 divided by the first significant digit of the given number or 𝐸𝑅 ≤ 1 1 𝑛−1 𝑎𝑚 10 ,where 𝑎𝑚 is first significant digit of number x. 61 Παράδειγμα How many digits are to be taken in computing 20 so that the error does not exceed 0.1%? 62 Σφάλμα, Απόλυτο Σφάλμα και σημαντικά ψηφία • Αν η τιμή 𝑥 είναι μια προσέγγιση της τιμής 𝑥 τότε : – Σφάλμα : 𝐸𝑥 = 𝑥 − 𝑥 – Απόλυτο σφάλμα : 𝐸𝑥 = 𝑥 − 𝑥 – Σχετικό σφάλμα : 𝑅𝑥 = 𝑥− 𝑥 , 𝑥 𝑥≠0 • Ο αριθμός 𝑥 λέμε ότι προσεγγίζει την πραγματική τιμή 𝑥 με 𝑑 σημαντικά ψηφία αν 𝑑 είναι ο μεγαλύτερος θετικός ακέραιος για τον οποίον ικανοποιείται η ανισότητα: 𝑥− 𝑥 𝑥 < 1 10−𝑑 2 • Εφαρμογή για 𝑥 = 3,14 και 𝑥 = 3,141592 63 Problem conditioning and algorithm stability • The problem is ill-conditioned if a small perturbation in the data may produce a large difference in the result. • The problem is well-conditioned otherwise. • The algorithm is stable if its output is the exact result of a slightly perturbed input. 64 An unstable algorithm 65 A stable algorithm 66 Effect of Carrying Significant Digits in Calculations 67 Find the contraction in the diameter Tc D D (T )dT Ta Ta=80oF; Tc=-108oF; D=12.363” α = a0+ a1T + a2 2 T 68 Thermal Expansion Coefficient vs Temperature D D T T(oF) α (μin/in/oF) -340 -300 2.45 3.07 -220 -160 4.08 4.72 -80 5.43 0 40 6.00 6.24 80 6.47 69 Regressing Data in Excel (general format) α = -1E-05T2 + 0.0062T + 6.0234 70 Observed and Predicted Values α = -1E-05T2 + 0.0062T + 6.0234 T(oF) α (μin/in/oF) Given α (μin/in/oF) Predicted -340 2.45 2.76 -300 3.07 3.26 -220 4.08 4.18 -160 4.72 4.78 -80 5.43 5.46 0 6.00 6.02 40 6.24 6.26 80 6.47 6.46 71 Regressing Data in Excel (scientific format) α = -1.2360E-05T2 + 6.2714E-03T + 6.0234 72 Observed and Predicted Values α = -1.2360E-05T2 + 6.2714E-03T + 6.0234 T(oF) α (μin/in/oF) Given α (μin/in/oF) Predicted -340 2.45 2.46 -300 3.07 3.03 -220 4.08 4.05 -160 4.72 4.70 -80 5.43 5.44 0 6.00 6.02 40 6.24 6.25 80 6.47 6.45 73 Observed and Predicted Values α = -1.2360E-05T2 + 6.2714E-03T + 6.0234 α = -1E-05T2 + 0.0062T + 6.0234 T(oF) α (μin/in/oF) Given α (μin/in/oF) Predicted α (μin/in/oF) Predicted -340 2.45 2.46 2.76 -300 3.07 3.03 3.26 -220 4.08 4.05 4.18 -160 4.72 4.70 4.78 -80 5.43 5.44 5.46 0 6.00 6.02 6.02 40 6.24 6.25 6.26 80 6.47 6.45 6.46 74 Truncation error • Error caused by truncating or approximating a mathematical procedure. 75 Example of Truncation Error Taking only a few terms of a Maclaurin series to x e approximate 2 3 x x e x 1 x .................... 2! 3! If only 3 terms are used, 2 x x Truncation Error e 1 x 2! 76 1 .2 e Calculate the value of with an absolute relative approximate error of less than 1%. e1.2 1.2 2 1.2 3 1 1.2 ................... 2! 3! n e 1 .2 Ea a % 1 1 __ ___ 2 2.2 1.2 54.545 3 2.92 0.72 24.658 4 3.208 0.288 8.9776 5 3.2944 0.0864 2.6226 6 3.3151 0.020736 0.62550 6 terms are required. How many are required to get at least 1 significant digit correct in your answer? 77 Taylor Series (1) 78 Taylor Series (2) To gain insight consider the mathematical formulation that is used widely in numerical methods - TAYLOR SERIES. A Taylor series :provides a means to predict a function value at one point in terms of the function value at and its derivatives at another point Some examples of Taylor series which you must have seen x2 x4 x6 cos( x) 1 2! 4! 6! x3 x5 x7 sin( x) x 3! 5! 7! x2 x3 e 1 x 2! 3! x 79 General Taylor Series The general form of the Taylor series is given by f x 2 f x 3 f x h f x f x h h h 2! 3! provided that all derivatives of f(x) are continuous and exist in the interval [x, x+h] . As Archimedes would have said, “Give me the value of the function at a single point, and the value of all (first, second, and so on) its derivatives at that single point, and I can give you the value of the function at any other point” 80 81 Example—Taylor Series Find the value of f 6 given that f 4 125, f 4 74, f 4 30, f 4 6 and all other higher order derivatives of f x at x 4 are zero. Solution: h2 h3 f x h f x f x h f x f x 2! 3! x 4, h 64 2 82 Example (cont.) Since the higher order derivatives are zero, 22 23 f 4 2 f 4 f 42 f 4 f 4 2! 3! 2 2 23 f 6 125 742 30 6 2! 3! 125 148 60 8 341 Note that to find f 6 exactly, we only need the value of the function and all its derivatives at some other point, in this case x 4 83 Derivation for Maclaurin Series for ex Derive the Maclaurin series x2 x3 e 1 x 2! 3! x The Maclaurin series is simply the Taylor series about the point x=0 h2 h3 h4 h5 f x h f x f x h f x f x f x f x 2! 3! 4 5 h2 h3 h4 h5 f 0 h f 0 f 0h f 0 f 0 f 0 f 0 2! 3! 4 5 84 Derivation (cont.) Since f ( x) e x , f ( x) e x , f ( x) e x , ... , f n ( x) e x and f n (0) e0 1 the Maclaurin series is then (e 0 ) 2 ( e 0 ) 3 f ( h ) ( e ) (e ) h h h ... 2! 3! 1 1 1 h h 2 h 3 ... 2! 3! 0 0 So, x 2 x3 f ( x) 1 x ... 2! 3! 85 The exponentia l function is computed by 2 3 4 n x x x x e x 1 x ... ... 2 3! 4! n! When x = 0.5 Terms Result εt (True percentage relative εa (Approx. percentage error) relative error) 1 1 (1.6487-1)/1.6487 = 39.3% 2 1.5 (1.6487-1.5)/1.6487 = 9.02% (1.5-1)/1.5 = 33.3% 3 1.625 1.44% 4 1.645833333 0.175% 1.27% 5 1.648437500 0.0172% 0.158% 6 1.648697917 0.00142% 0.0158% (1.625-1.5)/1.625 = 7.69% 86 How many terms should we use? Terms Result εt 1 1 39.3% 2 1.5 9.02% 33.3% 3 1.625 1.44% 7.69% 4 1.645833333 0.175% 1.27% 5 1.648437500 0.0172% 0.158% 6 1.648697917 0.00142% 0.0158% • Computation stops when |εa| εa < εs – εs = pre-determined acceptable percentage relative error 87 Error in Taylor Series The Taylor polynomial of order n of a function f(x) with (n+1) continuous derivatives in the domain [x, x+h] is given by n h2 h f x h f x f x h f ' ' x f n x Rn x 2! n! where the remainder is given by Rn n 1 x h x (n 1)! f n 1 c where x c xh that is, c is some point in the domain [x, x+h] 88 Example—error in Taylor series The Taylor series for e x at point x 0 is given by 2 3 4 5 x x x x ex 1 x 2! 3! 4! 5! It can be seen that as the number of terms used increases, the error bound decreases and hence a better estimate of the function can be found. How many terms would it require to get an approximation of e1 within a magnitude of true error of less than 10-6. 89 Example—(cont.) Solution: Using n 1 terms of Taylor series gives error bound of n 1 x h x 0, h 1, f ( x) e x Rn x f n 1 c n 1! n 1 0 1 Rn 0 f n 1 c n 1! n 1 1 ec n 1! Since x c xh 0 c 0 1 0 c 1 1 e Rn 0 (n 1)! (n 1)! 90 Example—(cont.) Solution: (cont.) So if we want to find out how many terms it would 1 require to get an approximation of e within a 6 10 magnitude of true error of less than , e 106 (n 1)! (n 1)! 106 e (n 1)! 106 3 (as we do not know the value of but it is less than 3) n9 So 9 terms or more are needed to get a true error 6 10 less than 91 Exercise-1 Use Taylor series to find the value of function f ( x) sin( x) for x 2 Compare this value with one get from a calculator ( 0.909296723 ) 92