math 330 13 november 2018 - mrwright.orghowever, arbitrary precision calculations may take much more...

5
Math 330 13 November 2018 Day19 Page 1

Upload: others

Post on 14-Apr-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Math 330 13 November 2018 - mrwright.orgHowever, arbitrary precision calculations may take much more time than machine precision calcula-tions. For example: In[15]:= errcentered[1,

Math 330 — 13 November 2018

Day19 Page 1

Page 2: Math 330 13 November 2018 - mrwright.orgHowever, arbitrary precision calculations may take much more time than machine precision calcula-tions. For example: In[15]:= errcentered[1,

Day19 Page 2

Page 3: Math 330 13 November 2018 - mrwright.orgHowever, arbitrary precision calculations may take much more time than machine precision calcula-tions. For example: In[15]:= errcentered[1,

Day19 Page 3

Page 4: Math 330 13 November 2018 - mrwright.orgHowever, arbitrary precision calculations may take much more time than machine precision calcula-tions. For example: In[15]:= errcentered[1,

Error comparison for first derivative approximationsWe will compare the errors in approximating the derivative of sin(x) at x = 1, using the forward differ-ence, backward difference, and centered difference approximations.

First, define the function f(x) = sin(x). Also define the three approximations.

In[1]:= f[x_] := Sin[x];

forward[x0_, Δx_] := f[x0 + Δx] - f[x0] Δx;

backward[x0_, Δx_] := f[x0] - f[x0 - Δx] Δx;

centered[x0_, Δx_] := f[x0 + Δx] - f[x0 - Δx] 2 Δx;

Next, define the error for each approximation of f ' (x0). This requires computing the exact value of f ' (x0).

In[5]:= errforward[x0_, Δx_] := Abs[forward[x0, Δx] - f'[x0]];

errbackward[x0_, Δx_] := Abs[backward[x0, Δx] - f'[x0]];

errcentered[x0_, Δx_] := Abs[centered[x0, Δx] - f'[x0]];

We will compute four approximations of each type, using the following values of Δx:

In[8]:= steps = {.1, .01, .001, .0001}

Out[8]= {0.1, 0.01, 0.001, 0.0001}

Now compute the errors for each approximation and print them in a table.

In[9]:= TableForm[Table[{errforward[1, Δx], errbackward[1, Δx], errcentered[1, Δx]}, {Δx, steps}],

TableHeadings → {steps, {"forward", "backward", "centered"}}]

Out[9]//TableForm=

forward backward centered0.1 0.0429386 0.0411384 0.000900054

0.01 0.00421632 0.00419831 9.00499 × 10-6

0.001 0.000420826 0.000420645 9.00505 × 10-8

0.0001 0.0000420744 0.0000420726 9.0043 × 10-10

Note that when we divide Δx by 10, the errors for the forward and backward approximations are also divided by about 10, but the error for the centered approximation is divided by about 100.

Extend the table for a few more rows. What do you observe?

In[10]:= steps = {0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001}

Out[10]= 0.1, 0.01, 0.001, 0.0001, 0.00001, 1. × 10-6

Page 5: Math 330 13 November 2018 - mrwright.orgHowever, arbitrary precision calculations may take much more time than machine precision calcula-tions. For example: In[15]:= errcentered[1,

In[11]:= TableForm[Table[{errforward[1, Δx], errbackward[1, Δx], errcentered[1, Δx]}, {Δx, steps}],

TableHeadings → {steps, {"forward", "backward", "centered"}}]

Out[11]//TableForm=

forward backward centered0.1 0.0429386 0.0411384 0.000900054

0.01 0.00421632 0.00419831 9.00499 × 10-6

0.001 0.000420826 0.000420645 9.00505 × 10-8

0.0001 0.0000420744 0.0000420726 9.0043 × 10-10

0.00001 4.20736 × 10-6 4.20734 × 10-6 1.1141 × 10-11

1. × 10-6 4.20747 × 10-7 4.20802 × 10-7 2.77169 × 10-11

Something is going wrong for the the centered approximation!

The problem is round-off error. Round-off error is a big concern when doing computations with decimal numbers.

In[12]:= Precision[errcentered[1, 0.000001]]

Out[12]= MachinePrecision

MachinePrecision means that Mathematica is using floating-point arithmetic, as implemented in the computer processor. For most computers, this means 64-bit “double precision” floating-point num-bers, which use 53 bits of memory to store significant digits. This amounts to almost 16 decimal digits of precision, as the following commands show:

In[13]:= $MachinePrecision

Out[13]= 15.9546

In[14]:= N[53 Log[10, 2]]

Out[14]= 15.9546

If we avoid typing decimals, Mathematica will attempt to compute using arbitrary precision arithmetic. However, arbitrary precision calculations may take much more time than machine precision calcula-tions. For example:

In[15]:= errcentered[1, 10^(-6)]

Out[15]= Cos[1] - 500 000 -Sin999 999

1 000 000 + Sin

1 000 001

1 000 000

The formula above is exact, but probably not helpful. We can ask Mathematica to convert it to a deci-mal number with, say, 20 digits:

In[16]:= N[errcentered[1, 10^(-6)], 20]

Out[16]= 9.0050384311352117048 × 10-14

That' s better! Lastly, let’s see what Mathematica says is the precision of this calculation:

In[17]:= Precision[errcentered[1, 10^(-6)]]

Out[17]= ∞

2 error_demo.nb