COST (GBP)
this unit 1.25
sub units 0.75
+
0
taylor table
Computes the first and second derivatives of a function at multiple points.
Controller: CodeCogs Email
Dependents
InfoInterface
C++
#include <codecogs/maths/calculus/differential/taylor_table.h>
using namespace Maths::Calculus::Diff;
std::vector<double> taylor1_table (double (*f)(double), std::vector<double> &points, double h, double gamma = 1.0)
Computes the value of the first derivative of a function at multiple points.
std::vector<double> taylor2_table (double (*f)(double), std::vector<double> &points, double h, double gamma = 1.0)
Computes the value of the second derivative of a function at multiple points.
Use the following HTML code to embed the calculators within other websites:
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 Maths/Calculus/Diff/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 \inline x_1, x_*, x_2 are chosen such that\displaystyle x_1 = x_* - h, \qquad x_2 = x_* + \gamma h
where \inline h, \inline \gamma are real positive constants corresponding to the precision and the symmetry of the interval of differentiation. The derivative is thus approximated at point \inline x_*.
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.Authors
- Lucian Bentea (November 2006)
Taylor1 Table
std::vector<double>taylor1_table( double (*f)(double)[function pointer]
std::vector<double>& points
double h
double gamma
= 1.0 )Example 1
#include <> #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.000000000907904
Parameters
- f the function to differentiatepoints the vector of abscissas at which to compute the derivativeh the value of the precision constant hgamma Default value = 1.0
Returns
- a vector containing the values of the first derivative of f evaluated at each of the given points
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 Table
std::vector<double>taylor2_table( double (*f)(double)[function pointer]
std::vector<double>& points
double h
double gamma
= 1.0 )Example 2
#include <> #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
Parameters
- f the function to differentiatepoints the vector of abscissas at which to compute the derivativeh the value of the precision constant hgamma Default value = 1.0
Returns
- a vector containing the values of the second derivative of f evaluated at each of the given points
Source Code
Source code is available when you buy a Commercial licence.
Not a member, then Register with CodeCogs. Already a Member, then Login.