diff .

“msharpmath”, The Simple is the Best

[100] 004 Tutorial diff                    differentiation

Click the above button for full-text tutorial of   ‘diff’ .

//==================================================================================
//   Umbrella ‘diff’  and  ’diff2′  for differentiation
//==================================================================================
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.

.central                            //  central difference scheme (default Spoke)
.forward                          //  forward difference scheme
.backward                       //  backward difference scheme
.eps/abseps(e=1.e-6)    //  absolute
.releps(e=1.e-6)             //  relative   (default Spoke)
.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 (gradient, divergence, curl, and Laplacian) space variables x,y,z are mandatorily specified.

.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)
.del[][] .x1 .x2 …  .xn ( f1,f2,f3, … , fm ) (c1,c2, …,cn)

//————————————————————————————————————
// centered difference
//————————————————————————————————————

#> diff .x(0) ( exp(x) ) .central .eps(0.1);
ans =       1.0016675

//————————————————————————————————————
// forward and backward difference
//————————————————————————————————————

#> diff .x(0) ( exp(x) ) .forward  .eps(0.1);
ans =       1.0517092

#> diff .x(0) ( exp(x) ) .backward .eps(0.1);
ans =      0.95162582

//————————————————————————————————————
// truncation
//————————————————————————————————————

#> diff .x(1e10) ( sqrt(x) ) .forward .eps(1.e-5);
ans =  4.3655746e-006

//————————————————————————————————————
// relative step size
//————————————————————————————————————

#> diff .x(1e10) ( sqrt(x) ) .forward .releps(1.e-5);
ans =  4.9999875e-006

//————————————————————————————————————
// single and multiple functions
//————————————————————————————————————

#> diff .x(0) ( sin(x) );
ans =               1

#> diff .x(0) ( sin(x),cos(x) );
ans =  [             1             0 ]

#> diff .x[5](0,1) ( x, x*x );   //  .central.releps(1.e-5)
ans =
[             1             0 ]
[             1           0.5 ]
[             1             1 ]
[             1           1.5 ]
[             1             2 ]

#> diff .x[5](0,1) ( x, x*x ) .togo(A,B); A; B;
A =
[             1 ]
[             1 ]
[             1 ]
[             1 ]
[             1 ]
B =
[             0 ]
[           0.5 ]
[             1 ]
[           1.5 ]
[             2 ]

#> diff2 .x(0) ( exp(x) ) .eps(0.1).central;
ans =       1.0008336

#> diff2 .x(0) ( exp(x) ) .eps(0.1).forward;
ans =       1.1060922

#> diff2 .x(0) ( exp(x) ) .eps(0.1).backward;
ans =       0.9055917

//————————————————————————————————————
// discretization theory
//————————————————————————————————————

#> (-2,0).dfdx(1) ;
// f’[i] = 1/(2h) ( f[i-2] -4f[i-1] +3f[i] ) + (-1/3)h^2 f^(3) + …

#> (-3,0).dfdx(1) ;
// f’[i] = 1/(6h) ( -2f[i-3] +9f[i-2] -18f[i-1] +11f[i] ) + (-1/4)h^3 f^(4) + …

#> (-2,0).dfdxm(1)  ;
ans =  [           0.5            -2           1.5 ]

#> (-3,0).dfdxm(1)  ;
ans =  [     -0.333333           1.5            -3       1.83333 ]

#> for.n(2,4) (0,n).dfdx(2) ;
// 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) + …

//————————————————————————————————————
// gradient
//————————————————————————————————————

#> for.n(1,8)  .del( exp(x)*cos(y) )(2,pi/6,0).eps(10^-n);
ans = <        7.084       -4.008            0 >
ans = <        6.464       -3.726            0 >
ans = <        6.406       -3.698  -8.882e-013 >
ans = <          6.4       -3.695            0 >
ans = <        6.399       -3.695            0 >
ans = <        6.399       -3.695  -8.882e-010 >
ans = <        6.399       -3.695            0 >
ans = <        6.399       -3.695  -8.882e-008 >

#> .del( x^3*y^2*z  )( 2,3,4 )       ;
ans = <          432          192           72 >

#> .del( x^3*y^2*z  )( 3,pi/3,4 )     .cyl;
ans = <        118.4         75.4        29.61 >

#> .del( x^3*y^2*z  )( 3,pi/4,pi/3 )  .sph;
ans = <        17.44         14.8        7.851 >

//————————————————————————————————————
// divergence
//————————————————————————————————————

#> .del*( x*y^2*z, x*x*y, x*z*z ) (2,3,4);
ans =        56.00008

#> .del * ( x*y^2*z, x*x*y, x*z*z ) (3,pi/3,4).cyl;
ans =       35.773146

#> .del * ( x*y^2*z, x*x*y, x*z*z ) (3,pi/4,pi/3).sph;
ans =       10.256043

//————————————————————————————————————
// curl
//————————————————————————————————————

#> .del ^ ( x*y^2*z, x*x*y, x*z*z ) (2,3,4);
ans = <            0            2          -36 >

#> .del ^ ( x*y^2*z, x*x*y, x*z*z ) (3,pi/3,4).cyl;
ans = <            0       -12.71        1.047 >

#> .del ^ ( x*y^2*z, x*x*y, x*z*z ) (3,pi/4,pi/3).sph;
ans = <        1.097       -1.321        5.424 >

//————————————————————————————————————
// Laplacian
//————————————————————————————————————

#> .del2 ( x^2*y^2*z^3 ) (2,3,4);
ans =       2528.0052

#> .del2 ( x^2*y^2*z^3 ) (3,pi/3,4).cyl;
ans =       645.60779

#> .del2 ( x^2*y^2*z^3 ) (3,pi/4,pi/3).sph;
ans =       16.102491

//————————————————————————————————————
// gradient
//————————————————————————————————————

#> .del[] .x1.x2.x3.x4 ( x1^3*x2^2*x3*x4^2 )( 2,3,4,5 );
ans =  [       10800.1       4800.02          1800       2880.01 ]

//————————————————————————————————————
// Jacobian
//————————————————————————————————————

#> .del[][] .x.y ( x*x*y, x+3*y, exp(x)*(y+1) )(2,3);
ans =
[       12.0001             4 ]
[             1             3 ]
[       29.5565       7.38906 ]

Comments are closed.