Cyber-Physical Systems Modeling and Simulation of Continuous Systems Matthias Althoff TU München 29. May 2015 Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 1 / 38
Ordinary Differential Equations Continuous Systems Continuous systems describe the dynamics of systems that can be described by a set of continuous states (e.g. position, temperature, etc.). Starting from an initial state vector x(t 0 ) and for a given input signal u(t), a continuous system creates a continuous output signal y(t). We always assume that the state, the input, and the output are vectors of proper dimension. Static systems are special cases, where the output directly follows from a function of the input: y(t) = g(u(t)). u(t) continuous system y(t) Train example from the first lecture: desired velocity v d velocity v t + controller t Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 2 / 38
Ordinary Differential Equations Continuous System in State Space Form Definition A continuous system in state space form C is a tuple (ordered set): where x(0) R n is the initial state, and C = (X,U c,y c,f,g,x(0)), X R n set of states U c R m set of continuous inputs Y c R o set of continuous outputs f : X U c X flow function ẋ(t) = f(x(t),u(t)) g : X U c Y c output function y(t) = g(x(t),u(t)) Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 3 / 38
Ordinary Differential Equations Example I: Mass Spring Damper (MSD) System mass m position s stiffness c damping coefficient d Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 4 / 38
Ordinary Differential Equations Equations of Each Component (MSD System) Variables: s (position), v = ṡ (velocity), a = v = s (acceleration), F e (external force); Parameters: m (mass), d (damping coefficient), c (stiffness), s 0 (position of relaxed spring) Mass F a s mass m F a = ma = m s. Damper F d s F d = d v = d ṡ. Spring F s s F s = c (s s 0 ). Sum of forces equals 0: m s +d ṡ +c(s s 0 )+F e = 0 Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 5 / 38
Ordinary Differential Equations Transformation to State Space Form (MSD System) The differential equation m s +d ṡ +c(s s 0 )+F e = 0 is of second order. To transform it in state space form we need a set of first order differential equations. This is achieved by introducing a new state variable for each higher derivative that has to be lowered: x 1 := s x 2 := v We also introduce the input u(t) = F e (t). The final set of differential equations is ẋ 1 = x 2, ẋ 2 = 1 m ( c(x 1 s 0 ) d x 2 u), which has the form ẋ(t) = f(x(t),u(t)). The output is the position of the mass: y = x 1. Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 6 / 38
Ordinary Differential Equations Example II: RLC Circuit inductance L current i e resistance R capacitance C voltage v Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 7 / 38
Ordinary Differential Equations Equations of each Component (RLC Circuit) Variables: i (current), v (voltage), i e (external current); Parameters: R (resistance), C (capacitance), L (inductance). Resistance resistance R i R v i R = 1 R v. Conductance i C v i C = C v. Inductance i L v i L = 1 L t t 0 v(τ)dτ +i L (t 0 ). Sum of currents equals 0: C v + 1 R v + 1 t L t 0 v(τ)dτ +i L (t 0 )+i e = 0. Differentiation with respect to time yields: C v + 1 R v + 1 L v + d dt i e = 0 Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 8 / 38
Ordinary Differential Equations Transformation to State Space Form (RLC Circuit) The differential equation C v + 1 R v + 1 L v + d dt i e = 0 is of second order. Analogously to the MSD system, we introduce state variables: x 1 := v x 2 := v We also introduce the input u(t) = d dt i e(t). The final set of differential equations is ẋ 1 = x 2, ẋ 2 = 1 C ( 1 L x 1 1 R x 2 u), which has the form ẋ(t) = f(x(t),u(t)). The output is the voltage: y = x 1. Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 9 / 38
Ordinary Differential Equations Comparison of Example I and II The mechanical and the electrical system have the same structure. Mechanical: Electrical: ẋ 1 = x 2, ẋ 2 = 1 m ( c(x 1 s 0 ) d x 2 u), y = x 1, ẋ 1 = x 2, ẋ 2 = 1 C ( 1 L x 1 1 R x 2 u), y = x 1, Their behavior is identical for m = C, c = 1 L, d = 1 R, and s 0 = 0. From now on, we will only use the abstract state space form, which represents all kinds of systems: mechanical, electrical, thermal, chemical, etc. Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 10 / 38
Analytical Solutions of Ordinary Differential Equations Analytical Solutions For a very limited class of ODEs, analytical solutions exist (i.e. solutions that can be expressed analytically in terms of a finite number of certain well-known functions). There exist formularies for known solutions. We will only look at the technique separation of variables. Given is the ODE Rearranging yields dυ dξ = g(ξ) f(υ). 1 dυ f(υ) dξ = g(ξ), and integrating both sides with respect to ξ results in 1 dυ f(υ) dξ dξ = g(ξ)dξ, or equivalently, 1 f(υ) dυ = g(ξ)dξ. Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 11 / 38
Analytical Solutions of Ordinary Differential Equations Example: Solution for Torricelli s Law (I) 2R h(t) The separation of variables makes it possible to solve the differential equation for a liquid that flows out of a tank according to Toricelli s law: 2r ḣ = k h, k = r R 2g, where h is the height of the fluid in the tank, r and R are the radii of the hole and the tank, and g is the gravity constant, see figure above. With υ = h, ξ = t, we obtain dυ dξ = g(ξ) f(υ), g(ξ) = 1, f(υ) = k υ = kυ 0.5. Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 12 / 38
Analytical Solutions of Ordinary Differential Equations Example: Solution for Torricelli s Law (II) Integration yields 1 f(υ) dυ = g(ξ)dξ = 1 k υ 0.5 dυ = 2 k υ0.5 +C 1 1dξ = ξ +C 2 Since 1 f(υ) dυ = g(ξ)dξ, one obtains 2 k υ0.5 = ξ +C υ 0.5 = k (ξ +C) 2 ( υ = k ) 2. 2 (ξ +C) The constant C is obtained from the initial state υ(0) = h 0 : υ(0) = ( k 2 C)2 = h 0 C = ± 2 k h0. Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 13 / 38
Analytical Solutions of Ordinary Differential Equations Example: Solution for Torricelli s Law (III) Inserting υ = h and ξ = t yields the solution ( h(t) = h0 k ) 2. 2 t Since the differential equation is no longer defined for h(t) = 0 (tank is empty), we obtain the final result ( h0 ) 2 k h(t) = 2 t for 0 t 2 h 0 k, 0 for 2 h 0 k < t. h(t) h 0 2 h 0 k t Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 14 / 38
Analytical Solutions of Ordinary Differential Equations Linear Time-Invariant Ordinary Differential Equations An important class are linear time invariant (LTI) ordinary differential equations since many technical systems can be described by LTI systems, LTI systems have an analytical solution, the solution for the initial state and the input can be obtained separately (superposition principle, see later). A one-dimensional LTI system is ẋ(t) = ax(t)+u(t), where x is the state and u is the input. We first consider the homogeneous solution (u(t) = 0): ẋ(t) = ax(t), for which separation of variables can be applied using υ = x, ξ = t. Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 15 / 38
Analytical Solutions of Ordinary Differential Equations Homogeneous Solution of Scalar LTI Systems Integration yields 1 f(υ) dυ = 1 aυ dυ = 1 a ln υ +C 1; Since 1 f(υ) dυ = g(ξ)dξ, one obtains 1 ln υ = ξ +C a υ = e a(ξ+c) υ = C e (aξ) g(ξ)dξ = The constant C is obtained from the initial state υ(t 0 ) = x 0 : υ(t 0 ) = C e (at0) = x 0 C = e ( at0) x 0. 1dξ = ξ +C 2 Homogeneous solution x (h) (t) = e a(t t0) x 0 Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 16 / 38
Analytical Solutions of Ordinary Differential Equations Particular Solution of Scalar LTI Systems We assume that variation of parameters (German: Variation der Konstanten ) yields the particular solution x (p) (t) for x 0 = 0, u(t) 0: x (p) (t) = c(t)x (h) (t). Re-arranging the LTI system and inserting x (p) (t) results in from which follows that c(t) = u(t) = ẋ (p) (t) ax (p) (t) t = ċ(t)x (h) (t)+c(t)ẋ (h) (t) c(t)ax (h) (t) }{{} =0 since ẋ (h) (t)=ax (h) (t) 1 u(τ)dτ +C = t 0 x (h) (τ) Particular solution (x 0 = 0 C = 0) x (p) (t) = c(t)x (h) (t) = t t t 0 e a(τ t0) u(τ)dτ +C t 0 e a(t τ) u(τ)dτ Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 17 / 38
Analytical Solutions of Ordinary Differential Equations Superposition Principle of LTI Systems Superposition principle If x (1) (t) and x (2) (t) are solutions of ẋ(t) = ax(t)+u(t) for x (1) 0, x(2) 0 and u (1) (t), u (2) (t), respectively, then x(t) = αx (1) (t)+βx (2) (t) is a solution for x 0 = αx (1) 0 +βx (2) 0 and u(t) = αu (1) (t)+βu (2) (t). Proof: x(t) = αx (1) (t)+βx (2) (t) ẋ(t) = αẋ (1) (t)+βẋ (2) (t) = α ( ax (1) (t)+u (1) (t) ) +β ( ax (2) (t)+u (2) (t) ) = a ( αx (1) (t)+βx (2) (t) ) +αu (1) (t)+βu (2) (t) = ax(t)+u(t). Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 18 / 38
Analytical Solutions of Ordinary Differential Equations Solution of Scalar LTI Systems Due to the superposition principle, the final solution is a combination of the homogeneous solution and the particulate solution: Solution for one-dimensional LTI systems x(t) = x (h) (t)+x (p) (t) = e a(t t 0) x 0 + t t 0 e a(t τ) u(τ)dτ Example: ẋ(t) = x(t)+u(t), x 0 = 1, u(t) = 2 (const) The solution is x (h) (t) = e t x (p) (t) = 2 t t 0 e (t τ) dτ = 2(1 e (t t 0) ) The particular solution always becomes dominant for stable systems. Why? x (h) (t) 1 0.5 0 x (p) (t) 2 1 0 x(t) 2 1.5 1 0 t 5 10 Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 19 / 38
Analytical Solutions of Ordinary Differential Equations Linear Time-Invariant Systems (n-dimensional) We are now considering n-dimensional instead of one-dimensional LTI systems: ẋ(t) = Ax(t)+u(t), where x R n is the state, u(t) R n is the input, and A R n n is the system matrix. The solution is analogous to one-dimensional LTI systems: Solution for n-dimensional LTI systems x(t) = x (h) (t)+x (p) (t) = e A(t t0) x 0 + t t 0 e A(t τ) u(τ)dτ The exponential matrix is defined as the power series e X 1 = k! Xk = I +X + 1 2! X2 + 1 3! X3 +... k=0 where I is the identity matrix. From this follows that d dt eat = d dt I + d dt At + d 1 dt 2! (At)2 +... = A+A 1 1! (At)+A 1 2! (At)2 +... = Ae At Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 20 / 38
Analytical Solutions of Ordinary Differential Equations Proof of Solution for Linear Time-Invariant Systems To prove correctness, we require the Leibniz integral rule: d dx v(x) u(x) Differentiation of x(t) yields F(x,φ)dφ = F(x,v) dv v(x) dx F(x,u)du dx + u(x) x F(x,φ)dφ t ẋ(t) = d dt ea(t t0) x 0 + d e A(t τ) u(τ)dτ dt t } 0 {{} =Ae A(t t0) x 0 +u(t)+ ( t =A e A(t t0) x 0 + Leibniz t t 0 Ae A(t τ) u(τ)dτ e A(t τ) u(τ)dτ t 0 =Ax(t) + u(t) ) +u(t) Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 21 / 38
Analytical Solutions of Ordinary Differential Equations Example: Mass Spring Damper (MSD) System Reminder: ẋ 1 = x 2, ẋ 2 = 1 m ( c(x 1 s 0 ) d x 2 û). After introducing the state matrix A and the input vector u as [ ] [ ] 0 1 0 A = c m d, u = c m m s 0 mû 1 The system dynamics can be written as ẋ(t) = Ax(t)+u(t). We use parameters from a quarter car model: m [kg] c [N/m] d [Ns/m] s 0 [m] 450 25000 1300 0 Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 22 / 38
Analytical Solutions of Ordinary Differential Equations Exact Solution of MSD System x1(t) 0.2 0 1 0.2 0 2 4 t 1 x2(t) 0 1 x2(t) 0 1 0 2 4 t 0.2 0 0.2 x 1(t) Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 23 / 38
Numerical Solutions of Ordinary Differential Equations Numerical Solutions Analytical solutions only exist for specific nonlinear ODEs and linear ODEs. All other systems have to be solved using numerical techniques. We will start with simple techniques and move towards more sophisticated techniques used in commercial tools (such as MATLAB/Simulink): Euler method Heun method Runge-Kutta method Runge-Kutta-Fehlberg method For simplicity, we will not consider inputs from now on. Inputs, however, can be easily integrated in each method. Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 24 / 38
Numerical Solutions of Ordinary Differential Equations Euler Method The Euler method is the simplest algorithm for approximating solutions of ODEs. The solution of the ODE ẋ(t) = f(x(t)) for a future point in time is x(t k+1 ) = x(t k )+ tk+1 t k f(x(τ))dτ. Since the integral cannot be exactly obtained in general, one can use: Euler method x(t k+1 ) x(t k )+(t k+1 t k )f(x(t k )). }{{} =:h The Euler method is an explicit method since it only uses values from the past. x(t) exact solution x(t k ) x(t k+1 ) h t k t k+1 Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 25 / 38 t
Numerical Solutions of Ordinary Differential Equations Heun Method Heun s method does not only use the slope at the previous time step, but the average slope from the previous and the next time step: x(t k+1 ) x(t k )+(t k+1 t }{{ k ) 1 } 2 =:h (f ( x(t k ) ) +f ( x(t k+1 ) )) The above scheme is also referred to as the trapezoidal rule, which is an implicit method since it requires future values. Solving for x(t k+1 ) requires solving a set of nonlinear equations, which is typically done using Newton s method. Heun s method approximates x(t k+1 ) by Euler s method: Heun s method x(t k+1 ) x(t k )+h 1 2 (K 1 +K 2 ) K 1 = f ( x(t k ) ) K 2 = f ( x(t k )+hk 1 ) Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 26 / 38
Numerical Solutions of Ordinary Differential Equations Illustration of Heun Method x(t) K 2 x(t k ) K 1 K 2 1 (K1 +K2) 2 x(t k+1 ) t k t k+1 t Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 27 / 38
Numerical Solutions of Ordinary Differential Equations Runge-Kutta Method The Runge-Kutta method is a very popular method to numerically solve ODEs. To further improve the accuracy, a new supporting point and four function evaluations are used: Runge-Kutta method x(t k+1 ) x(t k )+h 1 6 (K 1 +2K 2 +2K 3 +K 4 ) K 1 = f ( x(t k ) ) K 2 = f ( x(t k )+ 1 2 hk ) 1 K 3 = f ( x(t k )+ 1 2 hk ) 2 K 4 = f ( ) x(t k )+hk 3 Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 28 / 38
Numerical Solutions of Ordinary Differential Equations Illustration of Runge-Kutta Method K 4 x(t) K 3 x(t k+1 ) x(t k ) K 1 K 2 t k t k + h t k+1 2 t Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 29 / 38
Numerical Solutions of Ordinary Differential Equations Local Truncation Error The Heun method requires 2 function evaluations and the Runge-Kutta method even 4 function evaluations. Is the additionally effort justified? Yes! The accuracy is orders of magnitude better, making it possible to extend the step size (see exercise). Order of numerical methods The method has order p if there exist constants h and C such that x e (t k +h) x(t k +h) h C hp One can show that method: Euler Heun Runge-Kutta order: 1 2 4 Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 30 / 38
Numerical Solutions of Ordinary Differential Equations Comparison of Methods for h=0.01 We use the MSD system from previous slides to compute the error for h = 0.01 to the exact solution (exact solution exists for linear systems). 0.02 Euler Heun Runge Kutta 0.01 error of x 1 0 0.01 0.02 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 t Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 31 / 38
Numerical Solutions of Ordinary Differential Equations Comparison of Methods for h=0.01 We use the MSD system from previous slides to compute the error for h = 0.01 to the exact solution (exact solution exists for linear systems). 0.2 0.15 0.1 Euler Heun Runge Kutta error of x 2 0.05 0 0.05 0.1 0.15 0.2 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 t Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 31 / 38
Numerical Solutions of Ordinary Differential Equations Comparison of Methods for h=0.1 We use the MSD system from previous slides to compute the error for h = 0.1 to the exact solution (exact solution exists for linear systems). 0.5 Euler Heun Runge Kutta error of x 1 0 0.5 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 t Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 32 / 38
Numerical Solutions of Ordinary Differential Equations Comparison of Methods for h=0.1 We use the MSD system from previous slides to compute the error for h = 0.1 to the exact solution (exact solution exists for linear systems). 0.5 Euler Heun Runge Kutta error of x 2 0 0.5 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 t Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 32 / 38
Numerical Solutions of Ordinary Differential Equations Discretization and Rounding Error Numerical techniques suffer from two kinds of errors: Discretization error Refers to the error due to time discretization. Smaller step sizes h decrease the discretization error. Rounding error Refers to the error due to the finite number of digits to represent numbers in computers. Larger step sizes h increase the number of time steps and thus the number of computations with rounding errors. error There exists an optimal step size for the total error: discretization error total error rounding error h Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 33 / 38
Numerical Solutions of Ordinary Differential Equations Comparison of Step Sizes Error using Runge-Kutta for different step sizes: x 10 11 h=1e 3 6 x 10 14 h=1e 4 error of x 1 1 0 1 error of x 1 4 2 0 2 4 0 1 2 3 4 5 t 6 0 1 2 3 4 5 t 5 x 10 13 h=1e 5 error of x 1 0 5 The best step size is h = 1e 4. 10 0 1 2 3 4 5 t Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 34 / 38
Numerical Solutions of Ordinary Differential Equations Adaptive Step Size The discretization error varies depending on the characteristics of the ODE. In some areas, f(x) is sensitive with respect to changes of x, i.e. f(x) is large, while it is small elsewhere. x A popoular method that is based on the Runge-Kutta family of methods is the Runge-Kutta-Fehlberg method. It is implemented in MATLAB/Simulink as ode45. Basic idea of the Runge-Kutta-Fehlberg method 1 Compute one-step solution with lower order Runge-Kutta method (order 4). 2 Compute one-step solution with higher order Runge-Kutta method (order 5). 3 If the error is large, the step size is shortened, otherwise the step size is enlarged. Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 35 / 38
Numerical Solutions of Ordinary Differential Equations Runge-Kutta-Fehlberg Method K 1 = f ( x(t k ) ), K 2 = f ( x(t k )+ 1 4 hk ) 1 K 3 = f ( x(t k )+ 3 32 hk 1 + 9 32 hk ) 2 K 4 = f ( x(t k )+ 1932 2197 hk 1 7200 2197 hk 2 + 7296 2197 hk 3 K 5 = f ( x(t k )+ 439 216 hk 1 8hK 2 + 3680 513 hk 3 845 4104 hk 4 K 6 = f ( x(t k ) 8 27 hk 1 +2hK 2 3544 2565 hk 3 + 1859 4104 hk 4 11 40 hk 5 The solution of the 4 th order method is ( 25 x(t k+1 ) = x(t k )+h 216 K 1 + 1408 2565 K 3 + 2197 4101 K 4 1 ) 5 K 5 The solution of the 5 th order method is ( 16 x(t k+1 ) = x(t k )+h 135 K 1 + 6656 12825 K 3 + 28561 56430 K 4 9 50 K 5 + 2 ) 55 K 6 The new step size for a specified error tolerance tol is ( tolh h := 2 x(t k+1 ) x(t k+1 ) )1 4 h. ) ) ) (K 2 not used). (K 2 not used). Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 36 / 38
Numerical Solutions of Ordinary Differential Equations Adaptive Step size for MSD System We use the MSD system from previous slides to compute the varying step size h for tol = 1e 4 and the initial step size h = 0.01. 0.2 0.15 h 0.1 0.05 0 10 20 30 40 50 60 70 step nr Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 37 / 38
Numerical Solutions of Ordinary Differential Equations Adaptive Step size for MSD System We use the MSD system from previous slides to compute the varying step size h for tol = 1e 4 and the initial step size h = 0.01. 0.3 0.2 Runge Kutta Fehlberg exact 0.1 x 1 0 0.1 0.2 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 t The solution is very accurate for this system. However: One should always be skeptical with numerically obtained results. Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 37 / 38
Numerical Solutions of Ordinary Differential Equations Conclusions The main modeling formalism for dynamical continuous systems are ordinary differential equations (ODEs). There exist only a very limited amount of analytical solutions for OSEs. If they exist, one should prefer them over numerical solutions, of course. There exist analytical solutions for linear time-invariant systems. Numerical solutions of ODEs are best obtained with higher-order methods due to their preferred ratio of accuracy and computation time. Adaptive step size typically provides better results than fixed step size. One should always be skeptical with numerically obtained results. Matthias Althoff Modeling and Simulation of Cont. Systems 29. May 2015 38 / 38