web viewchapter 4 differentiation. 4-1 single variable function. 4-2 derivatives and plot. 4-3...
TRANSCRIPT
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
[100] 004 revised on 2012.12.03 cemmath
The Simple is the Best
Chapter 4 Differentiation
4-1 Single Variable Function4-2 Derivatives and Plot4-3 Higher Derivatives4-4 Numerical Theories4-5 Vector Calculus4-6 Multi-Variable Functions4-7 Summary
In this chapter, we discuss Umbrella ‘diff’ to handle differentiation of functions. Also, vector calculus such as gradient, divergence, curl and Laplacian are handled by operators ‘.del’, ‘.del*’, ‘.del^’ and ‘.del2’. For multi-variable functions, the gradient and the Jacobian matrix are implemented by using operators ‘.del[]’ and ‘.del[][]’. Numerical theories to differentiate a function are briefly addressed.
Section 4-1 Single Variable Function
■ Syntax of Umbrella ‘diff’. Differentiation of a function with respect to a single independent variable can be written as
dfdx
(a )=limh → 0
f ( a+h )− f (a )h
The syntax of our new Umbrella ‘diff’ for this situation is
diff .x(a) ( <<opt>>, f(x), g(x), h(x), … ) .Spoke .Spoke. …
1
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
and the relevant Spokes are
.central central difference scheme (default Spoke)
.forward forward difference scheme
.backward backward difference scheme .releps (e=1.e-6) relative ϵ rel (default Spoke) .eps/abseps(e=1.e-6) absolute ϵ|¿|¿ .togo (F,…) takeout derivatives as matrices F,…
.plot plot derivatives for one-variable case
■ Central Difference Scheme. Differentiation by the central difference scheme is defined as
f '( x ¿≅f ( x+ϵ )−f (x−ϵ )
2 ϵ
where the default value of ϵ is ϵ=10−5. The Spoke ‘central’ is used to evaluate the derivative, for example
#> diff .x(0) ( exp(x) ).central.eps(0.1); ans = 1.0016675
is equivalent to evaluate
e0.1−e−0.1
2 (0.1 )=1.0016675
■ Forward and Backward Difference Schemes. Differentiation by the forward- and backward-difference schemes are defined as
f '( x ¿≅f ( x+ϵ )− f (x )
ϵ, f '( x¿≅
f ( x )−f ( x−ϵ )ϵ
respectively. Using ϵ=0.1, Spokes ‘forward’ and ‘backward’ are used to evaluate the following two approximate derivatives
e0.1−e0
0.1=1.0517092, e0−e−0.1
0.1=0.9516258
by
2
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
#> diff .x(0) ( exp(x) ).forward.eps(0.1); #> diff .x(0) ( exp(x) ).backward.eps(0.1); ans = 1.0517092ans = 0.9516258
Note that the central difference shows better agreement with the exact value of e0=1.
■ Relative Step Size. Consider the derivative of f ( x )=√x at x=1010. If we apply the same procedure
#> diff .x(1e10) ( sqrt(x) ).forward.eps(1.e-5); ans = 0.0000044
the result differs from the expected value
12√1010
=0.000005
This is due to the truncation occurred by x+ϵ=1010+10−5. Therefore, we introduce a relative step size
h=|ϵ rel x|, f '( x ¿≅ f ( x+h )−f (x )h
for the case of the forward difference scheme. In particular, h=ϵ rel is used
when h<ϵ rel. For example,
#> diff .x(1e10) ( sqrt(x) ).forward.releps(1.e-5); ans = 0.0000050
yields the correct result 1 / 2√1010=0.000005. If not specified explicitly, the
central difference scheme and the default value of ϵ rel=10−5 is used. This means that
.central .releps(1.e-5)
3
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
is the default Spokes of Umbrella ‘diff’.
■ Multiple Functions. When a single function is differentiated, the final result is expressed as a number (i.e., double data). However, differentiation of multiple functions is expressed as a matrix. For example,
#> diff .x(0) ( sin(x) ); #> diff .x(0) ( sin(x),cos(x) );ans = 1.0000000ans = [ 1 0 ]
show the difference between the single and multiple functions.
Section 4-2 Derivatives and Plot
■ Derivative Functions. In the above, we evaluated the derivative at a given point. If we consider an interval, the result becomes a derivative function. This can be treated by the following syntax
diff .x[n=26,g=1](a,b) ( <<opt>>, f(x), g(x), h(x), … )
or
x = (a,b).span(n,g=1); diff [x] (a,b) ( <<opt>>, f(x), g(x), … )
The numerical result is expressed as a matrix. The differentiation method is by
default based on the central difference scheme and the relative step size , as was discussed above. For example,
#> diff .x[5](0,1) ( x, x*x ); // .central.releps(1.e-5)
yields the following unnamed matrix
ans = [ 1 0 ] [ 1 0.5 ] [ 1 1 ] [ 1 1.5 ] [ 1 2 ]
4
rel
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
When each derivative function is required, use of Spoke ‘togo’ fulfills this purpose, For example,
#> diff .x[5](0,1) ( x, x*x ) .togo(A,B);#> A; B;
creates two matrices
A = [ 1 ] [ 1 ] [ 1 ] [ 1 ] [ 1 ] B = [ 0 ] [ 0.5 ] [ 1 ] [ 1.5 ] [ 2 ]
■ Plot of Derivatives. Using Spoke ‘plot’ or ‘plot+’, it is possible to plot the derivatives. As an example, we consider two functions
f ( x )=sin x+3 , g ( x )=sin 2x+3
the derivatives of which are
f ' ( x )=cos x , g ' ( x )=2 cos2 x
Over an interval of 0 ≤ x≤ 2π , both the functions and their derivatives are plotted by the following commands
#> plot .x(0,2*pi) ( sin(x)+3,sin(2*x)+3 ); // .x[101](a,b)#> diff .x(0,2*pi) ( sin(x)+3,sin(2*x)+3 ) .plot+; // .x[ 26](a,b)
where the results are shown in Figure 1.
5
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
Figure 1 Plot derivatives
Section 4-3 Higher Derivatives
■ Numerical Differentiation of Higher Derivatives. To evaluate higher derivatives, the numerical theories discussed in the last section can be employed. However, due to subtraction between close numbers, truncation becomes the most critical factor in approximation. For this reason, we support only the second-order differentiation of functions. The modified Umbrella ‘diff2’ is adopted to find the second-order differentiation. For higher derivatives greater than 2nd-order, users can adopt the theoretical finite-difference approximations discussed in the last section.
The 2nd-order derivatives are approximated as
f i' '=
f i+1−2 f i+ f i−1
h2 +O (h2) central
f i' '=
f i+2−2 f i+1+f i
h2 +O (h2 ) forward
f i' '=
f i−2 f i−1+ f i−2
h2 +O (h2 ) backward
where the degree of accuracy varies depending on the numerical scheme. For a special case of f ( x )=ex
and h=0.1 f ''(0) can be evaluated in three different
ways by noting that
6
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
e−0.2=0.8187308 , e−0.1=0.9048374 , e0=1e0.1=1.1051709 , e0.2=1.2214028
The results are
f i' '=0.9048374−2 (1 )+1.1051709
(0.1 )2=1.0008336 central
f i' '=1−2 (1.1051709 )+1.2214028
(0.1 )2=1.1060922 forward
f i' '=0.8187308−2 (0.9048374 )+1
(0.1 )2=0.9055917 backward
These formula can be confirmed by the following Cemmath commands
#> diff2 .x(0) ( exp(x) ).eps(0.1).central; #> diff2 .x(0) ( exp(x) ).eps(0.1).forward;#> diff2 .x(0) ( exp(x) ).eps(0.1).backward; ans = 1.0008336ans = 1.1060922ans = 0.9055917
■ Spokes for 2nd-Order Differentiation. All the Spokes for differentiation are equally applicable to the second-order differentiation, i.e. ‘diff2’. For example, Spoke ‘plot’ yields a plot of 2nd-order derivative, as follows
#> diff2 .x(0,2) ( x^4-3*x^3+5*x-5 ).plot; #> plot+ .x(0,2) ( x^4-3*x^3+5*x-5 );
where the results are shown in Figure 2.
7
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
Figure 2 Plot function and 2nd-order derivatives
Section 4-4 Numerical Theories
■ Finite Difference Approximation. In the literature, it is well known that the finite-difference approximations are derived from the Taylor series. A few examples for approximating f ' (x) are
f i'=
3 f i−4 f i−1+ f i−2
2h+O (h2 )
f i'=
11 f i−18 f i−1+9 f i−2−2 f i−3
6 h+O (h3 )
In order to find the theoretical expressions for approximating derivatives, we provide a Tuple function ‘dfdx’ the syntax of which is
(n1,n2).dfdx(order)
where order represents the order of differentiation, and both n1 and n2 are the indices designating f n 1 and f n2. The two approximations listed above can be confirmed from the following Cemmath commands
#> (-2,0).dfdx(1) ; #> (-3,0).dfdx(1) ;
which result in
8
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
// f'[i] = 1/(2h) ( f[i-2] -4f[i-1] +3f[i] ) + (-1/3)h^2 f^(3) + ...// f'[i] = 1/(6h) ( -2f[i-3] +9f[i-2] -18f[i-1] +11f[i] ) + (-1/4)h^3 f^(4) + ...
Note that the error estimate is also printed as a result. The last result can be interpreted directly as
f i'=
−2 f i−3+9 f i−2−18 f i−1+11 f i
6h−1
4h3 f ''''+…
Also, the numerical values of each coefficient can returned as a matrix, if Tuple function ‘dfdxm’ is used. From the following commands
#> (-2,0).dfdxm(1) ; #> (-3,0).dfdxm(1) ;
For the above case, two matrices representing coefficients
ans = [ 0.5 -2 1.5 ] ans = [ -0.33333 1.5 -3 1.8333 ]
are displayed on the screen.
■ Higher Derivative Approximation. For higher derivatives, it is also possible to derive finite-difference approximations. From the following Cemmath commands
#> for.n(2,4) (0,n).dfdx(2) ;
we get forward-difference approximations for the second derivatives
// f''[i] = 1/(h^2) ( f[i] -2f[i+1] +f[i+2] ) + (1/1)h^1 f^(3) + ...
// f''[i] = 1/(h^2) ( 2f[i] -5f[i+1] +4f[i+2] -f[i+3] ) + (-11/12)h^2 f^(4) + ...
// f''[i] = 1/(12h^2) ( 35f[i] -104f[i+1] +114f[i+2] -56f[i+3] +11f[i+4] ) + (5/6)h^3 f^(5) + ...
9
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
The above results can be expressed in more clear form
f i' '=
f i−2 f i+1+ f i +2
h2 +h f i '''+…
f i' '=
2 f i−5 f i+1+4 f i+2−f i+3
h2 −1112
h2 f ''''+…
f i' '=
35 f i−104 f i+1+114 f i+2−56 f i+3+11 f i+ 4
12h2 + 56
h3 f 5+…
These expressions can be alternatively used to evaluate numerical approximations of derivatives, and this point will be discussed later.
Section 4-5 Vector Calculus
■ ‘.del’ for Gradient. In the three-dimensional space, the gradient of a function f (x , y , z) is defined as
∇ f =i ∂ f∂x
+ j ∂ f∂ y
+k ∂ f∂ z
∇ f =er∂ f∂ r
+eθ1r
∂ f∂ θ
+ez∂ f∂ z
∇ f =er∂ f∂ r
+eφ1r
∂ f∂ φ
+eθ1
r sin φ∂ f∂ θ
where the cylindrical (r , θ , z) and the spherical (r ,φ ,θ) coordinates are also considered. The operator ‘.del’ is innovated to handle these gradients in three coordinates.
For a given point (a , b , c )
, the gradient ∇ f is numerically calculated from
the following syntax
10
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
.del ( f(x,y,z) ) (a,b,c).del ( f(x,y,z) ) (a,b,c).cyl.del ( f(x,y,z) ) (a,b,c).sph
In these cases, both the cylindrical and spherical coordinates must be expressed in terms of x , y , z for consistency.
Operator ‘.del’ returns a three-dimensional vertex which can be easily converted to a matrix of dimension 3×1 (the date structure of vertex will be discussed later though). Another Spoke ‘eps’ controls the relative step size for evaluating the partial derivative. To examine the role of Spoke ‘eps’, let us execute the followings
#> for.n(1,5) .del( exp(x)*cos(y) )(2,pi/6,0).eps(10^-n);
The results are
ans = < 7.084 -4.008 0 >ans = < 6.464 -3.726 0 >ans = < 6.406 -3.698 0 >ans = < 6.4 -3.695 0 >ans = < 6.399 -3.695 0 >
It is obvious that the use of eps = 1.e-5 is reasonable, and therefore this is assigned as a default value. Using this default value,
#> .del( x^3*y^2*z )( 2,3,4 ) ; #> .del( x^3*y^2*z )( 3,pi/3,4 ) .cyl; #> .del( x^3*y^2*z )( 3,pi/4,pi/3 ) .sph;
produce the following results
ans = < 432 192 72 >ans = < 118.4 75.4 29.61 >ans = < 17.44 14.8 7.851 >
■ Operator ‘.del*’ for Divergence. The divergence of a vector field u=(u , v , w) is defined as
11
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
∇⋅ u=∂ u∂ x
+ ∂ v∂ y
+ ∂ w∂ z
∇⋅ u=1r
∂∂ r
(r u )+1r
∂ v∂θ
+ ∂ w∂ z
∇⋅ u= 1r2
∂∂ r
(r2 u )+ 1r sin φ
∂∂ φ
(sin φ v )+ 1r sin φ
∂ w∂θ
The divergence is numerically calculated from the following syntax
.del * ( u(x,y,z),v(x,y,z),w(x,y,z) ) (a,b,c)
.del * ( u(x,y,z),v(x,y,z),w(x,y,z) ) (a,b,c).cyl
.del * ( u(x,y,z),v(x,y,z),w(x,y,z) ) (a,b,c).sph
For example,
#> .del*( x*y^2*z, x*x*y, x*z*z ) (2,3,4); #> .del * ( x*y^2*z, x*x*y, x*z*z ) (3,pi/3,4).cyl; #> .del * ( x*y^2*z, x*x*y, x*z*z ) (3,pi/4,pi/3).sph;
yields the following results
ans = 56.0000800ans = 35.7731456ans = 10.2560429
■ Operator ‘.del^’ for curl. The curl of a vector field u=(u , v ,w) is defined as
∇× u=| i j k∂
∂ x∂
∂ y∂
∂ zu v w
|
12
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
∇× u=1r| er r eθ ez
∂∂r
∂∂ θ
∂∂ z
u rv w|
∇× u= 1r2 sin φ| er r eφ r sin φ eθ
∂∂ r
∂∂ φ
∂∂θ
u rv rw sin φ|The curl is numerically calculated from the following syntax
.del ^ ( u(x,y,z),v(x,y,z),w(x,y,z) ) (a,b,c)
.del ^ ( u(x,y,z),v(x,y,z),w(x,y,z) ) (a,b,c).cyl
.del ^ ( u(x,y,z),v(x,y,z),w(x,y,z) ) (a,b,c).sph
For example,
#> .del ^ ( x*y^2*z, x*x*y, x*z*z ) (2,3,4); #> .del ^ ( x*y^2*z, x*x*y, x*z*z ) (3,pi/3,4).cyl; #> .del ^ ( x*y^2*z, x*x*y, x*z*z ) (3,pi/4,pi/3).sph;
yields the following results
ans = < 0 2 -36 >ans = < 0 -12.71 1.047 >ans = < 1.097 -1.321 5.424 >
■ Operator ‘.del2’ for Laplacian. The Laplacian of a function f (x , y , z) is defined as
∇2 f = ∂2 f∂ yx2 +
∂2 f∂ y2 +
∂2 f∂ z2
∇2 f =1r
∂∂ r (r ∂ f
∂r )+ 1r2
∂2 f∂θ2 +
∂2 f∂ z2
13
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
∇2 f = 1r2
∂∂ r (r2 ∂ f
∂r )+ 1r2sin φ
∂∂ φ (sin φ ∂ f
∂ φ )+ 1r2sin 2φ
∂2 f∂θ2
Then, the divergence is numerically calculated from the following syntax
.del2 ( f(x,y,z) ) (a,b,c)
.del2 ( f(x,y,z) ) (a,b,c).cyl
.del2 ( f(x,y,z) ) (a,b,c).sph
An example for evaluating Laplacian is
#> .del2 ( x^2*y^2*z^3 ) (2,3,4); #> .del2 ( x^2*y^2*z^3 ) (3,pi/3,4).cyl; #> .del2 ( x^2*y^2*z^3 ) (3,pi/4,pi/3).sph;
which yields the following results
ans = 2528.0051988ans = 645.6077851ans = 16.1024906
Section 4-6 Multi-Variable Functions
■ Operator ‘.del[]’ for gradient. The gradient of a function f (x1 , x2 , …, xn) with multiple variables is defined as
∇ f =e1∂ f∂ x1
+e2∂ f∂ x2
+…+en∂ f∂ xn
The syntax for the gradient of a multi-variable function is
.del[] .x1.x2 .xn ( f(x1,x2, …,xn) ) (c1,c2, …, cn)
where all the Hub variables x1, x2, …, xn are dummy and returns a matrix (instead of vertex). The Spoke of ‘eps’ also applies to operator ‘.del[]’.
An example is taken for a function
14
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
f ( x1 , x2 , x3 , x4 )=x13 x2
2 x3 x42
at point ( x1 , x2 , x3 , x4 )=(2,3,4,5). For this case, let us find ∇ f by the following commands
#> .del[] .x1.x2.x3.x4 ( x1^3*x2^2*x3*x4^2 ) (2,3,4,5);
which result in
ans = [ 10800 4800 1800 2880 ]
■ Operator for ‘.del[][]’ for Jacobian Matrix. Let us consider a vector function defined as
( f 1 , f 2 , …, f m)
where each function f i is a multi-variable function of x i , i=1,2,3 ,…,n. Then, the Jacobian matrix J of dimension m× n is defined as
J=[ ∂ f i
∂ x j ]=|∂ f 1
∂ x1
∂ f 1
∂ x2⋯
∂ f 1
∂ xn
∂ f 2
∂ x1
∂ f 2
∂ x2⋯
∂ f 2
∂ xn
⋮ ⋮ ⋱ ⋮∂ f m
∂ x1
∂ f m
∂ x2⋯
∂ f m
∂ xn
|The syntax for the Jacobian matrix is
.del[][] .x1.x2 .xn ( f1,f2,f3, … , fm ) (c1,c2, …, cn)
The operator ‘.del[][]’ plays a simple role of differentiation only, as similar to the operator ‘.del[]’.
Consider a vector function of f =(x2 y , x+3 y ,e x ( y+1 ) ) , and find a
15
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
Jacobian matrix at point r=(2,3). This can be solved by
#> .del[][] .x.y ( x*x*y, x+3*y, exp(x)*(y+1) )(2,3);
and the result is
ans = [ 12 4 ]
[ 1 3 ] [ 29.557 7.3891 ]
Therefore, we get a Jacobian matrix at point
J=[ ∂ f i
∂ x j ]=[ 12 41 3
29.557 7.3891]Section 4-7 Summary
■ Derivatives with Umbrellas ‘diff’ and ‘diff2’. Derivatives of a function are calculated by the following syntax.
Point value (double)
diff .x(a) ( <<opt>>, f(x), g(x), h(x), … ) diff2 .x(a) ( <<opt>>, f(x), g(x), h(x), … )
Derivative function (matrix)
diff .x[n=26,g=1](a,b) ( <<opt>>, f(x), g(x), h(x), … ) diff2 .x[n=26,g=1](a,b) ( <<opt>>, f(x), g(x), h(x), … )
x = (a,b).span(n,g=1); diff [x] (a,b) ( <<opt>>, f(x), g(x), h(x), … )x = (a,b).span(n,g=1); diff2 [x] (a,b) ( <<opt>>, f(x), g(x), h(x), … )
■ Spokes. A number of Spokes are available as follows
.central central difference scheme (default Spoke)
16
(2,3)r
[100] 004 Chapter 4 Differentiation, Tutorial by www.msharpmath.com
.forward forward difference scheme
.backward backward difference scheme .releps(e=1.e-6) relative ϵ rel (default Spoke) .eps/abseps(e=1.e-6) absolute ϵ|¿|¿ .togo (F,…) takeout derivatives as matrices F,…
.plot plot derivatives for one-variable case
■ Finite Difference Approximation.
(n1,n2).dfdx(order) (n1,n2).dfdxm(order)
■ Vector Calculus. Vector calculus (gradient, divergence, curl, and Laplacian) can be numerically calculated by the following syntax.
.del ( f(x,y,z) ) ( a,b,c )
.del * ( u(x,y,z),v(x,y,z),w(x,y,z) ) ( a,b,c )
.del ^ ( u(x,y,z),v(x,y,z),w(x,y,z) ) ( a,b,c )
.del2 ( f(x,y,z) ) ( a,b,c )
■ Multi-Variable Functions. For the gradient and Jacobian matrix of a multi-variable function, we use
.del[] .x1 .x2 .xn ( f ) ( c1,c2, …,cn ) // gradient
.del[][] .x1 .x2 .xn ( f1,f2,f3, … , fm ) ( c1,c2, …,cn )
17