Process Automation CHEM-E7140

Similar documents
SIMULINK FOR BEGINNERS:

Experiment 8 SIMULINK

Experiment 6 SIMULINK

SIMULINK Tutorial. Select File-New-Model from the menu bar of this window. The following window should now appear.

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Introduction to Simulink

Session 3 Introduction to SIMULINK

Introduction to Matlab Simulink. Control Systems

SIGNALS AND LINEAR SYSTEMS LABORATORY EELE

Simulink Basics Tutorial

Introduction to Simulink. The Use of Mathematic Simulations in Electrical Engineering

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

UNIT 5. Simulink. 1. Introduction

Simulink Basics Tutorial

SIMULINK A Tutorial by Tom Nguyen

BME 5742 Bio-Systems Modeling and Control

Newsletter Issue 96 October 2004 MODELING OF PLANT CONTROLLERS USING THE PSS/E TO MATLAB-SIMULINK INTERFACE (PMSI) IN PSS/E 30

2 SIMULATING A MODEL Simulink Tutorial

Introduction to Simulink

Note 10 Introduction to MATLAB & SIMULINK

ProMoT Step by Step for Reaction Models in Process Engineering A Tutorial

[ MATLAB ] [ Resources ] PART TWO: SIMULINK

ELEC ENG 4CL4 CONTROL SYSTEM DESIGN

Lecture 10: Simulink. What is Simulink?

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

Systems & Control Lab.-Manual

What is Simulink. >>simulink

Objectives. Simulink Basics

Computer Aided Design (CAD) Lecture 10. Introduction to Simulink (3) Dr.Eng. Basem ElHalawany

Experiment 3. Getting Start with Simulink

MATLAB AND SIMULINK. Modeling Dynamic Systems J. ABELL

ME422 Mechanical Control Systems Matlab/Simulink Hints and Tips

EES Program Overview

8438/8838 User Manual User Manual of the 8438/8838 MATLAB Embedded Controllers

Quick Feature Tour. Quick Feature Tour Overview

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Using MATLAB, SIMULINK and Control System Toolbox

MODELING IN SCILAB: PAY ATTENTION TO THE RIGHT

The Cantor Handbook. Alexander Rieder

Introduction to MATLAB

Python Scripting for Computational Science

MapleSim User's Guide

The MATLAB system The MATLAB system consists of five main parts:

Modeling and Simulation

Mathematical Modelling Using SimScape (Mechanical Systems)

Introduction to Simulink

Introduction to the MATLAB SIMULINK Program

Isothermal Batch Reactor Modeling

Python Scripting for Computational Science

Modeling and Optimization of Real Systems

Example: Modeling a Cruise Control System in Simulink

IDA Simulation Environment Bris Data AB, September 1999

STEPHEN WOLFRAM MATHEMATICADO. Fourth Edition WOLFRAM MEDIA CAMBRIDGE UNIVERSITY PRESS

Scicos is a Scilab toolbox included in the ScicosLab package. The Scicos editor can be opened by the scicos command

Choose the file menu, and select Open. Input to be typed at the Maple prompt. Output from Maple. An important tip.

Text box. Command button. 1. Click the tool for the control you choose to draw in this case, the text box.

Importing Models from Physical Modeling. Tools Using the FMI Standard

Introduction to Simulink

AEMLog Users Guide. Version 1.01

MATLAB MATLAB mat lab funtool

Appendix A. HINTS WHEN USING EXCEL w

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

Scientific Computing: Lecture 1

Chapter 2. MathScript

Scientific Computing with Octave. Octave, GNUPlot and Other Tools Dr. Michael Rakijas Benedict G. Archer Sept. 18, 1999

This tutorial walks you through the process of using CC BATCH to simulate a batch distillation column.

FMI Kit for Simulink version by Dassault Systèmes

POLYMATH Example for the Numerical Solution of ODEs

xpc Target Tutorial Control System Design Feb. 15, 2004 For more detailed information, see the xpc target manual at:

A Simplified Vehicle and Driver Model for Vehicle Systems Development

Practice to Informatics for Energy and Environment

Microsoft Office Excel Use Excel s functions. Tutorial 2 Working With Formulas and Functions

