CFD Lecture 3

43
ENGR90024 COMPUTATIONAL FLUID DYNAMICS Lecture O03 Analysis of the Euler’s Method

description

This is a lecture note on CFD subject taught at University of Melbourne. It focuses on ODE topic. This is the 3rd lecture note on this subject

Transcript of CFD Lecture 3

Page 1: CFD Lecture 3

ENGR90024 COMPUTATIONAL FLUID DYNAMICS

Lecture O03

Analysis of the Euler’s Method!

!

Page 2: CFD Lecture 3

Higher Order Taylor MethodIn general, Euler’s method is not very accurate because it is derived by truncating Taylor series at Δt2.

Taylor series

�(tl+1) = �(tl) +�t

1!

d�

dt

����tl

Euler’s method

�(tl+1) = �(tl) +�t

1!f(tl,�l)

Page 3: CFD Lecture 3

To get a more accurate formula, we will derive a formula by truncating Taylor series at Δt3

Taylor series

�(tl+1) = �(tl) +�t

1!

d�

dt

����tl

+�t2

2!

d2�

dt2

����tl

Taylor method order 2

�(tl+1) = �(tl) +�t

1!f(tl,�l)

+�t2

2!

d2�

dt2

����tl

Page 4: CFD Lecture 3

�(tl+1) = �(tl) +�t

1!f(tl,�l) +

�t2

2!

d2�

dt2

����tl

�(tl+1) = �(tl) +�t

1!f(tl,�l) +

�t2

2!

d

dt

d�

dt

����tl

�(tl+1) = �(tl) +�t

1!f(tl,�l) +

�t2

2!

d

dtf(t,�(t))

����tl

�(tl+1) = �(tl) +�t

1!f(tl,�l) +

�t2

2!

✓@f

@t(tl,�l) +

@f

@�(tl,�l)

d�

dt

�(tl+1) = �(tl) +�t

1!f(tl,�l) +

�t2

2!

✓@f

@t(tl,�l) +

@f

@�(tl,�l)f(tl,�l)

Chain rule

Page 5: CFD Lecture 3

�(tl+1) = �(tl) +�t

1!f(tl,�l) +

�t2

2!

✓@f

@t(tl,�l) +

@f

@�(tl,�l)f(tl,�l)

◆O03.1

Equation (O03.1) is called the Taylor Method order 2. It is!more accurate than the Euler’s method because it has a local!truncation error O(Δt3)

Taylor method order 2•Advantage - More accurate•Disadvantage - Need to calculate derivatives of f(t,ɸ)

Page 6: CFD Lecture 3

Example O03.1: !Using Taylors method order 2, solve !!!!!For 0 < t < 8 with ɸ(t=0)=0 and a) Δt=2 b) Δt=1 c) Δt=0.5 d) Δt=0.1 !Compare your solution with Euler’s method

d�

dt= 1� �

Page 7: CFD Lecture 3

@f

@�= �1

For this question

�(tl+1) = �(tl) +�t

1!f(tl,�l) +

�t2

2!

✓@f

@t(tl,�l) +

@f

@�(tl,�l)f(tl,�l)

Taylors method order 2

We need to find all terms in the equation above

f(t,�) = 1� �

@f

@t= 0

Page 8: CFD Lecture 3

�(tl+1) = �(tl) +�t

1!f(tl,�l) +

�t2

2!

✓@f

@t(tl,�l) +

@f

@�(tl,�l)f(tl,�l)

f(t,�) = 1� � @f

@t= 0

�(tl+1) = �(tl) +�t

1!(1� �l) +

�t2

2!

�0 + 1(1� �l)

�l+1 = �l +�t

1!(1� �l) +

�t2

2!

�1� �l

@f

@�= �1

Page 9: CFD Lecture 3

function MPO03p1() clear all; close all; Delta_t=0.5; phi(1)=0.0; t=0:Delta_t:8.0 for l=1:length(t)-1 phi(n+1)=phi(n)+Delta_t*(1-phi(l))-(Delta_t^2/2)*(1-phi(l)) end plot(t,phi,'ko-') hold on ezplot(@(t)1-exp(-t),[0,8,0,2]) xlabel('t'); ylabel('\phi'); legend('Euler','True');

