#15 (03/27/2024)

Numerical differentiation

The only occasion where numerical differentiation is needed is when data are given numerically only, i.e.
x f (x)
1 1
2 8
3 27
4 64
numdiff.gif

Forward difference


f (x + h)
=
f (x) + h f ′(x) + h2

2!
f  " (x) + h3

3!
f  "′(x)+…
(1)
f (x) + h f ′(x),
(2)
hence
f ′(x) ≈ f (x + h) − f (x)

h
.
(3)

Backward difference


f ( xh )
=
f (x) − h f ′(x) + h2

2!
f  "(x) − h3

3!
f  "′(x) + …
f (x) − h f ′(x),
(4)
hence
f ′(x) ≈ f (x) − f (xh)

h
.
(5)

Central difference


f (x + h)
=
f (x) + h f ′(x) + h2

2!
f  "(x) + h3

3!
f  "′(x)+…
(6)
f (xh)
=
f (x) − h f ′(x) + h2

2!
f  "(x) − h3

3!
f  "′(x)+…
(7)
from which
f ( x + h ) − f ( xh ) = 2 h f ′(x) + 2 h3

3!
f  "′(x)+ …
(8)
hence

f ′(x) ≈ f ( x + h )− f ( xh )

2h
.
(9)
It is seen from the above that the central difference should yield better approximation as the truncation error is in the order of h2 while the truncation error for the forward and backward difference methods is in the order of h.

Second order derivative


f (x + h)
=
f (x)+h f ′(x) + h2

2!
f  "(x) + h3

3!
f  "′(x)+…
(10)
f (xh)
=
f (x)−h f ′(x) + h2

2!
f  "(x) − h3

3!
f  "′(x)+…
(11)
Adding the above two yields

f (x + h)+f (xh) = 2 f (x) + h2 f  "(x) + …
(12)
so

f  "(x) ≈ f (x + h) + f (xh) − 2 f (x)

h2
.
(13)

Example

The program below is to numerically differentiate a function given by the table below (numerical values of sin x for x = 0.0  ∼ 1.0). for x = 0 to x = 1 with the step size of h = 0.1 excluding at x=0 and x=1.0 where the central difference scheme does not work.
x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
f(x) 00.09980.19860.29550.38940.47940.56460.64420.71730.78330.8414

#include <stdio.h>
#define N 11

int main()
{
double y[N]={0, 0.0998, 0.1986, 0.2955, 0.3894, 0.4794, 0.5646, 0.6442, 0.7173, 0.7833, 0.8414};
double central[N], h=0.1;
int i;

/* Excluding x=0 and x=1.0 */
for (i=1;i<N-1;i++) central[i]= (y[i+1]-y[i-1])/(2*h);

printf ("   x    Central \n---------------------------\n");

for (i=1;i<N-1;i++)
 printf ("%lf %lf\n", i*h, central[i]);

return 0;
}


What if you want to obtain f′(x) at x = a with the same accuracy as the central difference ?

Obviously you cannot use the central difference scheme at x=a. Using the forward difference scheme, one can get

f′(x)  ∼  f(x+h) − f(x)

h
which is a poor approximation at best.
Consider

f(x + h)
=
f(x) + h f′(x) + h2

2!
f"(x) + …
(14)
f(x + 2 h)
=
f(x) + 2 h f′(x) + 4 h2

2!
f"(x) + …
(15)
The h2 term can be eliminated if one subtracts Eq.(15) from 4 times Eq.(14) as

4 f(x + h) − f(x + 2 h) = 3 f(x) + 2 h f′(x) + (higher order) …
(16)
from which one obtains

f′(x)  ∼  4 f(x+h) − f(x+2h) − 3 f(x)

2 h
(17)
Eq.(17) has the same order of accuracy as the central difference scheme.
So the program above can be modified as:

#include <stdio.h>
#define N 11

int main()
{
double y[N]={0, 0.0998, 0.1986, 0.2955, 0.3894, 0.4794, 0.5646, 0.6442, 0.7173, 0.7833, 0.8414};
double central[N], h=0.1;
int i;

for (i=1;i<N-1;i++) central[i]= (y[i+1]-y[i-1])/(2*h);

central[0]=(4*y[1]-y[2]-3*y[0])/(2*h);

printf ("   x    Central \n---------------------------\n");

for (i=0;i<N-1;i++)
 printf ("%lf %lf\n", i*h, central[i]);

return 0;
}

Integration review

f (x) f (x) dx
xα     (α ≠ −1) xα+ 1/(α+ 1)
1/x ln x
ex ex
1/(x2 + a2) arctan x
sin k x − cos k x/k
cos k x sin k x/k
Some exercise:

    (a)

    1

    x2 − 1
    dx

    (b)

    π

    0 
    x sin x dx

    (c)



    0 
    1

    x2 + 1
    dx

Numerical Integration

numint.gif
where
h = ba

2n
.
Note that the number of partitioning for the Simpson rule must be an even number (=2n).
For the coding purpose, it is convenient to write Eq.(30) as

I
=
h

3

f0+f2n
+
h

3
×4
f1 + f3 + f5 + …+f2n−1
+
h

3
×2
f2+f4+ f6 + …+ f2n−2
.
(31)
Implementation

/* Simpson's rule */
#include <stdio.h>
#include <math.h>

double f(double x)
 {return 4.0/(1.0+x*x);}

int main()
{
 int i, n ;
 double a=0.0, b=1.0 , h, s1=0.0, s2=0.0, s3=0.0, x;

 printf("Enter number of partitions  = ");
 scanf("%d", &n) ;

  h = (b-a)/(2.0*n) ;

  s1 = (f(a)+ f(b));

 for (i=1; i<2*n; i=i+2) s2 = s2 + f(a + i*h);

 for (i=2; i<2*n; i=i+2) s3 = s3 + f(a + i*h);

    printf("%20.12lf\n", (h/3.0)*(s1+ 4.0*s2 + 2.0*s3)) ;
 return 0;
}

According to error analysis, the truncation error for each rule is as follows:
where h is the step size and ξ is a value within the interval.

Computation of π



1

0 
1

1+x2
dx = π

4
.
1overx2plus1.gif


1

0 


 

1−x2
 
dx = π

4
.
sqrt1minusx2.gif
It is important to know that the accuracy of numerical integration generally depends on the derivatives of the function, f(x), to be integratated. Although the two examples above yield approximation to π, the convergence speed (and accuracy) varies greatly. For further information, read the following link:
Error analysis



File translated from TEX by TTH, version 4.03.
On 26 Mar 2024, 12:38.