ME 361-Differentiation

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

ME – 361 NUMERICAL METHODS FOR ENGINEERS

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

Numerical Differentiation

Given a complicated f(x) or a set of tabulated values, xi’s and corresponding


f(xi)’s:

Question: For a given x, in the table or not in the table

df d 2f
=? , 2
=? , etc.
dx dx

Answer: Replace f(x), or the tabulated data, with a simple function g(x).
Then, operate on g(x) instead

df dg d 2f d 2g
f (x)  g (x) ,  , 2
 , etc.
dx dx dx d x2

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

What is this simple fuction, g(x)?

Truncated Taylor series g(x) = f (x0) + (x – x0) f ’(x0) + ...


passes throught x0 only

Least Squares Data should have random statistical errors

Newton-Gregory Polynomials Forward, Backward, Central ...


Divided difference table

Cubic Splines gk(x) = ak (x – xi)3 + bk (x – xi)2 + ck (x – xi) + dk


passes through all the points with equal first
and second derivatives at the junctions

Something else Ratio of polynomials (Padé approximation), sine and


cosine functions (Fourier series), Hermite,
Chebyshev, others
Prof. Dr. Faruk Arınç Fall 2020
ME – 361 NUMERICAL METHODS FOR ENGINEERS

x - x0
Newton-Gregory forward polynomial s where h is constant
h
s (s - 1) 2 s (s - 1) (s - 2) 3
f(s)  Pn (s) = f0 + s Δf0 + Δ f0 + Δ f0 + ...
2! 3!

dPn (x) 1  1 1 
f '(s)  = f + (2 s - 1) Δ 2
f + . (....) Δ 3
f + ...
h  
0 0 0
dx 2 6 

If x = x0, f ‘(x0) = ?

1 f -f
Use one term: f '(x 0 )  f0 = 1 0
h h

1  1 2  1
Use two terms: f '(x)  f + (2 s - 1) Δ f =  - f2 + 4 f1 - 3 f0 
h 
0 0
2  2h

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

1
f1 - f 0 
-h
f ' (x 0 )  E '(x 0 )  f ''( ) One-term forward
h 2

- h2 '''
f ' (x 0 ) 
1
f1 - f -1  E ' (x 0 )  f ( ) One-term central
2h 6

f ' (x 0 ) 
1
- f 2  4 f1 - 3 f 0  h2 ''' Two-term forward
E ' (x 0 )  f ()
2h 3

- h 4 (5)
f ' (x 0 ) 
1
- f 2  8 f1 - 8 f - 1  f - 2  E ' (x 0 )  f ( ) Two-term central
12 h 30

f ' (x 0 ) 
1
2 f 3  9 f 2  18 f1  11 f 0  O(h3) Three-term forward
6h

Note that, using central differences, we get better accuracy with two-terms than
we do with four terms using forward differences.
Prof. Dr. Faruk Arınç Fall 2020
ME – 361 NUMERICAL METHODS FOR ENGINEERS

