18. finite difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18....

20
18. Finite Difference

Upload: others

Post on 16-Jun-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

18. Finite Difference

Page 2: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

• Consider function 𝑓 defined on uniform grids 𝑥𝑗 with equal spacing:

Finite-difference approximation on uniform grids

• Define difference operator:

𝐷𝑓 𝑥𝑗 = 𝑎−𝑓 𝑥𝑗−1 + 𝑎0𝑓 𝑥𝑗 + 𝑎+𝑓 𝑥𝑗+1 ≡ 𝑎−𝑓𝑗−1 + 𝑎0𝑓𝑗 + 𝑎+𝑓𝑗+1,

which approximates the derivative of 𝑓 at 𝑥 = 𝑥𝑗, i.e., 𝑓𝑥(𝑥𝑗) ≅ 𝐷𝑓(𝑥𝑗).

𝑓 𝑥𝑗−1 = 𝑓 𝑥𝑗 − ∆𝑥𝑓𝑥 𝑥𝑗 +1

2∆𝑥2𝑓𝑥𝑥 𝑥𝑗 −

1

6∆𝑥3𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

𝑓 𝑥𝑗+1 = 𝑓 𝑥𝑗 + ∆𝑥𝑓𝑥 𝑥𝑗 +1

2∆𝑥2𝑓𝑥𝑥 𝑥𝑗 +

1

6∆𝑥3𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

• Represent 𝑓 𝑥𝑗−1 and 𝑓 𝑥𝑗+1 by Taylor series:

• Determine 𝑎− , 𝑎0 , 𝑎+ such that 𝐷𝑓 𝑥𝑗 ≅ 𝑓𝑥 𝑥𝑗

𝐷𝑓 𝑥𝑗 = 𝑎− + 𝑎0 + 𝑎+ 𝑓 𝑥𝑗 + ∆𝑥 −𝑎− + 𝑎+ 𝑓𝑥 𝑥𝑗 +1

2∆𝑥2 𝑎− + 𝑎+ 𝑓𝑥𝑥 𝑥𝑗

+1

6∆𝑥3 −𝑎− + 𝑎+ 𝑓𝑥𝑥𝑥 𝑥𝑗 + 𝑂 ∆𝑥4

i.e. 𝑓𝑥 𝑥𝑗 ≅ 𝐷𝑓 𝑥𝑗 =−1

2∆𝑥𝑓 𝑥𝑗−1 +

1

2∆𝑥𝑓 𝑥𝑗+1

=1

2∆𝑥(𝑓𝑗+1 − 𝑓𝑗−1)

The truncation error of 𝐷𝑓 𝑥𝑗 is 1

6∆𝑥2𝑓𝑥𝑥𝑥 𝑥𝑗

𝑎− + 𝑎0 + 𝑎+ = 0

−𝑎− + 𝑎+ =1

∆𝑥

𝑎− + 𝑎+ = 0

𝑎0 = 0

𝑎+ =1

2∆𝑥

𝑎− = −1

2∆𝑥

𝑥𝑗 𝑥𝑗+1 𝑥𝑗−1

𝑓𝑗 𝑓𝑗+1

𝑓𝑗−1

Page 3: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

• Similarly, determine 𝑏−, 𝑏0, 𝑏+ such that 𝐷2𝑓 𝑥𝑗 = 𝑏−𝑓 𝑥𝑗−1 + 𝑏0𝑓 𝑥𝑗 + 𝑏+𝑓 𝑥𝑗+1 ≅ 𝑓𝑥𝑥 𝑥𝑗

⟹ 𝑏+ = 𝑏− =

1

∆𝑥2

i.e. 𝑓𝑥𝑥 𝑥𝑗 ≅ 𝐷2𝑓 𝑥𝑗 =1

∆𝑥2𝑓 𝑥𝑗−1 +

−2

∆𝑥2𝑓 𝑥𝑗 +

1

∆𝑥2𝑓 𝑥𝑗+1 =

1

∆𝑥2𝑓𝑗+1 − 2𝑓𝑗 + 𝑓𝑗−1

The truncation error of 𝐷2𝑓 𝑥𝑗 is 𝑂 ∆𝑥2

𝑏 = −2

∆𝑥2

𝑏− + 𝑏0 + 𝑏+ = 0

−𝑏− + 𝑏+ = 0

𝑏− + 𝑏+ =2

∆𝑥2

• Alternatively,

𝑓𝑥𝑥 𝑥𝑗 =𝑑

𝑑𝑥

𝑑𝑓

𝑑𝑥𝑥=𝑥𝑗

≅ 𝐷 𝐷𝑓 𝑥𝑗 =1