MAT 275 Laboratory 1 Introduction to MATLAB

Figure 1: Control & Simulation Loop

Lab. Manual. Practical Special Topics (Matlab Programming) (EngE416) Prepared By Dr. Emad Saeid

Assignment 2 in Simulation of Telesystems Laboratory exercise: Introduction to Simulink and Communications Blockset

2 Getting Started. Getting Started (v1.8.6) 3/5/2007

MATLAB AND MODELSIM LINKING

TransMu x. Users Manual. Version 3. Copyright PixelTools Corporation

LEARNING TO PROGRAM WITH MATLAB. Building GUI Tools. Wiley. University of Notre Dame. Craig S. Lent Department of Electrical Engineering

SAAM II Version 2.1 Basic Tutorials. Working with Parameters Basic

Rotary Motion Servo Plant: SRV02. Rotary Experiment #00: QuaRC Integration. Using SRV02 with QuaRC. Student Manual

Introduction to MATLAB

Table of Contents. Introduction.*.. 7. Part /: Getting Started With MATLAB 5. Chapter 1: Introducing MATLAB and Its Many Uses 7

AEMLog users guide V User Guide - Advanced Engine Management 2205 West 126 th st Hawthorne CA,

EXCEL 2003 DISCLAIMER:

ECE 463 Lab 1: Introduction to LabVIEW

Matlab Advanced Programming. Matt Wyant University of Washington

Chapter 1 Introduction to MATLAB

MATLAB Project: Getting Started with MATLAB

Supersonic Flow Over a Wedge

16.06/16.07 Matlab/Simulink Tutorial

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB

55. Sim Dynamic Simulations

Polymath 6. Overview

ADMINISTRATIVE MANAGEMENT COLLEGE

zenon manual Batch Control v.7.60

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

STATISTICAL TECHNIQUES. Interpreting Basic Statistical Values

Eric W. Hansen. The basic data type is a matrix This is the basic paradigm for computation with MATLAB, and the key to its power. Here s an example:

Transcription:

Process Automation CHEM-E7140 Tutorial 3: An introduction to Simulink 1

Contents 1 Simulink: a brief introduction...2 A. Getting started...2 B. Modeling dynamic models...3 C. Creating a new model...4 D. Simulating the model...7 Simulate a Simulink model with Matlab...8 Creating S-functions...9 E. S-function block... 10 4 Examples... 11 A. Example 1... 11 B. Example 2... 13 C. Example 3... 15 1 Simulink: a brief introduction The use of standard Matlab environment is sometimes limiting for simulation purposes. In fact, engineers and in particular control process engineers tend to think in terms of block diagrams. Although Matlab functions such as parallel, series and feedback allow the simulation of block diagrams, these functions are not visually pleasing. Simulink is a program that runs as a companion to Matlab. They form a package that serves as a vehicle for modeling dynamic systems. Simulink provides a graphical user interface (GUI) that is used in building block diagrams, performing simulations, as well as analyzing results. In Simulink, models are hierarchical so you can view a system at a high level, then double-click on blocks by using the mouse to go down through the design levels. Simulating a dynamic system with Simulink is basically a two-step process: 1. Use the Simulink editor to create a model of the system to be simulated. The model graphically depicts the time-dependent mathematical relationships between the system inputs, states and output. 2. Use Simulink to simulate the behavior of the system over a specific time span. Simulink performs the simulations using the information that you enter into the model. A. Getting started To start Simulink is sufficient to type simulink in the Matlab command window or click on the Simulink icon on the Matlab toolbar and the Simulink Library Browser appears (Figure 1). The libraries contain the different categories of blocks needed to build our models. Looking trough the different categories of blocks we can work 2

with are present: Sources: this library contains blocks that generate signals; Sinks: this library contains blocks that display or write block outputs; Discrete: this library contains blocks that describe discrete-time systems; Continuous: this library contains blocks that describe continuous systems; Math operations: this library contains blocks that describe general mathematical operations; Signals routing: this library contains blocks that allow multiplexing and demultiplexing, implementing external input/output, passing data to other parts of the model, creating subsystems and performing other functions; All Simulink blocks have a commons set of user-settable parameters called block properties. By setting these parameters (double-click the block and the block properties dialog box appears), you can customize the behavior of the block to meet the specific requirements of your model. B. Modeling dynamic models 3

