Modeling & Simulation 2018 Lecture 12. Simulations Claudio Altafini Automatic Control, ISY Linköping University, Sweden
Summary of lecture 7-11 1 / 32 Models of complex systems physical interconnections, not block diagrams supporting theory: bond graphs implementing language: modelica associated technical difficulty: DAE (lecture 11) Differentiation index Index = 1 easy Index 2 difficult Linear DAE-models, standard form Nonlinear DAE-models
Lecture 12. Numerical methods and simulations 2 / 32 Summary of today Overview of the different numerical methods for simulations Properties Accuracy Stability Variable step length Stiffness Methods for DAE In the book: Swedish: chapter 15 English: chapter 14
Standard formulation of a simulation problem 3 / 32 Given an ODE or a DAE: ẋ = f(x) F (ẋ, x) = 0 Numerical solution: assume you want to compute approximations of x at times 0 < t 1 < t 2 < t 3 < < t f. then your algorithm has to compute the values x 1, x 2, x 3,..., x f that approximate x(t 1 ), x(t 2 ), x(t 3 ),..., x(t f )
Euler forward method 4 / 32 The simplest methods are based on approximating the derivative with a finite difference: ẋ(t n ) x n+1 x n h the step length h is given by h = t n+1 t n If forward-difference and state space description are used then we have the (forward) Euler method: x n+1 = x n + hf(x n ) Leonhard Euler 1770
x Euler forward method: example 5 / 32 ODE Euler forward approximation ẋ = 10x x n+1 = x n + h ( 10x n ) = (1 10h)x n 1.2 1 0.8 cont h=0.01 h=0.05 h=0.1 0.6 0.4 0.2 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 time
Euler backward differences 6 / 32 differences can be taken also backwards ẋ(t n ) x n x n 1, h = t n t n 1 h When backward differences and state space models are used then one gets the Euler backward method x n = x n 1 + hf(t n, x n ) Backwards differences and DAE give F ( x n x n 1, x n, t n ) = 0 h Notice that this method requires solving a system of equations in x n
x Euler backward method: example ODE Euler backward approximation ẋ = 10x x n = x n 1 + h ( 10x n ) = 1 1 + 10h x n 1 7 / 32 1.2 1 0.8 cont h=0.01 h=0.05 h=0.1 0.6 0.4 0.2 0 0 0.2 0.4 0.6 0.8 1 1.2 time
General simulation method 8 / 32 More generally, given a simulation method for a state model of the type x n+1 = G(t n, x n k+1, x n k+2,..., x n, x n+1 ) k -step method: the new value of x is calculated from the previous k values of x Explicit method: G does not contain x n+1 Implicit method: G containing x n+1 Euler forward: explicit one-step method Euler backward: implicit one-step method
Why are there different approaches? 9 / 32 in matlab: many ODE solvers: ode45, ode23, ode113, ode15s, ode23s, ode23t, ode23tb examples of properties that must be handled Accuracy Complexity Stability Dealing with discontinuities Dealing with stiff systems Dealing with DAE which method is best depends on which property is considered the most interesting in a certain context
Accuracy 10 / 32 Global error E n = x(t n ) x n Can only be calculated exactly if the true solution is known exactly Local error = one-step error (provided all information from previous steps is exact); can often be calculated directly from the Taylor expansion Local error is typically proportional to h p+1 for some integer p: O(h p+1 ) Global error is then typically proportional to h p : O(h p ) number p is the order of the method Example: Euler method has the order 1
Stability The stability of the numerical simulation need not correspond to the stability of the underlying differential equation Example: Test equation 11 / 32 ẋ = λx, λ complex Stability region: Re(λ) < 0 Euler forward approximation x n+1 = (1 + hλ)x n Stability region of the Euler forward approximation: 1 + hλ < 1 The step size that can be selected is determined by both the accuracy and stability considerations
x Stability of Euler forward method: example ODE ẋ = 10x Euler forward approximation 12 / 32 x n+1 = x n + h ( 10x n ) = (1 10h)x n 1.5 1 0.5 h=0.01 h=0.08 h=0.2 h=0.21 0-0.5-1 -1.5 0 0.2 0.4 0.6 0.8 1 1.2 time When h > 0.2 solution grows unlimited
Stability of Euler backward method 13 / 32 Euler backward approximation: x n+1 = x n + hf(x n+1 ) Example: Test equation ẋ = λx, λ complex Stability region of the Euler backward approximation 1 hλ > 1 Stable systems can be simulated with arbitrary long step size without incurring in stability problems
x Stability of Euler backward method: example 14 / 32 ODE Euler backward approximation x n+1 = ẋ = 10x 1 1 + 10h x n 1.2 1 0.8 h=0.01 h=0.08 h=0.2 h=0.5 0.6 0.4 0.2 0 0 0.2 0.4 0.6 0.8 1 1.2 time stability is not lost when when h increases
Runge-Kutta-method 15 / 32 Idea: compute f at intermediate points k 1 = f(t n, x n ) k 2 = f(t n + h/2, x n + hk 1 /2) order = 2 x n+1 = x n + hk 2 A classical Runge-Kutta-method k 1 = f(t n, x n ) k 2 = f(t n + h/2, x n + hk 1 /2) k 3 = f(t n + h/2, x n + hk 2 /2)) k 4 = f(t n + h, x n + hk 3 ) x n+1 = x n + h(k 1 + 2k 2 + 2k 3 + k 4 )/6 order = 4
Adam s methods 16 / 32 general formula k x n = x n 1 + β j f n j f i = f(t i, x i ) j=0 explicit k-step Adams method k = 1 : x n = x n 1 + hf n 1 k = 2 : x n = x n 1 + h (3f n 1 f n 2 ) /2 k = 3 : x n = x n 1 + h (23f n 1 16f n 2 + 5f n 3 ) /12 k = 4 : x n = x n 1 + h (55f n 1 59f n 2 + 37f n 3 9f n 4 ) /24 order = k
Adam s methods 17 / 32 Implicit k-step Adams method k = 0 : x n = x n 1 + hf n k = 1 : x n = x n 1 + h (f n + f n 1 ) /2 k = 2 : x n = x n 1 + h (5f n + 8f n 1 f n 2 ) /12 k = 3 : x n = x n 1 + h (9f n + 19f n 1 5f n 2 + f n 3 ) /24 order = k + 1
Stability region for explicit Adams method 18 / 32 1.5 1 0.5 Im 0 k=1 k=2 k=3 k=4-0.5-1 -1.5-2.5-2 -1.5-1 -0.5 0 0.5 Re
Stability region for implicit Adams method 19 / 32 4 k=1 3 2 Im 1 0 k=2 k=3-1 -2-3 -4-6 -5-4 -3-2 -1 0 Re
Variable step length 20 / 32 Many modern methods use variable step length, to obtain the desired accuracy with minimum calculation work The following principles are often used the user specifies the desired local error the algorithm has a method to estimate the local error if the estimated local error is large h is reduced if the estimated local error is unnecessarily small h is increased Computing a suitable step length at each step requires performing extra calculations per step. It can anyway reduce the total amount of calculations because it reduces the total number of steps
Stiff differential equations 21 / 32 Stiffness: fast and slow dynamics in the same model Problem: The stability requirement could limit the step length more than the accuracy requirement Consequence: simulation can become very slow Methods for stiff problems - often implicit
Stiff differential equations: example 22 / 32 Example: ẋ = [ ] [ ] 10001 10000 2 x, x(0) = 1 0 1.0001 2 2 1.5 1.5 1 1 0.5 0.5 x 0 x 0 0.5 0.5 1 1 1.5 0 2 4 6 8 10 time 1.5 0 0.05 0.1 0.15 0.2 time
Differential-algebraic problem 23 / 32 F (ż, z, t) = 0 Simple idea: replace ż with backward difference zn zn 1 h This gives the equation F ( z n z n 1, z n, t n ) = 0 h to be solved at each iteration (often using Newton s method) More generally: one can replace ż with a higher order backward difference formula: k ż α i z n i i=0 where the coefficients α i are chosen so that the approximation error gows as O(h k+1 )
Index = 1 24 / 32 Let z(t) = z(t) + w(t) and let F (ż, z, t) = 0 have the linear approximation If 1. A has constant rank 2. (A, B) has index = 1 Aẇ + Bw = g(t) with A = F ż ( z, z, t), B = F z ( z, z, t) for all z, ż in a neighborhood of the current solution, = so does the backward difference method F ( k α i z n i, z n, t n ) = 0 i=0 for k < 7, and provides an accuracy O(h k )
Other methods 25 / 32 For DAE there are other methods for example implicit Runge-Kutta methods Also these work in principle straightforwardly for problems with index = 1 For problems with higher index, the numerical methods or the DAE structure must be modified before the problem is solved
ODE solvers in matlab 26 / 32 ode45: explicit Runge-Kutta; one-step solver ode23: explicit Runge-Kutta; one-step solver ode113: variable order Adams-Bashforth-Moulton solver; multistep solver ode15s: based on the numerical differentiation (or backward differentiation) formulas; multistep solver ode23s: based on a modified Rosenbrock formula of order 2; one-step solver ode23t: implementation of the trapezoidal rule; can solve DAEs ode23tb: 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
ODE solvers in matlab 27 / 32
Final slides: Summary 28 / 32
Final slides: Lab 29 / 32 Lab 1: all reports due: October 9 (passed) peer review due: October 16 (tomorrow) revised report due: October 23 (next Tuesday) Lab 2 & 3: should be completed this week
Final slides: Exam time: 2015-10-29, kl. 14-18 place: ISY datorsalen (Egypten, Asgård, Olympen, maybe Southfork) MAI datorsalen (Roxen, Boren, only if needed) 30 / 32 Meeting point: corridor in front of Egypten/Asgård
Final slides: Exam Tips for exam: see webpage 31 / 32 Be there a few minutes in advance Login in: use the exam account (tentamenxxx) you find in the exam sheet When printing out: write your AID number on the page (so that the guards know who is printing) Computer part of the exam: normally Matlab + System Identification Toolbox
One more final slide 32 / 32 Exjobb thesis in Complex Networks How to control complex (technological, social, biological) networks?? how to study dynamics over social networks Exjobb thesis in Systems Biology Structure and organization of biological networks (metabolic networks, gene networks, drug-target network) Modeling signaling pathways For details, contact me (claudio.altafini@liu.se)
Thank you!! www.liu.se