61
© Numerical Factory Numerical Methods in Engineering Dept. Matemàtiques ETSEIB - UPC BarcelonaTech Ordinary Differential Equations

# Ordinary Differential Equations - Numerical FactoryOrdinary Differential Equations • Analytical Solution: (only in few cases) • Separate variables: dy dx x dy x dx y x C ³ ³

others

• View
45

0

Embed Size (px)

Citation preview

Numerical Methods in Engineering

Dept. Matemàtiques ETSEIB - UPC BarcelonaTech

Ordinary Differential Equations

Differential Equations

Ordinary Differential Equations (ODE)

Equations including a variable and its derivatives

𝑢 = 𝑢(𝑡) dependent variable

𝑡 independent variable

𝑢′ =𝑑𝑢

𝑑𝑡

Partial Differential Equations (PDE)

𝑢 = 𝑢(𝑥, 𝑦), partial derivatives 𝜕𝑢

𝜕𝑥, 𝜕𝑢

𝜕𝑦

2

𝑢′ − 𝑢 = 0

𝑢′′ + 3𝑢′ − 5𝑢 + 𝑡 = 0

Examples:

Ordinary Differential Equations

• A differential equation defines a relationship between an unknown function and one or more of its derivatives

• In general an ODE is an expression of the form:

𝑢′ = 𝑓(𝑡, 𝑢 𝑡 )

Where 𝑢 = 𝑢 𝑡 is the dependent variable

t is the independent one.

f is supposed to be a continuous and smooth function.

3

Ordinary Differential Equations

• A second order differential equation would have the form:

• The Order of the ODE is the one of the biggest derivative.

}

does not necessarily have to includeall of these variables

4

𝑑2𝑢

𝑑𝑡2= 𝑓(𝑡, 𝑢,

𝑑𝑢

𝑑𝑡)

EDO examples

5

Examples (1):

The dissolution (solubilization) of a contaminant into groundwater is governed by the equation:

where kl is a lumped mass transfer coefficient and Cs is the maximum solubility of the contaminant into the water (a constant). Given C(0)=2 mg/L, Cs = 500 mg/L and

kl = 0.1 day-1, estimate C(0.5) and C(1.0) using a numerical method for ODE’s.

CCkdt

dCsl

EDO examples

6

Examples (2):

A mass balance for a chemical in a completely mixed reactor can be written as:

where V is the volume (10 m3), c is concentration (g/m3), Fis the feed rate (200 g/min), Q is the flow rate (1 m3/min), and k is reaction rate (0.1 m3/g/min). If c(0)=0, solve the ODE for c(0.5) and c(1.0)

2kVcQcFdt

dcV

EDO examples

7

Examples (3):

Before coming to an exam Friday afternoon, Mr. Bringer forgot toplace 24 cans of a refreshing beverage in the refrigerator. His guestare arriving in 5 minutes. So, of course he puts the beverage in therefrigerator immediately. The cans are initially at 75, and therefrigerator is at a constant temperature of 40.

The rate of cooling is proportional to the difference in thetemperature between the beverage and the surrounding air, asexpressed by the following equation with k = 0.1/min.

Use a numerical method to determine the temperature of thebeverage after 5 minutes and 10 minutes.

airTTkdt

dT

EDO examples

• Examples in General related with:

Particles and Solid Motion

Trajectory and Shooting Problems

Thermal and Chemical Process

Fluids

Waves

etc.

8

Ordinary Differential Equations

• Solutions are curves 𝑢(𝑡) defined from two different

problems:

• Initial Value Problem (IVP) (Cauchy’s problem)

• Boundary Value Problem (BVP)

9

Ordinary Differential Equations

• Analytical Solution: (only in few cases)

• Separate variables:

dy

dxx

dy x dx

yx

C

4

4

4

3

2

2

3

At this point lets consider

initial conditions.

y(0)=1

and

y(0)=2

10

Ordinary Differential Equations

• What we see are different values of C for the two

different initial conditions.

3

3

3

40 1

3

4 01 1

