Numerical Differentiation

 

On homework #2, we use the limit of difference quotient

 

 f’(x) = lim    f(x+h) – f(x) 

            h->0           h

 

We can approximate the derivative by using small h.

How small h has to be?

It depends on f(x). We can start with a large h and make it smaller and smaller until we sees that Dk (the approximation) is loosing precision.

 

Example: |Dk – Dk-1| < E then stop

 

Dk = f(x + hk) – f(x)   for k = 1, 2, …, N

                   Hk

 

Example: f(x) = ex. We want to approximate f’(x) at x =1

 

Dk = e(x+hk) – ex

                hk

 

hk                                           Dk

 

0          .1                                             2.8588                                    

1          .01                                           2.7319

2          .001                                         2.7196

3          .0001                                       2.7184

4          .00001                                     2.7183

5          .000001                                   2.7183

6          .0000001                                 2.7183

 

hk = 10-(k+1), In this case let E = .00001, the we stop at k = 5

 

This iterative approach to obtain the derivative is slow.

There are alternatives. One is called Central Differences Formulas   

 

Central Differences Formulas of order O(h2)

 

-         start with Taylor expansion: f(x+h) = f(x) + f’(x)h

We want x= x0+h, therefore, x-x0 = h

 

(I) f(x+h) = f(x) + f’(x) h + f’’(x) h2 + f’’’(c1) h3  which f’’’(c1) h3  is the error

                                                         2!               3!                       3!

 

              

  f(x-h) = f(x) + f’(x) (-h) + f’’(x) (-h)2 + f’’’(c2) (-h)3

                                                               2!                    3!

 

            = f(x) - f’(x) (h) + f’’(x) (h)2 - f’’’(c2) (h)3  (II)

                                                               2!                    3!

 

Subs (II) from (I)

 

f(x+h) – f(x-h) = 2f’(x) (h) + f’’’(c1) h3  + f’’’(c2) (h)3

                                                                            3!                

 

Our objective is to find f’(x)

           

f’(x) = 1   [ f(x+h) – f(x-h) – [f’’’(c1)  + f’’’(c2)  ] h3 ]

          2h                                              3!

 

f’(x) = f(x+h) – f(x-h) - [f’’’(c1)  + f’’’(c2)  ] h

                   2h                             2.3!

 

Which [f’’’(c1)  + f’’’(c2)  ] his the error

                                   2.3!

 

So, f’(x) ≈ f(x+h) – f(x-h)

                          2h

 

Example: f(x) = ex and h=.0001

    f’(x) = e1+.0001 – e1-.0001 = 2.7183

                    2*(.0001)

 

Centered Formula of order O(h4)

 

Do Taylor Expansion:

 

- f(x+h) = f(x) + f’(x) h + f’’(x) h2 + f’’’(x) h3 + f’’’’(x) h4 + f’’’’’(c1) h5                                          

       2!               3!                 4!               5!

   Which f’’’’’(c1) h5 is the error.                                      

                      5!

 

 

- f(x+h) = f(x) + f’(x) (-h) + f’’(x) (-h)2 + f’’’(x) (-h)3 + f’’’’(x) (-h)4 + f’’’’’(c2) (-h)5                                         

                  2!               3!                     4!                       5!

   Which f’’’’’(c2) (-h)5 is the error.                                      

                       5!

 

 

 

So, (I) f(x+h) – f(x-h) = 2 f’(x) h + 2 f’’’(x) h3 + [ f’’’’’(c1) + f’’’’’(c2) ] (h)5                                                                  

                                                                3!                              5!       

Which [ f’’’’’(c1) + f’’’’’(c2) ] (h)5  is the error

                            5!

 

Using step size 2h instead of f. in (I), we get

 

     f(x+2h) – f(x-2h) = 2 f’(x) 2h + 2 f’’’(x) (2h)3 + [ f’’’’’(c1) + f’’’’’(c2) ] (2h)5                                                                  

                                                                3!                              5!

 

                           (II) = 4 f’(x) h + 16 f’’’(x) (h)3 + 32 [ f’’’’’(c1) + f’’’’’(c2) ] (h)5                                                                  

                                                                3!                                   5!  

 Which 32 [ f’’’’’(c1) + f’’’’’(c2) ] (h)5  is the error

                                                                        5!

 