function MPO02p1() clear all; close all; Delta_t=2.0; t=0:Delta_t:8.0 phi(1)=0.0; for l=1:length(t)-1 phi(l+1)=phi(l)+Delta_t*(1-phi(l)) end plot(t,phi,'ko-') hold on ezplot(@(t)1-exp(-t),[0,8,0,2]) xlabel('t'); ylabel('\phi'); legend('Euler','True');

Euler’s method!Example O02.1

�l+1 = �l +�t(1� �l)

Taylor method order 2!Example O03.1

�l+1 = �l +�t

1!(1� �l) +

�t2

2!

�1� �l

Only need!to change one!line of code!

Page 10: CFD Lecture 3

function MPO03p1() clear all; close all; Delta_t=0.5; phi(1)=0.0; t=0:Delta_t:8.0 for l=1:length(t)-1 phi(l+1)=phi(l)+Delta_t*(1-phi(l))-(Delta_t^2/2)*(1-phi(l)) end plot(t,phi,'ko-') hold on ezplot(@(t)1-exp(-t),[0,8,0,2]) xlabel('t'); ylabel('\phi'); legend('Euler','True');

Output

0 1 2 3 4 5 6 7 80

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

t

φ

Taylor 2nd OrderTrueNote: the

Taylor method for Δt=2 predicts ɸ=0 for all values of t!

Page 11: CFD Lecture 3

0 1 2 3 4 5 6 7 80

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

t

φ

Taylor 2nd OrderTrue

0 1 2 3 4 5 6 7 80

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

t

φ

Taylor 2nd OrderTrue

0 1 2 3 4 5 6 7 80

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

t

φ

Taylor 2nd OrderTrue

0 1 2 3 4 5 6 7 80

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

t

φ

Taylor 2nd OrderTrue

Δt=2.0 Δt=1.0

Δt=0.5 Δt=0.1

Page 12: CFD Lecture 3

0 1 2 3 4 5 6 7 80

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

t

φ

Taylor 2nd OrderTrue

Δt=1.0

0 1 2 3 4 5 6 7 80

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

t

1−exp(−t)

φ

EulerTrue

Δt=1.0

0 1 2 3 4 5 6 7 80

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

t

φ

Taylor 2nd OrderTrue

Δt=0.5

0 1 2 3 4 5 6 7 80

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

t

φ

EulerTrue

Δt=0.5

Euler Taylor 2nd order

For a given Δt, the Taylor 2nd order method is more accurate than Euler

Page 13: CFD Lecture 3

End of Example O03.1

Page 14: CFD Lecture 3

STABILITY OF THE EXPLICIT EULER METHOD

(SEE PAGE 8 PRINTED LECTURE NOTES)

• In Lecture O02, we looked at the accuracy of the explicit Euler method. Let’s now look at the stability of the Euler method.!

• We know from previous lecture, the smaller Δt, the more accurate the solution, i.e. the truncation error go to zero. This feature is known as consistency.!

• When we make Δt big enough, the solution will “blow up”. Can we predict the value of Δt where the solution will “blow up”? For a numerical method to be useful, we need it to be stable.

Page 15: CFD Lecture 3

• Stability and consistency are two quite different things (see later). It ispossible for a method can be consistent but not stable.

• If a numerical method is both consistent and stable, it is convergent.

• To carry out stability analysis, consider the model problem

d�

dt= �� (O03.2)

where λ is a constant and is a complex number

� = �Re + i�Im

In many engineering problems, λRe is zero or negative. Forstability analysis, you will always assume that λRe is zero ornegative.

Page 16: CFD Lecture 3

Find analytical solution!at time level lΔt

Find approximated Euler’s! solution at time level lΔt

CompareStrategy d�

dt= ��

The analytical solution for Eq. (O03.2) can be written as

where ɸ1 is the initial value of ɸ

�(t) = �1e�t

= �1e(�Re+i�Im)t

= �1e�Retei�Imt

= �1e�Ret(cos(�Imt) + i sin(�Imt))

Page 17: CFD Lecture 3

Explicit Euler Method

Im

Ree

hIm

hRe

�(t)

�(t)

�(t)

�(t)

�(t)

�(t) = �1e�Ret(cos(�Imt) + i sin(�Imt))

Re

Analytical solution will decay with time if lRe<0

Page 18: CFD Lecture 3

Applying Euler’s formula for the model problem (Eq. (O03.2)) gives

�l+1 = �l + ��t�l

= (1 + ��t)�l

= ��l

where σ = (1+λΔt) is the amplification factor

