Intro to Simulink April 15, 2008 http://jagger.me.berkeley.edu/~pack/e177 Copyright 2005-8, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. What is Simulink? First perspective – Ordinary Differential Equation (ODE) solver – Differential equations described by block diagrams – Graphical interface to create/modify block diagrams There’s more, but we will probably not have time to cover – Hybrid systems –… ODE theory Consider an input/output system, with m inputs, q outputs, and governed by a set of n, first-order, coupled differential equations x1 (t ) f1 t , x1 (t ), x2 (t ), , xn (t ), u1 (t ), u2 (t ), , um (t ) x (t ) f t , x (t ), x (t ), , x (t ), u (t ), u (t ), , u (t ) m 2 1 n 2 1 2 2 x (t ) f t , x (t ), x (t ), , x (t ), u (t ), u (t ), , u (t ) m 2 1 n 2 1 n n y1 (t ) h1 t , x1 (t ), x2 (t ), , xn (t ), u1 (t ), u2 (t ), , um (t ) y2 (t ) h2 t , x1 (t ), x2 (t ), , xn (t ), u1 (t ), u2 (t ), , um (t ) yq (t ) hq t , x1 (t ), x2 (t ), , xn (t ), u1 (t ), u2 (t ), , um (t ) Here, f1 is a function that takes in 1+n+m arguments, and returns 1 value. Similar for f2, … and h1, h2, … ODE theory Look at the first n equations x1 (t ) f1 t , x1 (t ), x2 (t ), , xn (t ), u1 (t ), u2 (t ), , um (t ) x (t ) f t , x (t ), x (t ), , x (t ), u (t ), u (t ), , u (t ) 1 2 n 1 2 m 2 2 x (t ) f t , x (t ), x (t ), , x (t ), u (t ), u (t ), , u (t ) 1 2 n 1 2 m n n At each time t, the – current value of t – current value of x1, x2, … – current value of u1, u2,… These are called the state equations, and x is called the state. dictate the current value of the rate-of-change of xi. So, an initial value of x, x(t0) = x0, and the value of u(t) for t≥t0 yield the solution x(t) for t≥t0. Unless the form of the functions fi is particularly simple, solving for x must be done numerically. Output Equations Look at the rest of the equations y1 (t ) h1 t , x1 (t ), x2 (t ), , xn (t ), u1 (t ), u2 (t ), , um (t ) y (t ) h t , x (t ), x (t ), , x (t ), u (t ), u (t ), , u (t ) m 2 1 n 2 1 2 2 y (t ) h t , x (t ), x (t ), , x (t ), u (t ), u (t ), , u (t ) m 2 1 n 2 1 q q At each time t, the – current value of t – current value of x1, x2, … – current value of u1, u2,… dictates the current value of yi. So, these equations are quite simple, just how the outputs y (at time t) are defined as functions of the state (x) and input (u) at time t. Back to the state equations, where the action is… Numerical Solution of ODEs Simulink solves the state equations using numerical integration techniques, such as 4th and 5th order Runge-Kutta formulae. You can learn more about numerical integration by taking Math 128. We will not discuss this important topic in this class. However, to understand in a very crude manner how ODE solvers work, we will revisit the forward Euler method of approximate solution. Write the state equations x1 (t ) f1 t , x1 (t ), x2 (t ), , xn (t ), u1 (t ), u2 (t ), , um (t ) x (t ) f t , x (t ), x (t ), , x (t ), u (t ), u (t ), , u (t ) 1 2 n 1 2 m 2 2 x (t ) f t , x (t ), x (t ), , x (t ), u (t ), u (t ), , u (t ) 1 2 n 1 2 m n n in concise form x (t ) f (t , x(t ), u (t )) Numerical Solution of ODEs Taylor's expansion on a time-dependent function x, ignoring the higher order terms, gives an approximation formula x(t ) x(t ) x (t ) x(t ) f (t , x(t ), u (t ) Using the state equation Roughly, the ``smaller'' δ is, the better is the approximation. Euler's method propagates a solution by repeatedly using this approximation for a fixed δ ,called the stepsize. Writing out the first 4 time steps (ie., t=0, δ, 2δ, 3δ, 4δ) gives x( ) x(2 ) x(3 ) x(4 ) x(0) f (0, x(0), u (0)) x( ) f ( , x( ), u ( )) x(2 ) f (2 , x(2 ), u (2 )) x(3 ) f (3 , x(3 ), u (3 )) and so on. An approximate solution can be quickly propagated by calling the subroutine for f once each timestep. Numerical Solution of ODEs Once the approximate solution, x, is computed, computing the output, y simply involves evaluating the function h y(k ) h(k , x(k ), u (k )) In Runge-Kutta methods, more sophisticated approximations are made, resulting in – more computations (e.g., 4 evaluations of f for every time step), – much greater solution accuracy. In effect, more terms of the Taylor series are used, involving matrices of partial derivatives, and even their derivatives, df d 2 f d 3 f , 2 , 3 x, x, dx dx dx but without actually requiring explicit knowledge of these derivatives of the function f “direct feedthrough” in Output Equations In general, the output equations are of the form y1 (t ) h1 t , x1 (t ), x2 (t ), , xn (t ), u1 (t ), u2 (t ), , um (t ) y (t ) h t , x (t ), x (t ), , x (t ), u (t ), u (t ), , u (t ) 1 2 n 1 2 m 2 2 y (t ) h t , x (t ), x (t ), , x (t ), u (t ), u (t ), , u (t ) 1 2 n 1 2 m q q If, in fact, h does not have explicit u dependence, then the system is said to “have no direct feedthrough from u to y” y1 (t ) h1 t , x1 (t ), x2 (t ), , xn (t ) y (t ) h t , x (t ), x (t ), , x (t ) 1 2 n 2 2 y (t ) h t , x (t ), x (t ), , x (t ) 1 2 n q q Note: no direct feedthrough does not mean that u doesn’t affect y. It is still true that u drives x in the state equations, and the values of x define the values of y. ODE notation Recall, for shorthand, we write the state and output equations x1 (t ) f1 t , x1 (t ), x2 (t ), , xn (t ), u1 (t ), u2 (t ), , um (t ) x (t ) f t , x (t ), x (t ), , x (t ), u (t ), u (t ), , u (t ) 1 2 n 1 2 m 2 2 x (t ) f t , x (t ), x (t ), , x (t ), u (t ), u (t ), , u (t ) 1 2 n 1 2 m n n y1 (t ) h1 t , x1 (t ), x2 (t ), , xn (t ), u1 (t ), u2 (t ), , um (t ) y2 (t ) h2 t , x1 (t ), x2 (t ), , xn (t ), u1 (t ), u2 (t ), , um (t ) yq (t ) hq t , x1 (t ), x2 (t ), , xn (t ), u1 (t ), u2 (t ), , um (t ) as x (t ) f (t , x(t ), u (t )) y (t ) h(t , x(t ), u (t )) State equations Output equations General Form of a Continuous-Time block Input: u (vector valued) Output: y (vector valued) Internal State: x (vector valued) u Blk x Governing relationship between these signals is of the form x (t ) f (t , x(t ), u (t )) y (t ) h(t , x(t ), u (t )) Each block “knows” – the functions f and h which define its behavior, and – at all times, the current value of its internal state x • Initial value is specified by user. Implicitly, all blocks have time as an input. y Sources and Sinks Source: a block with no inputs, only outputs – Typical example: fixed “waveform” signal Src x y Sink: a block with no outputs, only inputs – Specific examples are blocks that represent data-logging (blocks that capture signals, but don’t “pass” anything on to another block) u Sink x Evolving a solution: Psuedo-code Current time: t Loop through blocks, determine output of each block – This uses the current time, and internal state x and output function h associated with the block. Using diagram, determine input to each block – The diagram dictates how the inputs to blocks are combinations of outputs of blocks. If the outputs are already known, then the inputs are determinable Using governing equations, determine x for each block – This uses the current time, and internal state x and state function f associated with the block. Advance time, to t+δ – Use numerical integration formula (Euler, Backwards Euler, RungeKutta, etc) to advance one time-step Update internal state x for each block. Repeat. Simulink Library The Simulink Library has folders of prebuilt blocks that have well-defined behaviors. We will primarily use 6 Libraries – Continuous – Math Operations – Signal Routing – Sinks – Sources – User-defined Functions Continuous Library One commonly used block in continuous-time library is the Integrator. The appearance and equations of the block are u 1 s y x (t ) u (t ) y (t ) x(t ) Equivalently t y(t ) x(0) u ( ) d 0 hence the name. The user specifies the value of x(0) by double-clicking on the icon, and entering value in dialog box. Running Simulink from Command line/function The sim command can be used to run a simulation from the command line, or inside a function, e.g., >> sim(‘ModelName’,TimeSpan,Options) function [arg1,arg2] = analyzesim(ModelName) ... Opt = simset(); % set options sim(ModelName,TSpan,Opt); ... The simset command sets options for the simulation. Two useful properties when sim is invoked in a function are SrcWorkspace: The workspace where dialog box entries are evaluated Default: ‘base’. Other options include ‘current’ and ‘parent’ DstWorkspace: The workspace where to assign variables defined in model Default is ‘current’. Other options are ‘base’ and ‘parent’ Simple Examples Build two simple examples using – Step Function (from Source) – Integrator (from Continuous) – Scope (from Sink) The files are e177model1.mdl and e177model2.mdl Be sure to create a variable named mygain in the workspace before running model2 – recall this is what is entered in the dialog box of the Gain block. A more complicated example is from ME132. For illustrative purposes, the files are given. The setup and model files are –setupAW132.m –exampleAW132.m Run the setup file, then run the simulations. Be sure to try changing the Antiwindup gain, as well as the gain on the measurement noise.