• https://me.yahoo.com
COST (GBP)
0.75
0.00
0

taylor

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

C++

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

Authors

Lucian Bentea (September 2006)

Taylor1

 doubletaylor1( double (*f)(double)[function pointer] double x double h double gamma = 1.0 )[inline]
Consider a function $\inline&space;f:[a,b]&space;\rightarrow&space;\mathbb{R}$ that is three times differentiable on its domain, and three abscissas $\inline&space;x_1,&space;x_*,&space;x_2&space;\in&space;[a,b]$ such that
$x_1&space;=&space;x_*&space;-&space;h,&space;\qquad&space;x_2&space;=&space;x_*&space;+&space;\gamma&space;h$
where $\inline&space;h,&space;\gamma$ are real positive constants. Then the approximate value of the first derivative $\inline&space;\displaystyle&space;\frac{df}{dx}$ at the abscissa $\inline&space;x_*$ is given by:

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

or

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

In the case $\inline&space;\gamma&space;=&space;1$ we have an error bound of:
$\epsilon&space;\leq&space;\frac{h^2}{6}&space;\sup_{a
while in the case when $\inline&space;\gamma&space;\neq&space;1$ the error is given by:
$\epsilon&space;=&space;(1&space;-&space;\gamma)&space;\frac{h}{2}\frac{d^2f}{dx^2}(x_*)&space;-&space;\frac{h^2}{3!(\gamma&space;+&space;1)}&space;&space;\left[\gamma^3&space;\frac{d^3f}{dx^3}(\xi_2)&space;+&space;\frac{d^3f}{dx^3}(\xi_1)\right]$
where $\inline&space;\xi_1&space;\in&space;(x_1,&space;x_*)$ and $\inline&space;\xi_2&space;\in&space;(x_*,&space;x_2)$. The error estimate for $\inline&space;\gamma&space;\neq&space;1$ comes from the Taylor formula applied to the given function $\inline&space;f$ at point $\inline&space;x_*$.

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

Example 1

Below we give an example of how to compute the first numerical derivative of $\inline&space;\cos&space;x$ at point $\inline&space;x&space;=&space;1$, considering $\inline&space;\gamma&space;=&space;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.
Source Code

Source code is available when you buy a Commercial licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.

Taylor2

 doubletaylor2( double (*f)(double)[function pointer] double x double h double gamma = 1.0 )[inline]
Consider a function $\inline&space;f:[a,b]&space;\rightarrow&space;\mathbb{R}$ that is four times differentiable on its domain, and three abscissas $\inline&space;x_1,&space;x_*,&space;x_2&space;\in&space;[a,b]$ such that
$x_1&space;=&space;x_*&space;-&space;h,&space;\qquad&space;x_2&space;=&space;x_*&space;+&space;\gamma&space;h$
where $\inline&space;h,&space;\gamma$ are real positive constants. Then the approximate value of the second derivative $\inline&space;\displaystyle&space;\frac{d^2f}{dx^2}$ at the abscissa $\inline&space;x_*$ is given by:

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

or

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

In the case $\inline&space;\gamma&space;=&space;1$ we have an error bound of:
$\epsilon&space;\leq&space;\frac{h^2}{12}&space;\sup_{a
while in the case when $\inline&space;\gamma&space;\neq&space;1$ the error is given by:
$\epsilon&space;=&space;\frac{h}{3(\gamma&space;+&space;1)}&space;&space;\left[\gamma^2&space;\frac{d^3f}{dx^3}(\xi_2)&space;-&space;\frac{d^3f}{dx^3}(\xi_1)\right]$
where $\inline&space;\xi_1&space;\in&space;(x_1,&space;x_*)$ and $\inline&space;\xi_2&space;\in&space;(x_*,&space;x_2)$. The error estimate for $\inline&space;\gamma&space;\neq&space;1$ comes from the Taylor formula applied to the given function $\inline&space;f$ at point $\inline&space;x_*$.

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

Example 2

Below we give an example of how to compute the second numerical derivative of $\inline&space;\cos&space;x$ at $\inline&space;x&space;=&space;1$, considering $\inline&space;\gamma&space;=&space;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.
Source Code

Source code is available when you buy a Commercial licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.