Numerical Computing and Computers 1. Introduction Numerical (Analysis) Method -- Computers Solution is always numerical Solution is an approximation Analytical Method – Calculus (Math) Solution might be in terms of math function(s) Solution can be exact xex dx 0 _______________ 1 + cos2(x)dx 0 2. Computer Arithmetic and Errors Input Error Algorithm Error Output Error a. Trunction Error – Errors cause by the method itself (Error occurs as a result of truncating or cutting) ex = 1 + x + x2/2! + x3 /3! + x4/4! + … sin x = x - x3 /3! + x5/5! - x7 /7! +… b. Round-off Error – Error caused by the computer imperfection. (Error produced by not using all the bits of a binary number due to fixed size memory) 1 Real Number ------------------------------------- __ Rational Irrational 2, __________ Integers Fractions How are numbers stored in a computer? How are they combined? What effect do these machine features have on numerical result? ================== 8/26/2003 ============== a. Integer number numbers Adv – Exactly represented in computer memory (no round-off errors) Fast, simple, and accurate in computation Disadv – Cannot handle fraction numbers Small range of representation (ex. –235 ~ 235-1) b. Floating point numbers Adv – Can handle fraction Much larger range of representation (1075 ~ 10-79 -1) DisAdv – Slower and round-off error Many decimal fractions cannot be exactly represented in the binary system Math ---- Real Numbers -- Continuous, infinite, uniformly distributed Computer --- Floating point numbers -- not continuous, finite, not uniformly distri. Mapping – real numbers (infinite set) and floating point numbers (finite set) Real Number line ………………………. Floating point number . . . …………. . –1 0 +1 continuous, infinite, uniformly . not cont, finite, not uniformly distr 2 Floating-point numbers -- Closely packed near 0 Many floating points can not be represented precisely. They are sparsely packed as they are far away from 0. Round-off Error – (conti) Error caused by the computer imperfection. (Error produced by not using all the bits of a binary number due to fixed memory size) a. Can’t be exactly represented due to limited word size. (0.1)10 = (0.001100110011………..) 2 (0.6)10 = (0.100110011………..) 2 Normalized and un-normalized CMPS 391 – Imaginary Computer sign mantissa x xxxx exponentiation x 0.1001 + 0.1100 ---------------- 1.0101 Normalize ------------> sign + mantissa 0.1010 exponentiation (characteristic) x S = 1 - ½ + 1/3 - ¼ + 1/5 - 1/6 +… = [(-1)n+1 /n] n=1 1 = 0.1000E +1 ½ = 0.1000E +0 1/3 = (0.33333333) = (0.010101…) 2 = (0.1010E-1) 1/4 = 0.1000E –1 1/5 = (0.2) 10 = (0.001100110011….) 2 = (0.1100E-2) S = (0.1000E+1)-(0.1000E+0)+(0.1010E-1)-(0.1000E–1)+(0.1100E-2)- … = 0.5625… Tv = True Value = 0.69314718 … Av = Approx Value = 0.5625… Absolute Error = Tv –Av = 0.1306471 Relative Error = Abso value / Tv = 0.1308471 / 0.69314718 .. = 0.18 3 Causes of serious round-off errors – A. Adding (subtracting) a large number to (from) a small number CS391 COMPUTER ± d1d2 d3 d4 * Be ex.1 x = 0.3555E+2 y = 0.2333E-2 0.3555 +0.00002333 z = 0.8222E-2 0.3555 + 0.000082 -------------- -------------- 0.3555 E+2 0.3555 E+2 (x + y) + z = x + (y + z) 0.2333 +0.8222 ============ 1.0555 E-2 0.3555 + 0.00010555 ============ 0.3556 E+2 ex.2 S = 1 + 1/3 + 1/9 + 1/27 + …= [1/3n] = 1.5 n=0 One term S = 1 ----------------------------------------Two terms S = 1+1/3 = 4/3 = 1.33333 -------------------Three terms S = 1 + 1/3 + 1/9 = 13/9 = 1. 44444----------- Relative Error ~ 0.5 / 1.5 = 33% ~11% ~ 4% Avoid adding a large number to a small number Remedy -S = ((((1+1/3)*1/3 +1) 1/3+1)*1/3 +1)…. or In case n = 7 (terms) --Sum the series backward -- avoid adding a large number to a small number S = (1 + (1/3 + (1/9 + (1/27 + (1/81 + (1/243 +1/729)))))) ~ 4 ex3. ax2 + bx + c = 0 if b2 - 4ac ≈ b2 _______ x = [-b b2 - 4ac ] / [2a] x1 = -b/a , x2 = 0 ( b2 >>>>>>>>>>>4ac ) if ( b2 - 4ac ) ≈ b2 not accurate (error) x2 - 105 x + 1 = 0 __________ 5 x1 = {10 - [(10)5] 2 – 4 } / 2 * ----- rationalize ….. 105 ___________ x2 = {105 + [(10)5] 2 – 4 } / 2 * ----- rationalize ---- undefined f(x) = ax2 + bx + c = 0 *** x= Rationalization _______ [-b- b2 - 4ac ] / [2a] , _______ rationalize = [2c] / [ -b+ b2 - 4ac ] *** Factoring to improve the accuracy of the result(s) f(x) = ax2 + bx + c = a[(x –p)(x-q)] = a[x2 - (p+q)x + pq] p+q = -b/a and pq = c/a B. Subtracting two almost equal numbers ex1. x = 0.32425 E2 , y= 0.32300E2 0.324 E2 - 0.323 E2 = 0.001E2 = 0.100E0 True value = 0.125 E0 Relative error = | 0.125 - 0.100| ∕ | 0.125 | = 5/25 = 20% ex2. ______ 10002 -- _______ 10000 ~~~ 0 These two numbers might be stored exactly the same internally ______ ______ 10002 - 10000 = 2/20 = 0.1 (rationalize) 5 6