1
f ''(x 0 )   f 2 - 2 f1 + f0  E''(x0 )  h f '''( ) Two-term forward
h2

1 - h 2 (4)
f ''(x 0 )  2  f1 - 2f0 + f-1  E''(x 0 )  f ( ) Two-term central
h 12

1
f ''(x 0 )   - f3  4 f 2 - 5 f1 + 2 f 0  O(h 2 ) Three-term forward
h2

1
f ''(x 0 )   - f2  16 f1 - 30 f 0  16 f - 1 - f*2  O(h 4 ) Three-term central
12 h 2

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

Error estimates for x - x0


s where h is constant
Newton-Gregory forward polynomial h

n
f (n+1) ( ) n
f (n+1) ( )
f(x) = Pn (x) +  x - x  i
n+1 !
E(x) = f(x) - Pn (x) =  x - x 
i 0
i
n+1 !
i 0

d  n f (n+1) ( ) 
E'(x) = f '(x) - Pn (x) =    x - x i  
dx  i 0 n+1 ! 
d  n  f (n+1) ( ) d  f (n+1) ( )  n
=   x - x i   n+1! + dx  n+1! 
dx  i 0
 x - x  i
   i 0

d  n  f (n+1) ( ) f (n+2) ( ) n
E'(x) =    x - xi   +  x - x 
 n+1 ! n+2 ! i
dx  i 0 i 0

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

d  n  f (n+1) ( ) f (n+2) ( ) n
E'(x) =    x - xi   +  x - x 
 n+1 ! n+2 ! i
dx  i 0 i 0

If x = xj, one of the tabulated values, the second term becomes zero,
and the first term is reduced to one product of (xj – xi)’s:

f (n+1) ( )
 x - xi 
n
E'(x j ) =
i 0
j
n+1 !
i j

For example, for the one-term forward formula, when xj = x0 is

f ' ' ( ) - h ''


E'(x 0 ) =  x 0 - x 1  = f ( )
2! 2

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

Difference Table for f(x) = e-x


x f (x) Δf Δ2 f Δ3 f Δ4 f
0 1
- 0.632120559
1 0.367879441 0.399576401
- 0.232544158 - 0.252580458
2 0.135335283 0.146995943 0.1596613
- 0.085548215 - 0.092919158
3 0.049787068 0.054076785 0.05873611
- 0.031471429 - 0.034183048
4 0.018315639 0.019893738 0.021607807
- 0.011577692 - 0.012575241
5 0.006737947 0.007318497
- 0.004259195
6 0.002478752
Prof. Dr. Faruk Arınç Fall 2020
ME – 361 NUMERICAL METHODS FOR ENGINEERS

Example: Using Newton-Gregory forward interpolating polynomial, calculate f’(x0)


at x0 = 2.0 for the function given in the difference table and estimate
the error.

Exact solution: f ' (2) = - e 2 = - 0.1353353

dP 1   2 f0  3 f0  4 f0 
f (x) 
'
=  f0 - + - + ... 
dx h  2 3 4 

N-G Forward 1 term 2 terms 3 terms 4 terms


Polynomial
P’(2) - 0.085 - 0.112 - 0.123 - 0.127

True % Error 37 % 17 % 9% 6%

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

Error estimation

h '' h   2 f0   2 f0
One-term forward: '
E (x 0 ) = - f ( )  -  2  =-
2 2  h  2h

 2 f0 0.054
E'est (2) =- =- = - 0.027
2h 2

'
True error: Etrue (2) = f ' (2) - P' (2) = - 0.135 - (- 0.085) = - 0.05

Note that, with our definition of differences:

 1
f ( n ) ( )  nf0  n  f ( n ) ( )  Dn f0 n!
h 
Prof. Dr. Faruk Arınç Fall 2020
ME – 361 NUMERICAL METHODS FOR ENGINEERS

Example: Consider the data points

i 1 2 3 4 5
xi -2 -1 0 1 2
yi 4 -1 2 1 8

a) Fit a fourth-degree polynomial, P4(x), in Lagrange form, passing through all


the given data points;

b) Interpolate y(x) at x = 0.5 and estimate the error;

c) Find the first derivative y’(x) at x = 1 and estimate the error;

d) Compare answers in (b) and (c) with those of the previous example where a
set of cubic splines is fitted to the same data points;

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

Solution: (a) Fourth-degree polynomial in Lagrange form:

(x-x1) (x-x 2 ) (x-x 3 ) (x-x 4 ) (x-x 0 ) (x-x 2 ) (x-x 3 ) (x-x 4 )


P4 (x) = y(x 0 ) + y(x1) +
(x 0 -x1) (x 0 -x 2 ) (x 0 -x 3 ) (x 0 -x 4 ) (x1-x 0 ) (x1-x 2 ) (x1-x 3 ) (x1-x 4 )

(x-x 0 ) (x-x1) (x-x 3 ) (x-x 4 ) (x-x 0 ) (x-x1) (x-x 2 ) (x-x 4 )


y(x 2 ) + y(x 3 ) +
(x 2 -x 0 ) (x 2 -x1) (x 2 -x 3 ) (x 2 -x 4 ) (x 3 -x 0 ) (x 3 -x1) (x 3 -x 2 ) (x 3 -x 4 )

(x-x 0 ) (x-x1) (x-x 2 ) (x-x 3 )


y(x 4 )
(x 4 -x 0 ) (x 4 -x1) (x 4 -x 2 ) (x 4 -x 3 )

P4 (x) = 0.16666667 (x+1) (x) (x-1) (x-2) + 0.16666667 (x+2) (x) (x-1) (x-2) +

0.5 (x+2) (x+1) (x-1) (x-2) - 0.16666667 (x+2) (x+1) (x) (x-2) +

0.33333333 (x+2) (x+1) (x) (x-1)

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

This is the fourth-degree polynomial passing through all the given data points.
The plot is given below.

8.0

6.0

4.0

2.0

0.0

-2.0
-2.0 -1.0 0.0 1.0 2.0

Compare this with the plot of the cubic splines.

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

Plots of the data and cubic splines

8.0

6.0

4.0

2.0

0.0

-2.0
-2.0 -1.0 0.0 1.0 2.0

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

The divided difference table for the given data is:

xi yi Dyi D2yi D3yi D4yi


-2.00 4.00
-5.00
-1.00 -1.00 4.00
3.00 -2.00
0.00 2.00 -2.00 1.00
-1.00 2.00
1.00 1.00 4.00
7.00
2.00 8.00

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

Note that the same fourth-degree interpolating polynomial can be found using
the divided difference table and the formula

Pn (x) = f (xi ) + (x - xi ) Dfi + (x - x i ) (x - xi+1 ) D2 fi + ... + (x - x i ) (x - x i+1) ... (x - xi+n-1 ) Dn fi

P4 (x)  4 - 5 (x + 2) + 4 (x + 2) (x + 1) D2fi - 2 (x + 2) (x + 1) x + (x + 2) (x + 1) x (x - 1)

The error estimator for the Newton-Gregory interpolating polynomial is

f (n1) ( )
n
E (x)  f (x) - Pn (x)   (x - x i )
i 0 (n  1) !

The error estimator for the first derivative at a tabulated data point xj is:

n
f (n1) ( )
E (x j )  f (x j ) -
' '
Pn' (x j )   (x j - xi )
i0 (n  1) !
ij

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

Lagrange form:

P4 (0.5) = 0.16666667 (0.5 + 1) (0.5) (0.5 - 1) (0.5 - 2) +


0.16666667 (0.5 + 2) (0.5) (0.5 - 1) (0.5 - 2) +
0.5 (0.5 + 2) (0.5 + 1) (0.5 - 1) (0.5 - 2) -
0.16666667 (0.5 + 2) (0.5 + 1) (0.5) (0.5 - 2) +
0.33333333 (0.5 + 2) (0.5 + 1) (0.5) (0.5 - 1)

P4(0.5) = 1.812500

Using divided differences:

P4 (x)  f(x0 )  (x - x0 ) Df0  (x - x 0 ) (x - x1) D2 f0  (x - x0 ) (x - x1) (x - x 2 ) D3f0


+ (x - x0 ) (x - x1) (x - x 2 ) (x - x 3 ) D 4f0

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

P4 (x)  4.0 - 5.0 (x - x0 ) + 4.0 (x - x0 ) (x - x1) - 2.0 (x - x0 ) (x - x1) (x - x 2 )


+ (x - x0 ) (x - x1) (x - x2 ) (x - x3 )

P4 (0.5)  4.0 - 5.0 (0.5 + 2)  4.0 (0.5 + 2) (x + 1) - 2.0 (x + 2) (x + 1) (x)


+ (0.5 + 2) (0.5 + 1) (0.5) (0.5 - 1)

P4(0.5) = 1.812500

The same answer is obtained because they are the same polynomial.

The cubic spline answer was 1.392857.

4
f (5) ( )
Error Calculation: E (x)  f (x) - P4 (x)   (x - xi )
i 0 5!
f (5) ( )
= (x - x0 ) (x - x1 ) (x - x2 ) (x - x3 ) (x - x 4 )
5!
Prof. Dr. Faruk Arınç Fall 2020
ME – 361 NUMERICAL METHODS FOR ENGINEERS

f (5) ( )
The fifth derivative is to be replaced by the fifth divided difference:  D5 f
5!
E (x)  (x - x0 ) (x - x1 ) (x - x 2 ) (x - x 3 ) (x - x 4 ) D5 f

But, the fifth divided difference does not exist. From the trend in the divided
difference table, assume that D5f = 0.5. Then:

E (0.5)  (0.5 + 2) (0.5 + 1) (0.5) (0.5 - 1) (0.5 - 2) (0.5) = 0.703125

The error estimate is almost 40 %. It was about 20% with cubic splines.

(c) One may use either the polynomial in Lagrange form or the polynomial
obtained from the divided difference table. It seems that it is easier to take
the first derivative of the later polynomial. Let’s do that:
Prof. Dr. Faruk Arınç Fall 2020
ME – 361 NUMERICAL METHODS FOR ENGINEERS

P4 (x)  4.0 - 5.0 (x - x0 ) + 4.0 (x - x0 ) (x - x1) - 2.0 (x - x0 ) (x - x1) (x - x 2 )


+ (x - x0 ) (x - x1) (x - x2 ) (x - x3 )

P4' (x)  - 5.0 + 4.0 (x - x0 ) + (x - x1)


- 2.0 (x - x1) (x - x2 ) + (x - x0 ) (x - x2 ) + (x - x0 ) (x - x1)
+ (x - x1) (x - x2 ) (x - x3 ) + (x - x0 ) (x - x2 ) (x - x3 )
+ (x - x0 ) (x - x1) (x - x3 ) + (x - x0 ) (x - x1) (x - x2 )

Prof. Dr. Faruk Arınç Fall 2020


ME – 361 NUMERICAL METHODS FOR ENGINEERS

Prof. Dr. Faruk Arınç Fall 2020

You might also like