4.taylor Expansion

You might also like

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

Taylor expansion

and
finite-difference approximations

Numerical Fluid
analysis Mechanics

CFD

Computer
Science
Finite-difference method
The basic philosophy of finite difference method is to replace the
derivatives of the governing equations with algebraic difference quotients.

This will result in a system of algebraic equations which can be solved for
the dependent variables at the discrete grid points in the flow field.
Taylor expansion
" ∂u % " ∂2 u % ( Δx ) 2 " ∂3u % ( Δx )3 " ∂4 u % ( Δx ) 4
ui+1, j = ui, j + $ ' Δx + $ 2 ' +$ 3 ' +$ 4 ' +....
# ∂x &i, j # ∂x &i, j 2 # ∂x &i, j 3! # ∂x &i, j 4!
" ∂u % " ∂2 u % ( Δx ) 2 " ∂3u % ( Δx )3 " ∂4 u % ( Δx ) 4
ui−1, j = ui, j − $ ' Δx + $ 2 ' −$ 3 ' +$ 4 ' +....
# ∂x &i, j # ∂x &i, j 2 # ∂x &i, j 3! # ∂x &i, j 4!

" ∂u % " ∂3u % ( Δx )3 5
ui+1, j − ui−1, j = 2 $ ' Δx + 2 $ 3 ' + O ( Δx )
# ∂x &i, j # ∂x &i, j 3!
FD approximation and error term
# ∂u & # ∂3u & ( Δx )3 5
ui+1, j − ui−1, j = 2 % ( Δx + 2 % 3 ( + O ( Δx )
$ ∂x 'i, j $ ∂x 'i, j 3!
2
# ∂u & ui+1, j − ui−1, j # ∂3u & ( Δx ) 4
→% ( = −% 3 ( + O ( Δx )
$ ∂x 'i, j 2Δx $ ∂x 'i, j 3!
2
# ∂u & ui+1, j − ui−1, j # ∂3u & ( Δx )
→% ( ≈ −% 3 (
$ ∂x 'i, j 2Δx $ ∂x 'i, j 3!

We therefore state that
u − ui−1, j # ∂u &
(1) i+1, j (central differencing) is a second-order approximation to % (
2Δx $ ∂x 'i, j
# ∂3u & ( Δx ) 2
(2) the leading order error term is − % 3 ( .
$ ∂x 'i, j 3!
ui+1, j − ui−1, j # δu & # ∂u &
(3) We shall write as % ( (shorthand notation), as one of the FD approximation to % (
2Δx $ δ x 'i, j $ ∂x 'i, j
# δu &
(4) There are many possible approximations, so many ways to write the details of % (
$ δ x 'i, j
# δu & ui+2, j − ui−2, j
e.g., % ( = is another possibility of second-order accuracy
$ δ x 'i, j 4Δx
More quotients for the first-order derivative

" ∂u % " ∂2 u % ( Δx ) 2 " ∂3u % ( Δx )3 " ∂4 u % ( Δx ) 4


ui+1, j = ui, j + $ ' Δx + $ 2 ' +$ 3 ' +$ 4 ' +....
# ∂x &i, j # ∂x &i, j 2 # ∂x &i, j 3! # ∂x &i, j 4!
" ∂u % ui+1, j − ui, j " ∂2 u % Δx " ∂u % u − ui, j
$ ' = −$ 2 ' → $ ' = i+1, j + O ( Δx ) First-order forward
# ∂x &i, j Δx # ∂x &i, j 2 # ∂x &i, j Δx

" ∂u % ui, j − ui−1, j


$ ' = + O ( Δx ) First-order backward
# ∂x &i, j Δx

δu " ∂u %
All the above are finite-difference quotients of the first order derivative $ ' :
δx # ∂x &i, j
ui+1, j − ui, j ui, j − ui−1, j ui+1, j − ui−1, j ui+2, j − ui−2, j
, , ,
Δx Δx 2Δx 4Δx
Error in the finite-difference approximation Programming topic:
Compare and plot the errors
𝐴𝑠𝑠𝑢𝑚𝑒 𝑢 = 𝑢 𝑥

Second-order scheme
𝑑𝑢 𝑢!"# − 𝑢!$# 1 𝑑 % 𝑢 &
= − Δ𝑥
𝑑𝑥 2Δ𝑥 6 𝑑𝑥 %

Application and verification:


𝑢 = exp −𝑥 & 𝑎𝑡 𝑥! = 1.0 𝑤𝑖𝑡ℎ Δ𝑥 = 0.2, 0.1, 0.05, 0.025, 0.0125

Exact value:
𝑑𝑢
= −2𝑥! exp −𝑥!& = −2 exp −1 = −0.735 758 882 343 …
𝑑𝑥
Finite difference approximation:
𝑑𝑢 𝑢!"# − 𝑢!"# exp − 1 + Δ𝑥 & − exp − 1 − Δ𝑥 &
≈ =
𝑑𝑥 2Δ𝑥 2Δ𝑥
Actual error
𝑢!"# − 𝑢!"#
𝑎𝑐𝑡𝑢𝑎𝑙 𝑒𝑟𝑟𝑜𝑟 ≈ − −2 exp −1
2Δ𝑥
Estimated error based on the leading-order term in the Taylor expansion
1 𝑑% 𝑢 1
𝑒𝑠𝑡𝑖𝑚𝑎𝑡𝑒𝑑 𝑒𝑟𝑟𝑜𝑟 ≈ %
Δ𝑥 &
= −8𝑥!% + 12𝑥! exp −𝑥!& Δ𝑥 &
6 𝑑𝑥 6
A code: FD_error.f90
real*8, dimension(5) :: error1, error2, dx
real*8, dimension(5) :: xL, xR
real*8 x,uexact
integer k
x = 1.d0 ! doubkle precision
uexact = -2.d0*x*dexp(-x*x) ! double precision exp function

! dx = (/0.2,0.1,0.05,0.025,0.0125/) ! Single precision demo


dx = (/0.2d0,0.1d0,0.05d0,0.025d0,0.0125d0/) ! Double precision demo

xL = x - dx
xR = x + dx
error1 = (dexp(-xR*xR) - dexp(-xL*xL))/2.d0/dx - uexact
error2 = (-8.d0*x**3 + 12.d0*x)*dexp(-x*x)/6.0*dx**2
do k=1,5
write(11,100) dx(k), error1(k), error2(k)
end do
100 format(2x,3(1PE24.15) ) Double precision is good up to 14 digits
end
A code: FD_error.f90
real*8, dimension(5) :: error1, error2, dx
real*8, dimension(5) :: xL, xR
real*8 x,uexact
integer k
x = 1.d0 ! doubkle precision
uexact = -2.d0*x*dexp(-x*x) ! double precision exp function

dx = (/0.2,0.1,0.05,0.025,0.0125/) ! Single precision demo


! dx = (/0.2d0,0.1d0,0.05d0,0.025d0,0.0125d0/) ! Double precision demo

xL = x - dx
xR = x + dx
error1 = (dexp(-xR*xR) - dexp(-xL*xL))/2.d0/dx - uexact
error2 = (-8.d0*x**3 + 12.d0*x)*dexp(-x*x)/6.0*dx**2
do k=1,5
write(11,100) dx(k), error1(k), error2(k)
end do
100 format(2x,3(1PE24.15) )
Single precision is good up to 7 digits
end
The error plot

Plotted by demo5.ncl

Observations:
(1) The actual error is very close to the estimated error
(2) A slope of 2 is observed à expected for second-order accuracy
Fortran functions: partial list

R = floating point values in single precision


D = floating point values in double precision
I = integers
C = complex floating point value

https://www.nsc.liu.se/~boein/f77to90/a5.html
Fortran functions: more

https://www.nsc.liu.se/~boein/f77to90/a5.html
Quotient for the second-order derivative
" ∂u % " ∂2 u % ( Δx ) 2 " ∂3u % ( Δx )3 " ∂4 u % ( Δx ) 4
ui+1, j = ui, j + $ ' Δx + $ 2 ' +$ 3 ' +$ 4 ' +....
# ∂x &i, j # ∂x &i, j 2 # ∂x &i, j 3! # ∂x &i, j 4!
" ∂u % " ∂2 u % ( Δx ) 2 " ∂3u % ( Δx )3 " ∂4 u % ( Δx ) 4
ui−1, j = ui, j − $ ' Δx + $ 2 ' −$ 3 ' +$ 4 ' +....
# ∂x &i, j # ∂x &i, j 2 # ∂x &i, j 3! # ∂x &i, j 4!
We can show easily that
ui+1, j − 2ui, j + ui−1, j " ∂2 u % " ∂4 u % ( Δx ) 2
2
= $ 2 ' + 2$ 4 '
( Δx ) # ∂x &i, j # ∂x &i, j 4!

ui+1, j − 2ui, j + ui−1, j " ∂2 u %
2
is second-order approximation to $ 2 '
( Δx ) # ∂x &i, j

(u − ui, j ) (u − ui−1, j ) " δu % " δu %


i+1, j

i, j $ ' −$ '
ui+1, j − 2ui, j + ui−1, j Δx Δx # δ x &i+0.5, j # δ x &i−0.5, j
2
= =
( Δx ) ( Δx ) ( Δx )

This is central difference of first-order derivative that is also calculated by central difference.
Quotient for the mixed derivative

" ∂2 u % ∂ " ∂u % ∂ " ui, j+1 − ui, j−1 %


$ ' = $ '= $ + O ( Δy 2 ) '
# ∂x∂y &i, j ∂x # ∂y & ∂x # 2Δy &
∂ " ui, j+1 − ui, j−1 %
= $
∂x # 2Δy
+ O ( )'&
Δy 2

1 *" ∂u % " ∂u % -
= ,$ ' − $ ' / + ( error term )
2Δy +# ∂x &i, j+1 # ∂x &i, j−1 .
ui+1, j+1 − ui−1, j+1 − ui+1, j−1 + ui−1, j−1
= + ( error term )
4ΔxΔy

Note the two-dimensional Taylor expansion


𝑢 𝑥 + 𝑑𝑥, 𝑦 + 𝑑𝑦
& %
𝜕 𝜕 1 𝜕 𝜕 1 𝜕 𝜕
= 𝑢 𝑥, 𝑦 + 𝑑𝑥 + 𝑑𝑦 𝑢 𝑥, 𝑦 + 𝑑𝑥 + 𝑑𝑦 𝑢 𝑥, 𝑦 + 𝑑𝑥 + 𝑑𝑦 𝑢 𝑥, 𝑦 + ⋯
𝜕𝑥 𝜕𝑦 2! 𝜕𝑥 𝜕𝑦 3! 𝜕𝑥 𝜕𝑦

Homework: derive the expression for the leading-order error terms.


Schemes for time advancement: 1st order

( )=F
du t
dt
(u (t ) ,t )
Forward Euler Scheme
( ) ( )=F
u t p+1 − u t p Δ𝑡 𝑑 !𝑢
(u (t ) ,t ) Error = −
dt p p 2 𝑑𝑡 !

u p+1 − u p
dt (
= F u p ,t p ) Explicit
What is the truncation error?
Backward Euler Scheme
( ) ( )=F
u t p+1 − u t p
dt
(u (t ) ,t )
p+1 p+1

u p+1 − u p
dt (
= F u p+1 ,t p+1 ) Implicit
Schemes for time advancement: 2nd order

( )=F
du t
dt
(u,t )

Adams-Bashforth, Explicit
( ) ( ) = 3F
u t p+1 − u t p
5 Δ𝑡 ! 𝑑"𝑢
dt 2
(u (t ) ,t ) − 12 F (u (t ) ,t )
p p p−1 p−1 Error term = −
12 𝑑𝑡 "
What is the truncation error?
Crank-Nicolson, Implicit
( ) ( )= 1F
u t p+1 − u t p
dt 2
(u (t ) ,t ) + 12 F (u (t ) ,t )
p p p+1 p+1
Notes

CFD deals with second-order PDE of different types

There are many possible approximations to a term: the error


can be derived from Taylor expansions.

Taylor expansions can be used creatively and frequently to understand many


different aspects of numerical methods

You might also like