Δ𝑥𝐷𝑓 𝑥

𝑗+12

− 𝐷𝑓 𝑥𝑗−

12

=1

Δ𝑥

𝑓 𝑥𝑗+1 − 𝑓(𝑥𝑗)

Δ𝑥−

𝑓 𝑥𝑗 − 𝑓(𝑥𝑗−1)

Δ𝑥=

1

∆𝑥2𝑓𝑗+1 − 2𝑓𝑗 + 𝑓𝑗−1

𝑥𝑗 𝑥𝑗+1 𝑥𝑗−1

𝑥𝑗−

12

𝑥𝑗+

12

Page 4: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

• Two approaches to approximate the derivatives:

A. Discretization in physical domain – difference operator adapted

B. Discretization in transformed domain – equation transformed

Finite difference on non-uniform grids

• Now, consider function 𝑓 defined on non-uniform grids 𝑥𝑗 with non-equal spacing:

𝑥𝑗 𝑥𝑗+1 𝑥𝑗−1

𝑓𝑗 𝑓𝑗+1 𝑓𝑗−1

Page 5: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

A. Discretization in physical domain

Again, define the difference operator as: 𝐷𝑓 𝑥𝑗 = 𝑎−𝑓 𝑥𝑗−1 + 𝑎0𝑓 𝑥𝑗 + 𝑎+𝑓 𝑥𝑗+1

𝑓 𝑥𝑗−1 = 𝑓 𝑥𝑗 − ∆𝑥−𝑓𝑥 𝑥𝑗 +1

2∆𝑥−

2𝑓𝑥𝑥 𝑥𝑗 −1

6∆𝑥−

3𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

𝑓 𝑥𝑗+1 = 𝑓 𝑥𝑗 + ∆𝑥+𝑓𝑥 𝑥𝑗 +1

2∆𝑥+

2𝑓𝑥𝑥 𝑥𝑗 +1

6∆𝑥+

3𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

𝑥𝑗 𝑥𝑗+1 𝑥𝑗−1

∆𝑥− ∆𝑥+

𝐷𝑓 𝑥𝑗 = 𝑎− + 𝑎0 + 𝑎+ 𝑓 𝑥𝑗 + (−∆𝑥−𝑎− + ∆𝑥+𝑎+)𝑓𝑥 𝑥𝑗

+1

2(∆𝑥−

2𝑎− + ∆𝑥+2𝑎+)𝑓𝑥𝑥 𝑥𝑗 +

1

6−∆𝑥−

3𝑎− + ∆𝑥+3𝑎+ 𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

∴ 𝑓𝑥 𝑥𝑗 = 𝑎−𝑓𝑗−1 + 𝑎0𝑓𝑗 + 𝑎+𝑓𝑗+1 −1

6∆𝑥−∆𝑥+𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

𝑎− =−∆𝑥+

∆𝑥−(∆𝑥+ + ∆𝑥−), ⟹ 𝑎0 =

∆𝑥+ − ∆𝑥−

∆𝑥−∆𝑥+, 𝑎+ =

∆𝑥−

∆𝑥+(∆𝑥+ + ∆𝑥−)

𝑎− + 𝑎0 + 𝑎+ = 0

−∆𝑥−𝑎− + ∆𝑥+𝑎+ = 1

∆𝑥−2𝑎− + ∆𝑥+

2𝑎+ = 0

Page 6: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

Similarly, it can be obtained,

𝑓𝑥𝑥 𝑥𝑗 = 𝑏−𝑓𝑗−1 + 𝑏0𝑓𝑗 + 𝑏+𝑓𝑗+1

−1

3∆𝑥− − ∆𝑥+ 𝑓𝑥𝑥𝑥 𝑥𝑗 −

1

12(∆𝑥−

2 − ∆𝑥−∆𝑥+ + Δ𝑥+2)𝑓𝑥𝑥𝑥𝑥 𝑥𝑗 + ⋯

𝑏− =2

∆𝑥−(∆𝑥+ + ∆𝑥−), 𝑏0 =

−2

∆𝑥−∆𝑥+, 𝑏+ =

2

∆𝑥+(∆𝑥+ + ∆𝑥−)

Page 7: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

B. Discretization in transformed domain

𝑑𝑓

𝑑𝑥𝑥=𝑥𝑗

=𝑑𝑓

𝑑𝑠𝑠=𝑠𝑗

𝑑𝑠

𝑑𝑥𝑥=𝑥𝑗

=1

𝑑𝑥𝑑𝑠 𝑠=𝑠𝑗

𝑑𝑓

𝑑𝑠𝑠=𝑠𝑗

≅1

𝑥′ 𝑠𝑗𝐷𝑠𝑓𝑗