Then multiple (I) by 8 and substract (II)

 

- 8(f(x+h) – f(x-h)) – (f(x+2h) – f(x-2h)) = 12 f’(x) h – 24 [ f’’’’’(c1) + f’’’’’(c2) ] h5                                    

                                                                                                             5!

 

So we have f’(x) = 8 f(x+h) – 8 f(x-h) + f(x-2h) – f(x+2h)

                                                       12h

 

                                + 24 [ f’’’’’(c1) + f’’’’’(c2) ] h5    1     

                                                            5!                    12h

 

        which       24 [ f’’’’’(c1) + f’’’’’(c2) ] h5       is the error O(h4)

                                                            5!                    12h

           

 

Let f(x) = sin(x); h=.001 centered formula O(h4); f’(x) = cos(x); f’(π/3) = cos(π/3) = .5

 

f’(π/3) ≈ -sin(π/3+.002) +8 sin (π/3+.001) – sin(π/3-.001) + sin(π/3-.002)

                                                          12*(.001)

            = .5

 

Using centered formula O(h2) previously

 

f’(x) ≈ sin(π/3+.001) - sin (π/3-.001)  = 4.99999917

                        2*(.001)

 

Using The Diff Quotient f’(x) ≈ sin(π/3+.001) - sin(π/3) = .499566904

                                                              (.001)

 

 

 

Numerical Integration

 

Trapezoidal Rule

 

It approximates the area under the curve with trapezoid

 

 

 

M = 3

 

A1 = (f0+f1).h ; A2 = (f2+f1).h ; A3 = (f2+f3).h ;

              2                       2                        2

 

A1 + A2 + A3 = h. [f0+f1+f2+f3 ]

2                          2

 

In general,

 b                                                                  m-1

∫  f(x) dx = h [ f(xo) + f(xm) + ∑ f(xk) ] for M+1 pts, xo, .., xm

 a                                           2                     K=1      

 

Example:

      1

1) ∫  e-x^2 dx    M = 4        

     -1                                                                     

h= b-a = 1-(-1) = .5

      m         4

 

 1

∫  e-x^2 dx  ≈ .5 [e-(-1)^2 + e-(-1)^2 + e-(-.5)^2 + e-(0)^2 + e-(.5)^2 ] = 1.4627

-1                                                 2

 

 

 

     π/2

2) ∫  sin(x) dx    M = 3

      0

h= b-a = π/2-(0) = π/6

      m         3

π/2

∫  sin(x) dx  ≈ π/6. [ sin(0) + sin(π/2) + sin(π/6) + sin(π/3) ] = .9770486

0                                                            2

 

Exact sol: 1

 

 

The Simpson Rule: it approximates the integral using theorem under a parabola every points.

 

The quadratic polynomial gives a better approximation then the line used in trapezoidal rule.

We use the Lagrange polynomial, we can obtain.

 

P2(x) = fo (x-x1)   (x-x2) + f1 (x-x0)   (x-x2) + f2 (x-x0)  (x-x1)

                 (x0-x1)(x0-x2)        (x1-x0)(x1-x2)        (x2-x0)(x2-x1)

 

x1-x0 = h ; x2-x0 = 2h ; x2-x1 = h …..  (1)

 

x2                       x2

∫  P2(x) dx = ∫  [ fo (x-x1)(x-x2) + f1 (x-x0)(x-x2) + f2 (x-x0)(x-x1) ] dx

x0                       x0            2h2                   (-h2)                   2h2 

 

We use substitution x = x0 + ht, therefore t = x - x0, dx = h.dt

                                                                          h       

t = x0 - x= 0 ; t = x2 - x= 2

        h                        h

 

x2                          2

