\[ f'(x) = \lim_{h \rightarrow 0}\frac{f(x+h)-f(x)}{h} \]
\[ f'(x) = \lim_{h \rightarrow 0}\frac{f(x+h)-f(x)}{h} \]
\[ f'(x) \cong \frac{f(x+h)-f(x)}{h} \]
\[ \begin{aligned} f'(x) \cong \frac{f(x+h)-f(x)}{h} \\ f'(x) \cong \frac{f(x)-f(x-h)}{h} \end{aligned} \]
\[ f'(x) \cong \frac{f(x+h)-f(x-h)}{2h} \]
finddiffindiff <- function(f,x,
h = x*sqrt(.Machine$double.eps))
{ return( ( f(x + h) - f(x) ) / h ) }
\[ f'(x) \cong \frac{f(x+h)-f(x)}{h} \]
f<-function(x){4*x-2}
findiff(f,2,h=0.1)
[1] 4
findiff(f,2)
[1] 4
f <- function(x)
{x^2 + 1}
findiff(f,2,h = 0.1)
[1] 4.1
findiff(f,2,h = 0.01)
[1] 4.01
findiff(f,2)
[1] 4
f <- function(x)
{x^2 + 1}
findiff(f,2,h = -1)
[1] 3
findiff(f,2,h = -0.1)
[1] 3.9
findiff(f,2,h = -0.01)
[1] 3.99
symdiffsymdiff <- function(f,x,
h = x*(.Machine$double.eps)^(1/3))
{ return( (f(x + h) - f(x - h)) / (2*h) ) }
\[ f'(x) \cong \frac{f(x+h)-f(x-h)}{2h} \]
f<-function(x){4*x-2}
symdiff(f,2,h=0.1)
[1] 4
symdiff(f,2)
[1] 4
f <- function(x)
{x^2 + 1}
symdiff(f,2,h = 1)
[1] 4
symdiff(f,2,h = 0.1)
[1] 4
symdiff(f,2,h = 0.01)
[1] 4
\[ \begin{aligned} f'(x) & \cong \frac{f(x+h)-f(x)}{h} \\ f'(x) &\cong \frac{f(x+h)-f(x-h)}{2h} \end{aligned} \]
f <- function(x){x^2 + 1}; x <- 2
h <- c(3e-4,3e-8,3e-12,3e-16)
findiff(f,2,h)
[1] 4.000300 4.000000 3.999763 5.921189
(h <- x*sqrt(.Machine$double.eps))
[1] 2.980232e-08
findiff(f,x)
[1] 4
f <- function(x){x^2 + 1}; x <- 2
h <- c(1e-1,1e-5,1e-12,1e-16)
symdiff(f,x,h)
[1] 4.000000 4.000000 4.000356 0.000000
(h <- x*(.Machine$double.eps)^(1/3))
[1] 1.211091e-05
symdiff(f,x)
[1] 4
f <- function(x){sin(x)}; x <- 0.9
h <- c(5e-2,5e-6,5e-10,5e-14)
symdiff(f,x,h)
[1] 0.6213510 0.6216100 0.6216101 0.6217249
(h <- x*(.Machine$double.eps)^(1/3))
[1] 5.449909e-06
symdiff(f,x)
[1] 0.62161
\[ f(x+h) = f(x) + f'(x)\left((x+h)-x)\right) + \frac{f''(x)}{2}\left((x+h)-x)\right)^2 + \ldots \]
\[ \begin{aligned} f(x+h) - f(x) & = f'(x)h + \frac{f''(x)}{2}h^2 + \ldots \\ f'(x) & = \frac{f(x+h) - f(x)}{h} - \frac{f''(x)}{2}h + \ldots \\ f'(x) & = \frac{f(x+h) - f(x)}{h} - \frac{f''(\xi(x))}{2}h, \,\, \,\, \xi(x) \in [x,x+h] \\ \end{aligned} \]
\[ f'(x) = \frac{f(x+h) - f(x)}{h} - \frac{h}{2}f''(c) \]
\[ E_T = \frac{h}{2}f''(c) \]
So if we use the forward difference formula with perfect computational accuracy, then \( E_T \) is simply the error that results from not using limits and rules of calculus.
Next, we look at roundoff error (see next slide).
\[ \begin{aligned} \tilde{f}(x+h) &= f(x+h) + e_1(x) \\ \tilde{f}(x) &= f(x) + e_2(x) \end{aligned} \]
\[ \begin{aligned} \tilde{f}'(x) &= \frac{\tilde{f}(x+h) - \tilde{f}(x)}{h} \\ &= \frac{f(x+h) - f(x) }{h} + \frac{e_1(x) - e_2(x)}{h} \\ &= \frac{f(x+h) - f(x) }{h} + E_R \end{aligned} \]
\[ \begin{aligned} \tilde{f}'(x) = \frac{f(x+h) - f(x) }{h} + E_R \end{aligned} \]
\[ E_R = \frac{e_1(x) - e_2(x)}{h} \]
\[ E = E_R + E_T = \frac{e_1(x) - e_2(x)}{h} + \frac{h}{2}f''(c) \]
\[ E = \frac{e_1(x) - e_2(x)}{h} + \frac{h}{2}f''(c) \]
Observe that round off increases as \( h \) gets smaller, while truncation error decreases as \( h \) get smaller.
We will use calculus to determine the optimal value of \( h \).
\[ E = \frac{e_1(x) - e_2(x)}{h} + \frac{h}{2}f''(c) \]
\[ \left|e_1 - e_2\right| \leq |e_1| + |e_2| = 2 \max \left\{ |e_1|, |e_2| \right\} = 2e \]
\[ M = \max_{x \leq c \leq x+h} |f''(c)| \]
\[ E = \frac{e_1 - e_2}{h} + \frac{h}{2}f''(c) \]
\[ E(h) \leq \frac{2e}{h} + \frac{M}{2}h \]
\[ E(h) = \frac{2e}{h} + \frac{M}{2}h \]
\[ E(h) = \frac{2e}{h} + \frac{M}{2}h \]
\[ \begin{aligned} E'(h) & = -\frac{2e}{h^2} + \frac{M}{2} \\ 0 & = -\frac{2e}{h^2} + \frac{M}{2} \\ h &= \sqrt{\frac{4e}{M}} \end{aligned} \]
\[ E(h) = \frac{2e}{h} + \frac{M}{2}h \]
\[ h = \sqrt{\frac{4e}{M}} \]
\[ h = x\sqrt{\epsilon} \]
\[ h = \sqrt{\frac{4e}{M}} \]
\[ h = x\sqrt{\epsilon} \]
library(pracma)
eps(1)
[1] 2.220446e-16
eps(2)
[1] 4.440892e-16
2*(eps(1))
[1] 4.440892e-16
c(eps(0.1),0.1*(eps(1)))
[1] 1.387779e-17 2.220446e-17
c(eps(0.8),0.8*sqrt((eps(1))))
[1] 1.110223e-16 1.192093e-08
\[ \begin{array}{cllr} f'(x) & = \frac{f(x+h) - f(x)}{h} &\leftrightarrow h^* = x\sqrt{\epsilon} \\ f'(x) & = \frac{f(x+h) - f(x-h)}{2h} & \leftrightarrow h^* = x\sqrt[3]{\epsilon} \end{array} \]
\[ \begin{aligned} f(x+h) & = f(x) + f'(x)h + \frac{f''(x)}{2}h^2 + \ldots \\ f(x-h) & = f(x) - f'(x)h + \frac{f''(x)}{2}h^2 + \ldots \\ \\ f(x+h) + f(x-h) & = 2f(x) + f''(x)h^2 + \ldots \\ \\ f''(x) & = \frac{f(x+h) - 2f(x) + f(x-h)}{h^2} - \frac{h^2}{12}f^{(4)}(c) \end{aligned} \]