Course Webpage

Suppose we know the values of a function $f$ at three points $$\left(x_{0},y_{0}\right)\qquad \left(x_{1},y_{1}\right)\qquad \left(x_{2},y_{2}\right)$$ with $$x_{1}=x_{0}+h\qquad x_{2}=x_{0}+2h\qquad 0\lt h \lt 1$$

  1. Define second order formulas approximating $f'(x_{0})$, $f'(x_{1})$, $f'(x_{2})$ using these three points.
  2. Define a second order formula approximating $f''(x_{1})$ using these three points.
  3. Use these formulas to approximate the first and second derivative of $f(x)=\ln x$ in $1.5$ with $h = 0.1$.

Define second order formulas approximating $f'(x_{0})$, $f'(x_{1})$, $f'(x_{2})$

With numbers

Let's first look at an example with numbers. Then we will generalize to any three nodes.

$$ \begin{array}{|c|ccc|} \hline k & 0 & 1 & 2\\ \hline x_{k} & 1.4 & 1.5 & 1.6\\ \hline y_{k}=\ln{x_{k}} & 0.3365 & 0.4055 & 0.4700\\ \hline \end{array} $$

The three points for the example will be

$$(x_0,y_0)=(1.4,0.3365)\quad(x_1,y_1)=(1.5,0.4055) \quad (x_2,y_2)=(1.6,0.4700)$$
$$h = x_1-x_0=x_2-x_1 = 0.1$$

Let's calculate the parabola that passes through these three points using the interpolating polynomial in Newton's form

$$ P_2(x)=[y_{0}]+[y_{0},y_{1}](x-x_{0})+[y_{0},y_{1},y_{2}](x-x_{0})(x-x_{1}) $$

We will calculate the coefficients using the divided differences table

$$ \begin{array}{cccc} x_0 & y_0 & & \\ & & [y_0,y_1]=\dfrac{y_1-y_0}{x_1-x_0} & \\ x_1 & y_1 & &[y_0,y_1,y_2]=\dfrac{[y_1,y_2]-[y_0,y_1]}{x_2-x_0}\\ & & [y_1,y_2]=\dfrac{y_2-y_1}{x_2-x_1} & \\ x_2 & y_2 & & \\ \end{array} $$

If we substitute $x_0,$ $x_1,$ $x_2,$ $y_0,$ $y_1$ and $y_2$ with their values

$$ \begin{array}{cccc} 1.4 & \fbox{$0.3365$}^{\;\large{c_0}} & & \\ & & \dfrac{0.4055-0.3365}{0.1}=\fbox{$0.6900$}^{\;\large{c_1}} & \\ 1.5 & 0.4055 & &\dfrac{0.6450-0.6900}{0.2}=\fbox{$-0.2250$}^{\;\large{c_2}} \\ & & \dfrac{0.4700-0.4055}{0.1}=0.6450 & \\ 1.6 & 0.4700 & & \\ \end{array} $$

And the interpolation polynomial is

$$ P_2(x)=c_0+c_1(x-x_{0})+c_2(x-x_{0})(x-x_{1}) $$

and using the $c_i$ values from the table

$$ P_2(x)=0.3365+0.6900(x-1.4)-0.2250(x-1.4)(x-1.5) $$

And this is the interpolation polynomial that passes through the three points

Now we calculate the derivative of $P_2$

$$ P'_2(x)=0.6900-0.2250[(x-1.4)+(x-1.5)] $$

and its second derivative

$$ P''_2(x)=-0.2250[2]= -0.4500 $$

And now we can use it to estimate the derivatives at the nodes

  • If we calculate the derivative at $x_0=1.4$ we are using a forward formula
$$ f'(1.4)\approx P'_2(1.4)=0.6900-0.2250[(1.4-1.4)+(1.4-1.5)] = 0.7125 $$
  • If we calculate the derivative at $x_1=1.5$ we are using a centered formula
$$ f'(1.5)\approx P'_2(1.5)=0.6900-0.2250[(1.5-1.4)+(1.5-1.5)] = 0.6675 $$
  • If we calculate the derivative at $x_2=1.6$ we are using a backward formula
$$ f'(1.6)\approx P'_2(1.6)=0.6900-0.2250[(1.6-1.4)+(1.6-1.5)] = 0.6225 $$

Differentiating again, we will have an approximation for the second derivative

$$ P''_2(x)=-0.2250(2)=-0.4500 $$
  • We will use it for the midpoint $x_1=1.5$ and it will be a centered formula
$$ f''(1.5)\approx P''_2(1.5)= -0.4500 $$

Let's compare the approximations with the exact values. Taking into account that