(O03.3)

Find analytical solution!at time level lΔt

Find approximated Euler’s! solution at time level lΔt

Compared�

dt= ��

�(t) = �1e�Ret(cos(�Imt) + i sin(�Imt))

Page 19: CFD Lecture 3

Thus, running a program using Euler’s method will give you

�1 = Given

�2 = ��1

�3 = ��2 = �2�1

= �3�1�4 = ��3

... =...

�l+1 = ��l

=...

= �l�1

For solution to be stable!|�| � 1

(O03.4)

�l+1 = ��l

Page 20: CFD Lecture 3

-1-2

� = 1 + ��t

� = (1 +�t�Re + i�t�Im)

|�|2 = (1 +�t�Re)2 + (�t�Im)2 1

�Im�t

�Re�t

Page 21: CFD Lecture 3

Example O03.2: !Write a Matlab program that uses Euler’s method to solve !!!

!!for 0 < t < 2 with ɸ(t=0)=1. Using the analysis from the previous slides, what are the values of Δt for the numerical solution to be stable?

d�

dt= �8�

Page 22: CFD Lecture 3

So for this problem λ=-8. So λRe=-8 and λIm=0.0. In order for the numerical solution to be stable, λΔt need to be within the stable region.!

d�

dt= �8�

We have

-1-2

�Im�t

�Re�t

-8Δt, Δt large

Page 23: CFD Lecture 3

�2 ��t 0

�2 �8�t 0

0 �t 1/4

-1-2

�Im�t

�Re�t

So for solution to !be stable Δt must!be between 0 and 0.25

Page 24: CFD Lecture 3

function MPO03p2() clear all; close all; Delta_t=0.01; t=0:Delta_t:2.0; %Preallocating memory phi=zeros(size(t)); phi(1)=1.0; for l=1:length(t)-1 phi(l+1)=phi(l)+Delta_t*(-8*phi(l)); end plot(t,phi,'ko-') hold on ezplot(@(t)exp(-8*t),[0,2,-2,2]) xlabel('t'); ylabel('\phi'); legend('Euler','True');

Page 25: CFD Lecture 3

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2−40

−20

0

20

40

60

80

100

t

φ

EulerTrue

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2−2

−1.5

−1

−0.5

0

0.5

1

1.5

2

t

φ

EulerTrue

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2−2

−1.5

−1

−0.5

0

0.5

1

1.5

2

t

φ

EulerTrue

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2−2

−1.5

−1

−0.5

0

0.5

1

1.5

2

t

φ

EulerTrue

Δt=0.5

Δt=0.25

Δt=0.1 Δt=0.01

Page 26: CFD Lecture 3

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2−40

−20

0

20

40

60

80

100

t

φ

EulerTrue

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2−2

−1.5

−1

−0.5

0

0.5

1

1.5

2

t

φ

EulerTrue

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2−2

−1.5

−1

−0.5

0

0.5

1

1.5

2

t

φ

EulerTrue

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2−2

−1.5

−1

−0.5

0

0.5

1

1.5

2

t

φ

EulerTrue

Δt=0.5

Δt=0.25

Δt=0.1 Δt=0.01

Euler solution is unstable for Δt=0.5

Euler solution is stable for Δt=0.1 & Δt=0.01Euler solution is neutrally stable for Δt=0.25

Page 27: CFD Lecture 3

-1-2

�Im�t

�Re�t

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2−40

−20

0

20

40

60

80

100

t

φ

EulerTrue

Δt=0.5

��t = �8⇥ 0.5 = �4

-4

Page 28: CFD Lecture 3

-1-2

�Im�t

�Re�t

��t = �8⇥ 0.25 = �2

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2−2

−1.5

−1

−0.5

0

0.5

1

1.5

2

t

φ

EulerTrueΔt=0.25

Page 29: CFD Lecture 3

-1-2

�Im�t

�Re�t

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2−2

−1.5

−1

−0.5

0

0.5

1

1.5

2

t

φ

EulerTrue

Δt=0.1

��t = �8⇥ 0.1 = �0.8

Page 30: CFD Lecture 3

-1-2

�Im�t

�Re�t

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2−2

−1.5

−1

−0.5

0

0.5

1

1.5

2

t

φ

EulerTrue

Δt=0.01

��t = �8⇥ 0.01 = �0.08

Page 31: CFD Lecture 3