∫  P2(x) dx = ∫  [ fo (x0 + ht - x1) (x0 + ht - x2) + f1 (x0 + ht - x0) (x0 + ht - x2)

x0                          0                         2h2                                                       h2                  

                        + f1 (x0 + ht - x0) (x0 + ht - x1) ] h dt

                                               2h2

                      2

      =  ∫  [ fo (x0 + ht - x1) (x0 + ht - x2) + f1 (ht) (x0 + ht - x2)

                      0                                          2h                               h

                        + f1 (ht) (x0 + ht - x1) ] dt

                                           2h

from (1):

                      2

                  =  ∫  [ fo (ht - h) (ht – 2h) + f1 (ht) (ht – 2h) + f1 (ht) (ht – h) ] dt

          0                          2h                               h                     2h

                                                                                            2   

      = fo (ht3 – 3ht2 + 2ht) - f(ht3 – 2ht2) + f2 (ht3-ht2)  |   

                         2   3        2                      3       2         2    3    2     0

                

      = h . (fo – 4 f1  + f2 )

                     3     

 

This equation is only for 3 pts. If interval [a,b] is subdivided into 2M intervals [xk, xk-1] of equal width, we can use the Simpson Rule every 3 pts.

 

Example: 2M = 6 ; we use 2M so that it is even

                  M = 3

 

M is the # of parabolas

So 2M intervals & 2M+1 pts and M parabolas

 

So A = A1 + A2 + A3 = h (fo + 4f1 + f2) + h (f2 + 4f3 + f4) + h (f4 + 4f5 + f6)

                                        3                          3                          3

         = h (fo + 4f1 + 2f2 + 4f3 + 2f4 + 4f5 + f6)

             3

 

In general:

A= h (fo + 4f1 + 2f2 + 4f3 + 2f4 + 4f5 + 2f6 + …. + 4f2M-1 + fM )

      3

 

Example:

 

π/2

∫  sin(x) dx    2M = 2, M = 1

0

h= b - a = π/2-(0) = π/4

      2M       2(1)

 

A = h (f0 + 4f1 + f2) = (π/4) (1/3) [0 + 4 (1/2) √2 + 1] = 1.002279878

       3              

 