3

0 2

4 02 2

3

xy C for y

C then C

for y

C and C

The resulting equations

are:

yx

yx

4

31

4

32

3

3

11

Ordinary Differential Equations

0

4

8

1 2

1 6

0 0.5 1 1 .5 2 2 .5x

y

y (0)=1

y (0)=2

y (0)=3

y (0)=4

12

Numerical Methods for ODE

• According to their precision, IVP methods:

– Euler Method

– Mid Point Method

– Runge-Kutta Methods

• According to their stability:

– Implicit Euler Method

• ODE Methods in Matlab

13

IVP Methods

• Focus is on solving ODE in the form

1

,

i i

duf t u

dt

u u m h

y

x

slope = mui

ui+1

h

This is the same as saying:

new value = old value + Slope x Step size

14

Euler’s Method

• Taylor’s Series:

• The first derivative provides a direct estimate of the slope at 𝑢 𝑡

𝑢 𝑡𝑛+1 = 𝑢 𝑡𝑛 + ℎ𝑑𝑢

𝑑𝑡𝑡𝑛

• The equation is applied iteratively, or one step at a time, over small distance in order to reduce the error

where

15

𝑢 𝑡 + ℎ = 𝑢 𝑡 + ℎ𝑑𝑢

𝑑𝑡𝑡 +

ℎ2

2

𝑑2𝑢

𝑑𝑡2𝑡 + ⋯+

ℎ𝑛

𝑛!

𝑑𝑛𝑢

𝑑𝑡𝑛𝑡

𝑑𝑢

𝑑𝑡𝑡𝑛 = 𝑓(𝑡𝑛, 𝑢𝑛)

Euler’s Method• Examp: Solve using Euler’s method:

First the analytical solution:

now imposing:

Euler’s Method• Examp: Solve using Euler’s method:

now numerically: (use Matlab)

Higher Order Taylor Series Methods

• This is simple enough to implement with polynomials

• Not so trivial with more complicated ODE

• In particular, ODE that are functions of both dependent and independent variables require chain-rule differentiation

• Alternative one-step methods are needed

2

1

' ,,

2

i i

i i i i

f x yy y f x y h h

18

18

Modification of Euler’s Methods

• A fundamental error in Euler’s method is that the derivative at the beginning of the interval is assumed to apply across the entire interval

• Two simple modifications will be demonstrated

• These modification actually belong to a larger class of solution techniques called Runge-Kutta which we will explore later.

19

Mid Point Method

• Uses Euler’s to predict a value of y at the midpoint of the interval

• This predicted value is used to estimate the slope at the midpoint y

xxi xi+1/2 xi+1

20

Mid Point Method

• We then assume that this slope represents a valid approximation of the average slope for the entire interval

• Use this slope to extrapolate linearly from xi to xi+1 using Euler’s algorithm

1 , ( , )2 2

i i i i i i

h hy y h f x y f x y

21

Heun’s Method (RK2)

• Determine the derivative for the interval

– the initial point

– end point

• Use the average to obtain an improved estimate of the slope for the entire interval

y

xi xi+1

22

Heun’s Method (RK2)

Use this “average” slope

to predict yi+1

h

yxfyxfyy iiii

ii2

,, 111

