Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers /...

21
Ordinary Differential Equations (ODEs) 1 Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften ETH Hönggerberg / HCI F128 – Zürich E-Mail: [email protected] http://www.morbidelli-group.ethz.ch/education/index d () (, ) d yt fty t

Transcript of Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers /...

Page 1: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Ordinary Differential Equations (ODEs)

d ( )( , )

d

y tf t y

t

Daniel Baur

ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften

ETH Hönggerberg / HCI F128 – Zürich

E-Mail: [email protected]

http://www.morbidelli-group.ethz.ch/education/index

Page 2: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

2Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Definition of an Implicit Algorithm

In an implicit algorithm, the function value at the next step appears on the right hand side of the step equation:

A simple example is the Backward Euler method:

1 1 1( , , , )n n n ny t y y F

1 1 1( , )n n n ny y hf t y

Page 3: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

3Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Example

Consider a batch reactor where these reactions take place

In our example k1 = 1 and k2 = 10

1

2

A 2B

B C

k

k

1

1 2

dAA

ddB

2 A Bd

kt

k kt

Page 4: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

4Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Analytical Solution

The system describing first order reactions in a batch reactor (assuming V = const. and T = const.) has the following general form

where A is a (N x N) matrix of constant coefficients The analytical solution to these systems reads

where B is matrix of constant coefficients and λ is the vector of the eigenvalues of A B can be found by imposing that the solution satisfies the initial

differential equaiton and the initial values of y

y y A

exp( )y t B

Page 5: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

5Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Analytical Solution of the Batch Reactor

The Jacobian matrix reads

Its eigenvalues are

Imposing A0 = 1 and B0 = 0, the solution reads

1

1 2

0

2

k

k k

J

1 1 2 2,k k

1

11 2

2 1

A( ) exp( )

2B( ) exp( ) exp( )

t k t

kt k t k t

k k

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

t

A,

B

A

B

Page 6: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

6Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Forward Euler Method

In the linear case, we can look at the Forward Euler method in a different way, by noticing that

With the definition of the Forward Euler algorithm

y will only converge towards a value if the spectral radius of the matrix (I + hJ) is smaller than 1, i.e.

,( , ) , in n n n i k

k

fy f t y y

y

J J

1 ( , )

( )n n n n

n

y y hf t y

h y

I J

max1 1h h I Jmax

2 20.2

10h

Page 7: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

7Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Solution with Forward Euler

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

t

A,

B

h = 0.05

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

t

A,

B

h = 0.10

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

t

A,

B

h = 0.15

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

t

A,

B

h = 0.20

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

t

A,

B

h = 0.21

Page 8: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

8Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Backward Euler Method

As mentioned before, the simplest implicit method is the Backward Euler method

If we substitute our problem

We obtain

In case of linear ODEs, this is a linear system of the form Ax = b that has to be solved at every iteration step

Note that in general this can be a non-linear system

1 1 1( , )n n n ny y hf t y

1 1 1( , )n n nf t y y J

1n nh y y I J

Page 9: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

9Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Stability of the Backward Euler Method

If we solve for the next step, we get

Again the spectral radius of the iteration matrix determines convergence:

As we can see, the Backward Euler method is stable for every system that it is well conditioned, i.e. if Re(λmax) < 0

Algorithms with this property are called A-stable algorithms There can be no stability limitation on the step size, which

also makes the Backward Euler better for stiff systems than the Forward Euler

1

1n ny h y

I J

1

max

11

1h

h

I J

Page 10: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

10Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Implicit Trapezoidal Rule

The implicit trapezoidal algorithm reads as follows

Substituting our problem, we get

This is again an A-stable algorithm

1 1 1( , ) ( , )2n n n n n n

hy y f t y f t y

1 1 1

1

( , )

2 2

n n n

n n

f t y y

h hy y

J

I J I J

max

max

1 / 21 Re 0

1 / 2

h

h

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

t

A,

B

A ExactB Exact

A Euler Backward

B Euler Backward

A TrapezoidB Trapezoid

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

t

A,

B

A ExactB Exact

A Euler Backward

B Euler Backward

A TrapezoidB Trapezoid

h = 0.1 h = 0.2

Page 11: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

11Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

A-Stability of other Algorithms

Explicit Runge-Kutta methods, as well as explicit multi-step methods can never be A-stable Note that the Forward Euler method can be seen as both an explicit

multi-step method or RK method of order 1

Implicit multi-step methods can only be A-stable if they are of order 2 or lower (second Dahlquist barrier)

