taylor_table
Computes the first and second derivatives of a function at multiple points.
You're viewing an older version of this page (#430). View the current version.
Interface
#include <codecogs/maths/calculus/differential/taylor_table.h>
using namespace Maths::Calculus::Differential;
Overview
This module computes either the first or the second numerical derivatives of a function at multiple points and returns a vector with each resulting value. In order to achieve this it uses the component taylor. The advantage is that the interval used in computing the numerical derivative need not be symmetrical around either one of the given points. Basically three abscissas are chosen such that
where ,
are real positive constants corresponding to the precision and the symmetry of the interval of differentiation. The derivative is thus approximated at point
.
Notice however that the functions in this module consider the same precision and symmetry constants when computing the numerical derivative at each of the given points.
FUNCTION
taylor1_table
Example 1
#include <codecogs/maths/calculus/diff/taylor_table.h>
#include <math.h>
#include <stdio.h>
// precision constant
#define H 0.0001
// function to differentiate
double f(double x)
{
return cos(x);
}
// the first derivative of the function, to estimate errors
double df(double x)
{
return -sin(x);
}
int main()
{
// display precision
printf("\n h = %.4lf\n\n", H);
printf(" f(x) = cos(x)\n");
printf("f`(x) = -sin(x)\n\n");
// initialise points table
double P[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// compute the first derivative at each point in the table
std::vector<double> points(P, P+10),
derivatives = Maths::Calculus::Diff::taylor1_table(f, points, H);
// display the results, including error estimation
printf("Point\tApproximation\t\tActual value\t\tError\n\n");
for (int i = 0; i < 10; i++)
printf("x = %d\t%.15lf\t%.15lf\t%.15lf\n", i,
derivatives[i], df(points[i]), fabs(df(points[i]) - derivatives[i]));
printf("\n");
return 0;
}Output
h = 0.0001
f(x) = cos(x)
f`(x) = -sin(x)
Point Approximation Actual value Error
x = 0 -0.841470983405614 -0.841470984807897 0.000000001402283
x = 1 -0.909297425311170 -0.909297426825682 0.000000001514512
x = 2 -0.141120007824946 -0.141120008059867 0.000000000234921
x = 3 0.756802494046756 0.756802495307928 0.000000001261172
x = 4 0.958924273062761 0.958924274663138 0.000000001600378
x = 5 0.279415497732546 0.279415498198926 0.000000000466379
x = 6 -0.656986597622516 -0.656986598718789 0.000000001096273
x = 7 -0.989358244972195 -0.989358246623382 0.000000001651187
x = 8 -0.412118484553760 -0.412118485241757 0.000000000687997
x = 9 0.544021109981466 0.544021110889370 0.000000000907904Parameters
Returns
FUNCTION
taylor2_table
Example 1
#include <codecogs/maths/calculus/diff/taylor_table.h>
#include <math.h>
#include <stdio.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("\n h = %.4lf\n\n", H);
printf(" f(x) = cos(x)\n");
printf("f``(x) = -cos(x)\n\n");
// initialise points table
double P[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// compute the second derivative at each point in the table
std::vector<double> points(P, P+10),
derivatives = Maths::Calculus::Diff::taylor2_table(f, points, H);
// display the results, including error estimation
printf("Point\tApproximation\t\tActual value\t\tError\n\n");
for (int i = 0; i < 10; i++)
printf("x = %d\t%.15lf\t%.15lf\t%.15lf\n", i,
derivatives[i], d2f(points[i]), fabs(d2f(points[i]) - derivatives[i]));
printf("\n");
return 0;
}Output
h = 0.0001
f(x) = cos(x)
f``(x) = -cos(x)
Point Approximation Actual value Error
x = 0 -0.540302319866019 -0.540302305868140 0.000000013997879
x = 1 0.416146818496858 0.416146836547142 0.000000018050284
x = 2 0.989992487602587 0.989992496600445 0.000000008997858
x = 3 0.653643589818356 0.653643620863612 0.000000031045256
x = 4 -0.283662183922072 -0.283662185463226 0.000000001541154
x = 5 -0.960170275904858 -0.960170286650366 0.000000010745508
x = 6 -0.753902247021491 -0.753902254343305 0.000000007321814
x = 7 0.145500035223032 0.145500033808614 0.000000001414419
x = 8 0.911130256680583 0.911130261884677 0.000000005204094
x = 9 0.839071522055730 0.839071529076452 0.000000007020722