=1

𝑥′ 𝑠𝑗

𝑓(𝑠𝑗+1) − 𝑓(𝑠𝑗−1)

2∆𝑠

Consider coordinate transformation 𝑥 = 𝑥(𝑠) which maps 𝑠 ∈ [0,1] to 𝑥 ∈ 𝑥𝑚𝑖𝑛, 𝑥𝑚𝑎𝑥 , and

grids are non-uniformly distributed in 𝑥, but uniformly distributed in 𝑠.

𝑥𝑗 𝑥𝑗+1 𝑥𝑗−1 𝑠𝑗 𝑠𝑗+1 𝑠𝑗−1

• If 𝑥 = 𝑥(𝑠) is a known function, 𝑥′(𝑠𝑗) can be evaluated analytically.

𝑑𝑓

𝑑𝑥𝑗

≅1

𝑥′ 𝑠𝑗

𝑓𝑗+1 − 𝑓𝑗−1

2∆𝑠≅

1𝑥𝑗+1 − 𝑥𝑗−1

2∆𝑠

𝑓𝑗+1 − 𝑓𝑗−1

2∆𝑠=

𝑓𝑗+1 − 𝑓𝑗−1

𝑥𝑗+1 − 𝑥𝑗−1

• If 𝑥 = 𝑥(𝑠) is only known at the grids, i.e. only 𝑥𝑗 = 𝑥(𝑠𝑗) are known, then

where , ∆𝑠 =1

𝑁 𝑠𝑗 =

𝑗

𝑁

▪ 1st derivative:

𝑥𝑗 = 𝑥(𝑠𝑗)

Page 8: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

𝑑2𝑓

𝑑𝑥2=

𝑑

𝑑𝑥

𝑑𝑓

𝑑𝑥=

𝑑

𝑑𝑠

𝑑𝑓

𝑑𝑥

𝑑𝑠

𝑑𝑥=

𝑑

𝑑𝑠

𝑑𝑓

𝑑𝑠

𝑑𝑠

𝑑𝑥

𝑑𝑠

𝑑𝑥=

𝑑

𝑑𝑠

𝑑𝑓

𝑑𝑠

1

𝑑𝑥𝑑𝑠

1

𝑑𝑥𝑑𝑠

⋯ (1)

=𝑑2𝑓

𝑑𝑠2

1

𝑑𝑥𝑑𝑠

+𝑑𝑓

𝑑𝑠

−𝑑2𝑥𝑑𝑠2

𝑑𝑥𝑑𝑠

2

1

𝑑𝑥𝑑𝑠

=1

𝑑𝑥𝑑𝑠

2

𝑑2𝑓

𝑑𝑠2−

𝑑2𝑥𝑑𝑠2

𝑑𝑥𝑑𝑠

3

𝑑𝑓

𝑑𝑠 ⋯ (2)

Therefore, the 2nd derivative can be evaluated in two ways.

▪ 2nd derivative:

Page 9: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

(𝟏) 𝑑2𝑓

𝑑𝑥2𝑥=𝑥𝑗

=1

𝑑𝑥𝑑𝑠 𝑠=𝑠𝑗

𝑑

𝑑𝑠

1

𝑑𝑥𝑑𝑠 𝑠=𝑠𝑗

𝑑𝑓

𝑑𝑠𝑠=𝑠𝑗

𝑠=𝑠𝑗

≅1

𝑥′ 𝑠𝑗𝐷𝑠

1

𝑥′ 𝑠𝑗𝐷𝑠𝑓 𝑠=𝑠𝑗

𝑠=𝑠𝑗

• If 𝑥 = 𝑥(𝑠) is a known function, 𝑥′(𝑠𝑗) can be evaluated analytically.

• If 𝑥 = 𝑥(𝑠) is only known at the grids, i.e. only 𝑥𝑗 = 𝑥(𝑠𝑗) are known, then

𝑑2𝑓

𝑑𝑥2𝑥=𝑥𝑗

≅1

𝑥′ 𝑠𝑗𝐷𝑠

1

𝑥′ 𝑠𝑗𝐷𝑠𝑓 𝑠=𝑠𝑗

𝑠=𝑠𝑗

=1

𝑥′ 𝑠𝑗

𝐷𝑠𝑓

𝑥′𝑗+

12

−𝐷𝑠𝑓

𝑥′𝑗−

12

≅Δ𝑠

𝑥𝑗+

12− 𝑥

𝑗−12

1

Δ𝑠

Δ𝑠

𝑥𝑗+1 − 𝑥𝑗

𝑓𝑗+1 − 𝑓𝑗Δ𝑠