End of Example O03.2

Page 32: CFD Lecture 3

Consider now the case where λ is purely imaginary,

d�

dt= i�Im�

For this case, iλImΔt, is always on the vertical axis of the stability diagram. It is not within the stability region. Hence, in this case, Euler’s method is always unstable.

� = �Re + i�Im

0d�

dt= ��

Page 33: CFD Lecture 3

Example O03.3: !Write a Matlab program that uses Euler’s method to solve !!!!!for 0 < t < 10 with ɸ(t=0)=1. Using the analysis from the previous slides, what are the values of Δt for the numerical solution to be stable?

d�

dt= 2i�

Page 34: CFD Lecture 3

The analytical solution for this problem can be written as

d�

dt= 2i� 0 t 10 �(t = 0) = 1

� = (ARe + iAIm)e2it� = Ae2it

� = (ARe + iAIm)(cos(2t) + i sin(2t))

� = (ARe cos(2t)�AIm sin(2t)) + i(ARe sin(2t) +AIm cos(2t))

But we are given that ɸ=1 for t=0

1 + 0i = ARe + iAIm

ARe = 1

AIm = 0

Hence

Page 35: CFD Lecture 3

So the analytical solution for this equation is

�(t) = e2it

= cos(2t) + i sin(2t)

d�

dt= 2i� 0 t 10 �(t = 0) = 1

� = (ARe + iAIm)e2it ARe = 1 AIm = 0

Page 36: CFD Lecture 3

function MPO03p3()!clear all;!close all;!Delta_t=0.02;! ! !t=0:Delta_t:10.0;!%Preallocate Memory!phi=zeros(size(t));!phi(1)=1.0;! ! !for n=1:length(t)-1! phi(n+1)=phi(n)+Delta_t*(i*2*phi(n));!end! !plot(t,imag(phi),'ko-')!xlabel('t');!ylabel('\phi');!hold on!ezplot(@(t)sin(2*t),[0,10,-5,5])!legend('Euler','True');

Page 37: CFD Lecture 3

0 1 2 3 4 5 6 7 8 9 10−5

−4

−3

−2

−1

0

1

2

3

4

5

t

φ

sin(2 t)

EulerTrue

0 1 2 3 4 5 6 7 8 9 10−5

−4

−3

−2

−1

0

1

2

3

4

5

t

φ

sin(2 t)

EulerTrue

0 1 2 3 4 5 6 7 8 9 10−5

−4

−3

−2

−1

0

1

2

3

4

5

t

φ

sin(2 t)

EulerTrue

0 1 2 3 4 5 6 7 8 9 10−5

−4

−3

−2

−1

0

1

2

3

4

5

t

φ

sin(2 t)

EulerTrue

Δt=0.5 Δt=0.2

Δt=0.1 Δt=0.02

Im(ɸ)

Page 38: CFD Lecture 3

-1-2

�Im�t

�Re�t

i

��t = 2i⇥ 0.5 = i

0 1 2 3 4 5 6 7 8 9 10−5

−4

−3

−2

−1

0

1

2

3

4

5

t

φ

sin(2 t)

EulerTrue

Δt=0.5

Page 39: CFD Lecture 3

-1-2

�Im�t

�Re�t

0.4i

��t = 2i⇥ 0.2 = 0.4i

0 1 2 3 4 5 6 7 8 9 10−5

−4

−3

−2

−1

0

1

2

3

4

5

t

φ

sin(2 t)

EulerTrueΔt=0.2

Page 40: CFD Lecture 3

-1-2

�Im�t

�Re�t

0.2i

��t = 2i⇥ 0.1 = 0.2i

0 1 2 3 4 5 6 7 8 9 10−5

−4

−3

−2

−1

0

1

2

3

4

5

t

φ

sin(2 t)

EulerTrueΔt=0.1

Page 41: CFD Lecture 3

-1-2

�Im�t

�Re�t

0.04i

��t = 2i⇥ 0.02 = 0.04i

0 1 2 3 4 5 6 7 8 9 10−5

−4

−3

−2

−1

0

1

2

3

4

5

t

φ

sin(2 t)

EulerTrueΔt=0.02

Page 42: CFD Lecture 3

For all values of Δt, λΔt is outside the stable region. Hence the numerical solution obtained using Euler’s method will never be stable for any value of Δt.

Page 43: CFD Lecture 3

End of Example O03.3