You are on page 1of 8

26/08/21

Climate Modelling
3rd year, SA Bachelor

2. Taylor’s expansion & Finite Difference Methods

Thomas Stocker: Introduction to Climate Modelling

The Primitive Equation System


(PES)

•  The PES à non-linear system à to be solved via


numerical methods

•  To solve the equations à approximations

1
26/08/21

The Primitive Equation System


du u tan φ 1 ∂p
−( f + )v + + Fx = 0
dt a ρ ∂x The Navier-Stokes
motion equations
dv u tan φ 1 ∂p
+( f + )u + + Fy = 0
dt a ρ ∂y
p = R ρT Ideal gas equation

∂p Hydrostatic equation
+ gρ = 0
∂z
dT Q
+ (γ −1)T ∇ ⋅ V = Thermodynamic equation
dt cp
∂ρ Mass conservation
+ ∇ ⋅ ρV = 0
∂t
Water conservation
∂ρ w
+ ∇ ⋅ ρ w V = [sources − sinks]
∂t
7 equations, 7 variables (u,v,w,p,T,ρ,ρw). 3

2. Finite difference approximations


§  Climate variables à continuous function in space & time
§  To estimate those variables à estimate values over a
finite, discrete points in the domain of the problem
§  To enumerate the points by the index i, j, k, s (time)

§  The ensemble of the points in the domain of space & time


à grid points
§  F(x,y,z,t) à the ensemble of a finite values at the grid
points à estimate a climate variable at those discrete grid
points

2
26/08/21

2. Finite difference approximations


§  e.g. f(t,S) is a continuous function in (t,S) plane
§  To approximate the values of f on a set of discrete points à
devide the S-axis and t-axis into equally spaced nodes at
distance ΔS and Δt apart à (t,S) plane becomes a mesh

2. Finite difference approximations


The Navier-Stokes motion equations

du u tan φ 1 ∂p
−( f + )v + + Fx = 0
dt a ρ ∂x
dv u tan φ 1 ∂p
+( f + )u + + Fy = 0
dt a ρ ∂y

The basic idea of Finite Difference method (FDM) à to replace the partial
derivatives by approximations obtained by Taylor expansions near the point of
interests

3
26/08/21

2. Finite difference approximations


The derivative of f(x):

f ( x + dx ) − f ( x)
∂ x f = lim
dx →0 dx
f ( x) − f ( x − dx )
∂ x f = lim
dx →0 dx
f ( x + dx ) − f ( x − dx )
∂ x f = lim
dx →0 2dx
x-Δx x x+Δx
All definitions are right when dx->0.

But if dx is not very small?

2. Finite difference approximations


Forward differencing
f ( x + dx ) − f ( x)
∂x f + ≈
dx

Backward differencing centered

f ( x) − f ( x − dx )
∂x f − ≈
dx forward
backward

Centered differencing
x-Δx x x+Δx
f ( x + dx ) − f ( x − dx )
∂x f ≈
2dx

4
26/08/21

Question
How good is a finite differencing approximation?

≈≠=
àTaylor’s expansion....

Taylor’s expansion
§  Taylor’s expansion of a given function about a single point to
approximate the derivative(s) at that point
à Derivatives are replaced by finite difference approximations
§  Suppose u is a function of x possessing derivatives of all orders in
the interval (x-nΔx, x+nΔx).
à We can obtain the value of u at points x±nΔx in terms of the
value of u(x) and its derivatives at point x.

Δx may be negative, however for convenience, we let Δx>0;


x< θ < x+Δx

5
26/08/21

Taylor’s expansion
Similarly

where x-Δx < θ < x

where x < θ < x+2Δx

Taylor’s expansion

where x -2Δx < θ < x

§  The value of Δx is taken to be small (Δx <1) à we can approximate


the series on the right-hand side by truncating the higher-order terms
à incorporate some error which is known as truncation error
§  In general, the more terms we keep on the right-hand side, the better
and more accurate the value of u(x±nΔx ) would be

