Computes the first and second derivatives of a function using the Taylor formula.

You're viewing an older version of this page (#420). View the current version.

View versions (1)

Interface

Overview

This module computes the first or second numerical derivatives of a function at a particular point using the Taylor formula. The advantage is that the interval used in computing the numerical derivative need not be symmetrical around the given point.

References

Mihai Postolache - "Metode Numerice", Editura Sirius

GPL Licence — free for non commercial use. See Licence details.

FUNCTION

taylor1

Consider a function f:[a,b] \rightarrow \mathbb{R} that is three times differentiable on its domain, and three abscissas x_1, x_*, x_2 \in [a,b] such that

x_1 = x_* - h, \qquad x_2 = x_* + \gamma h
(1)

where h, \gamma are real positive constants. Then the approximate value of the first derivative \displaystyle \frac{df}{dx} at the abscissa x_* is given by:

\frac{df}{dx}(x_*) \approx \frac{f(x_2) - f(x_1)}{2h} \qquad \mbox{if } \gamma = 1
(2)

or

\frac{df}{dx}(x_*) \approx \frac{f(x_2) - f(x_1)}{(\gamma + 1)h} \qquad \mbox{if } \gamma \neq 1.
(3)

In the case \gamma = 1 we have an error bound of:

\epsilon \leq \frac{h^2}{6} \sup_{a<x<b} \left|\frac{d^3f}{dx^3}\right|
(4)

while in the case when \gamma \neq 1 the error is given by:

\epsilon = (1 - \gamma) \frac{h}{2}\frac{d^2f}{dx^2}(x_*) - \frac{h^2}{3!(\gamma + 1)} 
\left[\gamma^3 \frac{d^3f}{dx^3}(\xi_2) + \frac{d^3f}{dx^3}(\xi_1)\right]
(5)

where \xi_1 \in (x_1, x_*) and \xi_2 \in (x_*, x_2). The error estimate for \gamma \neq 1 comes from the Taylor formula applied to the given function f at point x_*.

On account of the above relations you may notice that the error approaches zero as h approaches zero for \gamma \neq 1, and as h^2 approaches zero for \gamma = 1.

Example 1

Below we give an example of how to compute the first numerical derivative of \cos x at point x = 1, considering \gamma = 1 (equally spaced abscissas). The absolute error from the actual value of the derivative at that point is also displayed.

#include <codecogs/maths/calculus/diff/taylor.h>
#include <stdio.h>
#include <math.h>

// precision constant
#define H 0.001

// function to differentiate
double f(double x)
{
  return cos(x);
}

// the derivative of the function, to estimate errors
double df(double x)
{
  return -sin(x);
}

int main()
{
  // display precision
  printf("         h = %.3lf\n\n", H);

  // compute the numerical derivative
  double fh = Maths::Calculus::Diff::taylor1(f, 1, H);

  // display the result and error estimate
  printf("      f(x) = cos(x)\n");
  printf("     f`(1) = %.15lf\n", fh);
  printf("real value = %.15lf\n", df(1));
  printf("     error = %.15lf\n\n", fabs(fh - df(1)));

  return 0;
}

Output

h = 0.001

      f(x) = cos(x)
     f`(1) = -0.841470844562695
real value = -0.841470984807897
     error = 0.000000140245202

Parameters

f
the function to differentiate
x
the abscissa at which you want to compute the first derivative
h
the value of the precision constant h
gamma
Default value = 1.0

Returns

The first numerical derivative of the given function at the x abscissa.

FUNCTION

taylor2

Consider a function f:[a,b] \rightarrow \mathbb{R} that is four times differentiable on its domain, and three abscissas x_1, x_*, x_2 \in [a,b] such that

x_1 = x_* - h, \qquad x_2 = x_* + \gamma h
(6)

where h, \gamma are real positive constants. Then the approximate value of the second derivative \displaystyle \frac{d^2f}{dx^2} at the abscissa x_* is given by:

\frac{d^2f}{dx^2}(x_*) \approx \frac{f(x_1) - 2f(x_*) + f(x_2)}{h^2}
\qquad \mbox{if } \gamma = 1
(7)

or

\frac{d^2f}{dx^2}(x_*) \approx \frac{2}{h^2\gamma(\gamma + 1)} [\gamma f(x_1) - (1 + \gamma) f(x_*) + f(x_2)] \qquad \mbox{if } \gamma \neq 1.
(8)

In the case \gamma = 1 we have an error bound of:

\epsilon \leq \frac{h^2}{12} \sup_{a<x<b} \left|\frac{d^4f}{dx^4}(x)\right|
(9)

while in the case when \gamma \neq 1 the error is given by:

\epsilon = \frac{h}{3(\gamma + 1)} 
\left[\gamma^2 \frac{d^3f}{dx^3}(\xi_2) - \frac{d^3f}{dx^3}(\xi_1)\right]
(10)

where \xi_1 \in (x_1, x_*) and \xi_2 \in (x_*, x_2). The error estimate for \gamma \neq 1 comes from the Taylor formula applied to the given function f at point x_*.

On account of the above relations you may notice that the error approaches zero as h approaches zero for \gamma \neq 1, and as h^2 approaches zero for \gamma = 1.

Example 1

Below we give an example of how to compute the second numerical derivative of \cos x at x = 1, considering \gamma = 1 (equally spaced abscissas). The absolute error from the actual value of the derivative at that point is also displayed.

#include <codecogs/maths/calculus/diff/taylor.h>
#include <stdio.h>
#include <math.h>

// precision constant
#define H 0.0001

// function to differentiate
double f(double x)
{
  return cos(x);
}

// the second derivative of the function, to estimate errors
double d2f(double x)
{
  return -cos(x);
}

int main()
{
  // display precision
  printf("         h = %.4lf\n\n", H);

  // compute the numerical derivative
  double fh = Maths::Calculus::Diff::taylor2(f, 1, H);

  // display the result and error estimate
  printf("      f(x) = cos(x)\n");
  printf("    f``(1) = %.15lf\n", fh);
  printf("real value = %.15lf\n", d2f(1));
  printf("     error = %.15lf\n\n", fabs(fh - d2f(1)));

  return 0;
}

Output

h = 0.0001

      f(x) = cos(x)
    f``(1) = -0.540302319866019
real value = -0.540302305868140
     error = 0.000000013997879

Parameters

f
the function to differentiate
x
the abscissa at which you want to compute the second derivative
h
the value of the precision constant h
gamma
Default value = 1.0

Returns

The second numerical derivative of the given function at the x abscissa.