However, implicit RK methods of higher order can be A-stable

Page 12: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

12Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Multi-Step Algorithms

Multi-step methods use not only the current function value yn to compute the next value yn+1, but also function values at previous times (yn-1, ...)

In implicit solvers, they can be used in predictor / corrector pairs to avoid solving large systems of equations

One example are the Adams-Bashforth-Moulton methods

The corrector step can be looped until it converges, i.e. use the yn+1 from the corrector as the prediction and evaluate the corrector again, until its change is sufficiently small

1 1 1 2 2

1 1 1 1 1

23 ( , ) 16 ( , ) 5 ( , ) AB, Predictor12

5 ( , ) 8 ( , ) ( , ) AM, Corrector12

n n n n n n n n

n n n n n n n n

hy y f t y f t y f t y

hy y f t y f t y f t y

Page 13: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

13Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Convergence of the Corrector Step

Let us reformulate the AM corrector step to incorporate the convergence iteration

This iteration will only converge if the spectral radius of the iteration matrix is smaller than 1, i.e.

This restricts the maximum step size almost as badly as stability issues restrict the Forward Euler method

[ 1] [ ]1 1 1

[ ]1

5 812

5

12

m mn n n n n

mn

hy y y y y

h y k

J J J

J

maxmax

5 5 12 / 51

12 12h h h

J

Page 14: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

14Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Example of the AB/AM Algorithm

Startup was done with the Implicit Trapezoid rule; h = 0.2

The convergence loop more than triples the overall time needed, but it increases stability

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1With Convergence Loop

t

A,

B

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1.5

-1

-0.5

0

0.5

1

1.5

2

2.5No Convergence Loop

t

A,

B

Page 15: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

15Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Implicit Algorithms and Stiff Problems

Since implicit algorithms are generally more stable than explicit algorithms (some are even A-stable!), they fare much better with stiff problems, where the step size is often restricted by stability issues For non-A-stable implicit algorithms, the main goal is usually to get

the largest possible stability region, since this is the main advantage of implicit algorithms

However, this stability comes at the price of larger computational demand per step, which is needed to solve the arising algebraic equation systems There are however highly specialized algorithms to solve systems

arising from implicit solvers, which can take into account special features of the problem like sparseness or bandedness

Page 16: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

16Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Sparse and Banded Matrices

It is computationally very advatageous if sparse or in the best case even banded matrices can be used:

0 20 40 60 80 100

0

10

20

30

40

50

60

70

80

90

100

nz = 510

Sparse: Storing and operating on only

510 non-zero elements (times two for

their position) instead of 10’000

0 20 40 60 80 100

0

10

20

30

40

50

60

70

80

90

100

nz = 298

Banded: All non-zero elements are

grouped in a band around the diagonal,

which further simplifies computations

Page 17: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

17Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Variable Step Size and Order Algorithms

Since the step size h is of such importance for stability and accuracy, sophisticated algorithms adjust it during runtime

This requires error estimation, which is usually done by comparing the result to what the same algorithm produces with a higher order

Some algorithms even adjust their order p dynamically

Page 18: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

18Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Assignment 1

1. Implement the Implicit Trapezoid method (see slide 10) for the batch reactor given on slide 3. Simply use left division «\» to solve the arising linear systems. What is the maximum step size h that still insures stability? What step size h is needed to get a maximum error of 0.1% at all

time points, compared to the analytical solution? Plot the solutions.

Page 19: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

19Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Exercise 2

The following reaction scheme is known as the Oregonator

A and B are held constant

P and Q are constantlywithdrawn, therefore ≈ 0

1

2

3

4

5

A Y X P

X Y 2P

A X 2X 2Z

2X A P

B Z Y

k

k

k

k

k

Page 20: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

20Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Exercise 2 (Continued)

This leads to the following system of ODEs

21 2 3 4

1 2 5

3 5

dXAY XY AX 2 X

ddY

AY XY BZddZ

2 BX BZd

k k k kt

k k kt

k kt

Page 21: Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.

21Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

Assignment 2

1. Solve the system using a suitable built-in Matlab solver (which one? Note the dynamics!), using the following parameters: A = B = 0.5 = const.;

k1 = 1.34; k2 = 1e9; k3 = 8e3; k4 = 4e7; k5 = 1;X0 = Y0 = Z0 = 0.2; tspan = [0, 300];

Plot the solution. What is special, given the fact that this is a model for a chemical reaction?