−Δ𝑠

𝑥𝑗 − 𝑥𝑗−1

𝑓𝑗 − 𝑓𝑗−1

Δ𝑠=

1

𝑥𝑗+

12− 𝑥

𝑗−12

𝑓𝑗+1 − 𝑓𝑗𝑥𝑗+1 − 𝑥𝑗

−𝑓𝑗 − 𝑓𝑗−1

𝑥𝑗 − 𝑥𝑗−1

≅2

𝑥𝑗+1 − 𝑥𝑗−1

𝑓𝑗+1 − 𝑓𝑗𝑥𝑗+1 − 𝑥𝑗

−𝑓𝑗 − 𝑓𝑗−1

𝑥𝑗 − 𝑥𝑗−1=

2𝑓𝑗+1

∆𝑥+(∆𝑥+ + ∆𝑥−)+

2𝑓𝑗∆𝑥+∆𝑥−

+2𝑓𝑗−1

∆𝑥−(∆𝑥+ + ∆𝑥−)

𝑠𝑗 𝑠𝑗+1 𝑠𝑗−1

𝑠𝑗+

12

𝑠𝑗−

12

𝑥𝑗 𝑥𝑗+1 𝑥𝑗−1

𝑥𝑗+

12

𝑥𝑗−

12

𝑥𝑗 = 𝑥(𝑠𝑗)

(same as difference in physical coordinate)

𝑑2𝑓

𝑑𝑥2𝑥=𝑥𝑗

≅1

𝑥′ 𝑠𝑗𝐷𝑠

1

𝑥′ 𝑠𝑗𝐷𝑠𝑓 𝑠=𝑠𝑗

𝑠=𝑠𝑗

=1

𝑥′ 𝑠𝑗

𝐷𝑠𝑓

𝑥′𝑗+

12

−𝐷𝑠𝑓

𝑥′𝑗−

12

≅1

𝑥′ 𝑠𝑗 1

Δ𝑠

1

𝑥′ 𝑠𝑗+

12

𝑓𝑗+1 − 𝑓𝑗Δ𝑠

−1

𝑥′ 𝑠𝑗−

12

𝑓𝑗 − 𝑓𝑗−1

Δ𝑠

Page 10: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

𝟐 𝑑2𝑓

𝑑𝑥2𝑥=𝑥𝑗

=1

𝑑𝑥𝑑𝑠

2

𝑠=𝑠𝑗

𝑑2𝑓

𝑑𝑠2𝑠=𝑠𝑗

𝑑2𝑥𝑑𝑠2

𝑠=𝑠𝑗

𝑑𝑥𝑑𝑠

3

𝑠=𝑠𝑗

𝑑𝑓

𝑑𝑠𝑠=𝑠𝑗

≅1

𝑥′ 𝑠𝑗2 𝐷𝑠

2𝑓𝑗 −𝑥′′ 𝑠𝑗

𝑥′ 𝑠𝑗3 𝐷𝑠𝑓𝑗

𝑥′ 𝑠𝑗 ≅𝑥𝑗+1 − 𝑥𝑗−1

2∆𝑠, 𝑥′′ 𝑠𝑗 ≅

𝑥𝑗+1 − 2𝑥𝑗 + 𝑥𝑗−1

∆𝑠2

∴ 𝑑2𝑓

𝑑𝑥2𝑥=𝑥𝑗

≅4∆𝑠2

𝑥𝑗+1 − 𝑥𝑗−12

𝑓𝑗+1 − 2𝑓𝑗 + 𝑓𝑗−1

∆𝑠2−

𝑥𝑗+1 − 2𝑥𝑗 + 𝑥𝑗−1

∆𝑠2

8∆𝑠3

𝑥𝑗+1 − 𝑥𝑗−13

𝑓𝑗+1 − 𝑓𝑗−1

2∆𝑠

= 4𝑓𝑗+1 − 2𝑓𝑗 + 𝑓𝑗−1

𝑥𝑗+1 − 𝑥𝑗−12 − 4

𝑥𝑗+1 − 2𝑥𝑗 + 𝑥𝑗−1

𝑥𝑗+1 − 𝑥𝑗−13 (𝑓𝑗+1 − 𝑓𝑗−1)

=8∆𝑥+

∆𝑥+ + ∆𝑥−3𝑓𝑗+1 +

−8

∆𝑥+ + ∆𝑥−2𝑓𝑗 +

8∆𝑥−

∆𝑥+ + ∆𝑥−3𝑓𝑗−1

• If 𝑥 = 𝑥(𝑠) is a known function, 𝑥′(𝑠𝑗) can be evaluated analytically.

