\[ 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} \]
Knock knock.
Who's there?
Dozen.
Dozen who?
Dozen anyone wanna let me in?
It's inappropriate to make a dad joke if you are not a dad.
It's a faux pa.
\[ f'(x) \cong \frac{f(x+h)-f(x)}{h} \]
\[ \small{ \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} \]
findiff <- 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
symdiff <- 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
\[ \small{ \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
Rather than trial & error with h values to guess what is best, we will use calculus.
\[ \small{ \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} } \]
\[ \small{ f(x+h) = f(x) + f'(x)\left((x+h)-x)\right) + \frac{f''(x)}{2}\left((x+h)-x)\right)^2 + \ldots } \]
\[ \small{ \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''(c(x))}{2}h, \,\, \,\, c(x) \in [x,x+h] \\ \end{aligned} } \]
Taylor series expansion of \( f(x) \) about \( x \): \[ \small{ \begin{aligned} f(x+h) & = f(x) + f'(x)\left((x+h)-x)\right) + \frac{f''(x)}{2}\left((x+h)-x)\right)^2 + \ldots \\ &= f(x) + f'(x)h + \frac{f''(x)}{2}h^2 + \frac{f'''(x)}{6}h^3 + \ldots \\ f(x-h) & = f(x) - f'(x)h + \frac{f''(x)}{2}h^2 - \frac{f'''(x)}{6}h^3 + \ldots \\ \end{aligned} } \]
Then \[ \small{ \begin{aligned} f'(x) & = \frac{f(x+h) - f(x-h)}{2h} - \frac{f'''(x)}{6}h^3 + \ldots \\ f'(x) & = \frac{f(x+h) - f(x-h)}{2h} - \frac{h^2}{6}f'''(c(x)), \,\, \,\, c(x) \in [x-h,x+h] \\ \end{aligned} } \]
\[ \small{ \begin{aligned} f'(x) & = \frac{f(x+h) - f(x)}{h} - \frac{h}{2}f''(c(x)), \,\,\, \, c(x) \in [x,x+h]\\ \\ f'(x) & = \frac{f(x+h) - f(x-h)}{2h} - \frac{h^2}{6}f'''(c(x)), \, \,\,\, c(x) \in [x-h,x+h] \end{aligned} } \]
\[ \begin{aligned} E_T & = \frac{h}{2}f''(c(x)), \,\,\,\, c(x) \in [x,x+h] \\ E_T & = \frac{h^2}{6}f'''(c(x)), \,\,\,\, c(x) \in [x-h,x+h] \end{aligned} \]
\[ \begin{aligned} |E_T| & \leq \frac{h}{2}M, \,\, M = \max_{u \in [x,x+h]}|f''(u)|, \\ |E_T| & \leq \frac{h^2}{6}M, \,\, M = \max_{u \in [x-h,x+h]}|f'''(u)| \end{aligned} \]
\[ \small{ \begin{aligned} f'(x) & = \frac{f(x+h) - f(x)}{h} - \frac{h}{2}f''(c(x)), \\ f'(x) & = \frac{f(x+h) - f(x-h)}{2h} - \frac{h^2}{6}f'''(c(x)) \end{aligned} } \]
\[ \small{ f'(x) = \frac{f(x+h) - f(x)}{h} - \frac{h}{2}f''(c), \,\, c(x) \in [x,x+h] } \]
\[ \small{ E_T = \frac{h}{2}f''(c(x)) } \]
\[ \begin{aligned} \tilde{f}(x+h) &= f(x+h) + e_1(x) \\ \tilde{f}(x) &= f(x) + e_2(x) \end{aligned} \]
\[ \small{ \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(x)) \]
\[ 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| \leq 2 \max \left\{ |e_1|, |e_2| \right\} = 2e \]
\[ M = \max_{x \leq u \leq x+h} |f''(u)| \]
\[ \small{ E = \frac{e_1 - e_2}{h} + \frac{h}{2}f''(c) } \]
\[ \small{ E(h) \leq \frac{2e}{h} + \frac{M}{2}h } \]
\[ \small{ E(h) = \frac{2e}{h} + \frac{M}{2}h} \]
\[ \small{ E(h) = \frac{2e}{h} + \frac{M}{2}h } \]
\[ \small{ \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} } \]
\[ \small{ E(h) = \frac{2e}{h} + \frac{M}{2}h } \]
\[ \small{ h = \sqrt{\frac{4e}{M}} } \]
\[ \small{ h^* = x\sqrt{\epsilon} } \]
\[ \small{ h = \sqrt{\frac{4e}{M}} , \,\, h^* = x\sqrt{\epsilon} } \]
library(pracma)
c(eps(1), eps(8))
[1] 2.220446e-16 1.776357e-15
\[ \small{ \begin{aligned} M &= \max |f''(u) | \\ e_1 &= \tilde{f}(x+h) - f(x+h) \\ e_2 &= \tilde{f}(x) - f(x) \end{aligned} } \]
\[ \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} \]
\[ \small{ \begin{aligned} f(x+h) = f(x) + f'(x)h & + \frac{f''(x)}{2}h^2 + \frac{f'''(x)}{6}h^3 + \frac{f^{(4)}(x)}{24}h^4 + \ldots \\ f(x-h) = f(x) - f'(x)h & + \frac{f''(x)}{2}h^2 - \frac{f'''(x)}{6}h^3 + \frac{f^{(4)}(x)}{24}h^4 + \ldots \end{aligned} } \]
\[ \small{ \begin{aligned} f(x+h) + f(x-h) & = 2f(x) + f''(x)h^2 + \frac{f^{(4)}(x)}{12}h^4 + \ldots \\ \\ f''(x) & = \frac{f(x+h) - 2f(x) + f(x-h)}{h^2} - \frac{h^2}{12}f^{(4)}(c(x)) \end{aligned} } \]