A Simulink block diagram model is a graphical representation of a mathematical model of a dynamic system which is described by a set of equations. The mathematical equations described by a block diagram model are known as algebraic, differential, and/or difference equations. A classic block diagram model of a dynamic system graphically consists of blocks and lines (signals). The history of these block diagram model is derived from engineering areas such as Feedback Control Theory and Signal Processing. A block within a block diagram defines a dynamic system in itself. The relationships between each elementary dynamic system in a block diagram are illustrated by the use of signals connecting the blocks. Collectively the blocks and lines in a block diagram describe an overall dynamic system. Simulink extends these classic block diagram models by introducing the notion of two classes of blocks, nonvirtual block and virtual blocks. Nonvirtual blocks represent elementary systems. A virtual block is provided for graphical organizational convenience and plays no role in the definition of the system of equations described by the block diagram model. The term "time-based block diagram" is used to distinguish block diagrams that describe dynamic systems from that of other forms of block diagrams. In Simulink, we use the term block diagram (or model) to refer to a timebased block diagram unless the context requires explicit distinction. To summarize the meaning of time-based block diagrams: Simulink block diagrams define time-based relationships between signals and state variables. The solution of a block diagram is obtained by evaluating these relationships over time, where time starts at a user specified "start time" and ends at a user specified "stop time." Each evaluation of these relationships is referred to as a time step. Signals represent quantities that change over time and are defined for all points in time between the block diagram's start and stop time. The relationships between signals and state variables are defined by a set of equations represented by blocks. Each block consists of a set of equations (block methods). These equations define a relationship between the input signals, output signals and the state variables. Inherent in the definition of a equation is the notion of parameters, which are the coefficients found within the equation. C. Creating a new model As a first example, we shall construct a simple model that integrates a sine wave and displays the result along with the sine wave. 4

Creating an empty model To create a new model, click the New button on the Library Browser's toolbar (Windows) or choose New from the library window's File menu and select Model (Unix). Simulink creates an empty model in memory and displays it in a new model editor window. Adding Blocks Firstly, we need to copy blocks into the model window created in the previous section. Blocks can be copied from either the Library Browser (Windows only) or a library window (UNIX and Windows). The following steps describe how to add blocks to your example model using each of these methods: 1. Locate the Sine Wave block in the Sources library. On UNIX, in the Simulink library window, double-click the Sources library. On windows, in the left side of the Simulink Library Browser under the Simulink node, select the Sources library. The right pane of the Simulink Library Browser displays the contects of the Sources library. 2. Continue to copy the following blocks from their respective libraries into your model window: Scope block (from the Sinks library) Integrator block (from the Continuous library) Mux block (from the Signal Routing library) 5

Connecting blocks Now it's time to connect the blocks. The > symbol pointing out of a block is an output port; if the symbol points to a block, it is an input port (as schematically shown below) To connect blocks, you draw lines from their output ports to their input ports. For instance, to connect the Sine Wave block to the top input port of the Mux block in your example model we have first to position the pointer over the output port on the right side of the Sine Wave block. Then, hold down the mouse button and move the cursor to the top input port of the Mux block and eventually release the mouse button. We use the same procedure to connect the other block in the model. However, one line connects a line to the input port of another block. This line, called a branch line, connects the Sine Wave output to the Integrator block, and carries the same signal that passes from the Sine Wave block to the Mux block. Drawing a branch line is slightly different from drawing the lines you just drew. To weld a connection to an existing line, follow these steps: 1. Position the pointer on the line between the Sine Wave and the Mux block. 2. Press and hold down the Ctrl key. Press the mouse button, then drag the pointer to the Integrator block's input port or over the Integrator block itself. 3. Release the mouse button. Simulink draws a line between the starting point and the Integrator block's input port. The final model is represented as follows. 6