{

y

xi xi+1

23

y

xi xi+1

y

xxi xi+1

h

yxfyxfyy iiii

ii2

,, 111

24

Heun’s Method (RK2)

y

xxi xi+1

1

, , ( , )

2

i i i i i i

i i

f x y f x h y h f x yy y h

1i iy y m h

25

Heun’s Method (RK2)

Both Heun’s and the Mid Point Method have

been introduced graphically. However, the

algorithms used are not as straight forward

as they can be.

Let’s review the Runge-Kutta Methods.

Choices in values of variable will give us

these methods and more. It is recommend

that you use this algorithm on your homework

and/or programming assignments.

Runge-Kutta Methods

26

• RK methods achieve the accuracy of a Taylor series approach without requiring the calculation of a higher derivative

• Many variations exist but all can be cast in the generalized form:

y y x y h hi i i i 1 , ,{

is called the incremental function

27

27

Runge-Kutta Methods

, Incremental Function

can be interpreted as a representative slope over the interval

a k a k a k

where the a s are constant and the k s are

k f x y

k f x p h y q k h

k f x p h y q k h q k h

k f x p h y q k h q k h q k h

n n

i i

i i

i i

n i n i n n n n n

1 1 2 2

1

2 1 11 1

3 2 21 1 22 2

1 1 1 1 2 2 1 1 1

' ' :

,

,

,

, , , ,

28

a k a k a k

where the a s are constant and the k s are

k f x y

k f x p h y q k h

k f x p h y q k h q k h

k f x p h y q k h q k h q k h

n n

i i

i i

i i

n i n i n n n n n

1 1 2 2

1

2 1 11 1

3 2 21 1 22 2

1 1 1 1 2 2 1 1 1

' ' :

,

,

,

, , , ,

NOTE:

k’s are recurrence relationships,

that is k1 appears in the equation for k2

which appears in the equation for k3

This recurrence makes RK methods efficient for

computer calculations

29

Runge-Kutta Methods

Second Order RK Methods

y y a k a k h

where

k f x y

k f x p h y q k h

i i

i i

i i

1 1 1 2 2

1

2 1 11 1

,

,

a k a k a k

where the a s are constant and the k s are

k f x y

k f x p h y q k h

k f x p h y q k h q k h

k f x p h y q k h q k h q k h

n n

i i

i i

i i

n i n i n n n n n

1 1 2 2

1

2 1 11 1

3 2 21 1 22 2

1 1 1 1 2 2 1 1 1

' ' :

,

,

,

, , , ,

30

Second Order RK Methods

• We have to determine values for the constants a1, a2, p1

and q11

• To do this consider the Taylor series in terms of yi+1 and f(xi,yi)

y y a k a k h

y y f x y h f x yh

i i

i i i i i i

1 1 1 2 2

1

2

2, ' ,

31

31

a a

a p

a q

p qa

y y k k h

where

k f x y

k f x h y k h

i i

i i

i i

1 2

2 1

2 11

1 11

2

1 1 2

1

2 1

1 1 1 2 1 2

1

2

1

2

1

21

1

2

1

2

/ /

,

,

Case 1: a2 = 1/2

This is Heun’s Method with

a single corrector.

Note that k1 is the slope at

the beginning of the interval and k2 is

the slope at the

end of the interval.

y y a k a k h

where

k f x y

k f x p h y q k h

i i

i i

i i

1 1 1 2 2

1

2 1 11 1

,

,

32

a a

a p

a q

p qa

y y k h

where

k f x y

k f x h y k h

i i

i i

i i

1 2

2 1

2 11

1 11

2

1 2

1

2 1

1 1 1 0

1

2

1

2

1

2

1

2

1

2

1

2

,

,

y y a k a k h

where

k f x y

k f x p h y q k h

i i

i i

i i

1 1 1 2 2

1

2 1 11 1

,

,

Case 2: a2 = 1

This is the Improved Polygon

Method.

33

Third Order Runge-Kutta Methods

• Derivation is similar to the one for the second-order

• Results in six equations and eight unknowns.

• One common version results in the following

y y k k k h

where

k f x y

k f x h y k h

k f x h y hk hk

i i

i i

i i

i i

1 1 2 3

1

2 1

3 1 2

1

64

1

2

1

2

2

,

,

,

Note the third term

NOTE: if the derivative is a function of x only, this reduces to Simpson’s 1/3 Rule

34

34

Fourth Order Runge Kutta• The most popular

• The following is sometimes called the classical fourth-order RK method

y y k k k k h

where

k f x y

k f x h y k h

k f x h y hk

k f x h y hk

i i

i i

i i

i i

i i

1 1 2 3 4

1

2 1

3 2

4 3

1

62 2

1

2

1

2

1

2

1

2

,

,

,

,35

35

• Note that for ODE that are a function of x alone that this is also the equivalent of Simpson’s 1/3 Rule

34

23

12

1

43211

,

2

1,

2

1

2

1,

2

1

,

226

1

hkyhxfk

hkyhxfk

hkyhxfk

yxfk

where

hkkkkyy

ii

ii

ii

ii

ii

36

36

Systems of Equations• Many practical problems in engineering and science

require the solution of a system of simultaneous differential equations

11 1 2

22 1 2

1 2

, , , ,

, , , ,

, , , ,

n

n

nn n

dyf x y y y

dx

dyf x y y y

dx

dyf x y y y

dx

37

37

• Solution requires n initial conditions

• All the methods for single equations can be used

• The procedure involves applying the one-step technique for every equation at each step before proceeding to the next step

Systems of Equations

38

38

• high order EDO can be converted in Systems of EDO’s

Systems of Equations

39

39

• When there are zones with very different slope, then it is better to change the time step.

• Idea: Use two methods at the same time in order to compare the two results.

• Most well known is RK-F 4-5 (in Matlab ode45)

Automatic time step

41

41

• RK45: Only 6 eq. evaluations are needed

Automatic time step

42

42

• Especial version of RK4

• Change time step according error tolerance

Automatic time step

43

43

ODE Methods in MATLAB

• ode45 is based on an explicit Runge-Kutta (4,5) formula, the Dormand-Prince pair. It is a one-step solver -in computing y(tn), it needs only the solution at the immediately preceding time point, y(tn-1). In general,ode45 is the best function to apply as a "first try" for most problems.

• ode23 is an implementation of an explicit Runge-Kutta (2,3) pair of Bogacki and Shampine. It may be more efficient than ode45 at crude tolerances and in the presence of moderate stiffness. Like ode45, ode23 is a one-step solver.

• ode113 is a variable order Adams-Bashforth-Moulton PECE solver. It may be more efficient than ode45 at stringent tolerances and when the ODE file function is particularly expensive to evaluate. ode113 is a multistep solver - it normally needs the solutions at several preceding time points to compute the current solution.

44

ODE Methods in MATLAB

• ode15s is a variable order solver based on the numerical differentiation formulas (NDFs). Optionally, it uses the backward differentiation formulas (BDFs, also known as Gear's method) that are usually less efficient. Like ode113, ode15s is a multistep solver. Try ode15s when ode45 fails, or is very inefficient, and you suspect that the problem is stiff, or when solving a differential-algebraic problem.

• ode23s is based on a modified Rosenbrock formula of order 2. Because it is a one-step solver, it may be more efficient than ode15s at crude tolerances. It can solve some kinds of stiff problems for which ode15s is not effective.

• ode23t is an implementation of the trapezoidal rule using a "free" interpolant. Use this solver if the problem is only moderately stiff and you need a solution without numerical damping. ode23t can solve DAEs.

• ode23tb is an implementation of TR-BDF2, an implicit Runge-Kutta formula with a first stage that is a trapezoidal rule step and a second stage that is a backward differentiation formula of order two. By construction, the same iteration matrix is used in evaluating both stages. Like ode23s, this solver may be more efficient than ode15s at crude tolerances

45

ODE Methods in MATLAB

46

2D Fixed points

Stability of fixed points

• Linealized system:

𝑥′ = 𝐹 𝑡, 𝑥 → 𝑥′ = 𝐴𝑥

where 𝐴 =𝜕𝐹

𝜕𝑥(the jacobian matrix)

Example:

→ 𝑥′𝑦′

=0 2𝑦 − 1

2𝑥 − 1 0

𝑥𝑦

Linear ODE Systems

Linear ODE Systems

Linear ODE Systems

Linear ODE Systems

Linear ODE Systems

Linear ODE Systems

from Rafael Ramírez slides (https://mat-web.upc.edu/people/rafael.ramirez/slidesEDs_EDOs.pdf)

Linear ODE Systems

Linear ODE Systems

Linear ODE Systems

Linear ODE Systems