• If 𝑥 = 𝑥(𝑠) is only known at the grids, i.e. only 𝑥𝑗 = 𝑥(𝑠𝑗) are known, then

Page 11: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

• Clustered near one end: 𝑥 ∈ [0, ℎ] 𝑥 𝑠 = ℎ 1 +tanh 𝑐(𝑠 − 1)

tanh 𝑐 (clustered near 𝑥 = 0)

𝑥 𝑠 = ℎtanh 𝑐𝑠

tanh 𝑐 (clustered near 𝑥 = ℎ) • Clustered near one end: 𝑥 ∈ [0, ℎ]

𝑐 = 2.5

𝑥 = 𝑥(𝑠)

𝑠𝑗 𝑠𝑗+1 𝑠𝑗−1

𝑥𝑗

𝑥𝑗+1

𝑥𝑗−1

0 0 1

𝑐 = 1.5

𝑥 = 𝑥(𝑠)

𝑠𝑗 𝑠𝑗+1 𝑠𝑗−1

𝑥𝑗

𝑥𝑗+1

𝑥𝑗−1

0 0 1

𝑐 = 2.5

𝑥 = 𝑥(𝑠)

𝑠𝑗 𝑠𝑗+1 𝑠𝑗−1

𝑥𝑗

𝑥𝑗+1

𝑥𝑗−1

0 0 1

𝑐 = 1.5

𝑥 = 𝑥(𝑠)

1

𝑥𝑗

𝑥𝑗+1

𝑥𝑗−1

𝑠𝑗 𝑠𝑗+1 𝑠𝑗−1 0

0

Non-uniform grids

Page 12: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

𝑥 𝑠 = ℎtanh 2𝑐(𝑠 − 0.5)

tanh 𝑐 • Clustered near two ends: 𝑥 ∈ [−ℎ, ℎ]

• Clustered near center: 𝑥 ∈ [−ℎ, ℎ] 𝑥 𝑠 = ℎ −1 +tanh 2𝑐𝑠

tanh 𝑐 𝑠 ∈ 0, 0.5 𝑥 𝑠 = ℎ 1 +

tanh 2𝑐 𝑠 − 1

tanh 𝑐 𝑠 ∈ [0.5, 1]

𝑐 = 2.5

𝑥 = 𝑥(𝑠)

𝑠𝑗 𝑠𝑗+1 𝑠𝑗−1

𝑥𝑗 𝑥𝑗+1

𝑥𝑗−1

−ℎ 1

𝑐 = 2.5

𝑥 = 𝑥(𝑠)

0 −ℎ

𝑥𝑗 𝑥𝑗+1

𝑥𝑗−1

𝑠𝑗 𝑠𝑗+1 𝑠𝑗−1 0 1

𝑐 = 1.5

𝑥 = 𝑥(𝑠)

𝑥𝑗

𝑥𝑗+1

𝑥𝑗−1

𝑠𝑗 𝑠𝑗+1 𝑠𝑗−1 0 1 −ℎ

𝑐 = 2.5

𝑥 = 𝑥(𝑠)

𝑥𝑗 𝑥𝑗+1

𝑥𝑗−1

𝑠𝑗 𝑠𝑗+1 𝑠𝑗−1 0 1 -h

𝑐 = 1.5

𝑥 = 𝑥(𝑠)

Page 13: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

𝑥 0 = 1 +tanh(−𝑐)

tanh 𝑐2𝜋 = 0

𝑥 1 = 1 + 0 2𝜋 = 2𝜋

𝑑𝑥(𝑠)

𝑑𝑠= 2𝜋𝑐

sech2 𝑐(𝑠 − 1)

tanh 𝑐= 2𝜋𝑐

1 − tanh2 𝑐(𝑠 − 1)

tanh 𝑐

d2𝑥(𝑠)

d𝑠2= 2𝜋𝑐2

−2 sech2 𝑐(𝑠 − 1) ∙ tanh 𝑐(𝑠 − 1)

tanh 𝑐= 2𝜋𝑐2

−2(1 − tanh2 𝑐(𝑠 − 1))∙ tanh 𝑐(𝑠 − 1)

tanh 𝑐

𝑠𝑗 𝑠𝑗+1 𝑠𝑗−1

𝑥𝑗

𝑥𝑗+1

𝑥𝑗−1

𝑥 = 𝑥(𝑠)

Δ𝑥+

Δ𝑥−

Δ𝑠 Δ𝑠

𝑐 = 2.5

2𝜋

0

0 1

𝑥 𝑠 = 2𝜋 1 +tanh 𝑐(𝑠 − 1)

tanh 𝑐 maps 𝑠 ∈ [0, 1] to 𝑥 ∈ [0, 2𝜋] Consider