Here a Mux, multiplexer, block is present. It combines its inputs into a single output. An input can be a scalar, vector or matrix signal. Depending on its inputs, the output of a Mux block is a vector or a composite signal, i.e., a signal containing both matrix and vector elements. On the other side, a demultiplexer ( Demux) extracts the components as separated signals. D. Simulating the model Before simulating a model, we may want to set various simulation options, such as the simulation's start and stop time or the type of solver used to solve the model at each simulation time step. You can use the Configuration Parameters dialog box to specify a model's simulation options. Now we are ready to simulate your example model, observe its simulation results, and then save it. From our example model window Simulation menu, select Start. Simulink simulates your model, stopping when it reaches the stop time specified in the Configuration Parameters dialog box or when you select Stop from the Simulation menu. In your example model window, double-click the Scope block. The Scope window displays the simulation results. Eventually, from your example model window's File menu, select Save and enter a file name and location. Simulink saves your example model using the specified name and location. 7

Simulate a Simulink model with Matlab Sometimes, we may build a complicated model in Simulink and would like to derive either a transfer function, a state space model of the entire plant or more simply we want to plot some of your result from Matlab. In order to do this, we first need to define the model and then if needed to define the input and output signals of the model to be extracted. These signals are defined using the In and Out blocks in the Sources and Sinks library respectively. With the simple model in Section 1.3. Firstly, we define the input and output for the block and we save the Simulink model in our directory, for instance as, mod1io.mdl. With the Matlab command sim, this model can be dynamically simulated. The sim command executed the Simulink model, using the Configuration parameter dialog box setting, including the options specified on the Data Import/Export pane. The syntax is the following: [t,x,y] = sim('model',timespan,options,ut) [t,x,y1,...,yn] = sim('model',timespan,options,ut) where: t: returned time vector. x: returned state in matrix or structure format. The state matrix contains continuous states followed by discrete states. y: returned output in matrix or structure format. For block diagram models this contains all root-level outport blocks. y1,...,yn: can only be specified for block diagram models, where n must be the number of root-level outport blocks. Each outport will be returned in the y1,...,yn variables. 'model': Name of a block diagram model. timespan: One of: - TFinal, - [TStart TFinal], or - [TStart OutputTimes TFinal]. OutputTimes are time points which will be returned in t, but in general t will include additional time points. options: Optional simulation parameters. This is a structure created with simset using name value pairs. ut: Optional extern input. ut = [t, u1,... un] where t = [t1,..., tm] or ut is a string containing a function u=ut(t) evaluated at each time step. For table inputs, the input to the model is interpolated from ut. 8

Specifying any right hand side argument to sim as the empty matrix, [ ], will cause the default for the argument to be used. Only the first parameter is required. All defaults will be taken from the block diagram, including unspecified options. Any optional arguments specified will override the settings in the block diagram. Type help simset on the Matlab command window, to know more about all the possible sim options. Figure7 I/O Simulink model In order to run the model mod1io, in Figure 7, we define a script file to run the simulation using sim. Creating S-functions Complicated models, or models which are parametrized in a way that affects the structure of a model, should not always be implemented by drawing a block system. In Simulink, it is possible to implement nonlinear and complicated parts of a model in a separate m-file: the S-function. S-functions (system-functions) provide a powerful mechanism for extending the capabilities of Simulink. It is a computer language description of a Simulink block that can be implemented as either an M-file or a MEX-file functions. The later, that is the Matlab EXternal-file functions, are implemented in a different programming language: C, C++, Ada, or Fortran and their explanation is not covered in this Course. In this section we focus on the M-file S-function implementation in Simulink environment. Note: a template implementation of an M-file S-function, msfuntmpl_basic.m, can be opened by running the following command in the Matlab promt ( open msfuntmpl_basic ). The template consists of a top-level function and a set of skeleton subfunctions, each of which corresponds to a particular feature. The top-level function invokes the subfunction indicated by flag. The set of S-function callback methods perform tasks required at each simulation stage. During simulation of a model, at each simulation stage, Simulink calls the appropriate methods for each S-Function block in the model. Tasks performed by S-function methods include: 9

