In-class Example: Second Derivative

In-class Example: Second Derivative#

Let’s construct a difference approximation to the second derivative.

We’ll use the same Taylor expansions as before, but now we’ll include more terms:

\[f(x\pm h) = f(x) \pm f^\prime(x) h + \frac{1}{2} f^{\prime\prime}(x) h^2 \pm \frac{1}{6} f^{\prime\prime\prime}(x) h^3 + \mathcal{O}(h^4)\]

Now, let’s eliminate the first derivative by combining these as:

\[f(x+h) + f(x-h) = 2 f(x) + f^{\prime\prime}(x) h^2 + \mathcal{O}(h^4)\]

Notice that the third-derivative term also cancels.

We can now solve for the second derivative:

\[f^{\prime\prime}(x) = \frac{f(x+h) - 2 f(x) + f(x-h)}{h^2} + \mathcal{O}(h^2)\]

We expect this to be second-order accurate.

Let’s implement this now and see how it convergence on \(f(x) = \sin(x)\)