6
2 f a h 2f
h2 f a h f a h 2 f a h f h'' 2 a f '' a
f a h f a hf ' a f '' a and f a h f a hf ' a f '' a h
2 2
By adding the second-order Taylor series expansion, we get likewise the second derivative 26/08/21
Example 1.1 f a h 2f a f a h
f a h f a h 2 f a h 2 f '' a f '' a
Write the 2n 1 order Taylor series h 2 expansion of the function sin
f x f' x sin x cos x f '' x sin x f (3) x c
ample 1.1
an approximation of the second derivative, we
Taylor’s expansion (3)
ite the 2n 1 order Taylor f series
0 is more0,
This equation f ' than
expansion
accurate 0 the of
1, the
first f ''ones.
two 0To obtain
function 0,anfsin x0 about
approximation etc.derivative, we
of the1second
0.
h2 write the second-order Taylor series expansion
This equation is moreof accurate
f: than the first two ones. To obtain an approximation of the second derivative, we
(3)
f '' a x cos① Q1: Write the Taylor’s expansion till 2n+1 order for the
x 2 sin f' x x '' write
f Thisxequation sin
is morex3accurate
x Taylorxfthan
2the second-order
h
5seriesthexfirst two2 ofcos
expansion ones.
h f: n xetc.
x obtain
f : To 2 nan1approximation of the second derivative, we
2n 1
e the second derivative Then
f a h f a sin
(3) a xthe second-order
hf 'write f '' ax and f Taylor
function sin(x) at x=0 ? a h series expansion of
hf a hf ' a f ''1a 2
h x 2 x
0 0, f ' 0 1, f '' 0 0, f 0 1 etc. 3! h 5!
2 2
f a 2 hfn' a 1 !f2'' a
f a h f a hf ' a f '' a and f a h f2a hf ' ah f '' a 2
a f a h f a Taylor
By adding the second-order h fseries
a expansion,
hf ' a we2fget
2
and fthea second
'' alikewise h derivative2
By2adding
n 1 the second-order Taylor series expansion, we get likewise the second derivative
② Q2: Write the Taylor’s expansion till n order for the function
x3 x5 n Byxadding2the second-order
2 n 1 f a series
Taylor h 2 fexpansion,
a f a weh get likewise the second derivative
a1 h 2 f f aa hh f '' fa ax hf '' a2 f ax h 2 f '' a 2 f '' a f a h 2 f a f af ha h
f a h 2 f a
en sin x x f a h ef x at x=0 ?
h
3! 5! Examplef21.2 a h 1 f! a h 2 f a h f '' a
n 2
f '' a 2
h
h 2

Example③ Q3: Write the Taylor’s expansion till n order for the function
x about 0. Write
1.1
the
1 order Taylor
n-th
Example 1.1
Example 1.1
Write the 2n 1/(1-x) for x<1 at x=0?
Write the
series
order
1 order of
2nexpansion
Taylor
Taylor
the
series
series expansion
function sin x of the
about 0.
expansion
function sin x about 0.
of the function e x ab
ample
s x etc.1.2 Writexthe 2n 1 order Taylor series expansion of the function sin x about 0.
f x sinf x x f ' xf fe x x sin fxf'' x' f x
xcos sin
(3) ' x xcos
f ' x sincos
xxetc.x f efxx x cosfcosx(3)etc.
ex xxf f x'' fx'' fxcos''sinx xsin x
x etc. e x etc. (3)

ite the n-th order Taylor series expansion of the function e about 0.
(3)
x
④ Q4: Write the Taylor’s expansion till n order for the function
(3)
f 0 0, (3) f ' 0 1, f '' 0 0, f 0 1 etc.
00 1,1 fetc.
x f 0 0, f ' 0 1, f ''f 0x0 0,0,f f ' (3) '' 0 x0, f 0 1 etc. (3) (3)

x e x
f' x e f f'' xx03+x32+x+1 at x=2
e 1, f 'f 0 x2n 1x1, xef '' etc. 0 1,
3 5 x f 0 1 etc.
2n 1
3
2n 1
5
n
2n 1
5
xThensinsinx x n x x x x 2 n 1
x Then 1
n1 x 2 n x1 x
Then sin x (3)x 1 3! x5! x 2n 1 !x x
0 1, f ' 0 1, f '' 0 1, f 0
3! 5! 1 etc. 3!
2n 1 ! 5!
2
x3 2 n
x
1 ! n
x
x
2 3 Then
n eExample
11.21.2x
Example xn x
x x Example 1.2 x 2! 3! of n
of!the x
en
ut 0.e
x
1 x xWrite
n
Write xthen-th
the n-th order
order Taylor
Taylor series
series expansion
expansion the function
function e about
e about 0. 0. x

2! 3!Write thex n-thnorder


! Taylor
f fx x series
e e expansion
f ' f x' x e of
x
x
e the f x'' x e ee x fabout
f ''function x
x
f x 0.x e x
x
(3)
(3)
x
x
e etc.
etc.
x x (3) x
f x e f' x '' x1,
ef f0 0f 1, etc.
f ' f 0'e 0 1, ff1,'' f 0''x 0 1,e f1,(3) 1 etc.
f 0 0 1 etc. (3)