Setup. Prior to the first simulation loop, Simulink initializes the S-function. During this stage, Simulink - Initializes the structure that contains information about the S-function; - Sets the number and dimensions of input and output ports; - Sets the block sample times; - Set the number of input parameters for the block. InitializeConditions. Setup of initial condtitions for continiuos and descrete states. Outputs. Calculation of outputs in the major time step. After this call is complete, all the output ports of the blocks are valid for the current time step. Derivatives. Update of derivatives of continious states during simulation step. E. S-function block The S-Function block (Figure 8), in the User-defined functions library provides access to S-functions from a block diagram. Figure8 S-fuction block Below is reported the dialog box for the S-function block. From here we can note that it allows additional parameters to be passed directly to the named S-function. The function parameters can be specified as Matlab expressions or as variables separated by commas. It should be noticed that although individual parameters can be enclosed in brackets, the list of parameters must not be enclosed in brackets. The S-Function block displays the name of the specified S-function and the number of input and output ports specified by the S-function. Signals connected to the inputs must have the dimensions specified by the S- function for the inputs. 10

4 Examples A. Example 1 We consider the same system exposed in the first tutorial consisting of the sequence of reactions in an ideally mixed batch reactor. A reaction in which a substance A is converted into B, and B into C, occurs. This simple reaction can be expressed with the following model: Where the change in the concentration of A is given by Equation 1. In the same way, the change in the concentration of B is expressed by Equation 2 and concentration of C by Equation 3. Let k1=0.07 and k2=0.09. We want to present the concentration of A, Band C in the reactor as a function of time; knowing that at the time t=0 the concentration of A is 4 mol/m3 and that no B and C are present. The concentrations of that system can be found by integrating the above equations and we do that in Simulink environment only by drawing. We build the following model: 11

-0.07 1 s CA Gain Gain3-1 Integrator -0.09 Gain1 1 s Integrator1 CB Scope -1 Gain2 1 s Integrator2 CC Then, we set the initial conditions for the integrator blocks as follows: CA(0) = 4, CB(0) = 0, CC(0) = 0; Finally, we set the simulation time to 200 and simulate. Similar results than in the Matlab exercise are obtained: 12

B. Example 2 Let us consider the following mixing tank process, where two flows, F 1 and F 2 are mixed. There is one flow F out of the tank. The total mass balance of the tank and the component mass balance of the tank are described respectively as follows: dv dt = F 1 + F 2 F (4) d(cv) dt = (F 1 C 1 + F 2 C 2 FC) (5) where V is volume, C 1 and C 2 are the concentrations corresponding to flows F 1 and F 2, respectively, and C is the concetration of outfow F. The density of each flow is assumed to be constant and therefore they can be neglected. 3 The flow F can be described as F = k V d(cv) dt when the mass balance equations can be written dv 3 = F dt 1 + F 2 k V (6) 3 = (F 1 C 1 + F 2 C 2 k VC) (7) Now, Model the described system in Simulink and Find out the steady state of the process Study the model behavior when a step change to F 2 is performed. Following parameter values are given: F 1 = 0.5 m 3 /s and F 2 = 0.1 m 3 /s. C 1 = 8 mol/m 3 and C 2 = 4 mol/ m 3. k = 0.4 Initial conditions are given as follows: V(0) = 0.1 m 3 C(0) = 0 mol/m 3 13

The model of the mixing tank can be built in Simulink as follows: C1 C1 Product 1 s CV C2 C2 Product1 Add1 Integrator 1/u Fcn1 Product3 Scope Product2 F1 F1 1 s V F2 Add Integrator1 Scope1 F2 Manual Switch Step Gain k Fcn u^(1/3) The steady state is found out by simulating the model: The steady state is V S = 3.375 m 3 and C S = 7.333 mol/m 3 Then, set the step time to 40 and step size to 0.2 and simulate. 14

C. Example 3 In this example, we consider the mixing tank in Example 2, and we define a Simulink model by means of an S- function block. We know that the model is represented by Equations 6 and 7, so we use them directly to write the S-function: function ex3_mixingtank_sfunc(block) %ex3_mixingtank_sfunc % The setup method is used to set up the basic attributes of the S-function such as ports, parameters, etc. Do not add any other calls to the main body of the function. setup(block); %endfunction Function: setup =================================================== Abstract: Set up the basic characteristics of the S-function block such as: - Input ports - Output ports - Dialog parameters - Options Required : Yes C-Mex counterpart: mdlinitializesizes function setup(block) % Register number of ports block.numinputports = 4; block.numoutputports = 2; % Setup port properties to be inherited or dynamic block.setprecompinpportinfotodynamic; 15

