I used 3 midpoint formula is there any better method?
-
2Strictly speaking this makes no sense, there is no most accurate method completely agnostic of the function in question. – Ian May 16 '20 at 17:17
2 Answers
I'll make this moderately general.
We want to approximate $f'(x)$ given $f(x+mh)$ for $u \le m \le v$ where $u \lt v$ and $u$ and $v$ can be any integers.
I use the standard Taylor series approximation $f(x+h) \approx\sum_{k=0} \dfrac{f^{(k)}(x)h^k}{k!} $ where the sum is taken as far as wanted.
Then we want to determine the $a_m$ such that $f'(x)$ is best approximated by $\sum_{m=u}^v a_mf(x+mh) $ as functions of $u$ and $v$.
$\begin{array}\\ f'(x) &\approx \sum_{m=u}^v a_mf(x+mh)\\ &\approx \sum_{m=u}^v a_m\sum_{k=0} \dfrac{f^{(k)}(x)(mh)^k}{k!}\\ &\approx \sum_{m=u}^v a_m\sum_{k=0} \dfrac{f^{(k)}(x)m^kh^k}{k!}\\ &= \sum_{k=0}\dfrac{f^{(k)}(x)h^k}{k!}\sum_{m=u}^v a_m m^k\\ &= \sum_{k=0}\dfrac{f^{(k)}(x)h^k}{k!}s(k, u, v) \qquad\text{where } s(k, u, v) = \sum_{m=u}^v a_m m^k\\ \end{array} $
To make this sum as close to $f'(x)$ as possible, we want $s(0, u, v) = 0$, $hs(1, u, v) = 1$, and $h^ks(k, u, v) = 0$ for as many $k > 1$ as we can. This last equation is, of course, the same as $s(k, u, v) = 0$.
Since there are $v-u+1$ unknowns (the $a_m$), and there are always the first two equations ($k = 0, 1$), that leaves $v-u-1$ equations so $k$ goes from $2$ to $v-u$.
So the equations we want to solve for the $a_m$ are
$\begin{array}\\ 0 &= \sum_{m=u}^v a_m\\ \dfrac1{h} &= \sum_{m=u}^v a_m m\\ 0 &= \sum_{m=u}^v a_m m^k \qquad\text{for } k=2 \text{ to } v-u\\ \end{array} $
Examples.
If $v=u+1$, then
$\begin{array}\\ 0 &= a_u+a_{u+1}\\ \dfrac1{h} &= ua_u+(u+1)a_{u+1}\\ \end{array} $
so $\dfrac1{h} = a_{u+1}, -\dfrac1{h} = a_u $ and the approximation is $f'(x) \approx \dfrac{f(x+(u+1)h)-f(x+uh)}{h} $.
If $v = u+2$ then
$\begin{array}\\ 0 &= a_u+a_{u+1}+a_{u+2}\\ \dfrac1{h} &= ua_u+(u+1)a_{u+1}+(u+2)a_{u+2}\\ 0 &= u^2a_u+(u+1)^2a_{u+1}+(u+2)^2a_{u+2}\\ \end{array} $
Subtracting the first from the last two,
$\begin{array}\\ \dfrac1{h} &= a_{u+1}+2a_{u+2}\\ 0 &= (2u+1)a_{u+1}+(4u+4)a_{u+2}\\ \end{array} $
Again,
$\begin{array}\\ -\dfrac{2u+1}{h} &= (4u+4-(2(2n+1)))a_{u+2}\\ &= 2a_{u+2}\\ \end{array} $
so $a_{u+2} = -\dfrac{2u+1}{2h} $, $a_{u+1} = \dfrac1{h}-2a_{u+2} = \dfrac1{h}+\dfrac{2u+1}{h} =\dfrac{2u+2}{h} $, $a_u =-a_{u+1}-a_{u+2} =-\dfrac{2u+2}{h}+\dfrac{2u+1}{2h} =-\dfrac{2u+3}{2h} $, so $f'(x) \approx \dfrac{-(2u+1)f(x+(u+2)h)+(4u+4)f(x+(u+1)h)-(2u+3)f(x+uh)}{2h} $.
If $u = -1$, then $a_{-1} = -\dfrac1{2h} $, $a_0 = 0 $, and $a_1 = \dfrac1{2h} $ so $f'(x) \approx \dfrac{f(x+h)-f(x-h)}{2h} $.
Setting $h=1$ will simplify things, and there is probably a general form for the $a_m$, but I'll stop here.
The same thing can be done for nonconsecutive values of $m$, but that is left as an exercise.
- 107,799
You have step sizes that are multiples of $h=0.1$. The function values will be given with a certain number of digits after the dot, so have an error bounded by some $\mu=10^{-d-1}$.
If you take a difference formula of order 2 for the first derivative, then the total error will have components of size $\frac{\mu}{h}$ from the truncation error propagation and $h^2$ from the difference formula. The total error is approximately the minimum of these two terms, so that the best result for minimal effort is found where these terms balance. That happens when $\mu\sim h^3\implies d=2$. If you increase the order to 3 then $\frac\mu{h}$ and $h^3$ balance for $d=3$ etc.
For the second derivative you similarly get an error of $\frac\mu{h^2}$ for the propagated truncation error and $h^p$ for the method, balancing for $p+2=d+1$.
In summary this means that the choice of the difference formula depends also on the quality of the input values.
- 126,666