$$ f'(x) = \frac{1}{x} \quad f''(x) = -\frac{1}{x^2} $$
$$\begin{array}{|l|cccc|} \hline & f'(1.4) & f'(1.5) & f'(1.6) & f''(1.5)\\ \hline \mathrm{exact} & 0.7142 & 0.6667 & 0.6250 & -0.4444\\ \mathrm{approximate} &0.7125 & 0.6675 & 0.6225 & -0.4500 \\ \hline \mathrm{error} & 0.0017 & 0.0008 & 0.0025 & 0.0056\\ \hline \end{array}$$

Although the smallest error is returned by the centered formula, all errors are of the same order of magnitude since all of them are order two formulas.

With variables

Now we generalize to any three nodes.

If we use a $2^{o}$ degree interpolation polynomial at $x_{0}$, $x_{1}$, $x_{2}$, where $y_{j}=f(x_{j})$, the polynomial interpolation in Newton's form is:

$$ p(x)=[y_{0}]+[y_{0},y_{1}](x-x_{0})+[y_{0},y_{1},y_{2}](x-x_{0})(x-x_{1}) $$

We will calculate the coefficients using the divided differences table

$$ \begin{array}{cccc} x_0 & \fbox{$y_0$}^{\;\large{c_0}} & & \\ & & \fbox{$[y_0,y_1]=\dfrac{y_1-y_0}{x_1-x_0}$}^{\;\large{c_1}} & \\ x_1 & y_1 & &\fbox{$[y_0,y_1,y_2]=\dfrac{[y_1,y_2]-[y_0,y_1]}{x_2-x_0}$}^{\;\large{c_2}}\\ & & [y_1,y_2]=\dfrac{y_2-y_1}{x_2-x_1} & \\ x_2 & y_2 & & \\ \end{array} $$

con

  • The first coefficient of the polynomial is $$ c_0=y_{0} $$
  • The second coefficient of the polynomial $$ c_1=\frac{y_{1}-y_{0}}{x_{1}-x_{0}}=\frac{y_{1}-y_{0}}{h}, $$
  • And the third coefficient $$ c_2=\frac{[y_{1},y_{2}]-[y_{0},y_{1}]}{x_{2}-x_{0}}=\frac{1}{2h}\big(\frac{y_{2}-y_{1}}{h}-\frac{y_{1}-y_{0}}{h}\big)=\frac{1}{2h^{2}}\big(y_{0}-2y_{1}+y_{2}\big). $$

Summing up

$$ c_0=y_{0},\quad c_1=\frac{y_{1}-y_{0}}{h},\quad c_2=\frac{1}{2h^{2}}(y_{0}-2y_{1}+y_{2}). $$

As

$$ p(x)=c_0+c_1(x-x_{0})+c_2(x-x_{0})(x-x_{1}) $$

If we differentiate $p(x)$

$$ f'(x)\approx p'(x)=c_1+c_2((x-x_{0})+(x-x_{1})) $$

Forward formula

It is the formula for the node $x_{0}$

$$ f'(x_{0})\approx p'(x_{0})=c_1+c_2((x_{0}-x_{0})+(x_{0}-x_{1})), $$

Thus

$$ f'(x_{0})\approx\frac{y_{1}-y_{0}}{h}+\frac{1}{2h^{2}}\big(y_{0}-2y_{1}+y_{2}\big)(-h), $$

And finally

