Derivatives, Series Expansions

Derivatives

Derivative of a function

f'[x]≡(df/dx)=(lim_(Δx->0))((Δf)/(Δx))

is, geometrically, its slope at a given point

Excercise: Produce a similar plot with Mathematica.

More specifically, at a point x one can define

(df/dx)⇒((f[x+h]-f[x])/h)   - forward derivative

(df/dx)⇒((f[x]-f[x-h])/h)   - backward derivative

(df/dx)⇒((f[x+h/2]-f[x-h/2])/h)   - central derivative,

In the limit h->0 all three formulas give the same result. For h small but finite they provide a numerical approximation for the derivative that can be used if it is difficult to calculate an exact value or to obtain an exact expression for f'[x].

Finding derivatives is called differentiation. Derivatives of elementary functions are known from the calculus:

(d/dx)(x^α)=((αx)^(α-1))
(d/dx)(e^x)=(e^x)
(d/dx)Sin[x]=Cos[x];        (d/dx)Cos[x]=-Sin[x]

Further, derivatives of more complicated functions can be obtained with the help of the chain rule

(d/dx)f[g[x]]=(df/dg)(dg/dx)

etc. For instance,

(d/dx)(e^ax)=((d (e^ax))/(d(ax)))((d(ax))/dx)=(e^ax)a=((ae)^ax)

Mathematica can analytically calculate derivatives

f[x_]:=(x^α);
f'[x]

or

(∂_x)f[x]

or

D[f[x],x]

Mathematica can also calculate limits that can be used, in principle, to find derivatives

Limit[((f[x+h]-f[x])/h),h->0]

As the derivative of a function is also a function, one can differentiate it as well. In this way one can define second- and higher-order derivatives.

f''[x]≡(((d^2)f)/(dx^2));       (f^((n)))[x]= (((d^n)f)/(dx^n))

In Mathematica, arbitrary-order derivatives can be obtained by the general command

Derivative[Order][Function][Argument]

for instance,

Derivative[1][f][x]
Derivative[2][f][x]

etc.

Taylor series

Smooth functions can be expanded in Taylor series in the vicinity of (Subscript[x, 0]). Here is the Mathematica command for calculating a Taylor series of an arbitrary function up to the third order. The correction terms in this case start with (((x-x0))^4), in general.

Series[g[x],{x,x0,3}]

BTW the command

Series[g[x],{x,(Subscript[x, 0]),3}]

does not work because of a bug. Sometimes Mathematica does not treat symbols with subscripts well. Taylor expansion replaces g[x] by a polynomial in (x - x0) that (at least for |x - x0| small enough) becomes more and more accurate as more powers of (x - x0) are included. Example

Series[ArcTan[x],{x,0,3}]

Here Mathematica shows a weakness because, in fact, this expansion includes only odd powers and thus the correction term is ((O[x])^5) . One can compare ArcTan[x] with its different approximations by Taylor series.

T1[x_]=Normal[Series[ArcTan[x],{x,0,1}]]
T3[x_]=Normal[Series[ArcTan[x],{x,0,3}]]
T5[x_]=Normal[Series[ArcTan[x],{x,0,5}]]
T7[x_]=Normal[Series[ArcTan[x],{x,0,7}]]

Plot[{ArcTan[x],T1[x],T3[x],T5[x],T7[x]},{x,0,5},PlotRange->{0,2},PlotStyle->{Thick,Dashed,Dashed,Dashed,Dashed}]

One can see that the Taylor expansion of ArcTan[x] has a range of convergence |x|<1. Outside this range the Taylor expansion does not provide an approximation to the function, whatever the order.

Taylor expansion near infinity can be obtained as follows

Series[ArcTan[x],{x,∞,3}]

Let us test these expansions, too

