4.taylor Expansion
4.taylor Expansion
4.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 " ∂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 𝑑𝑥 %
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
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
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
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
This is central difference of first-order derivative that is also calculated by central difference.
Quotient for the mixed derivative
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
( )=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