Taylor series

This simulation calculates the Taylor series of a function

y = f(x)

in the vicinity of a model point x0, y0

y = f(x0) + f ´(x0)(x - x0)/1! + f´´(x0)(x - x0)2/2!+... + f(n)(x0)(x - x0)n/n!...

n! = 1∙2∙3∙4...∙ n:  n faculty

The first member of the series is an approximation within the interval by the value in the model point:  y ≈ f(x0). This is commonly called the zeroth approximation.

The first approximation considers the first derivative, and is the tangent to the curve at the model point. It is also called the linear approximation and is already quite useful for small intervals.

y ≈ f(x0) + f ´(x0)(x - x0)/1! = f(x0) + f´(x0)(x - x0)

The second or quadratic approximation adds a parabola of second order, considering also the curvature in the model point.

y ≈ f(x0) + f ´(x0)(x - x0)/1! + f´´(x0)(x - x0)2/2

(One should realize that the resulting polynom of second order is not identical to the parabola used in the Kepler algorithm of integration:  The Kepler parabola cuts the curve in three points, while the Taylor polynom touches it in one. Both approximations converge with decreasing interval width of integration).

The higher order approximations are polynomials of increasing order, which approach the function well over wider and wider intervals (for "well behaved" functions, which our examples are).

For the example of a 7th power parabola the 7th Taylor approximation is identical to the function itself, as all higher derivatives are 0. (the very small deviations from 0 seen in the number fields for f8 and f9 are residual calculation errors).

(x - 0)7 = y0 + f1(x - x0) + f2 (x - x0)2 + f3(x - x0)3 + f4(x - x0)4 + f5(x - x0)5

+ f6(x - x0)6 + f7(x - x0)7

The identity demonstrates that the original parabola x7can be interpreted as a Taylor expansion around x = 0. Indeed, as one draws the dot to x = 0, all factors in the number fields become zero except f7, which will be 1.

Calculation of the derivatives

With h − the difference between consecutive x values − sufficiently small, the first derivative (differential quotient) is approximated by the difference quotient:

y(1)(x) = (1/2h) [y(x+h) - y(x-h)]

The second derivative is approximated as the difference quotient of the first difference quotient:

y(2)(x)= (1/2h) [y(1)(x+h) - y(1)(x-h)] = (1/2h)2 [y(x+2h) - 2y(x) + y(x-1h)]

This algorithm can be continued, leading to the code lines shown below for the base function a[0] and its derivatives up to the 9th order a[9].

For the predefined functions most approximations do not differ visibly from the analytical derivatives, with the interval h being 1/1000 of the total range.

For still higher derivatives the limits of the accuracy of the approximation and also of the computing accuracy are stressed, and noise appears.

Code for calculating the derivatives: With s = 1/(2h)

a0=y0;

a1[j]=Math.pow(s,1)*(y2[1][j]-y1[1][j]);

a2[j]=Math.pow(s,2)*(y2[2][j]-2*y2[0][j]+y1[2][j]);

a3[j]=Math.pow(s,3)*(y2[3][j]-3*y2[1][j]+3*y1[1][j]-y1[3][j]);

a4[j]=Math.pow(s,4)*(y2[4][j]-4*y2[2][j]+6*y2[0][j]-4*y1[2][j]+y1[4][j]);

a5[j]=Math.pow(s,5)*(y2[5][j]-5*y2[3][j]+10*y2[1][j]-10*y1[1][j]+5*y1[3][j]-y1[5][j]);

a6[j]=Math.pow(s,6)*(y2[6][j]-6*y2[4][j]+15*y2[2][j]-20*y2[0][j]+15*y1[2][j]-6*y1[4][j]+y1[6][j]);

a7[j]=Math.pow(s,7)*(y2[7][j]-7*y2[5][j]+21*y2[3][j]-35*y2[1][j]+35*y1[1][j]-21*y1[3][j]+7*y1[5][j]-y1[7][j]);

a8[j]=Math.pow(s,8)*(y2[8][j]-8*y2[6][j]+28*y2[4][j]-56*y2[2][j]+70*y2[0][j]-56*y1[2][j]+28*y1[4][j]-8*y1[6][j]+y1[8][j]);

a9[j]=Math.pow(s,9)*(y2[9][j]-9*y2[7][j]+36*y2[5][j]-84*y2[3][j]+126*y2[1][j]-126*y1[1][j]+84*y1[3][j]-36*y1[5][j]+9*y1[7][j]-y1[9][j]);

Code for calculating the Taylor approximations

T0[j]=a0;

T1[j]=T0[j]+Math.pow(x-x0,1)*a1/(1);

T2[j]=T1[j]+Math.pow(x-x0,2)*a2/(1*2);

T3[j]=T2[j]+Math.pow(x-x0,3)*a3/(1*2*3);

T4[j]=T3[j]+Math.pow(x-x0,4)*a4/(1*2*3*4);

T5[j]=T4[j]+Math.pow(x-x0,5)*a5/(1*2*3*4*5);

T6[j]=T5[j]+Math.pow(x-x0,6)*a6/(1*2*3*4*5*6);

T7[j]=T6[j]+Math.pow(x-x0,7)*a7/(1*2*3*4*5*6*7);

T8[j]=T7[j]+Math.pow(x-x0,8)*a8/(1*2*3*4*5*6*7*8);

T9[j]=T8[j]+Math.pow(x-x0,9)*a9/(1*2*3*4*5*6*7*8*9);

The integers in the formulas of the derivatives follow simple generation rules; thus the series can be easily generated or continued.