Page 14: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

error

𝑥

50 grids

error

𝑥

10 grids

𝑥 𝑠 = 2𝜋 1 +tanh 𝑐 𝑠 − 1

tanh 𝑐 𝑐 = 2.5

finite difference in physical domain

finite difference in mapped domain using differenced 𝑥′(𝑠𝑗)

finite difference in mapped domain using analytical 𝑥′(𝑠𝑗)

Computing 1st derivative of 𝑓 𝑥 = sin 𝑥 using:

• Doing finite difference in physical domain seems to be more accurate than doing that in mapped domain.

• In particular, computing the mapping metric 𝑥′(𝑠𝑗)

using difference approximation is more accurate than that using the analytical value.

Page 15: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

compute 𝑥′(𝑠𝑗) 𝑑2𝑓

𝑑𝑥2≅

difference in physical domain

2𝑓𝑗+1

∆𝑥+(∆𝑥+ + ∆𝑥−)+

2𝑓𝑗

∆𝑥+∆𝑥−+

2𝑓𝑗−1

∆𝑥−(∆𝑥+ + ∆𝑥−)

difference in mapped domain

1

𝑥′ 𝑠𝑗𝐷𝑠

𝐷𝑠𝑓𝑗

𝑥′ 𝑠𝑗 𝑠=𝑠𝑗

analytically 1

𝑥′ 𝑠𝑗 1

Δ𝑠

1

𝑥′ 𝑠𝑗+

12

𝑓𝑗+1 − 𝑓𝑗

Δ𝑠−

1

𝑥′ 𝑠𝑗−

12

𝑓𝑗 − 𝑓𝑗−1

Δ𝑠

by difference 2𝑓𝑗+1

∆𝑥+(∆𝑥+ + ∆𝑥−)+

2𝑓𝑗

∆𝑥+∆𝑥−+

2𝑓𝑗−1

∆𝑥−(∆𝑥+ + ∆𝑥−)

𝐷𝑠2𝑓𝑗

𝑥′ 𝑠𝑗2 −

𝑥′′ 𝑠𝑗

𝑥′ 𝑠𝑗3 𝐷𝑠𝑓𝑗

analytically 1

𝑥′ 𝑠𝑗2 𝐷𝑠

2𝑓𝑗 −𝑥′′ 𝑠𝑗

𝑥′ 𝑠𝑗3 𝐷𝑠𝑓𝑗

by difference 8∆𝑥+𝑓𝑗+1

∆𝑥+ + ∆𝑥−3+

−8𝑓𝑗

∆𝑥+ + ∆𝑥−2+

8∆𝑥−𝑓𝑗−1

∆𝑥+ + ∆𝑥−3

same

Page 16: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

𝑥

error

10 grids

error

𝑥

50 grids 𝑥 𝑠 = 2𝜋 1 +

tanh(𝑐(𝑠 − 1))

tanh 𝑐 𝑐 = 2.5

finite difference in physical domain

finite difference in mapped domain (2) using differenced 𝑥′(𝑠𝑗)

finite difference in mapped domain (2) using analytical 𝑥′(𝑠𝑗)

Computing 2nd derivative of 𝑓 𝑥 = sin 𝑥 using:

finite difference in mapped domain (1) using analytical 𝑥′(𝑠𝑗)

• Again, for 2nd derivative, doing the finite difference in the physical domain is also more accurate than doing that in the mapped domain.

• Approximate the mapping metric 𝑥′(𝑠𝑗) using the

same difference formula is more accurate than that using the analytical form. Why?

Page 17: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

𝑑𝑓

𝑑𝑥𝑗

≅1

𝑥′(𝑠𝑗)

𝑓𝑗+1 − 𝑓𝑗−1

2∆𝑠≅

2∆𝑠

𝑥𝑗+1 − 𝑥𝑗−1

𝑓𝑗+1 − 𝑓𝑗−1

2∆𝑠

=1

(∆𝑥+ + ∆𝑥−) 𝑓 𝑥𝑗 + ∆𝑥+𝑓𝑥 𝑥𝑗 +

1

2∆𝑥+

2𝑓𝑥𝑥 𝑥𝑗 +1

6∆𝑥+

3𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

−𝑓 𝑥𝑗 + ∆𝑥−𝑓𝑥 𝑥𝑗 −1

2∆𝑥−

2𝑓𝑥𝑥 𝑥𝑗 +1

6∆𝑥−

3𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

= 𝑓𝑥 𝑥𝑗 +1

2∆𝑥+ − ∆𝑥− 𝑓𝑥𝑥 𝑥𝑗 +

1

6∆𝑥+