A1[x_]=Normal[Series[ArcTan[x],{x,∞,1}]]
A3[x_]=Normal[Series[ArcTan[x],{x,∞,3}]]
A5[x_]=Normal[Series[ArcTan[x],{x,∞,5}]]
A7[x_]=Normal[Series[ArcTan[x],{x,∞,7}]]
Plot[{ArcTan[x],A1[x],A3[x],A5[x],A7[x]},{x,0,5},PlotRange->{0,2},PlotStyle->{Thick,Dashed,Dashed,Dashed,Dashed}]

Again, the large-x expansion has the convergence range |x|>1.

One of the simplest examples of Taylor series is the geometrical progression

(1/(1-x))=1+x+(x^2)+(x^3)+...

Obviously the range of convergence of this series is |x|<1.

To the contrary, Taylor series of the exponential and sinusoidal functions near 0

Series[Exp[x],{x,0,5}]
Series[Sin[x],{x,0,5}]
Series[Cos[x],{x,0,5}]

converge everywhere. Let us illustrate it for Sin[x]

S1[x_]=Normal[Series[Sin[x],{x,0,1}]]
S3[x_]=Normal[Series[Sin[x],{x,0,3}]]
S5[x_]=Normal[Series[Sin[x],{x,0,5}]]
S7[x_]=Normal[Series[Sin[x],{x,0,7}]]
S9[x_]=Normal[Series[Sin[x],{x,0,9}]]
S11[x_]=Normal[Series[Sin[x],{x,0,11}]]
S13[x_]=Normal[Series[Sin[x],{x,0,13}]]
Plot[{Sin[x],S1[x],S3[x],S5[x],S7[x],S9[x],S11[x],S13[x]},{x,0,2π},PlotRange->{-2,2},PlotStyle->{Thick,Dashed,Dashed,Dashed,Dashed,Dashed,Dashed,Dashed}]

Function

Tanh[x]≡(((e^x)-(e^(-x)))/((e^x)+(e^(-x))))

is exponentially close to 1 at large x, thus its power expansion at large x does not exist

Series[Tanh[x],{x,∞,3}]

To obtain its expansion in powers of (e^(-x)), one can make a trick and at first expand the function

Tanh[Log[x]]≡((x-1/x)/(x+1/x))

Series[Tanh[Log[x]],{x,∞,4}]

and then replace x->(e^x) (ignoring the error message!)

Series[Tanh[Log[x]],{x,∞,4}]/.x->(E^x)

Taylor series (as well as limits) is the standard tool for resolving the behavior of a function at special points. For instance, what is the behavior of

f[x_]=((1-Cos[x])/(x^2))

at small x?

Limit[f[x],x->0]

f2[x_]=Normal[Series[f[x],{x,0,3}]]

Plot[{f[x],f2[x]},{x,0,10},PlotStyle->{Thick,Dashed},PlotRange->{-0.5,0.5}]

Derivatives and Taylor series

With the help of Taylor series one can investigate the accuracy of the finite-difference approximations for derivarives at the beginning of this chapter

Clear[f]
Series[((f[x+h]-f[x])/h) ,{h,0,2}]

One can see that

((f[x+h]-f[x])/h) =((df[x])/dx)+O[h]

The backward derivative has the same accuracy. To the contrary,

Series[((f[x+h/2]-f[x-h/2])/h) ,{h,0,2}]

that is, the centered derivative has the quadratic accuracy:

((f[x+h/2]-f[x-h/2])/h)  =((df[x])/dx)+O[(h^2)]

For this reason, one always should use the centered derivative.

One can propose even more accurate formulas for derivatives. For instance, there is an expression for the centered derivative with the O[(h^2)] accuracy

((-f[x+2h]+8f[x+h-8f[x-h]+f[x-2h])/h)  =((df[x])/dx)+O[(h^4)]

Excercise: Check this formula.

There are finite-difference expressions for higher-order derivatives, for instance

((f[x+h]+2f[x]-f[x-h])/(h^2))  =(((d^2)f[x])/(dx^2))+O[(h^2)]

Excercise: Check this formula.