$$ \fbox{$f'(x_{0})\approx\dfrac{-3y_{0}+4y_{1}-y_{2}}{2h}$} $$

Centered formula

It is the formula for the node $x_{1}$

$$ f'(x_{1})\approx p'(x_{1})=c_1+c_2((x_{1}-x_{0})+(x_{1}-x_{1})), $$

Thus

$$ f'(x_{1})\approx\frac{y_{1}-y_{0}}{h}+\frac{1}{2h^{2}}\big(y_{0}-2y_{1}+y_{2}\big)h, $$

And finally

$$ \fbox{$f'(x_{1})\approx\frac{-y_{0}+y_{2}}{2h}$} $$

Backward formula

It is the formula for the node $x_{2}$

$$ f'(x_{2})\approx p'(x_{2})=c_1+c_2((x_{2}-x_{0})+(x_{2}-x_{1})), $$

Thus

$$ f'(x_{2})\approx\frac{y_{1}-y_{0}}{h}+\frac{1}{2h^{2}}\big(y_{0}-2y_{1}+y_{2}\big)(2h+h), $$

And finally

$$ \fbox{$f'(x_{2})\approx\frac{y_{0}-4y_{1}+3y_{2}}{2h}$} $$

Define a second order formula approximating $f''(x_{1})$

If we differentiate $p'(x)$

$$ f'(x)\approx p'(x)=[y_{0},y_{1}]+[y_{0},y_{1},y_{2}]((x-x_{0})+(x-x_{1})), $$

also

$$ f'(x)\approx p'(x)=c_1+c_2((x-x_{0})+(x-x_{1})), $$

We have

$$ f''(x)\approx p''(x)=2c_2, $$

Thus

$$ f''(x_1)\approx p''(x_1)=2c_2=\frac{1}{h^{2}}\big(y_{0}-2y_{1}+y_{2}\big), $$

that is

$$ \fbox{$f''(x_1)\approx \frac{1}{h^{2}}\big(y_{0}-2y_{1}+y_{2}\big)$} $$

Or, if we call $x_1=a$ then $x_0 = a-h$ and $x_2 = a+h$ and another form of this formula is $f''(a)\approx p''(a)$

$$ \fbox{$f''(a)\approx \frac{f(a-h)-2f(a)+f(a+h)}{h^{2}}$} $$

Use these formulas to approximate the first and second derivative of $f(x)=\ln x$ in $1.5$ with $h = 0.1$

  • The forward formula is $$ f'(x_{0})\approx\frac{-3y_{0}+4y_{1}-y_{2}}{2h}. $$ If the node is $x_0 = 1.5$ then $x_1= 1.6$ and $x_2 = 1.7$ and we need the values $$ \begin{array}{|c|ccc|} \hline k & 0 & 1 & 2\\ \hline x_{k} & 1.5 & 1.6 & 1.7\\ \hline y_{k}=\ln{x_{k}} & 0.4055 & 0.4700 & 0.5306\\ \hline \end{array} $$ and using these values in the formula $$ f'(1.5)\approx \frac{-3(0.4055)+4(0.4700)-(0.5306)}{2(0.1)}=0.6645 $$

  • The centered formula is $$ f'(x_{1})\approx\frac{-y_{0}+y_{2}}{2h}. $$ If the node is $x_1 = 1.5$ then $x_0= 1.4$ and $x_2 = 1.6$ and we need the values $$ \begin{array}{|c|ccc|} \hline k & 0 & 1 & 2\\ \hline x_{k} & 1.4 & 1.5 & 1.6\\ \hline y_{k}=\ln{x_{k}} & 0.3365 & 0.4055 & 0.4700\\ \hline \end{array} $$ and using these values in the formula $$ f'(1.5)\approx\frac{-0.3365+0.4700}{2(0.1)}= 0.6675 $$

  • The backward formula is $$ f'(x_{2})\approx\frac{y_{0}-4y_{1}+3y_{2}}{2h}. $$ If the node is $x_2 = 1.5$ then $x_1= 1.4$ and $x_0 = 1.3$ and we need the values $$ \begin{array}{|c|ccc|} \hline k & 0 & 1 & 2\\ \hline x_{k} & 1.3 & 1.4 & 1.5\\ \hline y_{k}=\ln{x_{k}} & 0.2624 & 0.3365 & 0.4055\\ \hline \end{array} $$ and using these values in the formula $$ f'(1.5)\approx\frac{0.2624-4(0.3365)+3(0.4055)}{2(0.1)}=0.6645 $$ And the exact value of the first derivative is $$f(x)=\ln x \quad f'(x) = \frac{1}{x} \quad f'(1.5) = \frac{1}{1.5}=0.6667$$

  • We built a centered formula for the second derivative approximation. Thus, if the point is $x_1 = 1.5$ then $x_0= 1.4$ and $x_2 = 1.6$ and we need the values $$ \begin{array}{|c|ccc|} \hline k & 0 & 1 & 2\\ \hline x_{k} & 1.4 & 1.5 & 1.6\\ \hline y_{k}=\ln{x_{k}} & 0.3365 & 0.4055 & 0.4700\\ \hline \end{array} $$ And the approximation is $$ f''(x_1)\approx \frac{1}{h^{2}}\big(y_{0}-2y_{1}+y_{2}\big)=\frac{1}{0.1^{2}}\big(0.3365-2(0.4055)+0.4700\big)=-0.4500 $$ And the exact value of the second derivative is $$f(x)=\ln x \quad f'(x) = \frac{1}{x} \quad f''(x) = -\frac{1}{x^2}\quad f''(1.5) = -\frac{1}{1.5^2}=-0.4444$$

Let's compare the approximate results with the exact ones

$$\begin{array}{|l|ccc|c|} \hline & & f'(1.5) & & f''(1.5)\\ & \mathrm{forward} &\mathrm{centered}& \mathrm{backward}& \mathrm{centered}\\ \hline \mathrm{approximate} & 0.6645 & 0.6675 & 0.6644 & -0.4500\\ \mathrm{exact} & & 0.6667 & & -0.4444 \\ \hline \mathrm{error} & 0.0022 & 0.0008 & 0.0023 & 0.0056 \\ \hline \end{array}$$

Although the smallest error is given again by the centered formula, all errors are of the same order of magnitude since all of them are order two formulas.