06.09.2021 Views

First Semester in Numerical Analysis with Julia, 2020a

First Semester in Numerical Analysis with Julia, 2020a

First Semester in Numerical Analysis with Julia, 2020a

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 1. INTRODUCTION 45<br />

the average of two numbers, a+b . For a =2.954 and b = 100.9, the true average is 51.927.<br />

2<br />

However, four-digit arithmetic <strong>with</strong> round<strong>in</strong>g yields:<br />

( ) ( ) ( )<br />

100.9+2.954 fl(103.854) 103.9<br />

fl<br />

= fl<br />

= fl =51.95,<br />

2<br />

2<br />

2<br />

which has a relative error of 4.43 × 10 −4 . If we rewrite the averag<strong>in</strong>g formula as a + b−a,on<br />

2<br />

the other hand, we obta<strong>in</strong> 51.93, which has a much smaller relative error of 5.78 × 10 −5 .The<br />

follow<strong>in</strong>g table displays the exact and 4-digit computations, <strong>with</strong> the correspond<strong>in</strong>g relative<br />

error at each step.<br />

a+b<br />

b−a<br />

a b a+ b b − a a + b−a<br />

2 2 2<br />

4-digit round<strong>in</strong>g 2.954 100.9 103.9 51.95 97.95 48.98 51.93<br />

Exact 103.854 51.927 97.946 48.973 51.927<br />

Relative error 4.43e-4 4.43e-4 4.08e-5 1.43e-4 5.78e-5<br />

Example 21. There are two standard formulas given <strong>in</strong> textbooks to compute the sample<br />

variance s 2 of the numbers x 1 , ..., x n :<br />

[ ∑n<br />

1. s 2 = 1<br />

n−1 i=1 x2 i − 1 (∑ n<br />

n i=1 x i) 2] ,<br />

2. <strong>First</strong> compute ¯x = 1 n<br />

∑ n<br />

i=1 x i, and then s 2 = 1<br />

n−1<br />

∑ n<br />

i=1 (x i − ¯x) 2 .<br />

Both formulas can suffer from roundoff errors due to add<strong>in</strong>g large lists of numbers if n is<br />

large, as mentioned <strong>in</strong> the previous example. However, the first formula is also prone to error<br />

due to cancellation of lead<strong>in</strong>g digits (see Chan et al [6] for details).<br />

For an example, consider four-digit round<strong>in</strong>g arithmetic, and let the data be<br />

1.253, 2.411, 3.174. The sample variance from formula 1 and formula 2 are, 0.93 and 0.9355,<br />

respectively. The exact value, up to 6 digits, is 0.935562. Formula 2 is a numerically more<br />

stable choice for comput<strong>in</strong>g the variance than the first one.<br />

Example 22. We have a sum to compute:<br />

e −7 =1+ −7<br />

1 + (−7)2 + (−7)3<br />

2! 3!<br />

+ ... + (−7)n .<br />

n!<br />

The alternat<strong>in</strong>g signs make this a potentially error prone calculation.<br />

<strong>Julia</strong> reports the "exact" value for e −7 as 0.0009118819655545162. If we use <strong>Julia</strong> to<br />

compute this sum <strong>with</strong> n =20, the result is 0.009183673977218275. Here is the <strong>Julia</strong> code<br />

for the calculation:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!