7.1: Derivatives
- Page ID
- 34842
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Suppose we have discretized a function of one variable, obtaining a set of \(\psi_n \equiv \psi(x_n)\) as described above. For simplicity, we assume that the discretization points are evenly-spaced and arranged in increasing order (this is the simplest and most common discretization scheme). The spacing between points is defined as
\[h \equiv x_{n+1} - x_n.\]
Let us discuss how the first and higher-order derivatives of \(\psi(x)\) can be represented under discretization.
7.1.1 First Derivative
The most straightforward representation of the first derivative is the forward-difference formula:
\[\psi'(x_n) \approx \frac{\psi_{n+1} - \psi_n}{h}\]
This is inspired by the usual definition of the derivative of a function, and approaches the true derivative as \(h \rightarrow 0\). However, it is not a very good approximation. To see why, let's analyze the error in the formula, which is defined as the absolute value of the difference between the formula and the exact value of the derivative:
\[\mathcal{E} = \left|\psi'(x_n) - \frac{\psi_{n+1} - \psi_n}{h}\right|\]
We can expand \(\psi_{n+1}\) in a Taylor series around \(x_{n}\):
\[\psi_{n+1} = \psi_n + h\, \psi'(x_n) + \frac{h^2}{2}\psi''(x_n) + \frac{h^3}{6}\psi'''(x_n) + O(h^4) \]
Plugging this into the error formula, we find that the error decreases linearly with the spacing:
\[\mathcal{E} = \left| \frac{h}{2}\psi''(x_n) + O(h^2)\right| \sim O(h).\]
There is a better alternative, called the mid-point formula. This approximates the first derivative by sampling the points to the left and right of the desired position:
\[\psi'(x_n) \approx \frac{\psi_{n+1} - \psi_{n-1}}{2h}.\]
To see why this is better, let us write down the Taylor series for \(\psi_{n\pm1}\):
\[\psi_{n+1} = \psi_n \,+\, h\, \psi'(x_n) \,+\, \frac{h^2}{2}\psi''(x_n) \,+\, \frac{h^3}{6}\psi'''(x_n) \,+\, \frac{h^4}{24}\psi''''(x_n) + O(h^5) \]
\[\psi_{n-1} = \psi_n \,-\, h\, \psi'(x_n) \,+\, \frac{h^2}{2}\psi''(x_n) \,-\, \frac{h^3}{6}\psi'''(x_n) \,+\, \frac{h^4}{24}\psi''''(x_n) \,+\, O(h^5) \]
Note that the two series have the same terms involving even powers of \(h\), whereas the terms involving odd powers of \(h\) have opposite signs. Hence, if we subtract the second series from the first, the result is
\[\psi_{n+1} - \psi_{n-1} = 2 h\, \psi'(x_n) + 2 \frac{h^3}{6}\psi'''(x_n) + O(h^5) \]
Because the \(O(h^{2})\) terms are equal in the two series, they cancel out under subtraction, and only the \(O(h^{3})\) and higher terms survive. After re-arranging the above equation, we get
\[\psi'(x_n) = \frac{\psi_{n+1} - \psi_{n-1}}{2 h} + O(h^2).\]
Hence, the error of the mid-point formula scales as \(O(h^2)\), which is a good improvement over the \(O(h)\) error of the forward-difference formula. What's especially nice is that the mid-point formula requires the same number of arithmetic operations to calculate as the forward-difference formula, so this is a free lunch!
It is possible to come up with better approximation formulas for the first derivative by including terms involving \(\psi_{n\pm 2}\) etc., with the goal of canceling the \(O(h^{3})\) or higher terms in the Taylor series. For most practical purposes, however, the mid-point rule is sufficient.
7.1.2 Second Derivative
The discretization of the second derivative is easy to figure out too. We again write down the Taylor series for \(\psi_{n\pm1}\):
\[\psi_{n+1} = \psi_n \,+\, h\, \psi'(x_n) \,+\, \frac{h^2}{2}\psi''(x_n) \,+\, \frac{h^3}{6}\psi'''(x_n) \,+\, \frac{h^4}{24}\psi''''(x_n) \,+\, O(h^5) \]
\[\psi_{n-1} = \psi_n \,-\, h\, \psi'(x_n) \,+\, \frac{h^2}{2}\psi''(x_n) \,-\, \frac{h^3}{6}\psi'''(x_n) \,+\, \frac{h^4}{24}\psi''''(x_n) \,+\, O(h^5) \]
When we add the two series together, the terms involving odd powers of \(h\) cancel, and the result is
\[\psi_{n+1} + \psi_{n-1} = 2\psi_n + h^2 \psi''(x_n) + \frac{h^4}{12}\psi''''(x_n) + O(h^5).\]
A minor rearrangement of the equation then gives
\[\psi''(x_n) \approx \frac{\psi_{n+1} - 2\psi_n + \psi_{n-1}}{h^2} + O(h^2).\]
This is called the three-point rule for the second derivative, because it involves the value of the function at the three points \(x_{n+1}\), \(x_{n}\), and \(x_{n-1}\).