2 − ∆𝑥+∆𝑥− + ∆𝑥−2 𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

𝑥𝑗 𝑥𝑗+1 𝑥𝑗−1

∆𝑥− ∆𝑥+

(∆𝑥+ − ∆𝑥−) = 𝑥𝑗+1 − 2𝑥𝑗+𝑥𝑗−1= 𝑥 𝑠𝑗+1 − 2𝑥 𝑠𝑗 + 𝑥 𝑠𝑗−1

= 𝑥 𝑠𝑗 + ∆𝑠𝑥′ 𝑠𝑗 +∆𝑠2

2𝑥′′ 𝑠𝑗 + 𝑂 ∆𝑠3 + ⋯− 2𝑥 𝑠𝑗 + 𝑥 𝑠𝑗 − ∆𝑠𝑥′ 𝑠𝑗 +

∆𝑠2

2𝑥′′ 𝑠𝑗 − 𝑂 ∆𝑠3 + ⋯

= ∆𝑠2𝑥′′ 𝑠𝑗 + ⋯

∆𝑥+2 − ∆𝑥+∆𝑥− + ∆𝑥−

2 = 𝑥𝑗+1 − 𝑥𝑗2− 𝑥𝑗+1 − 𝑥𝑗 𝑥𝑗 − 𝑥𝑗−1 + 𝑥𝑗 − 𝑥𝑗−1

2

= 𝑥 𝑠𝑗 + ∆𝑠 𝑥′ 𝑠𝑗 + 𝑂 ∆𝑠2 − 𝑥 𝑠𝑗2

− 𝑥 𝑠𝑗 + ∆𝑠 𝑥′ 𝑠𝑗 + 𝑂 ∆𝑠2 − 𝑥 𝑠𝑗 𝑥 𝑠𝑗 − 𝑥 𝑠𝑗 + ∆𝑠 𝑥′ 𝑠𝑗 − 𝑂 ∆𝑠2

− 𝑥 𝑠𝑗 − 𝑥 𝑠𝑗 + ∆𝑠 𝑥′ 𝑠𝑗 − 𝑂 ∆𝑠2 2

= ∆𝑠2 𝑥′ 𝑠𝑗2+ 𝑂 ∆𝑠3 − ∆𝑠2 𝑥 𝑠𝑗

2+ 𝑂 ∆𝑠3 + ∆𝑠2 𝑥′ 𝑠𝑗

2+ 𝑂 ∆𝑠3

= ∆𝑠2 𝑥′ 𝑠𝑗2+ ⋯

∴ 2∆𝑠

𝑥𝑗+1 − 𝑥𝑗−1

𝑓𝑗+1 − 𝑓𝑗−1

2∆𝑠= 𝑓𝑥 𝑥𝑗 +

∆𝑠2

2𝑥′′(𝑠𝑗)𝑓𝑥𝑥 𝑥𝑗 +

∆𝑠2

6𝑥′(𝑠𝑗)

2𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

= 𝑓𝑥 𝑥𝑗 +∆𝑠2

63𝑥′′(𝑠𝑗)𝑓𝑥𝑥 𝑥𝑗 + 𝑥′(𝑠𝑗)

2𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

• Compute metric derivative by finite difference:

Page 18: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

𝑑𝑓

𝑑𝑥𝑗

≅1

𝑥′ 𝑠𝑗

𝑓𝑗+1 − 𝑓𝑗−1

2∆𝑠

=1

𝑥′(𝑠𝑗)

1

2∆𝑠 𝑓 𝑥𝑗 + 𝑥𝑗+1 − 𝑥𝑗 𝑓𝑥 𝑥𝑗 +

1

2𝑥𝑗+1 − 𝑥𝑗

2𝑓𝑥𝑥 𝑥𝑗 +

1

6𝑥𝑗+1 − 𝑥𝑗

3𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

−𝑓 𝑥𝑗 + 𝑥𝑗 − 𝑥𝑗−1 𝑓𝑥 𝑥𝑗 −1

2𝑥𝑗 − 𝑥𝑗−1

2𝑓𝑥𝑥 𝑥𝑗 +

1

6𝑥𝑗 − 𝑥𝑗−1

3𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

=1

𝑥′(𝑠𝑗)

1

2∆𝑠 𝑥𝑗+1 − 𝑥𝑗−1 𝑓𝑥 𝑥𝑗 +

1

2𝑥𝑗+1 − 𝑥𝑗

2− 𝑥𝑗 − 𝑥𝑗−1

2𝑓𝑥𝑥 𝑥𝑗

+1

6𝑥𝑗+1 − 𝑥𝑗

3+ 𝑥𝑗 − 𝑥𝑗−1

3𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

𝑥𝑗+1 − 𝑥𝑗−1 = 𝑥 𝑠𝑗 + ∆𝑠𝑥′ 𝑠𝑗 +1

2∆𝑠2𝑥′′ 𝑠𝑗 +

1

6∆𝑠3𝑥′′′ 𝑠𝑗 + ⋯

−𝑥 𝑠𝑗 + ∆𝑠𝑥′ 𝑠𝑗 −1

2∆𝑠2𝑥′′ 𝑠𝑗 +

1

6∆𝑠3𝑥′′′ 𝑠𝑗 + ⋯

= 2∆𝑠𝑥′ 𝑠𝑗 +1

3∆𝑠3𝑥′′′ 𝑠𝑗 + ⋯

𝑥𝑗+1 − 𝑥𝑗2− 𝑥𝑗 − 𝑥𝑗−1

2

= 𝑥 𝑠𝑗 + ∆𝑠𝑥′ 𝑠𝑗 +1

2∆𝑠2𝑥′′ 𝑠𝑗 + ⋯− 𝑥 𝑠𝑗

2

− 𝑥 𝑠𝑗 − 𝑥 𝑠𝑗 + ∆𝑠𝑥′ 𝑠𝑗 −1

2∆𝑠2𝑥′′ 𝑠𝑗 + ⋯

2

= 2∆𝑠3𝑥′ 𝑠𝑗 𝑥′′ 𝑠𝑗 + ⋯

𝑥𝑗+1 − 𝑥𝑗3+ 𝑥𝑗 − 𝑥𝑗−1

3

= 𝑥 𝑠𝑗 + ∆𝑠𝑥′ 𝑠𝑗 +1

2∆𝑠2𝑥′′ 𝑠𝑗 + ⋯− 𝑥 𝑠𝑗

3

+ 𝑥 𝑠𝑗 − 𝑥 𝑠𝑗 + ∆𝑠𝑥′ 𝑠𝑗 −1

2∆𝑠2𝑥′′ 𝑠𝑗 + ⋯

3

= 2∆𝑠3 𝑥′ 𝑠𝑗3+ ⋯

• Compute metric derivative analytically:

Page 19: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

∴ 1

𝑥′(𝑠𝑗)

𝑓𝑗+1 − 𝑓𝑗−1

2∆𝑠=

1

𝑥′(𝑠𝑗)

1

2∆𝑠 2∆𝑠𝑥′ 𝑠𝑗 𝑓𝑥 𝑥𝑗 +

1

3∆𝑠3𝑥′′′ 𝑠𝑗 𝑓𝑥 𝑥𝑗 + ⋯

+1

22∆𝑠3𝑥′ 𝑠𝑗 𝑥′′ 𝑠𝑗 𝑓𝑥𝑥 𝑥𝑗 + ⋯

+1

62∆𝑠3 𝑥′ 𝑠𝑗

3𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

= 𝑓𝑥 𝑥𝑗 +1

6∆𝑠2

𝑥′′′ 𝑠𝑗

𝑥′ 𝑠𝑗𝑓𝑥 𝑥𝑗 +

1

2∆𝑠2𝑥′′ 𝑠𝑗 𝑓𝑥𝑥 𝑥𝑗 +

1

6∆𝑠2 𝑥′ 𝑠𝑗

2𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

= 𝑓𝑥 𝑥𝑗 +1

6∆𝑠2

𝑥′′′ 𝑠𝑗

𝑥′ 𝑠𝑗𝑓𝑥 𝑥𝑗 + 3𝑥′′ 𝑠𝑗 𝑓𝑥𝑥 𝑥𝑗 + 𝑥′ 𝑠𝑗

2𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

2∆𝑠

𝑥𝑗+1 − 𝑥𝑗−1

𝑓𝑗+1 − 𝑓𝑗−1

2∆𝑠= 𝑓𝑥 𝑥𝑗 +

∆𝑠2

2𝑥′′(𝑠𝑗)𝑓𝑥𝑥 𝑥𝑗 +

∆𝑠2

6𝑥′(𝑠𝑗)

2𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

= 𝑓𝑥 𝑥𝑗 +1

6∆𝑠2 3𝑥′′(𝑠𝑗)𝑓𝑥𝑥 𝑥𝑗 + 𝑥′(𝑠𝑗)

2𝑓𝑥𝑥𝑥 𝑥𝑗 + ⋯

compute metric derivative analytically results in one more term in truncation error.

Comparing with computing metric derivative using finite difference,

Page 20: 18. Finite Difference - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/18. Finite_Difference.pdf · A. Discretization in physical domain – difference operator adapted

Exercises