Now try 2M = 4 ; M =2

 A = h (fo + 4f1 + 2f2 + 4f3 + f4) = (π/8) [ sin (0) + 4 sin  (π/8) + 2 sin (π/4) + 2 sin (3π/8) +

        3

                                                     sin(π/2) = 1.000134585

 

 

Numerical Optimization

 

-         Minimization of a function f(x). Let I = [a,b] an interval

-         Local maximum value at x = p in the interval I = [a,b], if f(x) ≤ f(p) for all x in I

-        

-         Local Minimum value at x = p in the interval I = [a,b], if f(x) ≥ f(p) for all x in I

-        

-         A function is increasing in interval I = [a,b], if x1 < x2 and f(x1) < f(x2) for all x1,x2 in I

-         If f’(x) > 0 for all x in I, then f(x) is increasing in the interval I

A function is decreasing in interval I = [a,b], if x1 < x2 and f(x1) > f(x2) for all x1,x2 in I

 

If f’(x) <0 for all x in I, then f(x) is decreasing in the interval I

If there is a max or min value at x = p, then f’(p) = 0

 

If f’’(p) < 0, f(p) is local min

If f’’(x) > 0, f(p) is local max

 

Minimization using derivative

  

Assume we want to minimize f(x) and it has a unique min at pm a<p<b. we can solve the function f’(x) = 0 using any methods for non-linear equation – Newton, Secant, Bisection, Regula-False. f’(x) can be computed numerically, using the centered

 

The Golden Ratio

 

Assume p(x) is unimodal (one minimum) in the interval [a,b], we divide the interval into 3 sub intervals a < c < d < b

 

Then we evaluate f(x) at c and d

If f(c) < f(d), then the new interval to search will be reduced to [a,d]

 

 

 

 

 

If( f(c) > f(d), then the new interval to search will be [c,b]

c & d would be any 2 values such that a < c < d < b, we choose c &d to such a way that we reduce the number computations done.

 

We choose c, d such that c = a +(1-γ) (b-a)

                                         d = a + γ (b-a)

By using golden ratio to choose c, d. we reduce the number of operations that we need to do.

 

If the new interval becomes [a,d] then the odd d will be the new b, and the old c becomes new d. The new c will have to be recomputed as well as f(c).

If the new interval becomes [c,b] then old c will be new a, and the old d becomes new c. The new d will be have to be recomputed.

 

So, at every iteration, we need to recomputed only 1 value either f(c) or f(d) instead of 2 values.

 

Example:

 

f(x) = x2 +1 [-2,1]

 

i =1; a = -2; b =1

c = a +(1-γ) (b-a)                                                         d = a + γ (b-a)

   = -2 + (1 - .01803) (1-(-2))                                         = -2 + .61803 (3)

   = -.8541                                                                      = -.14380    

 

f(a1) = 5    f(b1) = f(1) = 2

f(c1) = f(-.8541) = 1.72948

f(d1) = f(-.14589) = 1.02188

 

f(c) > f(d)

Now interval is [-.8541, 1]

a = -.8541; b = 1; c = a + (1-γ) (b-a) = -.8341 + (1-.61803) (1-(-.8541)) = -.14164

d = a + γ (b-a) = -.8541 + .61803 (1-(-.8541)) = .291789

 

f(a2) = 1.72949; f(b2) = f(1) = 2; f(c2) = f(-.14589) = 1.02188

f(d2) = f(.291789) = 1.08514

Since f(c) < f(d); the minimum is the left side of d.

Now, a3 = a2 and b3 = d2

Look in the interval [a2,d2]

 

c = 3       

a3 = a2 = -.8541; b3 = d2 = .291789; c2 = a + (1-γ) (b-a) = -.4164

d3 = c2 = -.14584

 

f(a3) = f(a2) = f(-.8541) = 1.72949

f(b3) = f(d2) = f(.291789) 1.08514

f(c3) = f(-.4164) = 1.17339

 

Since f(c3) < f(d3); a4 = c3 and b4 = b3. Now interval [-.1464, .291]

 

The Gradient Method or Steepset Desant

 

-         Assume we want to minimize f(x) of N variables where x = (x1, x2, x3, …, xn)

-         The gradient f(x) is a vector function defined as

 

∆f(x) = ( df(x), df(x), …, df(x) )

               dx1    dx2          dxn

 

From the concept of gradient, we know that the gradient vector points in direction of greatest increase of f(x)

Then -∆f(x) will point to the direction of the greatest decrease of f(x)

 

To obtain the minimum using the gradient method, starting point p0 we move along the line in the direction of greatest decrease, that is -∆f(x).

 

In the simplest form, p1 = p0 – Go .h

Where h = a small constant < 1

            G is the gradient at P0

 

Pk+1 = Pk – Gk. h; Gk = -∆f(xk)

 

Example: y = x2 +1 and y’ = 2x, gradient = G = 2x

Let h = .1

Start at

x0 = -1, G0 = 2(-1) = -2

x1 = x0 - G0.h; G1 = 2(-.8) = -1.6

     = -1 - (-2)(.1) = -.8

x2 = x1 - G1. h = (-.8) - (-1.6)(.1) = -.64, G2 = 2(-.64) = -1.281

x3 = x2 - G2. h = (-.64) - (-1.281)(.1) = -.512, G3 = 2(-.512) = -

 

x4 = x3 - G3. h = -.4096

x5 = x4 - G4. h = -.32768

 

Numerical Method for Diff Equations

 

Consider the diff equation

dy = ky

dt

 

dy = k.dt

y

 

dy = ∫ k.dt

   y

 

ln(y) = k1t + k2

Some diff equation does not have an analytical solution, so they have to be approximated using numerical methods.

 

1st method: Euler’s Method

let [a,b] be the interval over which we want to find the solution y’ = f(t,y)