cs249 Homework 1 Solutions 1.1 --- 1) >> absoluteError = 22/7 - pi absoluteError = 0.0013 >> relativeError = absoluteError / pi relativeError = 4.0250e-04 = .0402 % 2) a) .800 + .333 = 1.33 absoluteError = 0.00333333333333 relativeError = 0.00294117647059 b) 0.8 * 0.333 = 0.266 absoluteError = 6.666666666666488e-04 relativeError = 0.00250000000000 c) .333 - .273 = .060 .060 + .150 = .210 absoluteError = 6.060606060606100e-04 relativeError = 0.00287769784173 d) .333 - .273 = .060 .060 - .150 = -.090 absoluteError = 6.060606060606100e-04 relativeError = -0.00677966101695 The difference between (c) and (d) is the difference between addition and subtraction. Subtracting two similar values tends to increase relative errors by "losing" significant digits. 1.2 --- 1) 1 1 2 3 5 8 13 21 34 55 89 F(10) = 89 2 and 3) Here is the content of fibonacci1.m t1 = 1 / sqrt (5); t2 = (1 + sqrt (5)) / 2; t3 = (1 - sqrt (5)) / 2; f = t1 * (t2^(n+1) - t3^(n+1)) >> n = 10; >> fibonacci1 f = 89.00000000000003 >> n=100; >> fibonacci1 f = 5.731478440138190e+20 1.3 --- 1. I couldn't find a value of x that yielded different results. 2. This one is easy to break to varying degrees: >> x = 1e-6; 1 - sin(x)^2 - cos(x)^2 ans = 1.110223024625157e-16 >> x = pi/2; 1 - sin(x)^2 - cos(x)^2 ans = -3.749399456654644e-33 3. This one is harder to break. > x = 1e-6; sinh(x/2) - sqrt (1/2 * (cosh(x) - 1)) ans = -2.222465162926600e-11 4. I couldn't break this one.