f 0 1, f ' 0 Function
1, f '' 0 1, f 0of 1xtwo
x x x real
(3)
etc. x x2variables:
2 let f x, y be a function of two r
3 3 n n

Function of two real variables: 2letThen


f e x,1n yx 2!be2! 3!a 3!function x two real variables x and y . It is assumed th
x of
x n
Then e 1 x
x x x n

3
x x n! n!
x
Then e x 1 x xn x
first and second partial derivatives, which are continuous in the v
n
2! 3! Function! f f has
first and second partial derivatives, which
Function are
of of
twotwo continuous
real
real let letf xin
variables:
variables:
Taylor’s expansion
f , yx,the
ybe be vicinity
a function
a function ofof
of two reala
two point
real variables
variables x and by . Ity. is.We
ax ,and note
Itassumed
is assumed
that that
f has

al variables x and y . It is assumed that has variables:


f real first and
f f x
Function of two first and letsecond
second y partial
f x,partial derivatives,
be derivatives,
a function ofwhich
twowhich are continuous
arevariables
real continuous iny .the
in the
x and It isvicinity
vicinity of aofpoint
assumed a point
that , b a., bWe. We
f ahas notenote f xf, y x, , y
x
x x
2 2 2 2 x 2
f f f f f f2 f f 2 2 f f
2 2 f 2 f 2
y x, ya, b, . We note
inity of af point first fand f xx,xy f
y ,, derivatives, y, f,xy,are x
f f xx, ,yxwhich
,y ,yin ,thefvicinity
,f yf x, yx, and f x f,yyand
x,, yaxypoint a, bx.,We x,.fyxy
.fx, yThe
yf note yx,, second-order
y
xsecond-order and Taylo
, partial f f f f f f
xx second continuous f2of , yand f .x The
The ,second-order Taylor series
Taylor series
y 2 x y y y xx xx xx xy xy 2 yy yy
y 2 y yx y 2 x x x 2 x2 yx y y x y x y2
2 2 2
x y y
f f expansion of off fabout
expansion thethe
about pointa, ba, bisf written
point as as
is written
f yy x, y . Theofsecond-order
expansion f about⑤  f ypoint
Taylor
the , a
x, y series f xx x, y , f xy x, y and f x, y . The second-order Taylor series
x 2 , b is written x y x xas
expansion
f fx, xy, y f of
y yyb2 b yy
fa, ba, b f about
f fa, ba, b thef point a, b is written as
y Q5: Write the Taylor’s expansion, 2 a a nd order for f(x,y) at (a,b)
fa, ba, b x x y y
1!as 1! 1! 1!
x expansion
a of f about they point b a1, b1 is written
f x, y f a, b f x a, b x a f yy x baxa, ba f xx f xxa, bax, b 2 x2axa ay yb fbxy f axy, ba, b yy by 2 bbf yy2 fayy, b a,+b x+ ax 2 +a 2y+ by 2 b 2x, y x, y
2 2

1!f x, y f af, b x, yf1! 2! 2! af, b