block.setprecompoutportinfotodynamic; % Override input port properties % First flow block.inputport(1).dimensions = 1; block.inputport(1).datatypeid = 0; % double block.inputport(1).complexity = 'Real'; block.inputport(1).directfeedthrough = false; block.inputport(1).samplingmode = 'Sample'; % Second flow block.inputport(2).dimensions = 1; block.inputport(2).datatypeid = 0; % double block.inputport(2).complexity = 'Real'; block.inputport(2).directfeedthrough = false; block.inputport(2).samplingmode = 'Sample'; % First concentration block.inputport(3).dimensions = 1; block.inputport(3).datatypeid = 0; % double block.inputport(3).complexity = 'Real'; block.inputport(3).directfeedthrough = false; block.inputport(3).samplingmode = 'Sample'; % Second concentration block.inputport(4).dimensions = 1; block.inputport(4).datatypeid = 0; % double block.inputport(4).complexity = 'Real'; block.inputport(4).directfeedthrough = false; block.inputport(4).samplingmode = 'Sample'; % Override output port properties % Volume block.outputport(1).dimensions = 1; block.outputport(1).datatypeid = 0; % double block.outputport(1).complexity = 'Real'; block.outputport(1).samplingmode = 'Sample'; % Concentration block.outputport(2).dimensions = 1; block.outputport(2).datatypeid = 0; % double block.outputport(2).complexity = 'Real'; block.outputport(2).samplingmode = 'Sample'; % Register parameters block.numdialogprms = 2; % initial states % Register sample times % [0 offset] : Continuous sample time % [positive_num offset] : Discrete sample time % % [-1, 0] : Inherited sample time % [-2, 0] : Variable sample time block.sampletimes = [0 0]; % Specify the block simstatecompliance. block.simstatecompliance = 'DefaultSimState'; % Set the number of continious states block.numcontstates = 2; % number of cont. states: x(1) = volume % and x(2) = concentration*volume 16

----------------------------------------------------------------- The MATLAB S-function uses an internal registry for all block methods. You should register all relevant methods (optional and required) as illustrated below. You may choose any suitable name for the methods and implement these methods as local functions within the same file. See comments provided for each function for more information. ----------------------------------------------------------------- block.regblockmethod('initializeconditions', @InitializeConditions); block.regblockmethod('outputs', @Outputs); % Required block.regblockmethod('derivatives', @Derivatives); block.regblockmethod('terminate', @Terminate); % Required %end setup InitializeConditions: Functionality : Called at the start of simulation and if it is present in an enabled subsystem configured to reset states, it will be called when the enabled subsystem restarts execution to reset the states. Required : No C-MEX counterpart: mdlinitializeconditions function InitializeConditions(block) % Define initial conditions % Initial state: from the block parameters block.contstates.data(1) = block.dialogprm(1).data(1); % Volume block.contstates.data(2) = block.dialogprm(1).data(2); % Concentration*Volume %end InitializeConditions Outputs: Functionality : Called to generate block outputs in simulation step Required : Yes C-MEX counterpart: mdloutputs function Outputs(block) block.outputport(1).data = block.contstates.data(1); % Volume block.outputport(2).data = block.contstates.data(2)/block.contstates.data(1); % Concentration %end Outputs Derivatives: Functionality : Called to update derivatives of continuous states during simulation step Required : No C-MEX counterpart: mdlderivatives 17

function Derivatives(block) k = block.dialogprm(2).data; block.derivatives.data(1) = block.inputport(1).data+... block.inputport(2).data-k*block.contstates.data(1)^(1/3); block.derivatives.data(2) =... block.inputport(1).data*block.inputport(3).data+... block.inputport(2).data*block.inputport(4).data-... k*block.contstates.data(1)^(1/3)*... (block.contstates.data(2)/block.contstates.data(1)); %end Derivatives Terminate: Functionality : Called at the end of simulation for cleanup Required : Yes C-MEX counterpart: mdlterminate function Terminate(block) %end Terminate Having saved the S-function as ex3_mixingtank_sfunc.m, the Simulink model can be created as reported in figure below. 18