x a, b 2fcomes y a, bthe Schwarz theorem: f x aif ,fbis a function of twofvariables y a, b
The factor from which admits continuous partial
1 2 1
1! The factor 2 1!
comes from the Schwarz 1! theorem:
2 22
1!
if f is a function of two variables which
2
admits continuous
2
partia
2x a 2 f xx a, b x a2 second fx a, baderivatives,
y b bf then aa , bhavey b yf afb, b + . ff xyyAsa a +, b
y b + x,xy seta x + a hy xb a h and x, y
2 2 f 2 f 2 2 2
2derivatives,
2
f yy a, b + x2!a + y b x, y 2! second
xx
1x a y then f xy we
xy , bwe before, if we
2 have x yyyx y y xy x . As before, if we set x a h x a h and
b kx, weifcan af can f thethe , bexpression
ofaabove 2which
asx a continuous y bpartialf xy a, b y b
a xx
The factor 2 comes fromy theb Schwarz
k y theorem: rewrite above follows
The
of two factor
variables2which
comes from
admits ⑥ the
continuous Schwarz
partial y theorem:
b k
Q6: ? Taylor’s expansion, 2 if f isisarewrite
2!y bnd order for f(x,y)=(x+y)
k , we function
functiontwo
1
variables
of two
expression
2 at (0,0)
admits
asvariables
follows which admits continuo
f a2, bf hf2x f a, b kf y a, b
f a h, b k h12 f a, b 2hkf xy a, b k 2 f yy2 a, b
f a weh, bhave
k2 f a, b 2hf x. aAs kf y a, bif2 wehxx2 set
, b before, f xx ax, b a 2hhkf xy x a,ab h k and
f yy a, b
ore, if we set x a h x a h and The factor 2 comes from the Schwarz theorem: if f is a function
second derivatives, then
f ndx y y xf 2
second derivatives, ⑦  then we have
Q7: ? Taylor’s expansion, 2
Function
let f .x1 , x2As , , x p before,
order for f(x,y)=-cosx cosy at (0,0)
Function of p real variables:
of ptherealabove
variables: let fas xfollows
be a function of if p real
wevariables
, x p be a function of p real variables
setx1 , xx2x,, x ,,xap ., The hsecond-order
x a
x p . The second-order
1 , x2 ,
y b k y b k , we can rewrite x y y
expressionx 2 1 2
2
ws Taylor series expansion of f about the point a1 , a2 , , a p is written as f f
y b k 2 y b k , fwe ⑧  second
Taylor series expansion
Q8: ? Taylor’s expansion, 2
a hcan
, b k rewrite
f a, b hfthe x a, babove
derivatives,
1 of2 f about the e
kf y a, b order for f(x,y)=
nd
expressionh f xx a, b p 2as
-x2-y2acos(xy)
point 1 , a2 ,
then
, a p is written as
a, bf k 2 f yy aat(0,0)
hkf xyfollows ,b we have . As be
kf xy a, b k f yy a, b f x1 , x2 , , x p
f x1 , x2 , , x p
f a1 , a22 , , a p
f a ,a , ,ap j 1
p
xj aj
x j a jx j
fa1 , a2 , , a p
a1 , a2 , , a p
x y y x
Function of p real variables: let f x1 , x2 , , x p1 1be 2a2function ofj 1p real variables
x j x1 , x2 , , x p . The second-order
f a h , b k f a , b hf
p real variables x1 , x2 , , x p . The second-order
x a , b kf
y b y a , b 1k p ph f
y xx a , b 2
2 hkf a, b k 2 f yy a, b
b k , we can rewrite the above expression as foll
f 2 xy
Taylor series expansion of f about the point 2 p px
j aj
2! 1j 1ak1 , 1a2 , x, aj p ais
xkwritten
ak as f a1 , a2 , , a p
j xk akx j xk a1 , a2 , , a p
f a function of p real variables x1 , x12 , 2, x p . The secon
itten as 2! j 1 k 1 x j xk
Function of p real variables: let f x1 , x2 ,
, ,ba p ,kx px j abe
p
f Example
f x1 , x2 , , x p af a1, a2h, 1.3 jf aa1,, ab2 , , a p hf x a, b kf y a, b h f xx 7a, b 2
cos x cos y 2
Example
Write 1.3 j 1
the second-order
x
Taylorj series expansion of the function f x, y about the point (0,0).
Taylor series expansion of f about
Write the
the point
second-ordera Taylor
, a , series is written
, aexpansion as f
of the function
f xp, y p sin x cos y , f x1, y 2 2 cos x sin yp, f x, y cos x cos y , f
x, y
x, y
cos x cos y about the point (0,0).
sin x sin y et
1 f
Function sina jxp
f x, y x jof x real
y ,a f xvariables:
, a x, sin, ay , f letx, y f cosxx cos
, xy , ,f x,,y x sin xbe
sin y a
et function of
x y xx xy
cos , y acos
f2! x, y p cosx
x cos ky k f x 0,x0 10 , 2 f 0,p0 0 , f x, y 11 , f 2 0, 0 0 etpf 0, 0 1
y xx xy

f f 0, 0
yy x j k y xx xy yy
j 1k 1
f x, y cos x cos y 0, f 0, 0 0, f x, y 1, f 0, 0 0 et f 0, 0 1
26/08/21

Practice #2 with Python


Practice#2.1
1.  Write a program to plot the analytical result of ex for x=0, 0.1, …, 2.
2.  Write a program to calculate (approximately) the function ex for 0≤x<2 by using
the Taylor’s expansion
3.  Compare with the analytical results (overlay on the plot)
4.  Find out the truncation order for a good approximation (for x<1; for x<2 and so on)
Pratice#2.2:
§  Write a program to do a similar analysis for the approximation of sin(x)
§  Do a similar analysis for the approximation of 1/(1-x) for x≠1
§  Do a similar analysis for the approximation of x3+x2+x+1

Expected results from Practice #2.1


Taylor’s expansion of ex

16

You might also like