Stephen L. Campbell, Jean-Philippe Chancelier and Ramine Nikoukhah Modeling and Simulation in Scilab/Scicos Mathematics Subject Classification (2000): 01-01, 04-01, 11 Axx, 26-01 Library of Congress Control Number: 2005930797 ISBN-10: 0-387-27802-8 ISBN-13: 978-0387278025 Printed on acid-free paper. © 2006 Springer Science + Business Media, Inc. All rights reserved. This work may not be translated or copied in whole or in part without the written permission of the publisher (Springer Science+Business Media, Inc., 233 Spring Street, New York, NY 10013, USA), except for brief excerpts in connection with reviews or scholarly analysis. Use in connection with any form of information storage and retrieval, electronic adaption, computer software, or by similar or dissimilar methodology now known or hereafter developed is forbidden. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. Printed in the United States of America. 987654321 springeronline.com (BPR/HAM) Preface Scilab (http://www.scilab.org) is a free open-source software package for scientiﬁc computation. It includes hundreds of general purpose and specialized functions for numerical computation, organized in libraries called toolboxes that cover such areas as simulation, optimization, systems and control, and signal processing. These functions reduce considerably the burden of programming for scientiﬁc applications. One important Scilab toolbox is Scicos. Scicos (http://www.scicos.org) provides a block-diagram graphical editor for the construction and simulation of dynamical systems. Scilab/Scicos is the only open-source alternative to commercial packages for dynamical system modeling and simulation packages such as MATLAB/Simulink and MATRIXx/SystemBuild. Widely used at universities and engineering schools, Scilab/Scicos is gaining ground in industrial environments. This is due in part to the creation of an international consortium in 2003 that a number of large and small companies have joined. The consortium is responsible for providing well-tested and documented releases for various platforms and coordinates the development of the core product and toolboxes, which is done by research groups, in particular, at INRIA1 and ENPC.2 Currently there are over 10,000 monthly Scilab downloads from http://www.scilab.org alone. Scilab includes a full user’s manual, which is available with search capabilities in a help window. All commands, their syntax, and simple illustrative examples are given. While very useful in ﬁnding out the details of a particular command, this manual does not provide a tutorial on the philosophy of either Scilab or Scicos. Nor does it address how to use several of these commands together in the solution of a technical problem. The objective of this book is to provide a tutorial for the use of Scilab/Scicos with a special emphasis on modeling and simulation tools. While it will provide useful information to experienced users, it is designed to be accessible to beginning users from a variety of disciplines. Students [33] and academic and industrial scientists and engineers should ﬁnd it useful. The discussion includes some information on modeling and simulation in order to assist the reader in deciding which simulation tools might be most useful to them. Every software environment has its special features, some would say quirks, that experienced users automatically take into account but often prove confusing to beginning users. We have tried to point these out where appropriate. The book is divided into two parts. The ﬁrst part concerns Scilab and includes a tutorial covering the language features, the data structures and specialized functions for doing graphics, importing and exporting data, interfacing with external routines, etc. It 1 2 Institut National de Recherche en Informatique et en Automatique Ecole Nationale des Ponts et des Chaussées VI Preface also covers in detail Scilab numerical solvers for ODEs (ordinary diﬀerential equations) and DAE’s (diﬀerential-algebraic equations). Even though the emphasis is placed on modeling and simulation applications, this part provides a global view of the product. The second part is dedicated to modeling and simulation of dynamical systems in Scicos. Scicos provides a block-diagram editor for constructing models. This type of modeling tool is widely used in industry because it provides a means for constructing modular and reusable models. This part contains a detailed description of the editor and its usage, which is illustrated through numerous examples. It also covers advanced subjects such as constructing new blocks and batch simulation. Code generation and debugging are other topics covered. Finally, a new extension of Scicos is discussed. This extension allows the use of components described by the Modelica (http://www.modelica.org) language. There have been several previous books concerning Scilab. Most of these have been in French [18, 3, 2, 1, 25] and dealt with earlier versions of Scilab, as in [16]. This book is unique in a number of ways. It is the ﬁrst to deal with the new Scilab 3.1 version. This book is also the ﬁrst to focus on simulation and modeling. It is also the ﬁrst to put a major emphasis on Scicos and discuss Scicos in depth. The source of all the examples presented in this book can be downloaded from http://www.scicos.org. Finally, a large number of people have supported us in many ways. We would especially like to thank our wives, Gail Campbell and Homa Nikoukhah, and our parents, Aline and René Chancelier, for their support in this and everything else we do. Steve Campbell Jean-Philippe Chancelier Ramine Nikoukhah Contents Part I Scilab 1 General Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1 What Is Scilab? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 How to Start? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.2 First Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.3 Line Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.4 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3 Typical Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.4 Scilab on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 3 4 4 4 5 6 6 7 2 Introduction to Scilab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1 Scilab Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.1 Matrix Construction and Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.3 Boolean Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.4 Polynomial Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.5 Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.6 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.7 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Scilab Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.1 Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.2 Iterations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.3 Scilab Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.4 Debugging Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Input and Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.1 Display of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.2 Formatted Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.3 Input Output in Binary Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.4 Accessing the Host System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.5 Graphical User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4 Scilab Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4.1 Basic Graphing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4.2 Graphic Tour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4.3 Graphics Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 11 12 17 19 20 21 22 26 27 28 29 31 35 37 37 38 40 42 43 48 48 49 53 VIII Contents 2.4.4 Scilab Graphics and LATEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4.5 Old Graphics Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5 Interfacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.1 Linking Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.2 Writing an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.3 Dynamic Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 60 62 63 66 69 3 Modeling and Simulation in Scilab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 3.1 Types of Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 3.1.1 Ordinary Diﬀerential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 3.1.2 Boundary Value Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 3.1.3 Diﬀerence Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 3.1.4 Diﬀerential Algebraic Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 3.1.5 Hybrid Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 3.2 Simulation Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 3.2.1 Ordinary Diﬀerential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 3.2.2 Boundary Value Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 3.2.3 Diﬀerence Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 3.2.4 Diﬀerential Algebraic Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 3.2.5 Hybrid Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 4 Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1 Comments on Optimization and Solving Nonlinear Equations . . . . . . . . . . . 4.2 General Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3 Solving Nonlinear Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4 Nonlinear Least Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.5 Parameter Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.6 Linear and Quadratic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.6.1 Linear Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.6.2 Quadratic Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.6.3 Semideﬁnite Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.7 Diﬀerentiation Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.7.1 Higher Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 107 108 112 113 117 119 119 120 120 120 122 5 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1 Modeling and Simulation of an N -Link Pendulum . . . . . . . . . . . . . . . . . . . . . 5.1.1 Equations of Motion of the N -Link Pendulum . . . . . . . . . . . . . . . . . . . 5.1.2 Generated Code and Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1.3 Maple Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Modeling and Simulation of a Car . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.1 Basic Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.2 Equations of Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.3 Simulation Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.4 Scilab Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.5 Simulation Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3 Open-Loop Control to Swing Up a Pendulum . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.1 Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.2 Control Problem Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.3 Optimization Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.4 Implementation in Scilab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 125 126 130 133 135 135 136 138 139 141 142 142 142 143 145 Contents IX 5.4 Parameter Fitting and Implicit Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 5.4.1 Mathematical Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 5.4.2 Scilab Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Part II Scicos 6 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 7 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1 Construction of a Simple Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1.1 Running Scicos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1.2 Editing a Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1.3 Diagram Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1.4 Changing Block Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.2 Symbolic Parameters and Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.3 Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.3.1 Placing a Super Block in a Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.3.2 Editing a Super Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.4 Save and Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.4.1 Scicos File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.4.2 Super Block and Palette . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.5 Synchronism and Special Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 163 163 163 165 166 169 173 173 174 175 175 176 176 8 Scicos Formalism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.1 Activation Signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.1.1 Block Activation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.1.2 Activation Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.2 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.3 Always Active Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.4 Constant Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.5 Conditional Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 179 179 181 182 183 184 184 9 Scicos Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.1 Block Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.1.1 External Activation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.1.2 Always Activation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.1.3 Internal Zero-Crossing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.2 Blocks Inside Palettes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.3 Modifying Block Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.4 Super Block and Scifunc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.4.1 Super Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.4.2 Scifunc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.5 Constructing New Basic Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.5.1 Interfacing Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.5.2 Computational Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.5.3 Saving New Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.6 Constructing and Loading a New Palette . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 189 189 191 192 192 193 193 193 194 194 195 197 207 207 X Contents 10 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.1 Predator Prey Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2 Control Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.3 Signal Processing Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.4 Queuing Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.5 Neuroscience Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.6 A Fluid Model of TCP-Like Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.7 Interactive GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 209 210 213 216 218 220 221 11 Batch Processing in Scilab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1 Piloting Scicos via Scilab Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1.1 Function scicosim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1.2 Function scicos simulate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Data Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2.1 Context Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2.2 Input/Output Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2.3 Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.4 Steady-State Solution and Linearization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.4.1 Scilab Function steadycos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.4.2 Scilab Function lincos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 227 228 232 233 234 234 236 237 243 247 248 12 Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.1 Code Generation Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.2 Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.2.1 Continuous-Time Activation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.2.2 Synchronicism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.3 A Look Inside . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.4 Some Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.5 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 253 257 257 258 258 260 263 13 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.1 Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.1.1 Block Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.1.2 Errors During Numerical Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.1.3 Other Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.2 Debugging Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.3.1 Log File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.3.2 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 267 267 268 269 269 270 271 271 14 Implicit Scicos and Modelica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.2 Internally Implicit Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.3 Implicit Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.3.1 Scicos Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.3.2 Scicos Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.3.3 Block Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.4 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 273 275 275 276 276 276 277 Contents XI A Inside Scicos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.1 Scicos Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.1.1 Main Editor Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.1.2 Structure of scs m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.2 Scicos Complier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.2.1 First Compilation Stage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.2.2 Second Compilation Stage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.2.3 Structure of %cpr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.2.4 Partial Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.3 Scicos Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 281 281 283 286 286 287 287 290 291 B Scicos Blocks of Type 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 B.1 Type 5 Block for the Bouncing Ball Example . . . . . . . . . . . . . . . . . . . . . . . . . 293 B.2 Animation Block for the Cart Pendulum Example . . . . . . . . . . . . . . . . . . . . . 294 C Animation Program for the Car Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 D Extraction Program for the LATEX Graphic Example . . . . . . . . . . . . . . . . 301 E Maple Code Used for Modeling the N -Link Pendulum . . . . . . . . . . . . . . . 303 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 Part I Scilab 1 General Information 1.1 What Is Scilab? There exist two categories of general scientiﬁc software: computer algebra systems that perform symbolic computations, and general purpose numerical systems performing numerical computations and designed speciﬁcally for scientiﬁc applications. The best-known examples in the ﬁrst category are Maple, Mathematica, Maxima, Axiom, and MuPad. The second category represents a larger market dominated by MATLAB. Scilab, which is free open-source software, belongs to this second category. Scilab is an interpreted language with dynamically typed objects. Scilab runs, and is available in binary format, for the main available platforms: Unix/Linux workstations (the main software development is performed on Linux workstations), Windows, and MacOSX. MacOSX users can also install Scilab using ﬁnk. Compiling Scilab from the source code is also possible and is fairly straightforward. Scilab was originally named Basile and was developed at INRIA as part of the Meta2 project. Development continued under the name of Scilab by the Scilab group, which was a team of researchers from INRIA Metalau and ENPC. Since 2004, Scilab development has been coordinated by a consortium. Scilab can be used as a scripting language to test algorithms or to perform numerical computations. But it is also a programming language, and the standard Scilab library contains around 2000 Scilab coded functions. The Scilab syntax is simple, and the use of matrices, which are the fundamental object of scientiﬁc calculus, is facilitated through speciﬁc functions and operators. These matrices can be of diﬀerent types including real, complex, string, polynomial, and rational. Scilab programs are thus quite compact and most of the time are smaller than their equivalents in C, C++, or Java. Scilab is mainly dedicated to scientiﬁc computing, and it provides easy access to large numerical libraries from such areas as linear algebra, numerical integration, and optimization. It is also simple to extend the Scilab environment. One can easily import new functionalities from external libraries into Scilab by using static or dynamic links. It is also possible to deﬁne new data types using Scilab structures and to overload standard operators for new data types. Numerous toolboxes that add specialized functions to Scilab are available on the oﬃcial site. Scilab also provides many visualization functionalities including 2D, 3D, contour and parametric plots, and animation. Graphics can be exported in various formats such as Gif, Postscript, Postscript-Latex, and Xﬁg. In addition to Scilab’s user interface functions, the Scilab Tcl/Tk interface can be used to develop sophisticated GUI’s (Graphical user interfaces). 4 1 General Information Scilab is a large software package containing approximately 13,000 ﬁles, more than 400,000 lines of source code (in C and Fortran), 70,000 lines of Scilab code (specialized libraries), 80,000 lines of online help, and 18,000 lines of conﬁguration ﬁles. These ﬁles include • • • • • • • • • • • • Elementary functions of scientiﬁc calculation; Linear algebra, sparse matrices; Polynomials and rational functions; Classic and robust control, LMI optimization; Nonlinear methods (optimization, ODE and DAE solvers, Scicos, which is a hybrid dynamic systems modeler and simulator); Signal processing; Random sampling and statistics; Graphs (algorithms, visualization); Graphics, animation; Parallelism using PVM; MATLAB-to-Scilab translator; A large number of contributions for various areas. 1.2 How to Start? 1.2.1 Installation Scilab is available for downloading at http://www.scilab.org. The procedure for installing Scilab depends on the operating system (Windows, MacOSX, Linux, or Unix), and information can be found on the website. The user has the choice between installing the binary version (if one is available for the host system) and compiling the source version. To compile the source version, the host system must be equipped with appropriate C and Fortran compilers. For the Windows operating system, a Visual C++ compiler sufﬁces because an f2c (Fortran-to-C) translator is included in the source code. Note that C and Fortran compilers are already installed on most Linux platforms, and for other Unix systems, if native compilers are not available, freely available GNU compilers can be used. On computers running MacOSX there is also the option of installing Scilab using ﬁnk. In this book, SCI designates the directory in which Scilab is installed. We use the Unix notation for specifying path names. For example, SCI/routines/machine.h is the ﬁle machine.h in the subdirectory routines. Under the Windows operating system, all the “ / ” should be replaced with “ \ ”. Even though there are no restrictions on its use, Scilab is copyrighted; see the ﬁle license.txt (English) or licence.txt (French) on the website. 1.2.2 First Steps Running Scilab opens up a command window; see Figure 1.1. The look of this window may diﬀer depending on the window manager. The Scilab command window is an interactive window where the user is invited to enter a command at Scilab’s prompt (-->). The command must be validated with a carriage return, after which Scilab executes the command and returns control to the user by displaying a new prompt. 1.2 How to Start? 5 Figure 1.1. Scilab’s main window. The best way to start exploring Scilab is to run the demos. This can be done by clicking on the Demos button at the top of the command window (under the Windows operating system, the Demos button is in the “?” menu). The demos are chosen to present typical uses of the software and some of its specialized toolboxes. For each demo, the user can see the corresponding Scilab source code, which shows that the data types used in Scilab are, for the most part, vectors and matrices. Their usage, very close to the usual matrix notation, results in compact and readable code. This, and the fact that there is no need for type declaration, compilation, or memory allocation, makes Scilab a lot easier to use than low-level languages such as C and Fortran. Just as for any other interpreted language, however, there is a price to be paid in terms of eﬃciency. This could become a factor in some applications. 1.2.3 Line Editor Limited editing facilities are available in the command window. Besides the usual cut and paste operations, line editing can be done using control characters as is done in emacs: Ctrl-b (pressing b while holding the Ctrl key down) for moving the cursor back by one character, Ctrl-f for moving it forward, Ctrl-a to place the cursor at the beginning of the command line, and Ctrl-e for placing it at the end. Also Ctrl-k erases the part of the command line between the current position of the cursor and the end of the line and saves it in a buﬀer, and Ctrl-y inserts the content of the buﬀer at the current position of the cursor. Previously entered commands can be searched using up and down arrows or equivalently with Ctrl-p and Ctrl-n. Under Unix and Linux operating systems, an additional feature is provided for recalling a previously entered command by typing the beginning of the command line after an exclamation point, followed by a carriage return. All the commands entered in Scilab are automatically saved in a ﬁle called scilab.hist in the user’s home directory. 6 1 General Information 1.2.4 Documentation Scilab has a comprehensive online help facility, which can be consulted through the commands help and apropos. To consult the manual page corresponding to a Scilab function, the command help followed by the name of the function can be used. This opens up a browser window displaying the manual page in question. The manual page contains a detailed description of the function and a number of examples of its usage. The examples can be cut and pasted into Scilab’s command window to be executed. The browser, which can also be accessed by clicking on the help button at the top of the command window, contains a list of all the functions classiﬁed by theme in diﬀerent chapters (see Fig. 1.2). The manual page of a function can then be obtained by clicking on its name. To obtain a list of Scilab functions corresponding to a keyword, the command apropos followed by the keyword should be used. Figure 1.2. Help browser window. The demos are also a good source of inspiration. They present simple examples of Scilab programming situations frequently encountered by users. The graphics demos, for example, give an overall picture of what can be done in Scilab as far as visualization is concerned. The demos’ source codes often provide a good starting point for developing complex applications. The demos provide examples for doing graphics, signal processing, systems control, Scilab simulation (in particular, the famous bicycle example), Scicos simulation, and a lot more. 1.3 Typical Usage A typical Scilab user spends most of his time going back and forth between Scilab and a text editor. For the most part, Scilab programs contain very few lines of instructions, 1.4 Scilab on the Web 7 Figure 1.3. Graphic window. thanks to the powerful data types and primitives available in Scilab. These programs can be written using a built-in editor, which is activated through the Editor menu button or using the user’s favorite text editor (for example vi or emacs, for which there exists a Scilab mode, under Unix, and notepad or wordpad under Windows), then loaded into Scilab. This can be done through the built-in editor menus or using Scilab functions getf and exec (discussed in Section 2.2), or through the menu File operations. 1.4 Scilab on the Web The latest release of Scilab, its documentation, and many third-party contributions (toolboxes) can be found on the oﬃcial Scilab home page at http://www.scilab.org On this site, a bug and request report system (based on Bugzilla) for Scilab is available: http://scilabsoft.inria.fr/cgi-bin/bugzilla_bug/index.cgi There is also a newsgroup dedicated to Scilab: comp.soft-sys.math.scilab A speciﬁc site devoted to teaching with Scilab is also available at http://cermics.enpc.fr/scilab/ Finally, the email address [email protected] can be used for contacting the Scilab group concerning contributions or simply for asking questions. 2 Introduction to Scilab The Scilab language was initially devoted to matrix operations, and scientiﬁc and engineering applications were its main target. But over time, it has considerably evolved, and currently the Scilab language includes powerful operators for manipulating a large class of basic objects. In Scilab, objects are never declared or allocated explicitly; they have a dynamic type and their sizes can dynamically change according to applied operators or functions. The following Scilab session illustrates the creation of an object, its dynamic change, and its deletion. Note that the function typeof returns the type of the object, isdef tests the presence of the object, and clear destroys it and frees the corresponding memory. −→ a= rand(2,3); −→ typeof(a) ans = ← 2x3 random matrix a is created ← type of a, constant stands for real or complex matrix constant −→ a= [a, zeros(2,1)] a = ! ! 0.2113249 0.7560439 0.0002211 0.3303271 −→ a= ’scilab’; typeof(a) ans = ← size of a dynamically increases 0.6653811 0.6283918 0. ! 0. ! ← redeﬁning a through reassignment string −→ exists(’a’) ans = ← checks if a exists 1. −→ clear(’a’); exists(’a’) ans 0. = ← removing a explicitly from current environment and verifying that a is gone 10 2 Introduction to Scilab Available memory for a Scilab session can be controlled through the use of the stacksize command. It is of course bounded by available dynamic heap memory (allocated memory through the use of the malloc function). Using Scilab commands who and whos, one can get the name and size of dynamic objects present in the current session: −→ stacksize() ans ! ← available memory (number of double) and maximum number of variables = 1000000. 6185. ! −→ A=rand(1000,1000); !--error 17 rand: stack size exceeded (Use stacksize function to increase it) −→ stacksize(6000000); −→ A=rand(1000,1000); −→ whos(’-name’,’A’); Name Type ← increasing Scilab available memory ← A is created, note that an expression ending with ; is evaluated without display ← get info on variable A Size Bytes A constant 1000 by 1000 8000016 By default, numbers in Scilab are coded as double-precision ﬂoats. The accuracy of double-precision computation is machine-dependent and can be obtained through the predeﬁned Scilab variable %eps, called the machine precision. %eps is the largest doubleprecision number for which 1+(%eps)/2 is indistinguishable from 1. The set of numbers in Scilab can be extended by adding %inf (inﬁnity) and %nan (not a number). This is done with the Scilab function ieee: −→ ieee() ans = ← get the default ﬂoating-point exception mode 0. −→ 1/0 !--error 27 division by zero... −→ ieee(2) −→ 1/0 ans = ← 1/0 raises an error in the default mode ← using the standard ieee ﬂoating-point exception mode ← 1/0 evaluates now to + ∞ Inf −→ 0/0 ans = ← no error is produced Nan Scilab expressions are evaluated by the scilab interpreter. The interpretor has access to a large set of functions. Some of these functions are Scilab-coded functions (we shall see 2.1 Scilab Objects 11 later that Scilab is a programming language), some are hard-coded functions (C or Fortran coded). This ability of Scilab to include C or Fortran coded functions means that a large amount of existing scientiﬁc and engineering software can be used from within Scilab. Scilab is a rich and powerful programming environment because of this large number of available functions. The users can also enrich the basic Scilab environment with their own Scilab or hard-coded functions, and moreover this can be done dynamically (hard-coded functions can be dynamically loaded in a running Scilab session); we shall see how this can be done later. Thus, toolboxes can be developed to enrich and adapt Scilab to speciﬁc needs or areas of application. 2.1 Scilab Objects Scilab provides users with a large variety of basic objects starting with numbers and character strings up to more sophisticated objects such as booleans, polynomials, and structures. A Scilab object is a basic object or a set of basic objects arranged in a vector, a matrix, a hypermatrix, or a structure (list). As already mentioned, Scilab is devoted to scientiﬁc computing, and the basic object is a two-dimensional matrix with ﬂoating-point double-precision number entries. In fact, a real scalar is nothing but a 1 × 1 matrix. The next Scilab session illustrates this type of object. Note that some numerical constants are predeﬁned in Scilab. Their corresponding variable names start with %. In particular, π is %pi, and e, the base of the natural log, is %e. Note also that a:b:c gives numbers starting from a to c spaced b units apart. −→ a=1:0.6:3 a = ! 1. 1.6 ← a is now a scalar matrix (double) 2.2 2.8 ! −→ b=[%e,%pi] b = ! 2.7182818 ← b is the 1x2 row vector ﬁlled with predeﬁned values (e, π) 3.1415927 ! New types have been added as Scilab has evolved, but the matrix aspect has always been kept. String matrices, boolean matrices, sparse matrices, integer matrices (int8, int16, int32), polynomial matrices, and rational matrices are now available in the standard Scilab environment. Complex structures called lists (list, tlist, and mlist) are also available. Note also that functions in Scilab are considered as objects as well. −→ a= "Scilab" a = ← a 1x1 string matrix Scilab −→ b=rand(2,2) b = ! ! 0.2113249 0.7560439 ← a matrix 0.0002211 ! 0.3303271 ! 12 2 Introduction to Scilab −→ b= b>= 0.5 b = ← a boolean matrix ! F F ! ! T F ! −→ L=list(a,b) L = ← a list L(1) Scilab L(2) ! F F ! ! T F ! −→ A.x = 32;A.y = %t A = ← a structure implemented using mlist x: 32 y: %t −→ a= spec(rand(3,3)) a = ! ! ! ← eigenvalues: a vector of complex numbers 1.8925237 ! 0.1887123 + 0.0535764i ! 0.1887123 - 0.0535764i ! It is possible to deﬁne new types in Scilab in the sense that it is possible to deﬁne objects whose dynamic type (the value returned by typeof) is user-deﬁned. Scilab operators, for example the + and * operators, can be overloaded for these dynamically deﬁned types. The new types are deﬁned and implemented with tlist and mlist primitive types (see Section 2.1.6). 2.1.1 Matrix Construction and Manipulation As already pointed out, one of the goals of Scilab is to give access to matrix operations for any kind of matrix types. In this section we highlight general functions and operators that are common to all matrix types. A matrix in Scilab refers to one- or two-dimensional arrays, which are internally stored as a one-dimensional array (two-dimensional arrays are stored in column order). It is therefore always possible to access matrix entries with one or two indices. Vectors and scalars are stored as matrices. Multidimensional matrices can also be used in Scilab. They are called hypermatrices. Elementary construction operators, which are overloaded for all matrix types, are the row concatenation operator “;” and the column concatenation operator “,”. These two operators perform the concatenation operation when used in a matrix context, that is, when they appear between “[” and “]”. All the associated entries must be of the same 2.1 Scilab Objects 13 type. Note that in the same matrix context a white space means the same thing as “,” and a line feed means the same thing as “;”. However, this equivalence can lead to confusion when, for example, a space appears inside an entry, as illustrated in the following: −→ A=[1,2,3 +5] A = ! 1. 2. ← here A=[1,2,3,+5] with a unary + 3. 5. ! −→ A=[1,2,3 *5] A = ! 1. 2. ← here A=[1,2,3*5] with a binary * 15. ! −→ A=[A,0; 1,2,3,4] A = ! ! 1. 1. 2. 2. 15. 3. 0. ! 4. ! ’ and .’ transpose (conjugate or not) diag (m,n) matrix with given diagonal (or diagonal extraction) eye (m,n) matrix with one on the main diagonal grand (m,n) random matrix int integer part of a matrix linspace or “ : ” linearly spaced vector logspace logarithmically spaced vector matrix reshape an (m,n) matrix (m*n is kept constant) ones (m,n) matrix consisting of ones rand (m,n) random matrix (uniform or Gaussian) zeros (m,n) matrix consisting of zeros .*. Kronecker operator Table 2.1. A set of functions for creating matrices. Table 2.1 describes frequently used matrix functions that can be used to create special matrices. These matrix functions are illustrated in the following examples: −→ A= [eye(2,1), 3*ones(2,3); linspace(3,9,4); zeros(1,4)] A = ! ! ! ! 1. 0. 3. 0. 3. 3. 5. 0. −→ d=diag(A)’ d = 3. 3. 7. 0. 3. 3. 9. 0. ! ! ! ! ← main diagonal of A as a row matrix 14 ! 2 Introduction to Scilab 1. 3. 7. 0. ! −→ B=diag(d) B = ! ! ! ! 1. 0. 0. 0. 0. 3. 0. 0. ← builds a diagonal matrix 0. 0. 7. 0. 0. 0. 0. 0. ! ! ! ! −→ C=matrix(d,2,2) C = ! ! 1. 3. ← reshape vector d 7. ! 0. ! The majority of Scilab functions are implemented in such a way that they accept matrix arguments. Most of the time this is implemented by applying mathematical functions elementwise. For example, the exponential function exp applied to a matrix returns the elementwise exponential and diﬀers from the important matrix exponential function expm. −→ A=rand(2,2); −→ B=exp(A) B = ! ! 1.2353136 2.1298336 ← a random matrix (uniform law) ← elementwise exponential 1.0002212 ! 1.3914232 ! −→ B=expm(A) B = ! ! 1.2354211 0.9918216 ← square matrix exponential 0.0002901 ! 1.391535 ! Extraction, Insertion, and Deletion To specify a set of matrix entries for the matrix A we use the syntax A(B) or A(B,C), where B and C are numeric or boolean matrices that are used as indices. The interpretation of A(B) and A(B,C) depends on whether it is on the left- or right-hand side of an assignment = if an assignment is present. If we have A(B) or A(B,C) on the left, and the right-hand side evaluates to a nonnull matrix, then an assignment operation is performed. In that case the left member expression stands for a submatrix description whose entries are replaced by the ones of the right matrix entries. Of course, the right and left submatrices must have compatible sizes, that is, they must have the same size, or the right-hand-side matrix must be a scalar. If the right-hand-side expression evaluates to an empty matrix, then the operation is a deletion operation. Entries of the left-hand-side matrix expression are deleted. Assignment or deletion can change dynamically the size of the left-hand-side matrix. 2.1 Scilab Objects −→ clear A; −→ A(2,4) = 1 A = ! ! 0. 0. 0. 0. ← assigns 1 to (2,4) entry of A 0. 0. 0. ! 1. ! −→ A([1,2],[1,2])=int(5*rand(2,2)) A = ! ! 1. 3. 0. 1. 0. 0. 0. 1. 8. 8. ← ‘‘:’’ stands for all indices, here all the rows of A 0. ! 1. ! −→ A(:,$)=[] A = ! ! ← submatrix deletion 0. ! 1. ! −→ A(:,1)= 8 A = ! ! ← assignment for changing a submatrix of A 0. ! 1. ! −→ A([1,2],[1,3])=[] A = ! ! 15 ← deletion, ‘‘$’’ stands for the last index 8. ! 8. ! −→ A(:,$+1)=[4;5] A = ! ! 8. 8. ← adding a new column through assignment 4. ! 5. ! When an expression A(B) or A(B,C) is not the left member of an assignment, then it stands for a submatrix extraction and its evaluation builds a new matrix. −→ A = int(10*rand(3,7)); −→ B=A([1,3],$-1:$) B ! ! = 2. 2. ← int integer part ← extracts a submatrix using row 1 and 3 and the two last columns of A 8. ! 3. ! When B and C are boolean vectors and A is a numerical matrix, A(B) and A(B,C) specify a submatrix of matrix A where the indices of the submatrix are those for which B and C take the boolean value T. We shall see more on that in the section on boolean matrices. 16 2 Introduction to Scilab Elementary Matrix Operations Table 2.2 contains common operators for matrix types and in particular numerical matrices. If an operator is not deﬁned for a given type, it can be overloaded. The operators in the table are listed in increasing order of precedence. In each row of the table, operators share the same precedence with left associativity except for the power operators, which are right associative. Operators whose name starts with the dot symbol “.” generally stand for term-byterm (elementwise) operations. For example, C=A.*B is the term-by-term multiplication of matrix A and matrix B, which is the matrix C with entries C(i,j)=A(i,j)*B(i,j). | logical or & logical and ~ logical not ==, >=, <=, >, < ,<>, ~= comparison operators +,binary addition and subtraction +,unary addition and subtraction .*, ./, .\, .*., ./., .\., *, /, /., \. “multiplications” and “divisions” ^,** , .^ , .** power ’, .’ transpose Table 2.2. Operator precedence. Note that these operators include many important equation-solving operations such as least squared solutions. The following calculations illustrate the use of several of these operators. −→ A=(1:3)’ * ones(1,3) A = ! ! ! 1. 2. 3. 1. 2. 3. 1. ! 2. ! 3. ! −→ A.* A’ ans = ! ! ! 1. 2. 3. ← transpose ’ and usual matrix product * ← multiplication tables: using a term-by-term product 2. 4. 6. 3. ! 6. ! 9. ! −→ t=(1:3)’;m=size(t,’r’);n=3; −→ A=(t*ones(1,n+1)).^(ones(m,1)*[0:n]) A ! ! ! = 1. 1. 1. 1. 2. 3. 1. 4. 9. ← term-by-term exponentiation to build a Vandermonde matrix A(i, j) = tj−1 i 1. ! 8. ! 27. ! −→ A=eye(2,2).*.[1,2;3,4] A = ← Kronecker product 2.1 Scilab Objects ! ! ! ! 1. 3. 0. 0. 2. 4. 0. 0. 0. 0. 1. 3. 0. 0. 2. 4. 17 ! ! ! ! −→ A=[1,2;3,4];b=[5;6]; −→ x = A \ b ; norm(A*x -b) ans = ← \ for solving a linear system Ax=b. 0. −→ A1=[A,zeros(A)]; x = A1 \ b x = ← underdetermined system: a solution with minimum norm is returned ! - 4. ! ! 4.5 ! ! 0. ! ! 0. ! −→ A1=[A;A]; x = A1\ [b;7;8] x = ← overdetermined system: a least squared solution is returned ! - 5. ! ! 5.5 ! 2.1.2 Strings Strings in Scilab are delimited by either single or double quotes “ ’ ” or “ " ”, which are equivalent. If one of these two characters is to be inserted in a string, it has to be preceded by a delimiter, which is again a single or double quote. Basic operations on strings are the concatenation (operator “ + ”) and the function length, which gives the string length. As expected, a matrix whose entries are strings can be built in Scilab, and the two previous operations extend to string matrix arguments as do the usual row and column concatenation operators. A string is just a 1x1 string matrix whose type is denoted by string (as returned by typeof). −→ S="a string with a quote character <<"’>> " S = a string with a quote character <<’>> −→ S=’a long string 0... −→ using continuation ’ S = a long string ← ... used to continue on next line using continuation −→ S=[’A’,’string’;’2x2’,’matrix’] S = ← a string matrix 18 2 Introduction to Scilab !A ! !2x2 string ! ! ! matrix −→ length(S) ans = ! ! 1. 3. ← length of each string of S in a matrix 6. ! 6. ! ascii execstr grep part msscanf msprintf strindex string stripblanks strsubst tokens strcat length conversion from string to ascii values send a string to the Scilab interpreter search for occurences of a string in a string matrix substring extraction scans input from a string according to a format builds a string by output according to a format ﬁnds occurrences of strings in a string converts data to string remove leading and trailing white (blank) characters string substitution in a string matrix string tokenizer concatenate string matrix entries length of string matrix entries Table 2.3. Some string matrix functions. String matrix utility functions are listed in Table 2.3. The next session shows how some of them can be used. −→ A=rand(2,8,’n’); −→ A=sign(A); −→ A=string(A) A = !-1 ! !1 ← Normal law ← just keep the signs ← convert to string matrix 1 1 1 1 1 -1 1 1 1 1 -1 -1 -1 1 −→ A=strsubst(A,’1’,’+’); −→ A=strsubst(A,’-+’,’-’) A = !! !+ + + + + + - + + + + - - - + ! ! ! ← string substitution ← string substitution ! ! ! The command execstr can be used to evaluate a string as a Scilab expression. The given string is evaluated using values from the current context. Thus, string matrices 2.1 Scilab Objects 19 can be used to build Scilab expressions, which then can be evaluated as if they were entered interactively in Scilab. An important extra argument ’errcatch’ can be given to the execstr function in order to supress the Scilab standard error mechanism while the string is evaluated. −→ name =’x’; n=3; val=[45,67,34]; −→ str = name +string(1:n)+’=val(’ +string(1:n)+’);’ str = !x1=val(1); x2=val(2); x3=val(3); −→ execstr(str); −→ [x1,x2,x3] ans = ! 45. 67. ← a string vector ! ← Scilab evaluation of str ← x1, x2 and x3 are now deﬁned 34. ! 2.1.3 Boolean Matrices A boolean variable can take only the two values “true” T and “false” F. Two predeﬁned Scilab variables %t and %f respectively evaluate to T and F and can be used to build boolean matrices, for example through the use of concatenation operations. Comparison operators (“ == ”, “ > ”, “ >= ”, “ <= ” and “~=”) also give boolean matrices as the result when their arguments are matrices or one matrix and one scalar. Logical operators such as “ & ” (and) , “ | ” (or), and “~” (not) can be used as expected with boolean matrix arguments. The logical function and (resp. or) takes as argument a single boolean matrix and returns the logical and (resp. or) of the matrix entries. Boolean matrices are used in Scilab in conjunction with conditional expressions such as the if and while conditions, which will be described later. −→ true=%t; −→ if true then disp("Hello"), end Hello ← deﬁne boolean variable ← conditional display The following Scilab session shows simple instructions involving booleans. We see in particular that even though matrix booleans are not coded as numbers, they can be used in numerical computations: −→ ~(1>=2) ans = T −→ %t&%t ans = T −→ x=-10:0.1:10; 20 2 Introduction to Scilab −→ y=((x>=0).*exp(-x))+((x<0).*exp(x)); −→ y=bool2s([%t,%f]) y = ! 1. ← automatic boolean-to-scalar conversion ← explicit boolean-to-scalar conversion 0. ! We have mentioned previously that submatrix extraction can be done with boolean vectors. This is illustrated in the following session, where we also introduce the function find, which returns the indices of the true entries of a boolean matrix. −→ A = int(10*rand(1,7)) A = ! 2. 7. 0. 3. 6. 6. −→ A( A>= 3) = 0 A = ! 2. 0. 0. ← indices are given by a boolean matrix A>=5 0. 0. 0. −→ I=find(A== 0) I = ! 2. 3. 4. 8. ! 0. ! ← indices of A entries equal to 0 5. 6. 7. ! 2.1.4 Polynomial Matrices Polynomials are Scilab objects. Most operations available for constant matrices are also available for polynomial matrices. A polynomial can be deﬁned using the Scilab function poly. It can be deﬁned based either on its roots or its coeﬃcients, as can be seen from the following Scilab session. −→ p=poly([1 3],’s’) p = ← polynomial deﬁned by its roots 2 3 - 4s + s −→ q=poly([1 2],’s’,’c’) q = ← polynomial deﬁned by its coeﬃcients 1 + 2s Note that the ’s’ argument in poly speciﬁes the character to be used for displaying the formal parameter of the polynomial. At initialization, the variable %s is deﬁned to be the polynomial s. Polynomials can be added together, multiplied, concatenated to form matrices, etc., provided they use the same formal parameter. 2.1 Scilab Objects −→ p+q+1 ans = 21 ← polynomial/constant addition 2 5 - 2s + s −→ [p*q,1] ans = ! ! 2 3 3 + 2s - 7s + 2s ← matrix polynomial 1 ! ! A polynomial can even be divided by a polynomial, and a polynomial matrix inverted, but the result, in general, is not a polynomial. It is a rational function, which is another Scilab object constructed using a list object, as we shall see later. 2.1.5 Sparse Matrices Many areas of application lead to problems involving large matrices. But very often, large matrices involved in applications have many zero entries. Thus representations of matrices where matrix storage is reduced by coding only the nonzero matrix entries have been developed and are known as sparse matrix representations. Diﬀerent coding schemes exist for performing this compression task and some schemes are tailored to speciﬁc applications. In Scilab, the sparse coding of an (m,n) matrix is as follows. A ﬁrst array of size m contains pointers to row descriptors. Each row descriptor is coded with two arrays. The ﬁrst array contains the column indices of the nonnull row entries, and the second array contains the associated nonzero values. Some problems that could not be stored in Scilab memory in full form can be accessible through the use of sparse storage. As one can imagine, the reduced storage leads to more complex and slower operations on sparse matrices than on full matrices. The basic function used for building sparse matrices in Scilab is the sparse function. Note that only numerical or boolean matrices can be stored as sparse matrices. −→ A=sprand(100,100,0.1); −→ whos(’-type’,’sparse’) Name Type ← a (100,100) sparse matrix with 10% of nonzero values ← check used memory Size Bytes A sparse 100 by 100 Name Type ← used memory for the same matrix with a full implementation Size Bytes B constant 100 by 100 −→ B=full(A); −→ whos(’-name’,’B’); −→ timer();inv(B);timer() ans = 0.01 13360 80016 ← CPU time for full matrix inversion 22 2 Introduction to Scilab −→ timer();inv(A);timer() ans = ← CPU time for sparse matrix inversion 0.28 While useful in illustrating the commands, this example was only 100 × 100, which is not considered today to be a very large matrix. Also, this matrix was random, and many sparse matrices, such as those that arise by the discretization of PDEs, are not random. They have stronger structural properties that can be exploited. 2.1.6 Lists Scilab lists are built with the list, tlist, and mlist functions. These three functions do not exactly build lists, but they can be considered to be structure builders in the sense that they are used to aggregate under a unique variable name a set of objects of diﬀerent types. They are implemented as an array of variable-size objects that is not a list implementation. A type corresponds to each builder function, and they are recursive types (a list element can be a list). • If the list constructor is used, then the stored objects are accessed by an index giving their position in the list. • If the tlist constructor is used, then the built object has a new dynamic type and stored objects can be accessed through names. Note also that the ﬁelds are dynamic, which means that new ﬁelds can be dynamically added or removed from an occurrence of a tlist. A tlist remains a list, and access to stored objects through indices is also possible. • The mlist constructor is a slight variation of the tlist constructor. The only diﬀerence is that the predeﬁned access to stored objects through indices is no longer eﬀective (it is, however, possible using the getfield and setfield functions). Also, extraction and insertion operators can be overloaded for mlist objects. This means that it is possible to give a meaning to multi-indices extraction or insertion operations. hypermat objects that implement multidimensional arrays are implemented using mlist in Scilab. Note that many Scilab objects are implemented as tlist and mlist, and from the user point of view this is not important. For example, suppose that you want to deﬁne a variable with an extendable number of ﬁelds. This is done very easily through the use of the “.” operator: −→ x.color = 4; −→ x.value = rand(1,3); −→ x.name = ’foo’; −→ x ans = color: 4 value: [0.2113249,0.7560439,0.0002211] name: "foo" ← x is a tlist of type struct (st) 2.1 Scilab Objects 23 Among Scilab objects coded this way, it is important to mention the rational matrices and the linear state-space systems, which play important roles in modeling and analysis of linear systems. The following session illustrates the use of rational matrices in Scilab (recall that %s is by default the polynomial s). Note that all the elementary operations are overloaded for rational matrices. −→ r=1/%s r = ← deﬁning a rational number 1 s −→ a=[1,r;1,1] a = ! ! ! ! ! ! ! 1 1 1 s 1 1 1 1 ← rational matrix construction ! ! ! ! ! ! ! −→ b=inv(a) b = ! s ! ----! - 1 + s ! ! - s ! ----! - 1 + s ← matrix inversion - 1 ----- 1 + s s ----- 1 + s ! ! ! ! ! ! ! −→ b.num ans = ! s ! ! - s ← numerator ﬁeld - 1 s ! ! ! −→ b.den ans = ! - 1 + s ! ! - 1 + s ← denominator ﬁeld - 1 + s - 1 + s ! ! ! A linear state-space system is characterized in terms of four matrices, A, B, C, and D; we will describe and use these systems later for modeling linear systems. The Scilab function ssrand deﬁnes a random system with given input, output, and state sizes. 24 2 Introduction to Scilab −→ sys=ssrand(1,1,2) sys = sys(1) !lss A B C ← deﬁnes a linear system (state-space system:) D X0 dt ! sys(2) = A matrix = ! - 0.7616491 ! 0.6755537 1.4739762 ! 1.1443051 ! sys(3) = B matrix = ! ! 0.8529775 ! 0.4529708 ! sys(4) = C matrix = ! 0.7223316 1.9273333 ! sys(5) = D matrix = 0. sys(6) = X0 (initial state) = ! ! 0. ! 0. ! sys(7) = Time domain = c −→ sys.A ans = ! - 0.7616491 ! 0.6755537 ← extract the A matrix 1.4739762 ! 1.1443051 ! Common operations on lists are illustrated in the following example. Note that one can deﬁne an entry starting somewhere other than entry 1, but then the list will create early places for entries. This is analogous to Scilab automatically setting up a 3 × 5 matrix when the instruction A(3,5)=6.7 is entered. Except now, instead of extra zero entries, an entry called Undefined ﬁlls out the earlier positions in the list. −→ L=list() L = () ← an empty list 2.1 Scilab Objects −→ L(2) = testmatrix(’magi’,3) L = 25 ← list assignment using L(i)=val. Note that L(1) does not exist L(1) Undefined L(2) ! ! ! 8. 3. 4. 1. 5. 9. 6. ! 7. ! 2. ! −→ L(0) = 34; −→ L($) = ’X’ L = ← add an element at the begining of list ← replace last element L(1) 34. L(2) Undefined L(3) X −→ L($+1) = ’Y’ L = ← add an element at end of list L(1) 34. L(2) Undefined L(3) X L(4) Y −→ [a,b]=L([1,3]) b = X a = ← extraction, we can extract several argument in one call 26 2 Introduction to Scilab 34. −→ L(2)=null(); ← deletion of the second list element 2.1.7 Functions We have used many Scilab functions in previous examples through function calls without precisely deﬁning what a function is. A function is known through its calling syntax. For example, the calling syntax for the sine function sin is the same as that for the hyperbolic sine function sinh. There is a small diﬀerence between sin and sinh. sin is a hard-coded function (hardcoded functions are sometimes called primitives) and sinh is a Scilab-coded function (they are sometimes called macros in Scilab). Thus, sin and sinh do not have the same type. The answer to typeof will be fptr for sin and function for sinh. But both sin and sinh are Scilab objects. They have a type, and can be used as variables and function arguments. A Scilab-coded function can be deﬁned interactively using the keywords function and endfunction, loaded from a scilab script using exec or getf, or saved and loaded in binary mode using save and load. In addition, a library of functions can be deﬁned and loaded in Scilab. At startup, a number of function libraries are loaded, providing a rich set of functions available to the user. The source code of a Scilab-coded functions can be examined using the fun2string function. We will discuss the construction of new functions in Scilab later: in Section 2.2.3 we go over Scilab-coded functions, and in Section 2.5, hard-coded functions are covered. Here, we look at an elementary example to give a feeling for the syntax. −→ function y=foo(x,g) ; y=g(x); endfunction −→ typeof(foo) ans = ← a function ← a function is a Scilab object function −→ foo(%pi,sin) ans = ← functions can be used as function arguments (primitive sin) 1.225D-16 −→ foo(%pi,sinh)== sinh(%pi) ans = ← the same with the macro sinh T −→ v=rand(1,10); −→ foo(3,v) ans = ← function call and matrix extraction have the same syntax. 0.0002211 As with other variables, functions can be removed or masked by assignment. For example, if a session starts with the command sin=4, then the sin variable is 4 and no longer 2.2 Scilab Programming 27 the sin primitive. However, primitives and scilab functions available through libraries (for example those provided by Scilab default libraries) are just hidden by this mechanism. If sin is cleared by the command clear sin, then the sin primitive will return to the current environment. −→ sin = [1,3]; −→ sin(2) ans = ← the sin primitive is hidden by the deﬁned variable 3. −→ clear sin −→ sin(2) ans = ← clear the sin variable ← the sin primitive is found and called 0.9092974 2.2 Scilab Programming A Scilab program is a set of instructions to be executed in a speciﬁc order. These instructions can be typed one by one at Scilab’s prompt, but they can also be placed in an ASCII ﬁle (using for example Scilab’s built-in editor) and executed with the Scilab command exec. Such a ﬁle is called a script and may contain function deﬁnitions. By convention, Scilab script ﬁle names are terminated with the suﬃx .sce, but if the script contains only function deﬁnitions, then the .sci suﬃx is used. In that case the command getf can be used instead of exec to load the functions. The ﬁle extensions .sci and .sce are just conventions, since Scilab functions exec and getf do not check ﬁle name suﬃxes. However, in most editors with a Scilab mode, the mode activation is controlled by the ﬁle-name suﬃx. Under the Windows operating system, a ﬁle can be drag-and-dropped into Scilab. In this case, the ﬁle extensions are used to select appropriate actions such as loading with getf and executing with exec. It is also possible to execute a script when launching Scilab from a shell window (Unix or MSDOS), using the calling option scilab -f <script-file-name>. If the script is terminated by the instruction quit, then Scilab will quit at the end of the script execution. This provides for batch mode execution in Scilab. When Scilab is launched, a speciﬁc script ﬁle called scilab.star is executed. This ﬁle contains, in particular, instructions for initializing a number of Scilab variables and loading various libraries. Then, if there exists a ﬁle called .scilab or scilab.ini in the user’s home directory (the home directory is deﬁned by the environment variable HOME), Scilab executes it as a script. Finally, Scilab looks in the directory where Scilab is launched and executes the ﬁle .scilab or scilab.ini, if it exists. Iteration and branching instructions are a fundamental part of programming, so we begin by looking at these instructions in Scilab. 28 2 Introduction to Scilab 2.2.1 Branching Branching instructions are used to make block execution of code depend on boolean conditions. The simplest form of branching instruction in Scilab takes the following form: if <condition> then <instructions> end The block of instructions <instructions> will be executed if the condition (<condition>) evaluates to a boolean T. Since Scilab is a matrix language, the condition evaluation can be a boolean matrix or a scalar matrix. If it is a matrix, the condition is considered as a T boolean only if all the matrix entries are true boolean values or if all the scalar matrix entries are nonnull. Thus, implicitly <condition> is evaluated as an and(<condition>). The following session generates the logarithm of a random 3 × 3 matrix and checks whether it is real. −→ A=log(rand(3,3)) ; −→ if imag(A)==0 then disp(’A is a real matrix ’); end A is a real matrix The following syntax is used for the two branch form: if <condition> then <instructions> else <instructions> end The ﬁrst block is executed when <condition> evaluates to T; otherwise, the second block is evaluated. −→ if imag(A)==0 then −→ disp(’A is a real matrix ’); A is a real matrix −→ else −→ disp(’A is complex’); −→ end A multiple branch version is also available. Successive else statements are coded using the elseif keyword. Note that there also exists a multiple branch execution instruction named select, which can be used when execution control depends on a set of predeﬁned values: select case case ... else end <expr> , <expr1> then <instructions> <expr2> then <instructions> <instructions> In executing select, the value of the <expr> statement is successively compared to the values of <expr1>, <expr2>, . . . . As soon as both evaluated expressions evaluate to equal values (== operator), the execution branches to the associated block. If no equal case is detected, then the else block, if present, will be executed. 2.2 Scilab Programming 29 2.2.2 Iterations Two iterative control structures exist in Scilab. They are the for loop and the while iterator. For execution eﬃciency, one should carefully check to see wether iterative control structures can be replaced by matrix operations, which usually execute faster. The for loop syntax is as follows: for <name>=<expr> <instructions> end The <expr> instruction is evaluated once. Then, the inner block will be iteratively executed, and at each iteration the <name> variable will take a new value. If the <expr> instruction evaluation gives a matrix, then the number of iterations is given by the number of columns, and the loop variable <name> will take as value the successive columns of the matrix. If the <expr> instruction evaluation gives a list, then the number of iterations is given by the list length, and the loop variable <name> will take as value the successive values of the list elements. A break statement can be present in the <instructions> block of the for loop. If the break instruction is reached during execution, then the block execution stops and execution continues beyond the end of the for loop. The following Scilab session illustrates several aspects of using for, including the differences in CPU time when the alternative of using matrix operations exists. This session uses the function pmodulo(n,i), which returns the remainder upon dividing n by i, n mod i. The session also uses the timer function to ﬁnd the CPU time for a computation. −→ n=89; −→ isprime=%t; −→ for i=2:(n-1) ← iterate on integers from 2 to n-1 −→ if pmodulo(n,i)==0 then isprime=%f; break; end −→ end −→ isprime ans = ← checks result stored in variable isprime T −→ n=16778; −→ timer(); −→ res=[]; −→ for i=2:(n-1) −→ if pmodulo(n,i)==0 then −→ res=[res,i]; −→ end −→ end −→ t1=timer(); −→ res ans = ! 2. ← now illustrate diﬀerence in CPU time ← start timer ← want all the divisors of n ← size of vector res increases at each iteration ← CPU time elapsed from last call to timer ← all the divisors of n 8389. ! 30 2 Introduction to Scilab −→ v=2:(n-1); −→ timer(); −→ I=find(pmodulo(n,v)==0); −→ res = v(I) res = ! 2. ← speeding up computation using matrix computation ← indices of divisors using find ← all the divisors of n 8389. ! −→ t2=timer(); −→ [t1,t2] ans = ! 0.83 ← the CPU time of each example 0.01 ! The while iterator is especially useful when it is not known ahead of time how many iterations are to be performed. It has the following syntax: while <condition> <instructions> end The <instructions> block statements are executed while the <condition> evaluates to boolean T. As in the if statement, the condition evaluation can be a boolean matrix or a numerical matrix. If it is a matrix, then <condition> is considered as a T boolean only if all the matrix entries are true boolean values or if all the matrix entries are nonzero. If <condition> always evaluates to T, then a break statement will be needed to stop the <while> loop. The next three examples use the fact that large enough real numbers are considered to be inﬁnity %inf, which can be compared or added to ﬁnite real numbers. −→ x=1; while exp(x)<>%inf ; x=x+1; end ← a simple while with a scalar condition ← checks result −→ [exp(x-1),exp(x)]==%inf ans = ! F T ! −→ x=[1:3]; while exp(x)<>%inf ; x=x+1; end −→ exp(x)==%inf ans = ← a simple while with matrix condition ← one entry is %inf ! F F T ! −→ x=1; −→ while %t −→ if exp(x)== %inf then −→ break; −→ end −→ x=x+1; −→ end −→ [exp(x-1),exp(x)]==%inf ans = ! F T ! ← inﬁnite loop, need a break to quit the while ← quit the loop when exp(x) equals ∞ ← checks result 2.2 Scilab Programming 31 2.2.3 Scilab Functions It is possible to deﬁne new functions in Scilab. What distinguishes a function from a script is that a function has a local environment that communicates with the outside through input and output arguments. A function is deﬁned using the keyword function followed by its calling syntax, a set of Scilab instructions, and the keyword endfunction. More precisely, the following form of syntax is used to deﬁne a function: function [<name1>,<name2>,...]=<name-of-function>(<arg1>,<arg2>,...) <instructions> endfunction When deﬁning a function we have to give the function name (<name-of-function>), the list of calling arguments (<arg1>,<arg2>,...), and the list of variables that are used to return values (<name1>,<name2>,...). Note that a function can return more than one value. Calling a function is done as follows: <name-of-function>(<expr1>,<expr2>,...) or [<v1>,<v2>,...,<vp>]=<name-of-function>(<expr1>,<expr2>,...) In the ﬁrst case the returned value is the value of the ﬁrst returned argument by the function evaluation. In the second case the p ﬁrst returned values of the function evaluation will be copied in the p variables whose names are given by [<v1>,<v2>,...,<vp>]. When a function is called, the expressions given as arguments are ﬁrst evaluated and their values are passed to the function evaluation. Thus, Scilab uses a calling by value mechanism for all argument types. However, if an argument of a calling function instruction is a variable name and if that variable is not changed in the function evaluation body, then the variable will not be copied during the function call. In the evaluation of the body of a function, variable search is performed ﬁrst in the local environment of the function evaluation and then in the calling environments. Thus a variable that is not a calling variable and not locally deﬁned in the function can still have a value if it is deﬁned in a calling environment. However, the variable in the calling environment cannot be changed. If an assignment statement involving this variable is used, then a local copy will be created. The function body evaluation normally stops when all the <instructions> are executed or when the ﬂow of instructions reaches a return statement. When function evaluation stops, the ﬂow of instruction returns to the caller. The returned values <name1>, <name2>, . . . have the values that they had when the function body evaluation stopped. The following session gives two examples of deﬁning a function and evaluating it. The second function deﬁnes the factorial function. −→ function B=f(A) ← a typical function with one input and one output −→ B=string(sign(A)); −→ B=strsubst(strsubst(B,’1’,’+’),’-+’,’-’); −→ endfunction −→ f(rand(2,5,’n’) ) ans = 32 !! !+ 2 Introduction to Scilab + + + + + + + - ! ! ! −→ function y=fact(x) ← a recursive function can be deﬁned: fact calls fact → − if x <= 1 then y=x; else y=x*fact(x-1); end −→ endfunction −→ fact(4) ans = 24. This next script deﬁnes several functions and illustrates the rules for searching variable values when a function is evaluated. This is important but quite technical and can be omitted for ﬁrst-time readers. −→ function y=f(x); y=2*x; endfunction −→ x=90; −→ f() ans = ← argument x is not given but exists in the calling environment. Thus, the calling sequence works 180. −→ f(5,7) ← error: two many given arguments !--error 58 incorrect number of arguments in function call... arguments are : x −→ [a,b]=f(5) ← error: asking for too many returned arguments !--error 59 incorrect # of outputs in the function arguments are : y −→ function y=f(x); z=x; endfunction −→ y=89; −→ z=67; −→ w=f(x) w = ← a new deﬁnition of f ← y is not computed inside f but it has a value in the calling environment. This is the returned value 89. −→ z ← z was not modiﬁed by execution of f 2.2 Scilab Programming ans 33 = 67. −→ function y=f(); y=x; endfunction −→ x=5; −→ y=f() y = ← x is not locally deﬁned: the value of x in the calling environment is used 5. −→ function y=f(); x= 2*x; y=x; endfunction −→ y=f() y = ← a local variable x is created 10. −→ x=[56,67]; −→ function y=f(); x(1)= 5; y=x −→ y=f() y = endfunction ← again a local variable x is created leading to a scalar y 5. As already pointed out, a function that is speciﬁed with n calling arguments and p returned values can be used in a calling statement with fewer than n calling values, and the number of requested returned arguments can be less than p. During execution of a function body the actual number of given arguments and the number of requested outputs are available through the use of the function argn. A statement like [lhs,rhs]=argn() in a function body will return in lhs (resp. rhs) the number of requested output arguments (resp. the number of given input arguments). The function writer can take advantage of this for enabling optional argument passing, as shown in the following example. This example also shows that the function error can be used to produce an error statement in Scilab. −→ function [u,v]=f(x,y) −→ [lhs,rhs]=argn() −→ if rhs <= 0 then error(’at least one argument must be given’); end −→ if rhs <= 1 then y=2; end −→ if lhs == 2 then −→ u=x; v=y; −→ else −→ u=x+y; −→ end −→ endfunction −→ [u,v]=f(4) v = 34 u 2 Introduction to Scilab 2. = 4. If the function deﬁned has n calling arguments, then you do not normally have the right to call a function with more than n calling arguments. There is one exception, which is described now. If the last argument of a function deﬁnition with n arguments is named varargin, then the function can be called with more than n arguments. Inside the function body, the varargin variable will be a Scilab list that contain as elements the values of the arguments from the nth one to the last one. −→ function [l]=f(x,varargin); l = varargin; endfunction −→ f(0,1,2) ← varagin is set to list (1,2) ans = ans(1) 1. ans(2) 2. The same mechanism also exists for the output through the keyword varargout, as illustrated in the following session. −→ function [varargout]=f() −→ varargout=list(1,2,3) −→ endfunction −→ [a,b]=f() b = a ← f can be called with more than 1 output 2. = 1. Changing a variable in a calling environment, as a side eﬀect of a function call, can sometimes be useful. This is achieved in Scilab through the use of global variables. The global keyword can be used to declare a global variable. Once a variable is declared global in an environment its value can be changed from within that environment. For example, a global(’x’) statement must be present in the body deﬁnition of a function if the function is to be used to modify this global variable x. −→ global a; −→ isglobal(a) ans = ← a is now a global variable, its default value is [] ← just checks . . . 2.2 Scilab Programming 35 T −→ function f(); global(’a’); a=int(10*rand(1,4)); endfunction −→ f() ← calling f, which contains a global declaration of a −→ a ← calling f has changed a ans = ! 2. 7. 0. 3. ! 2.2.4 Debugging Programs When programming in any language, it is nearly impossible to write large programs without errors. Programming in Scilab is not an exception to this rule. A set of Scilab utilities helps users detect and correct bugs in their code. In particular, one can interrupt the execution of Scilab code using the command pause (menu Stop of the Control menu in the main menu) or using the interruption key code Ctrl-C . When entering a pause, the Scilab prompt changes, thereby signaling to the user that the interpreter has entered a pause sequence, and shows the pause level. When a function is called, its code is executed in a local environment. The same situation occurs when entering a pause. The current execution ﬂow is stopped and the interpreter is called in a new local environment. One has to consider that the environments are stacked and the current environment is at the top of the stack. When entering a function call or a pause, a new environment is pushed to the top of the stack. When code is executed in the environment that is at the top of the stack, it has read access to variables from the other stacked environments and read-write access to the global environment. When a pause is entered through the use of Ctrl-C, execution stops at the current execution position. Using whereami (or where), it is possible to detect where the pause has occurred. It is, however, easier to explicitly insert a pause command in Scilab code or in a function in order to precisely control where a pause is needed to detect wrong execution of code. This can be done in an editor by explicitly inserting pause statements. −→ a=34; −→ function y=f(x); pause; a = %pi; pause; y=g(x); endfunction −→ function y=g(x); b = %e ; y=sin(x); pause; endfunction −→ f(5) -1->a ans = ← a pause in f. We can check variables from the stacked environments 34. -1->b= 56; ← here we create a variable in the pause environment -1->resume ← quit the ﬁrst pause and stop at the second one still in f -1->a ans ← the value of a in the local environment of f = 3.1415927 36 2 Introduction to Scilab ← quit the pause and stop in the next one in g -1->resume ← the value of b in g environment -1->exists(’b’,’local’) ans = 0. ← go on -1->resume ans = - 0.9589243 −→ a=g(4); -1->[y]=resume(456); −→ a ans ← stop in g ← quit the pause environment and copy a variable in function g environment ← the returned g value is the y value that was set by the resume function = 456. Leaving the pause mode and ending the debugging is done with the abort command. Changing function code by inserting pause commands implies that you have to reload the function code in Scilab each time you change the code. The function setbpt can be used to do the same task but without changing function code. In the local environment of the pause, one can check the status of variables, and the current execution can be resumed with the command resume. Execution ﬂow can also be interrupted using abort (resp. quit). Execution is then stopped and all the environments are popped from the stack in the case of an abort command. Just the current environment is popped in the case of a quit command. (Here, getting something oﬀ the stack is called a pop and putting something on the stack is called a push.) Step-by-step execution of code or of a function can also be enabled through the use of the exec command. When a function call is performed with exec, the function is executed in the current environment and one has to provide values for the function calling arguments in the current environment before calling exec. −→ function y=f1(x) −→ y=sin(x).*x −→ y = y+2, −→ endfunction −→ getf(’stepf.sci’,’n’); −→ x=int(10*rand(1,4)); −→ x=90; −→ exec (f1,7) step-by-step mode: enter carriage return to proceed >> y=sin(x).*x y = 80.4597 >> ← load f1 with getf option ’n’ ← argument of f1 ← single-step execution f1 2.3 Input and Output Functions 37 y = y+2, y = 82.4597 >> return! Note that by default, the source code of the function instructions is hidden when getf is used. In order to see the source code, one has to load the function into the Scilab environment with the getf command associated with the optional argument ’n’. Finally, it should be noted that the built-in editor also provides some debugging facilities. 2.3 Input and Output Functions The default for Scilab commands is to show the results in the command window unless they create a graphic, and then the default is to show the result in a graphics window. However, it is often desired to have the result saved in a ﬁle or printed on a printer. In this section we discuss how to control the input and output of diﬀerent types of data. 2.3.1 Display of Variables As already seen in previous examples, when a Scilab instruction is followed by a comma or a line feed, a display of the instruction evaluation result is provided. If instructions are ended by a semicolon or belong to the body of a function, then no display occurs. Sometimes one wants values of variables inside of functions to be displayed or output to a ﬁle. Explicit display of the value of a variable, or of the result of an instruction evaluation, is achieved by the function disp or print. These two functions provide a display similar to the one obtained automatically in the command window. The ﬁrst argument of the print function is used to control the output stream (disp uses the Scilab command window as output stream). One can use a ﬁle name given by a Scilab string or a ﬁle descriptor (returned by the file command). Alternatively one can use the predeﬁned value %io(2), which stands for the standard output stream in the Scilab command window. −→ a=[%pi,4,%inf] a = ! 3.1415927 4. ← the result is displayed Inf ! −→ print(%io(2),a) a = ! 3.1415927 4. −→ print(’a.txt’,a) ← using print to display a variable (same as disp) Inf ! ← print to ﬁle a.txt print can be used to force the display of variables inside a function during its execution, as illustrated below: 38 2 Introduction to Scilab −→ function y=f(x) ; a = 2*%pi, y = x+a, endfunction −→ f(2); ← a is not displayed −→ function y=g(x) ; a = 2*%pi, print(%io(2),a), y = x+a, endfunction −→ g(2); ← force display of a using print a = 6.2831853 The format used by disp and print for displaying real numbers can be controlled using the format function. 2.3.2 Formatted Input and Output Several Scilab functions can used to perform formatted input-output. The functions write and read are based on Fortran formatted input and output. They redirect input and output to streams, which are obtained through the use of the function file. They can also use the standard Scilab input-output streams, which are given by the predeﬁned variables %io(2) and %io(1). We will not describe these functions in detail here although they can be quite useful for interfacing with Fortran software. We will focus here on Scilab functions that emulate standard C input and output functions. They give access to formatted (ASCII or binary) input or output on the standard input-output streams, on ﬁle streams, and on strings. mprintf mfprintf msprintf mscanf mfscanf msscanf fprintfMat fscanfMat mgetl mputl mopen mclose formatted output on standard output stream formatted output in a ﬁle stream formatted output in a string matrix formatted input on standard input stream formatted input in a ﬁle stream formatted input in a string matrix scalar matrix output in a ﬁle read a scalar matrix from a ﬁle input a ﬁle as an mx1 string matrix output a string matrix in a ﬁle open a ﬁle stream close a ﬁle stream Table 2.4. Input-output functions. The function mopen is a Scilab implementation of the standard C function fopen with the following syntax: [fd,err]=mopen(filename, mode, swap) Here filename is a string that denotes a ﬁle name. The mode argument is also a string, whose length does not exceed three (its default value is ’rb’) that describes the stream mode to be used. The input stream is obtained with (’r’) and the output stream with (’w’). Append mode is obtained by adding an (’a’) character, and a bidirectional stream for input and output is obtained with (’r+’). An added ’b’ character will specify that streams are to be used in binary mode. 2.3 Input and Output Functions 39 The mclose function can be used to close opened input or output streams, even the streams opened with the file command (the number of opened streams is system limited). It is possible to close all the opened streams using the instruction mclose(’all’). The formatted input and output functions are very similar to functions provided by the standard C input-output library. For example, the format speciﬁcation follows the format speciﬁcation of C functions. However, Scilab input-output formatted functions automatically take care of the matrix aspect of Scilab objects. When using a formatted output stream, the arguments of the conversion speciﬁers are searched in the rows of the Scilab arguments. When using a formatted input it is possible to specify the number of times the format has to be repeated, for example, up to the end of ﬁle. Thus it is possible using one read instruction to obtain a matrix, or a set of matrices, row after row: −→ mprintf(’| %5d | %5.4f |\n’, (1:2)’,[%pi;%e]) | | ← formatted output sequentially applied to rows 1 | 3.1416 | 2 | 2.7183 | −→ function table(A) −→ [m,n]=size(A); −→ format= "%5.0f |"; −→ format=strcat(format(ones(1,n))); −→ format1= part("-",ones(1,5+2)); −→ format1=strcat(format1(ones(1,n))); −→ mprintf(format1+’\n’); −→ mprintf(’|’+format+’\n’+format1+’\n’,A); −→ endfunction −→ A=int(20*rand(2,5)); −→ table(A) ← format for each entry ← repeat the format n times ← a string made of ‘‘-’’ ← same format for each row of A ← A dislayed as integer values in a table ----------------------------------| 4 | 0 | 13 | 16 | 17 | ----------------------------------| 15 | 6 | 12 | 13 | 1 | ----------------------------------−→ S=msprintf(’%d %d \n’,int(20*rand(3,2))); −→ A= rand(4,3); −→ fprintfMat(’test’,A); B=fscanfMat(’test’); −→ norm(B-A) ans = ← using a string matrix as output (S) ← read and write of a constant matrix in an ascii ﬁle ← testing the write-read sequence 0.0000007 ← opens a ﬁle for reading ← scanning a ﬁle with the same format on each row up to end of ﬁle −→ fd=mopen(’test’,’r’); −→ L=mfscanf(-1,fd,"%f%f%f") L ! = 0.231224 0.3076090 0.3616360 ! 40 ! ! ! 2 Introduction to Scilab 0.216463 0.883389 0.6525130 0.932962 0.214601 0.3126420 −→ norm(L-A) ans = 0.292227 ! 0.5664250 ! 0.482647 ! ← note that numerical precision depends on selected format directives 0.0000007 −→ mclose(fd); ← close the opened ﬁle The next example describes a formatted input where strings and scalars are mixed in a ﬁle that contains information on some Scicos ﬁles. The ﬁle mfscanf.dat is the following An example of reading a file with a separator of type ’[ ]*,[ ]*’ ------------------------------------------------------------------------CLKIN_f.sci , 98 , 16 CLKINV_f.sci , 91 , 16 CLOCK_f.sci , 116 , 16 CONST_f.sci , 50 , 8 CURV_f.sci , 80 , 12 We now illustrate how to use the function mfscanf to read out separately the numerical and the string information in this ﬁle. −→ fd=mopen(’mfscanf.dat’,’r’); ← opens a ﬁle in read mode −→ mgetl(fd,2); ← bypassing the ﬁrst two lines −→ [n,a,b,c]=mfscanf(-1,fd,’%[^,],%*[, ]%d%*[, ]%d\ n’); ← formatted read −→ n ans ← number of read arguments = 3. −→ stripblanks(a)’ ans = !CLKIN_f.sci CLKINV_f.sci ← removing spaces and tranposing string matrix CLOCK_f.sci −→ [b,c] ans = ! ! ! ! ! 98. 91. 116. 50. 80. CONST_f.sci CURV_f.sci ! ← numerical values 16. 16. 16. 8. 12. ! ! ! ! ! −→ mclose(fd); ← close the opened ﬁle stream 2.3.3 Input Output in Binary Mode Scilab has its own binary internal format for saving Scilab objects and Scilab graphics in a machine-independent way. It is possible to save variables in a binary ﬁle from the 2.3 Input and Output Functions 41 current environment using the command save. A set of saved variables can be loaded in a running Scilab environment with the load command. The functions save and load use a machine-independent binary format. Note that the advantage of the binary mode is that save and load can be done without any loss of numerical accuracy. For saving and reloading graphics in a Scilab binary format, the functions xsave, xload can be used (note that these functions can also be accessed through menus). It is also possible within Scilab to read and write any binary ﬁle through the use of a set of functions that will be described here. Note that as in the previous section, the functions described here need a stream as ﬁrst argument. Input or output streams are created with the mopen function already described, which is to be used here with a binary ﬂag (’rb’ or ’wb’). Functions described in Table 2.5 can be used for binary input-output. The mget and mput use the mode (little or big “endian”) that was speciﬁed by the mopen call, but it is always possible to disable this default behavior by using the appropriate optional argument. Full information on each command is found in the online help. mget mput mgetstr mputstr mtell mseek meof binary input binary output string input string output returns current access position for an open stream changes the access position for an open stream tests the end-of-ﬁle indicator Table 2.5. Binary input-output functions. In order to produce machine-independent binary input and output, the functions described in Table 2.5 always output data using the little endian format. It is possible to disable this default feature using the swap parameter. If swap is set to 0, then binary input and output is performed using the native format of the machine executing Scilab. This can be useful when binary ﬁles coming from other software are to be read into Scilab. A set of functions for input and output of sound ﬁles is provided in the directory SCI/macros/sound in the Scilab main directory. They can be used as programming examples for the functions listed in Table 2.5. We conclude this section with a small example dealing with the binary input and output of a constant matrix. −→ x=testmatrix(’magic’,4); −→ fd=mopen(’save.dat’,’wb’); −→ mput(length(’x’),’i’,fd) → mputstr(’x’,fd) ; − −→ mput(size(x,’r’),’i’,fd) −→ mput(size(x,’c’),’i’,fd) −→ mput(x,’d’,fd); −→ mclose(fd); −→ clear x; −→ fd=mopen(’save.dat’,’rb’); −→ ls=mget(1,’i’,fd); ← a 4x4 test matrix ← opens a ﬁle for writing in binary mode (’b’ is useful on windows) ← writes an integer. The length of the string ’x’ ← writes a string ’x’ ← writes an integer (number of rows) ← writes an integer (number of columns) ← writes an array of doubles ← closes the ﬁle output stream ← opens a ﬁle for reading in binary mode ← reads an integer 42 2 Introduction to Scilab −→ name=mgetstr(ls,fd); ← reads a string. The string length is given by ls −→ m=mget(1,’i’,fd);n=mget(1,’i’,fd); ← reads two integers m and n −→ data=mget(m*n,’d’,fd); ← reads m*n doubles in a vector −→ code= msprintf(’%s=matrix(data,%d,%d);’,name,m,n); −→ execstr(code); ← reshape the matrix and give it the correct name (i.e, x) −→ mclose(fd) ; ← close the ﬁle −→ and(x == testmatrix(’magic’,4)) ← test ans = T 2.3.4 Accessing the Host System As already noted, formatted and binary inputs and outputs use streams to access or create ﬁles. Streams are created and opened through the command mopen and closed using the mclose (or through the file command for Fortran-compatible streams). File streams are opened given a ﬁle pathname, which is coded as a Scilab string and can be absolute or relative. The preferred syntax for ﬁle names is the Posix-style syntax (which works on Windows operating systems as well), i.e., with a slash as directory separator. If necessary, the pathconvert function can be used for pathname translation. Note that a relative pathname is always considered relative to the Scilab current directory. The name of this current directory is returned by the function getcwd or pwd and can be changed using the chdir or cd function. Given a pathname, it is possible to access the information about a ﬁle using the fileinfo function. For example, fileinfo(’foo’)<>[] will evaluate to %t if ﬁle ’foo’ exists. Finally, the list of ﬁles in a given directory can be obtained as a vector string using the commands ls, dir, and listfiles. It is possible obtain information through global Scilab variables about the running Scilab program, in particular on the operating system it was built for and the embedded programs that were used at compilation. For example, the boolean variable %tk can be used to check wether the current Scilab version is built with interaction with Tcl/Tk (See also %gtk for Gtk support and %pvm for parallel virtual machine support). MSDOS is a boolean variable used to check wether the underlying operating system is Windows or a Unix-like system. Running a shell script from Scilab is done using unix and related commands unix_g, unix_s (equivalently host), unix_w, and unix_x. These functions diﬀer from each other in the way they return the result of the command: in the current Scilab window (unix and unix_w), in a graphics widget (unix_x), in a Scilab variable (unix_g), or without returned result (unix_s). Note that contrary to what their names suggest, these commands work on Unix, Mac, and Windows operating systems. It is sometimes useful to use the MSDOS variable before using the unix command since the syntax of shell commands diﬀers between Unix and Windows. This is illustrated in the following script: −→ %tk ans = ← checks wether we have Tcl/Tk support T −→ MSDOS ← is it a Windows version of Scilab ? 2.3 Input and Output Functions ans 43 = F −→ if ~MSDOS then −→ os=unix_g(’uname’); −→ else −→ os=’windows"; −→ end −→ os ans = ← Operating system name ? Linux −→ if MSDOS then −→ l=unix_g(’dir sc*.sci’); −→ else −→ l=unix_g(’ls sc*.sci’); −→ end −→ l l = !scalarbase.sci ! !scalarops.sci ! !scalar.sci ← list directory contents ← list directory contents ! ! ! ! ! 2.3.5 Graphical User Interface So far, we have described input-output interaction with Scilab through keyboard interaction in the Scilab graphics command window or through predeﬁned menus. It is also possible to interact with Scilab through dialog windows and to create speciﬁc dialog windows. We will discuss how to do this here. Scilab menus (in the command window and in the graphics windows) can be customized using Scilab commands. addmenu, delmenu, setmenu, or unsetmenu can be respectively used to add, delete, activate, or deactivate Scilab menus. One can associate a menu handler to a speciﬁed menu through a Scilab function. For example addmenu(’foo’) (resp. addmenu(’foo’,4)) will place a menu button foo in the Scilab command window (resp. graphics window 4), which when activated will call Scilab function foo (resp. foo_4). A set of predeﬁned popup dialog windows are also available in Scilab. Some examples are given in Figure 2.1. The simplest one is x_message, which is used to open a popup message window. Others are x_choose, which is used to perform a selection in a predeﬁned list; x_dialog, which opens a dialog to get a response from the user as a Scilab string; x_choices, which is used to select a set of items each one being chosen through a toggle list; x_mdialog, which is used for a set of x_dialog-like interactions or for acquiring a matrix; and xgetfile, which is a dialog used to get a ﬁle path. −→ rep=x_choose([’S.L.Campbell’,’J.P.Chancelier’,’R.Nikoukhah’],’Authors’); −→ x_message([’Well, it’’s time ’;’to get something to eat !’]); 44 2 Introduction to Scilab −→ chateaux=[’Ch^ ateau Pétrus’,’Ch^ ateau Margaux’,... → − ’Ch^ ateau Talbot’,’Ch^ ateau Belgrave’]; −→ l1=list(’Wine’,1,chateaux); −→ l2=list(’Cheese’,2,[’Rocquefort’,’Ossau’,’Tome de Savoie’]); −→ rep=x_choices(’Wine and Cheese menu’,list(l1,l2)); Figure 2.1. Examples of predeﬁned popup dialog windows. Mouse and key events in graphics windows are returned by xclick and xgetmouse, and an event handler can be set for a graphics window with the function seteventhandler. It is possible to build more sophisticated graphics user interfaces by mixing Scilab and Tcl/Tk code, which can interact through data exchange. This is made possible through a set of Scilab functions that interact with Tcl/Tk: • TCL_GetVar, TCL_SetVar are used to exchange numerical data or data coded through strings. • TCL_ExistVar is used to check for the existence of a Tcl/Tk variable. • TCL_EvalStr is used to send a Tcl expression contained in a Scilab string to the Tcl interpreter for evaluation. • TCL_EvalFile is used to send to the Tcl interpreter a ﬁle name of a Tcl script to be executed. • ScilabEval is a Tcl instruction that can be used in a Tcl script. The Tcl string argument given to ScilabEval is evaluated as a Scilab expression. We will give a small example of Tcl/Tk window creation with two range controls that can be used to change two scalar parameters a and b in a surface plot. The following script deﬁnes a function called plot3d_tcl. This function uses global variables to keep track of two parameters a and b. In plot3d_tcl, calls are made to TCL_GetVar to get the value of Tcl variables named a and b. Since TCL_GetVar returns a string, a call 2.3 Input and Output Functions 45 to evstr is made to evaluate the string. If a or b in the Scilab environment diﬀers from the Tcl/Tk values, then the Scilab values are updated and a surface is drawn. Graphics will be discussed in more detail in Section 2.4. Here we just note that plot3d will draw a surface and that the drawing of the surface depends on the two Scilab parameters a and b. Thus, if a or b is changed at the Tcl/Tk level, then a call to plot3d_tcl will draw a new surface. Note that a ﬂicker eﬀect will be seen in the graphics window when redrawing is performed. We will see in Section 2.4 how to avoid this. function []=plot3d_tcl() global(’a’,’b’); a_new=evstr(TCL_GetVar("a")); b_new=evstr(TCL_GetVar("b")); if a<>a_new | b<>b_new then a=a_new ; b = b_new ; t=linspace(0,2,200); xbasc();t=linspace(-b*%pi,b*%pi,20);plot3d(t,t,a*sin(t)’*cos(t)); end endfunction This function can be checked now with the following, which plots the surface: −→ TCL_SetVar(’a’,’3’); → TCL_SetVar(’b’,’2’); − −→ plot3d_tcl(); ← setting a to 3 in Tcl/Tk ← setting b to 3 in Tcl/Tk Now we want a dialog window with two range controls in order to change the values of a and b interactively. This can be done as described in the next Tcl/Tk script. # tcl/tk script toplevel .top wm resizable .top 0 0; set a 5; set b 2; #—————————wm title .top {tk test}; global ok wait but; proc runScilab {bidon} {ScilabEval plot3d tcl()}; #—– top label frame .top.widtop label .top.widlabel −text {Surface parameters} pack .top.widlabel −in .top.widtop −side top −pady 5 #—- a range control for a frame .top.wid frame2 label .top.widlabel a −text {a} scale .top.widscale a −variable a −width 8 −state normal −from 1 −to 10 \ −resolution .01 −orient horizontal −width 8 −command {runScilab} pack .top.widlabel a −in .top.wid frame2 −side left −anchor w −padx 5 \ −expand true pack .top.widscale a −in .top.wid frame2 −side left −anchor e −padx 5 #—– a range control for b 46 2 Introduction to Scilab frame .top.wid frame3 label .top.widlabel b −text {b} scale .top.widscale b −variable b −width 8 −state normal −from 1 −to 3 \ −resolution .01 −orient horizontal −width 8 −command {runScilab} pack .top.widlabel b −in .top.wid frame3 −side left −anchor w −padx 5 \ −expand true pack .top.widscale b −in .top.wid frame3 −side left −anchor e −padx 5 #——————pack .top.widtop −in .top pack .top.wid frame2 −in .top pack .top.wid frame3 −in .top #——————raise .top This is a pure Tcl/Tk script, wich is mostly independent of Scilab except for the fact that we call the ScilabEval function when a range control is changed. As already mentioned, ScilabEval is a Tcl function that sends its argument as a Scilab expression to the Scilab interpreter. Here, as expected, the ScilabEval function will send to the Scilab interpreter the string ’plot3d_tcl()’. Thus a range control modiﬁcation will lead to a call to the plot3d_tcl function. In order to launch the Tcl/Tk script we use TCL_EvalFile at the Scilab level. A picture of the Tcl/Tk dialog is provided in Figure 2.2 −→ TCL_EvalFile("range.tcl"); ← running the Tcl/Tk script from Scilab For a restricted set of Tcl/Tk dialogs predeﬁned in Scilab and called uicontrols, the process we have just described can be simpliﬁed in the sense that the Tcl/Tk script can be replaced by a Scilab script using the figure, uicontrol, and uimenu functions. The range controls used in the previous example are part of this restricted set of Tcl/Tk dialogs. We will now give the same example of a surface drawing but now managed through uicontrols. The function plot3d_uicontrol is the function that is activated when a range control (also called slider in uicontrol manual pages) is changed (this is called a callback), and the function myuidialog is used to build the Tcl/Tk window with the two sliders and the two text areas that display the sliders’ values. The two functions share data through global Scilab variables. A picture of the Tcl/Tk dialog composed with uicontrols is provided in Figure 2.3 function plot3d_uicontrol() global(’a_slider’,’a_text’,’b_slider’,’b_text’,’a’,’b’,’activate’,’n_fact’); if activate == %t then a=get(a_slider,’Value’)/n_fact; b=get(b_slider,’Value’)/n_fact; set(a_text,’String’,’a=’+string(a)) set(b_text,’String’,’b=’+string(b)) t=linspace(0,2,200); xbasc();t=linspace(-b*%pi,b*%pi,20);plot3d(t,t,a*sin(t)’*cos(t)); end endfunction 2.3 Input and Output Functions 47 Figure 2.2. Example of a Tcl/Tk dialog. function myuidialog() global(’a_slider’,’a_text’,’b_slider’,’b_text’,’a’,’b’,’activate’,’n_fact’); n_fact=100; ← scaling because sliders work with integers activate=%f ; ← delay callback execution f = figure("Position",[50 50 200 60],"Unit", "pixel"); a_text = uicontrol(f,"Position",[140 10 55 15],"Style","text",... "String" , "a=5") a_slider = uicontrol(f, "Position" , [10 10 120 15],.. "Style" , "slider",... "Min" , 1*n_fact,... "Max" , 10*n_fact,... "Value" , 5*n_fact,... "SliderStep", [1 1],... "callback" , "plot3d_uicontrol()") b_text = uicontrol(f, "Position",[140 35 55 15],"Style","text",... "String" , "b=2") b_slider = uicontrol(f, "Position" , [10 35 120 15],... "Style" , "slider",... "Min" , 1*n_fact,... "Max" , 3*n_fact,... "Value" , 2*n_fact,... "SliderStep", [1 1],... "callback" , "plot3d_uicontrol()") activate=%t; ← now we accept callback calls endfunction 48 2 Introduction to Scilab Figure 2.3. Examples of uicontrol Tcl/Tk dialog. 2.4 Scilab Graphics There are numerous Scilab graphics commands. A full discussion of them all would be a book by itself. Instead, we will ﬁrst introduce some of the more basic graphics commands that are regularly used in simulation and modeling. Once users can generate the basic graphs they need, they start to want to do more sophisticated graphics. For these we will just give some general rules for performing graphics in the subsequent sections and give an example that illustrates a part of what is possible. Details can be found in the on-line manual. 2.4.1 Basic Graphing One of the ﬁrst things that someone interested in modeling and simulation wants to do is to generate plots. These plots can be graphs of functions, surfaces, curves, or data. Readers familiar with other matrix-based environments will have no problem with Scilab graphics. But readers coming from working with equation-based languages such as MAPLE and Mathematica will have to think a little bit diﬀerently. Scilab oﬀers great control over how and where the graphics occur and how they look. But all this control can be confusing at ﬁrst. In this section we show how to quickly and simply generate some of the more common types of graphics. The remaining subsections will describe how to modify the basic graphical output. The plot2d command generates two-dimensional graphs. Given vectors x = [x1 , . . . , xn ] and y = [y1 , . . . , yn ], plot2d(x,y) will plot the points {(x1 , y1 ), . . . , (xn , yn )}. There are several options for con- necting the points, which can be speciﬁed using optional calling arguments. The default in plot2d is to connect them with a straight line. Some of the other most popular alternatives are already coded as plot2d2 , which assumes that the graph is a piecewise constant function, so it looks like a bar graph; plot2d3, which plots a vertical line up to each point; and plot2d4, which connects the plotted points with arrows. By default, plot2d creates a graphics window called Scilab Graphic (0). This window can be cleared by the command clf or xbasc. Additional calls to plot2d with no clearing commands or redirection of output will result in all plots being on the same graph (for Scilab version 3.0 or greater). Axes will automatically be adjusted. It is of course possible to have many graphics windows at the time in a Scilab session. A window, which is described by an integer id i, can be selected for drawing by using the command xset(’window’,i). If the window does not exist, it will be created. Moreover, a graphics window can be partitioned into subwindows using the command subplot. Later we will give some of the ways that the appearance of a plot can be altered with Scilab commands. But it is important to note that it is possible to make many of these changes using the Edit menu of the graphics window. Merely select Figure Properties. There is a menu bar at the top labeled Edit properties for:. This will give you menus 2.4 Scilab Graphics 49 for all the more important properties. The most commonly used ones are probably Axes and Polyline. There are a number of ways to control the ﬂow of Scilab output. Here we note that the Scilab graphics window has a ﬁle button. If clicked, it provides an easy way to export the graph into a number of formats including postscript. Note also that the graphics window has a zoom button, which allows you to select part of the graph and zoom in on it. The fact that the graphics routines start with matrices sometimes creates problems. For example −→ tt=0:0.1:3;plot2d(tt,sin(tt)); will generate the graph of sin(t) on the interval [0, 3]. However, −→ function y=myf(t);y=sin(t)*sin(t);endfunction; −→ tt=0:0.1:3;plot2d(tt,f(tt)) will produce an error message and not the graph of sin2 (t). The problem is that the function myf has to be able to take the vector tt and evaluate it entry by entry. The following code will generate the graph of sin2 (t): −→ function y=myf(t);y=sin(t).*sin(t);endfunction −→ tt=0:0.1:3;plot2d(tt,f(tt)) Note that the function is deﬁned using elementwise multiplication .* instead of *. A number of graphs created using the basic versions of plot2d and some of its features can be found in Chapter 3 Plots of a surface z = f (x, y) can be done in two ways. The scilab command plot3d works much like plot2d in that it has the syntax plot3d(x,y,z), where x, y, z are vectors. However, there is also the function fplot3d, which has the basic syntax fplot3d(x,y,f), where f is an external of the type y=f(x,y). Here x and y determine the grid on which f will be evaluated. The default for plot3d and fplot3d is for the graph to appear in a graphics window. Again the window can be exported into a postscript ﬁgure ﬁle by using one of the ﬁle options. Another useful feature is the 3D Rot. button of the graphics window. If this button is clicked, then one can click and hold on the ﬁgure, rotate using the mouse, and then click to release the ﬁgure. While the viewing point of the ﬁgure can also be adjusted by using optional calling arguments, it is usually easiest to ﬁnd the best viewing angle by using the 3D Rot. option. Surface plots can be altered using the Edit menu of the graphics window just as 2D plots are. 2.4.2 Graphic Tour We start with a number of scripts to make a tour of Scilab graphics functionalities. Two graphics styles coexist in Scilab today. But most high-level graphical functions are used in a similar fashion in both styles, so the examples provided in this ﬁrst part work with both, as did the examples of the previous section. We shall later focus on the diﬀerences of the two styles and, in particular, present the object-oriented aspects of the new style. Parametric Curve Plot in R3 The parametric plot of (sin(t), t cos(t)/R, t/100) on [−20π, 20π] using a uniform grid of 2000 points with R = 20π is created by 50 2 Introduction to Scilab −→ t=linspace(-20*%pi,20*%pi,2000); −→ param3d1(sin(t),t.*cos(t)/max(t),t/100) ← an R3 curve (Figure˜2.4) The graph appears in Figure 2.4. The division by max(t) has the eﬀect of scaling the second entry so that for any given interval it will have maximum value 1. Plot a Surface in R3 The surface plot of z = sinh(x) cos(y) on −π ≤ x ≤ π, −π ≤ y ≤ π is created by −→ x=linspace(-%pi,%pi,40); y=linspace(-%pi,%pi,40); −→ plot3d(x,y,sinh(x’)*cos(y)) ; ← an R3 surface (Figure˜2.5) Note the transpose used on the x. 15 10 Z 0.8 0.6 0.4 0.2 0.0 −0.2 −0.4 −0.6 −0.8 5 Z 0 −5 −1.0 −1.0 −0.8 −0.8 −0.6 −10 −0.6 −0.4 −0.4 −0.2 −0.2 0.0 0.0 0.2 0.2 0.4 Y 0.4 0.6 0.6 0.8 −15 −4 −3 −2 −1 0.8 1.0 0 X Y 1.0 Figure 2.4. Parametric plot. 1 2 3 4 4 3 2 1 0 −1 −2 −3 −4 X Figure 2.5. Surface plot in R3 . Plot a Vector Field in R2 The plot of the vector ﬁeld for the diﬀerential equation ẋ1 = x2 , ẋ2 = −x1 (1 − x21 )x2 , in the region −1 ≤ x ≤ 1, −1 ≤ y ≤ 1 is created by −→ function [xdot]=derpol(t,x); −→ xdot=[x(2);-x(1)+(1 - x(1)**2)*x(2)]; −→ endfunction −→ xf= linspace(-1,1,10);yf= linspace(-1,1,10); −→ fchamp(derpol,0,xf,yf); ← an R2 vector ﬁeld (Figure˜2.6) 2.4 Scilab Graphics 51 Plot a Histogram Generating a random vector with a normal distribution, plotting this vector as a histogram, and then plotting the normal distribution on the same graph is given by the following: −→ v=rand(1,2000,’n’); −→ histplot([-6:0.4:6],v,[1],’015’,’ ’,[-4,0,4,0.5],[2,2,2,1]); ← a histogram −→ function [y]=f2(x); y=exp(-x.*x/2)/sqrt(2*%pi); endfunction; −→ x=-6:0.1:6;x=x’;plot2d(x,f2(x),1); ← superimposing an R2 curve (Figure˜2.7) 1.0 0.8 0.6 0.4 0.2 0.0 −0.2 −0.4 −0.6 −0.8 −1.0 −1.0 0 −0.8 −0.6 −0.4 −0.2 0.0 0.2 0.4 0.6 0.8 1.0 Figure 2.6. A vector ﬁeld. −4 4 Figure 2.7. A histogram. Polar Plot in R2 The plot in polar coordinates of ρ = sin(2θ) cos(2θ) for 0 ≤ θ ≤ 2π is created by the following commands: −→ theta=0:.01:2*%pi;rho=sin(2*theta).*cos(2*theta); −→ polarplot(theta,rho); ← an R2 curve in polar coordinates (Figure˜2.8) Plot a Surface Given by Facets in R3 If a surface in R3 is parameterized by two variables, then by assigning a grid to these two variables we can create a surface composed of facets. This is illustrated below. −→ function [x,y,z]=f3(alpha,theta) −→ x=cos(alpha).*cos(theta); −→ y=cos(alpha).*sin(theta); −→ z=sinh(alpha); −→ endfunction −→ alphagrid=linspace(-%pi/2,%pi/2,40);thetagrid=linspace(0,2*%pi,20); −→ [x1,y1,z1]=eval3dp(f3, alphagrid, thetagrid); ← building a set of facets −→ plot3d1(x1,y1,z1); ← an R3 surface composed by a set of facets (Figure˜2.9) 52 2 Introduction to Scilab 0.5 120 60 0.4 0.3 150 2.5 30 2.0 0.2 1.5 1.0 0.1 0.5 180 0 Z 0.0 −0.5 −1.0 −1.5 −2.0 210 330 −2.5 −1.0 240 −0.8 −0.6 −0.4 −0.2 300 Y Figure 2.8. A polar plot. 0.0 0.2 0.4 0.6 0.8 1.0 1.0 0.8 0.6 0.4 0.2 0.0 −0.2 −0.4 −0.6 −0.8 −1.0 X Figure 2.9. A surface described by facets. In the following examples, graphics objects are used explicitly. Thus these examples work only in the new graphics mode. Note, however, that the graphics functions xrect and xpoly can also be used in the old graphics mode. Basic Graphics Functions: xrects In the next example, each column of rect speciﬁes a rectangle in terms of the coordinates of its upper left corner, width, and height. Then xrects plots these rectangles with a color determined by its second input variable. −→ xbasc() −→ a=get("current_axes"); ← gets a handle on the newly created axes −→ a.title.font_size = 4; ← changes the title font size −→ a.data_bounds=[0,0;1,1]; ← sets the boundary values for x and z −→ xset(’clipgrf’); ← setting a clip zone in the graphic frame −→ n=20; −→ rects=[rand(1,n);rand(1,n);0.2*ones(1,n);0.2*ones(1,n)]; −→ xrects(rects,rand(1,n)*20); ← a set of rectangles −→ xset(’clipgrf’); ← deactivates the clip zone −→ xtitle(’A set of rectangles’); ← (Figure˜2.10) Graphics handles and their properties will be discussed in the next section. Basic Graphics Functions: xpolys xpolys can be used to draw a set of line segments. The next example generates the endpoints of several line segments and then calls xpolys to draw the line segments. −→ a=get("current_axes"); ← gets a handle on the newly created axes −→ a.axes_visible="off"; ← makes the axes invisible −→ a.data_bounds=[-2*cos(%pi/6),0;2*cos(%pi/6),3]; ← sets the boundary values for x and z −→ a.box="off"; −→ theta=[-%pi/6,%pi/2,%pi*7/6,-%pi/6]; −→ T=[cos(theta);sin(theta)]; −→ Ts=(1/(1+2*sin(%pi/6)))*rotate(T,%pi); 2.4 Scilab Graphics 53 −→ x=[T(1,:);Ts(1,:)]’; y=[T(2,:);Ts(2,:)]’; −→ x=[x+cos(%pi/6),x-cos(%pi/6),x]; −→ y=[y+sin(%pi/6),y+sin(%pi/6),y+1+2*sin(%pi/6)]; −→ a.thickness=8; ← sets line thickness −→ xpolys(x,y); ← draws a set of polylines (Figure˜2.11) A set of rectangles Figure 2.10. Drawing rectangles. Figure 2.11. Drawing polygons. More examples with animations are provided in Section 5.1.2 and Appendix C. 2.4.3 Graphics Objects In Scilab version 3.1, the new graphics mode is the default graphics mode. The old graphics mode is still available, and in a Scilab session the user can simultaneously have graphical windows in both old and new modes. The two modes share the high-level graphics functions deﬁned in Table 2.6 but diﬀer in their philosophy. The new graphics mode provides functions for accessing properties of graphics objects or changing the graphics object hierarchy. In the new graphics mode, it is not the graphics function calls that are recorded, but rather the state of the graphics is recorded. The state of the graphics can be viewed as an instance of an object called a Figure. A Figure instance can be built by calling graphics functions. It contains in its ﬁelds the graphical hierarchy of objects that compose the displayed ﬁgure. Thus a hierarchy of objects, which are called entities, compose a graphic Figure instance. Each entity has its own ﬁelds, which can also be entity instances. For example, the top-level object is always a Figure that contains a list of Axes (graphics subwindows). An Axes entity contains in its ﬁelds basic graphics objects like Polylines, Arc, . . . . A set of entities can be grouped using an Aggregation entity. The main purpose of this new graphics mode is to give the user the possibility to access each entity in the object’s hierarchy that composes a displayed graphic. By accessing we mean that the user can set or get object properties, for example the color of a displayed curve, using Scilab functions set or get. The displayed graphic is automatically updated to reﬂect the property changes. As noted earlier, the properties of graphics objects can be changed through the Edit menu of graphics windows. This is particularly useful in taking a Scilab-generated ﬁgure and preparing it for use in presentations or publications. Properties such as color and style 54 2 Introduction to Scilab of lines and graphs are found under the properties of Polyline. A slide will adjust the color. The axis style and labeling are under the properties of Axes. Thus, from the user point of view, a Figure can be viewed as an instance of a dynamic tree object. The nodes of the tree are called entities. The user can change the graph by adding or deleting nodes and changing the node properties. The Scilab name of a node pointer is a handle. The two graphics modes can coexist in a Scilab session, but a default mode is set by the Scilab startup: set("old_style","off"|"on") can be used to set the default mode, and set("figure_style","new"|"old") can be used to open a graphical window in the speciﬁed mode. We will now give some examples showing some of the graphics entities in action. In this ﬁrst example a graphics window is created, a default scale is set by calling plot2d, and a rectangle is drawn. In order to change the rectangle’s properties, we ﬁrst need to get a handle on it. Since the rectangle is the last drawn object, it is the current entity of the current graphics window, and in that case r=get("hdl") is the easiest way to access the rectangle. It is then possible to change the displayed rectangle’s properties. −→ a=get("current_axes"); −→ a.data_bounds=[-1,-1;1,1]; −→ xrect(0,0,0.5,0.5) −→ r=get("hdl"); −→ r ans = ← get a handle on the newly created axes ← set the boundary values for x and z ← draw a rectangle ← get the current node, i.e, the last created objects ← r is a handle to a Rectangle entity Handle of type "Rectangle" with properties: =========================================== parent: Axes children: [] mark_mode = "off" mark_style = 0 mark_size_unit = "tabulated" mark_size = 0 mark_foreground = -1 mark_background = -2 line_mode = "on" line_style = 0 thickness = 1 fill_mode = "off" foreground = -1 data = [0,0,0.5,0.5] visible = "on" clip_state = "off" clip_box = [] −→ r.fill_mode="on"; −→ r.foreground=5; −→ r_line_style=6; ← changing the displayed rectangle’s properties In order to illustrate entities navigation, we can accomplish the same thing as the previous example in a more complex way by navigating, from node to node, in the graph of the objects associated with the ﬁgure. 2.4 Scilab Graphics −→ h=get("current_figure"); −→ ch = h.children; −→ ch == get(’current_axes’) ans = 55 ← handle to the default graphic window ← the Axes entities ← the same ? T −→ r1= ch.children; −→ r1== r ans = ← graphic entities ← we have found the rectangle again T Since get("current_figure") and get(’current_axes’) are frequently used, they can be abbreviated as gcf() and gca(). One can also use gdf() and gda() to get a default ﬁgure and default axes, which are undrawn objects that are used as placeholders to store global default values used in creating respectively a new ﬁgure or new axes. −→ q=gda(); −→ q.thickness=2; −→ q.grid=[4,4]; −→ q.font_size=4; −→ x=(0:0.1:2*%pi)’; −→ plot2d(x,sin(x),rect=[0,-2,2*%pi,2]); −→ xdel(); −→ sda(); −→ plot2d(x,sin(x),rect=[0,-2,2*%pi,2]); ← get the default axes ← default line thickness to 2 ← I want a grid on my plot ← increase default font size ← reset the default axes to default values ← just check it We have said that through properties one can change the graphical aspect of a displayed ﬁgure. But most of the time we want to set properties for undisplayed objects and only when all the properties are set to our requested values do we want to see the ﬁgure. This is easy to do since all the graphics entities have a visible property, which can be on or off. Moreover, we can start a graphics function call sequence by drawlater and end it with drawnow to control the visibility process. This is illustrated by the following script, which produces Figure 2.12. u = %pi*(-1:0.2:1)/2; v = %pi/2*(-1:0.2:1); n = size(u,’*’); x= cos(u)’*exp(cos(v)); ← a surface parameterized by u,v y= cos(u)’*sin(v); z= sin(u)’*ones(v); col=ones(u)’*cos(v); ← a color for each vertex col=(n-1)*(col-min(col))/(max(col)-min(col))+1; ← rescale colors drawlater(); ← draw later ! [xx,yy,zz]=nf3d(x,y,z); ← from (x,y,z) to four sided faces [xx,yy,zzcol]=nf3d(x,y,col); ← change the colors as well xx=[xx,-xx];yy=[yy,-yy];zz=[zz,zz];zzcol=[zzcol,zzcol]; ← symmetry a=gca(); ← handle to the current axes plot3d(xx,yy,list(zz,-zzcol)); ← Let’s do it f=gcf(); ← handle to the ﬁgure f3d=a.children; ← just one child, it’s our 3d plot 56 2 Introduction to Scilab ← set the hidden face’s colors ← set the view angles ← set the colormap ← show the result in a graphics window f3d.hiddencolor=n; a.rotation_angles = [55,110]; f.color_map= graycolormap(n); drawnow(); Z 1.0 0.8 0.6 0.4 0.2 0.0 −0.2 −0.4 −0.6 −0.8 −1.0 −1.0 −0.8 −0.6 −0.4 −0.2 0.0 0.2 0.4 0.6 Y 0.8 1.0 3 2 1 0 X −1 −2 −3 Figure 2.12. plot3d with the new graphic. For those who prefer a graphical interface, keep in mind that an Edit button is available in the graphics window menu, which when launched opens up Tcl/Tk menus that can be used to navigate and change graphical object properties. Tables 2.6, 2.7, and 2.8 summarize the Scilab graphics functions, graphics primitives, and graphics window primitives respectively. 2.4.4 Scilab Graphics and LATEX This section is for those interested in inserting Scilab graphics into LATEX documents [23] (the typesetting system used to produce this book). Inserting a Scilab graphic in a LATEX document is an easy task since Scilab can export graphics as Postscript ﬁles and there are commands in LATEX to include Postscript ﬁles. Exporting a scilab graphic to Postscript can be, as already noted, achieved through the File menu of the graphics window, but it is also possible to use the Scilab command xs2eps (or xs2ps and the program BEpsf) to generate a Postscript ﬁle. However, sometimes one wants to have a ﬁgure that is a mixture of Scilab graphics images and LATEX mathematical expressions. This can require translating the Postscript strings inserted in a Postscript document to LATEX commands in order, for example, to get mathematical expressions inserted in a graphic. There exists a LATEX package named psfrag that facilitates this task. Using psfrag you can substitute strings present in the Postscript ﬁle by LATEX expressions. The idea is to position strings in Scilab graphics 2.4 Scilab Graphics champ,champ1,fchamp contour,fcontour,contour2d, contourf, fcontour2d eval3d,eval3dp,genfac3d fec geom3d grayplot,fgrayplot hist3d, histplot isoview legends Matplot,Matplot1 param3d,param3d1 paramfplot2d plot, plot2d[1-4],fplot2d plot3d,plot3d1,fplot3d, fplot3d1,plot3d2,plot3d3 polarplot Sfgrayplot, Sgrayplot subplot 57 2D vector ﬁelds 2D Level curves 3D utility functions for facets computation 2D plot of a linear function on triangles from 3D coordinates to 2D projected ones 2D level curves as ﬁlled rectangles histograms select isometric scales 2D legends 2D plot of matrices 3D parametric curves animation of 2D parametric curves 2D curves surface drawing 2D polar curves 2D level curves (smoothed with fec) split graphics window into subwindows Table 2.6. Graphics functions. xarc,xarcs xarrows xaxis xclea xclip xfarc,xfarcs xfpoly,xfpolys xfrect xgrid xnumb xpoly,xpolys xrect xrects xrpoly xsegs xstring,xstringb xstringl xtitle draw ellipses and circle draw a set of arrows axis drawing clear a rectangular zone ﬁx a clip zone paint ellipses and circle paint polylines paint a rectangle add a grid on 2D graphics draw numbers draw a polyline draw a rectangle draw or paint a set of rectangles draw a regular polygon draw a set of segments draw a string return the bounding box of a string add title to a graphic Table 2.7. Graphics primitives. using ASCII tags that can be substituted later by LATEX expressions. For example, in the following code the string exprlatex can be replaced by the LATEX expression 2 e−x /2 y= √ 2π using the LATEX code \psfrag{exprlatex}{$y=\frac{e^{-x^2/2}}{\sqrt{2\pi}}$} This is done by generating the ﬁgure in Scilab by 58 2 Introduction to Scilab driver graycolormap, hotcolormap, . . . xbasc xbasimp xbasr xclear xclick,xgetmouse xdel xend xget xgetech xgraduate,graduate xinfo xinit xlfont xload xname xpause xs2fig,xs2gif, xs2ppm,xs2ps xsave xselect xset xsetech xtape winsid select graphics driver predeﬁned colormaps clears graphics window and recorded graphics sends Postscript graphics to printer redraws a graphics window clears graphics window wait for mouse click delete a graphics window stop a driver session get graphics context values get current scale utility for graduation print a string in graphics window information widget initialize a graphics driver add a font to the current driver saved graphics reloader (saved with xsave) give a name to current graphics window pause save a graphics to ﬁle in Xfig, gif, ppm, or Postscript format save graphics in a machine-independent format (use xload to reload) raise a graphics window set graphics context values ﬁx the current scale recorded graphics management return the graphics window’s numbers Table 2.8. Graphics window primitives. −→ v=rand(1,2000,’n’); −→ histplot([-6:0.4:6],v,[1],’015’,’ ’,[-4,0,4,0.5],[2,2,2,1]); ← histogram −→ function [y]=f2(x); y=exp(-x.*x/2)/sqrt(2*%pi); endfunction; −→ x=-6:0.1:6;x=x’;plot2d(x,f2(x),1); ← superposing an R2 curve −→ xstring(2.3,0.2,’exprlatex’); ← adding a string Then after the Scilab ﬁgure is exported, in this case with the name figpsfrag.eps, the following LATEX commands generate Figure 2.13 in this book: \begin{figure} \psfrag{exprlatex}{$y=\frac{e^{-x^2/2}}{\sqrt{2\pi}}$} \begin{center} \includegraphics[width=8cm]{code-book/figpsfrag} \end{center} \caption{Added \LaTeX\, expression with \tt{psfrag}.} \end{figure} There is another solution for Scilab Postscript generated ﬁles since they contain, in the Postscript code, LATEX information about the inserted strings (their positions and contents). It is easy to write a Scilab function to extract this information in order to use it at the LATEX level. One such function is xs2latex, provided in Appendix D. Running 2.4 Scilab Graphics 59 2 y= −4 /2 e−x √ 2π 4 Figure 2.13. Added LATEX expression with psfrag. this function, for example with the command xs2latex(0,’figps.ps’), produces two ﬁles figps-ps.eps and figps-tex.tex, which can be inserted in a LATEX document as follows: \begin{figure}[hbtp] \begin{center} \begin{picture}(0,0) \includegraphics[scale=1.0]{code-book/figps-ps.eps} \end{picture} \input{code-book/figps-tex.tex} \end{center} \caption{\label{figpslatex}Figure using \verb+xs2latex+.} \end{figure} Note that in order to change the default scale, it is not enough just to change scale in the code. The scale factor also has to be changed in figps-tex.tex by changing the unitlength. Using the xs2latex macro, we can produce a ﬁgure with embedded LATEX strings and mathematical expressions as shown in Figure 2.14. The Scilab code used to produce Figure 2.14 is given below. This code also presents new graphics primitives in new graphics style: −→ t=-%pi:0.05:%pi; −→ a=gca(); −→ a.font_size=5; −→ plot2d(t,sin(5*t).*exp(-t/2)) −→ hl=legend([’$\sin(5 t)\exp(-t/2)$’],4); −→ a.title.font_size=5; −→ a.x_label.font_size = 5; −→ a.y_label.font_size = 5; −→ a.auto_ticks=’off’; −→ a.box="off"; −→ a.x_location="middle"; −→ a.y_location="middle"; −→ xt=[-%pi,0,%pi]; 60 2 Introduction to Scilab y −π 0 π x sin(5t) exp(−t/2) Figure 2.14. Figure obtained with xs2latex. −→ a.x_ticks= tlist([’ticks’,’locations’,’labels’],xt,... ["$-\pi$","$0$","$\pi$"]); −→ xtitle("","$x$","$y$"); −→ xs2latex(xget("window"),"figps"); 2.4.5 Old Graphics Style As pointed out earlier, the old graphics style is considered dated and will not be maintained in the future. However, many examples and contributions to Scilab (the most important one being Scicos) still use the old graphics style, so we will give a brief presentation of the old graphics style here. In the old graphics style, the standard graphics output can be a graphics window, a Postscript ﬁle, or an Xﬁg ﬁle depending on the selected graphics driver. The current graphics driver is obtained with the driver() function. A driver is selected using the same function with the desired driver given as a string. For example, driver(’Pos’) selects Postscript. Alternatively, one can use the function xinit or use the Scilab graphic export menu that appears under the ﬁle button on the Scilab Graphics window. When the selected driver is the one that redirects output to graphic windows, all Scilab graphics instructions are recorded sequentially. When a graphics window is resized or when a zoom is executed, Scilab replays the recorded instructions and produces a new graphic adapted to the size changes. In the same way, graphics instructions can be replayed after a driver change. For example, they can be used to obtain a Postscript version of a displayed graphics window. This can de done explicitly using the function xs2ps. The default driver associated with a graphics window (the Rec driver) enables the recording of graphics instructions. Recorded instructions can be replayed using the function xtape or the function xbasr. Function xbasc can be used to clear the current window and 2.4 Scilab Graphics 61 to clear the associated recorded graphics instructions. Destroying a graphics window by clicking or by calling xdel also clears the associated recorded graphics. It can sometimes be useful to use a graphics window without graphic recording, for example during animations. This is easily done by switching to the driver X (or W). The default graphics behavior is that graphics display appears as soon as a graphics function is called. It is possible to attach a pixmap to a graphics window in order to redirect graphics outputs to that pixmap. The graphics window is redrawn using the pixmap status by a user speciﬁc command. The pixmap mode of a graphics window is a window graphics parameter, and each graphics window has a large set of parameters. They deﬁne the graphics context of a graphics window. Many graphics windows can coexist, but only the graphics context of the current graphics window can be changed. The ID of the current graphics window (which is an integer) can be obtained using xget(’window’), and changing the graphics window is done with xset(’window’,n). The graphic context parameters of the current graphics window are obtained with the function xget and changed using the function xset. The pixmap mode can be very useful, in particular for animation, so let us consider it more carefully. This mode is selected by calling xset(’pixmap’,1). As explained earlier, when this mode is selected, graphics are directed to a pixmap. The content of the pixmap is copied to the graphics window when the xset(’wshow’) instruction is executed. The pixmap used for graphics is cleared by calling wset(’wwpc’). This mode can be used for graphics animations since it avoids the ﬂicker caused by having to clear a graphics window before redrawing it. The following script generates a movie of a rectangle that moves and changes its color pattern. set old_style on xsetech(frect=[0,0,10,10]) xrect(0,10,10,10) n=100; xset(’pixmap’,1) driver(’X11’); for k=-n:n, a=ones(n,n); a= 3*tril(a,k)+ 2*a; a= a + a’; k1= 3*(k+100)/200; Matplot1(a,[k1,2,k1+7,9]) xset(’wshow’) xset(’wwpc’) end xset(’pixmap’,0) The ﬁnal point concerns scale computations. Graphics functions that aim at drawing simple objects, for example rectangles xrects, or polylines xpoly, use the current graphics scale. It is good scilab programming to set a graphics scale before calling such functions even if no error messages are provided by Scilab if the scales are not set. A current scale is set each time a high-level graphics function is called, such as plot2d, or when an explicit call to the function xsetech is used. The default behavior of high-level functions is to adapt the current graphics scale so as to remain compatible with previous graphics function calls. But optional parameters can be used to change this default behavior. This is illustrated by the next example, which generates the graphs in Figure 2.15 and Figure 2.16. 62 2 Introduction to Scilab −→ xbasc(); −→ t=linspace(0,2*%pi); −→ plot2d(t,sin(t)) −→ plot2d(t,sinh(t/%pi)) ← the current scale changes so as to contain the two graphs (Figure˜2.15) −→ [a,b,c,d]=xgetech(); −→ b ans = ! 0. - 1. 7. ← get the current scale [xmin,ymin,xmax,ymax] 4. ! −→ xbasc(); −→ plot2d(t,sin(t)) −→ plot2d(t,sinh(t/%pi),strf="000") −→ [a,b,c,d]=xgetech(); −→ b ans = ! 0. - 1. 7. ← we don’t want to change the current scale (Figure˜2.16) ← get the current scale [xmin,ymin,xmax,ymax] 1. ! 4.0 1.0 3.5 0.8 3.0 0.6 2.5 0.4 2.0 0.2 1.5 0.0 1.0 −0.2 0.5 −0.4 0.0 −0.6 −0.5 −0.8 −1.0 0 1 2 3 4 5 6 Figure 2.15. Automatic rescale. 7 −1.0 0 1 2 3 4 5 6 7 Figure 2.16. Using current scale. 2.5 Interfacing In many areas, especially in modeling and simulation, there are computer programs written in other languages that we may want to interface with Scilab. Depending on the application area and the problem, these other programs could be written in C, C++, FORTRAN, etc. In principle, this interfacing could go either way. For example, we might want a FORTRAN program to use Scilab or for Scilab to use a FORTRAN program. We shall assume that the goal is for Scilab to be able to use another program. This section will discuss how this interfacing is done. There are two somewhat diﬀerent scenarios in which such a program is to be used. 2.5 Interfacing 63 The ﬁrst scenario is that the outside program is being called from within a Scilab function. For example, the outside program might return the value of the right-hand side of the diﬀerential equation that we wish to integrate with Scilab’s ordinary diﬀerential solver ode. In that case, the interface is somewhat simpler and is described in Subsection 2.5.1. In the second scenario we wish to have the given program become a Scilab function that we want to be able to call and use just like any other built-in Scilab function. This is discussed in Sections 2.5.2 and 2.5.3 it will often require some knowledge of the C programming language and the language used by the given external program. 2.5.1 Linking Code Writing an interface is not always necessary in order to access external code. Suppose, for example, that you want to create a new Scicos block whose behavior is described by C-code. You do not need to access your block from the Scilab interpreter since the call will be performed directly by Scicos’s internal simulator. In this case what you need is just to link your external code and update the Scilab internal table of dynamically linked functions. Then Scicos will be able to access the new code. In fact, most of the Scilab functions that accept an external as argument can use the dynamic linking facility to access externally coded functions. We note ode, dassl, optim, and fsolve. Of course, when using this facility the calling sequence of the external function is constrained by the function it will be passed to. Each external-aware function has its own imposed calling sequence. There’s also a special Scilab primitive called call, which can be used to call from the Scilab interpreter a dynamically linked routine that has in its arguments just integer, double, or character pointers. It can be used for testing external code. Just as in the previous section, we still have to create a shared library and load it in Scilab. Linking external code can be done in two diﬀerent ways. The function addinter is used to add new primitives to Scilab and requires the writing of interfaces. The function link just adds a new entry in the internal Scilab table of external functions. As pointed out earlier, such entries can be used by some Scilab primitives. Here we will use the link command through the ilib_for_link encapsulation. We encapsulate the call to dlaswp in another function to simplify the number of requested arguments: #include <string.h> #include "stack-c.h" extern int C2F(dlaswp)(int *n,double *A,int *lda,int *k1,int *k2,int *ipiv,int *step); int dlaswp1(double *A,int *m,int *n,int *mipiv,int *ipiv) { const int un =1; C2F(dlaswp)(n,A,m,&un,mipiv,ipiv,&un); } We can compile the code using a small Scilab script called builder.sce (a more precise description of builders is given in Section2.5.3) : −→ names=[’dlaswp1’]; // entry points 64 2 Introduction to Scilab −→ −→ files = ’dlaswp1.o’; // object files −→ flag = ’c’ ; −→ ilib_for_link(names,files,[],"c"); generate a loader file generate a Makefile: Makelib running the makefile compilation of dlaswp1 building shared library (be patient) In the previous code flag=’c’ is just there to specify that the function dlaswp is a C-coded function. This is used to guess the proper naming convention of entry points. We load the code in a running Scilab with the generated loader.sce ﬁle: −→ // generated by builder.sce: Please do not edit this file −→ // -----------------------------------------------------−→ dlaswp1_path=get_absolute_file_path(’loader.sce’); −→ link(dlaswp1_path+’libdlaswp1.so’,[’dlaswp1’],’c’); shared archive loaded Link done Now we can call the newly linked function not directly, but, for example, through the call function. The call function plays the role of the interface for a small subset of argument types. For example, we can call dlaswp as follows: shared archive loaded Link done −→ perm=[1,3,4,3];mp=size(perm,’*’); −→ A=eye(4,4);[m,n]=size(A); −→ y=call("dlaswp1",A,1,"d",m,2,"i",n,3,"i",mp,4,"i",perm,5,"i","out",1) y = ! ! ! ! 1. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 1. ! ! ! ! The ﬁrst call argument is the name of the function to be called. It is the name that was given to the link command as the entry point name. Other arguments from A to "out" give information on the arguments that are to be passed to dlaswp1. Then the arguments that follow the keyword "out" give information about returned values. Each transmitted argument is described by three entries. For example, A,1,"d" means that the ﬁrst argument of dlaswp1 is an array of double (precision) that points to the data of matrix A. The 1 that follows the "out" keyword means that the ﬁrst returned value is also the ﬁrst argument. To provide a more explicit example, suppose that you want to ﬁnd a zero of the nonlinear equation cos(x)x2 − 1 = 0. You can build a Scilab function: 2.5 Interfacing 65 −→ function y=f(x) −→ y=cos(x)*x^2-1 −→ endfunction and using fsolve and the function f as its argument try to ﬁnd a zero numerically: −→ y0=10; y=fsolve(y0,f) y = 11.003833 −→ f(y) ans = 3.819D-14 However, we shall use this problem to illustrate linking. Suppose that the function f is C-coded (for example for eﬃciency) and it respects the calling convention of the Scilab fsolve primitive. One such implementation of f would be the following C-code: #include <math.h> void f(int *n,double *x,double *fval,int *iﬂag) { *fval = cos(*x)*(*x)*(*x) − 1; } As before, we build and load a shared library using ilib_for_link. Then we pass the problem to solve to function fsolve by just giving a string that gives the name of the entry point to use, which in this problem is "f". The result is: −→ ilib_for_link(’f’,’f.o’,[],’c’); generate a loader file generate a Makefile: Makelib running the makefile compilation of f building shared library (be patient) shared archive loaded Link done −→ y0=10; y=fsolve(y0,"f") y = 11.003833 Many other Scilab functions can use C- and Fortran-coded functions directly, including ode, optim, etc. 66 2 Introduction to Scilab 2.5.2 Writing an Interface The main purpose of this section is to show how to integrate C- compatible functions into Scilab. Suppose that we want to add new primitives to Scilab that will call external Clike functions. By C-like functions, we mean functions that can be coded in any language that remains compatible with C code in terms of linking and calling stack conventions. Examples include Fortran code and C++ (“extern C”) functions. We start with two examples that are Fortran-coded functions from the LAPACK library. [4] The ﬁrst function is dgetrf, which performs an LU factorization of a given matrix. Given a m×n matrix A coded as an array of double-precision scalars of size m×n and an integer array of size Max(m,n), denoted by ipiv, the dgetrf routine performs an in-place LU factorization of a matrix. In fact, the LU factorization can be done with row interchanges. The integer array ipiv is here just to retain a coding of the row permutation. A Fortran subroutine can be called from C code with the convention that all arguments are transmitted through pointers. Note also that the naming conventions diﬀer slightly from C to Fortran and are compiler-dependent. We enclose the Fortran name in a C2F(.) macro call, which will take care of naming conventions. Thus, the calling sequence for dgetrf from C-code is as follows: extern int C2F(dgetrf)(int *m,int *n,double *A,int *lda,int *ipiv,int *info); The second function we want to use is dlaswp. This routine simply applies a permutation described by an ipiv integer array to a given matrix: extern int C2F(dlaswp)(int *n,double *A,int *lda,int *k1,int *k2,int *ipiv,int *step); In order to access these two functions we add two Scilab primitives also called dgetrf and dlaswp with the following calling syntax: [LU,perm]=dgetrf(A); [C]=dlaswp(B,perm); We still need to write glue code for converting Scilab arguments to C form, calling the external functions, and back-converting C objects to Scilab returned values. Most of the time the glue code is written in C and is called an interface, so that an interface is just a C-function. We need an interface for each function to be included into Scilab. The two interfaces for dgetrf and dlaswp are as follows: #include <string.h> #include "stack-c.h" extern int C2F(dgetrf)(int *m,int *n,double *A,int *lda,int *ipiv,int *info); int int dgetrf(char *fname) { int l1, m1, n1, mipiv,nipiv=1,lipiv,info; CheckRhs(1,1); CheckLhs(1,2); GetRhsVar(1, "d", &m1, &n1, &l1); mipiv= Max(m1,n1); CreateVar(2, "i", &mipiv,&nipiv, &lipiv); C2F(dgetrf)(&m1,&n1,stk(l1),&m1,istk(lipiv),&info); if ( info < 0 ) 2.5 Interfacing 67 { Scierror(999,"%s: argument %d of dgetrf has an illegal value\r\n", fname,−info); return 0; } } else if ( info > 0 ) { sciprint("%s: U(%d,%d) is exactly zero\r\n",info+1,info+1); } LhsVar(1) = 1; LhsVar(2) = 2; return 2; #include <string.h> #include "stack-c.h" extern int C2F(dlaswp)(int *n,double *A,int *lda,int *k1,int *k2,int *ipiv,int *step); int int dlaswp(char *fname) { int l1, m1, n1, mipiv,nipiv,lipiv,info,un=1; CheckRhs(2,2); CheckLhs(1,2); GetRhsVar(1, "d", &m1, &n1, &l1); GetRhsVar(2, "i", &mipiv,&nipiv, &lipiv); mipiv= mipiv*nipiv; C2F(dlaswp)(&n1,stk(l1),&m1,&un,&mipiv,istk(lipiv),&un); LhsVar(1) = 1; return 1; } Note that in the two previous interfaces we have used functions, macros, or data from the interface library (CheckRhs, CheckLhs, GetRhsVar, CreateVar, and LhsVar). The header ﬁle stack-c.h is there to provide macro deﬁnitions and prototypes for the interface library functions. At ﬁrst sight, these two functions might seem complex, but most of the time we do not have to write the full interface from scratch. Picking pieces of code from the examples provided is the easiest way to write new functions. We won’t describe in full detail all the functions that are present in the interface library. Rather we will try to give the rules that are to be followed for writing an interface. How does the interface work? When at the Scilab level the user enters the command [LU,perm]=dgetrf(rand(5,5)), the arguments of the dgetrf function are ﬁrst evaluated and their resulting values are stored in a stack (the function calling stack) in the order they appear in the calling sequence. Here we just have one argument; rand(5,5) is evaluated and the resulting matrix is stored on the calling stack at position 1. The Scilab calling sequence is also analyzed by the interpreter in order to evaluate how many returned arguments are expected. Here two returned arguments are expected. The glue code for dgetrf, here called int_dgetrf, is then called by the interpreted code. The ﬁrst job to be performed by the interface is to check that the number of given arguments is correct and that the number of expected returned arguments is also correct. This 68 2 Introduction to Scilab is performed by CheckLhs and CheckRhs . For example, if the number of given arguments is outside of the given bounds, then CheckRhs will print an error message and we return from the interface to the Scilab interpreter with a Scilab error raised. In the interface each variable present on the stack is characterized by an integer that is its position on the stack. Calling arguments are, when entering the interface, stored on the stack from position 1 to n if n is the number of given arguments. The next job to be performed is to check that each given argument has the expected type and proper dimensions. If the type is correct, then we need to give a pointer to the associated argument data. This pointer will be given to the C-function we are interfacing. The next command, GetRhsVar(1, "d", &m1, &n1, &l1); checks that the ﬁrst given argument is a scalar matrix ("d") and, in case of success, returns in m1 and n1 the matrix dimensions and in l1 an identiﬁer that can be used to get a pointer to the double array data (stk(l1)). Before calling the function dgetrf, we need to allocate space in the Scilab calling stack for the integer array ipiv, which is to be of dimensions 1\timesMax(m1,n1). The C-code mipiv= Max(m1,n1); CreateVar(2, "i", &mipiv,&nipiv, &lipiv); will create a scalar matrix with the proper size. The "i" is here to say that the array is to be considered as an integer array. Data access is done through an integer pointer istk(lipiv). Since the ﬁrst available position is position 2, we use this position for our newly created variable. The calling syntax of CreateVar is the same as the calling syntax of GetRhsVar, but note that in creating a variable, the dimension parameters are used as input data. We are now ready to call the function dgetrf. After the call, the Scierror function can be used to raise a Scilab error and the sciprint function can be used to display warning messages or other relevant information. After the call, we want to return the result of the LU factorization, which is stored in the double array stk(l1), and the coded permutation, which is stored in istk(lipiv). Notice that in dgetrf the double array stk(l1) is used as both input and output variable. However, there is no risk in modifying a Scilab input variable since when we are in the interface GetRhsVar, we do not have direct access to a given argument but only to a copy of that argument. It is thus safe to modify the m1xn1 values of the double array stk(l1) and to use this variable as a Scilab return value. We have checked at the beginning of the interface the number of requested returned values. At the end we just have to indicate at which location on the stack the returned arguments are to be found. The syntax LhsVar(i)=j is used to say that the ith value to be returned is at position j on the stack. The second variable to be returned is the integer array. Note that by default Scilab matrices are stored as a double array and that we have used the data of the second variable as an integer array. Since we have declared that the second variable was an integer array ("i"), Scilab will perform an automatic conversion of the array when returning from the interface. All interfaces are based on the same pattern, and a number of examples are available in the subdirectories of SCI/examples. For example, the ﬁles included in the directory SCI/examples/interface-tour-so give a tour of almost all of the functions that can be used for interfacing. Note that it is possible, for example, to interface a function that itself uses a function as a parameter and at the Scilab level we often want to pass a Scilab 2.5 Interfacing 69 function as an argument. It is also explained how to call the Scilab interpreter within an interface to compute an intermediate value. In the MATLAB world, interfaces are known under the name of mexﬁles. As explained in SCI/examples/mex-examples, mexﬁles can be emulated in Scilab and can be used as interfaces for Scilab functions. 2.5.3 Dynamic Loading Writing glue code is just the ﬁrst step. In order to use the desired code, we need to compile the written code as a shared library. Usually this is .so ﬁles in Unix and .dll ﬁles in Windows. Then we need to load it in Scilab, and tell Scilab the correspondence between the interface names and the names of primitives to be added. Since this process is system-dependent, we have developed Scilab script to hide system-dependent code from the Scilab user. We have two interface functions that are coded in the two ﬁles intdgetrf.c and intdlaswp.c. Note that there are no limitations or constraints on the number of ﬁles and their names. Using a text editor, we have to write a small Scilab script named builder.sce. This is a canonic name and most Scilab users know that running builder.sce is used to set up a contribution or an interface: −→ // This is the builder.sce that must be run from this directory −→ ilib_name = ’liblapack’; ← interface library name −→ files = [’lapack.o’]; ← objects ﬁles −→ libs = []; ← other libraries needed for linking −→ table =[’dgetrf’, ’int_dgetrf’; ← association table (scilab˙name, interface-name) −→ ’dlaswp’, ’int_dlaswp’]; −→ // do not modify below −→ // ---------------------------------------------−→ ilib_build(ilib_name,table,files,libs) Of course, one can use as a model the builder.sce ﬁle contained in the directory SCI/examples/interface-tutorial-so/builder.sce. It can be easily adapted to create a new interface. builder.sce provides the following information: • ilib_name is a string that stands for the library name (a set of interfaces) that we are building. • files is a string matrix that gives the object ﬁles that are needed to build the library. Note that the object ﬁles are suﬃxed with .o even under Windows. A unique builder.sce ﬁle is to be built and it should run as well under Unix and Windows. • libs is a string matrix that gives a list of libraries (shared libraries) that are needed by the object ﬁles. Note that in our example, we are interfacing functions from the LAPACK library, which is an already loaded Scilab executable. Thus we do not need to provide object ﬁles or libraries for providing the code of the dgetrf and dlaswp functions. • table is a string matrix, where the association between a Scilab name and an interface name is provided. For example, its ﬁrst line ’dgetrf’, ’int_dgetrf’; indicates that the Scilab function dgetrf is implemented via the interface int_dgetrf. Running the builder.sce script from Scilab will create a shared library and a set of new ﬁles. 70 2 Introduction to Scilab −→ // This is the builder.sce that must be run from this directory −→ ilib_name = ’liblapack’; ← interface library name −→ files = [’lapack.o’]; ← objects ﬁles −→ libs = []; ← other libraries needed for linking −→ table =[’dgetrf’, ’int_dgetrf’; −→ ’dlaswp’, ’int_dlaswp’]; ← association table (scilab˙name, interface-name) −→ // do not modify below −→ // ---------------------------------------------−→ ilib_build(ilib_name,table,files,libs) generate a gateway file generate a loader file generate a Makefile: Makelib running the makefile compilation of lapack building shared library (be patient) Among the created ﬁles, the most important one is a new Scilab script called loader.sce, which can be used when we need to load the shared library in a running Scilab. The loader.sce uses the Scilab function addinter to load the shared library and update the primitive table. In practice, one executes the builder.sce once. Note that when running a builder.sce script the current Scilab directory must be the directory containing the builder.sce script, and the library is loaded with the loader.sce script each time we start a new Scilab session. −→ // generated by builder.sce: Please do not edit this file −→ // -----------------------------------------------------−→ liblapack_path=get_file_path(’loader.sce’); −→ functions=[ ’dgetrf’; −→ ’dlaswp’; −→ ]; −→ addinter(liblapack_path+’/liblapack.so’,’liblapack’,functions); shared archive loaded Among the other generated ﬁles we note: • A ﬁle named after the library name liblapack.c, called a “gateway” ﬁle, is also generated. It contains the main entry point of the library, which is used by the addinter function. • A makeﬁle named Makelib, which is system-dependent and, which can be used to rebuild the shared library. This makeﬁle can be very useful for debugging purposes when the shared library build process fails. We can now test the added primitives: −→ exec (’loader.sce’); shared archive loaded −→ A=rand(5,5); 2.5 Interfacing 71 −→ B=A; −→ [LU,perm]=dgetrf(A); ← call dgetrf −→ [E]=dlaswp(eye(A),perm); ← apply permutation to identity matrix −→ L=tril(LU,-1)+eye(LU);U=triu(LU); ← separate L and U −→ if norm(L*U-E*A) > 10*%eps then pause; end ; ← check result 3 Modeling and Simulation in Scilab One of the fundamental problems in many areas of science and engineering is the problem of modeling and simulation. Scilab provides a large array of tools for developing and simulating models of several types. For several of these tools it is possible to use them with abbreviated commands and default values of some parameters. However, to know how to choose the appropriate tools and how to get the kind of answers desired, it is often necessary to know something about how the algorithms are set up and what to do if there are diﬃculties. In this chapter we shall describe the types of models that are available in Scilab and some of the tools available for their simulation. We shall also give some general comments about how to use the tools and how to choose between the tools. 3.1 Types of Models 3.1.1 Ordinary Diﬀerential Equations One of the most popular types of model is that of ordinary diﬀerential equations (ODE) such as ẏ = f (t, y). (3.1) Here y and f may be vector valued, y is a function of the real variable t, and ẏ denotes the derivative of y with respect to t. System (3.1) is a ﬁrst-order equation since it involves only ﬁrst-order derivatives. Some models are initially formulated in terms of higher derivatives, but they can always be rewritten as ﬁrst-order systems by the introduction of additional variables. The Scilab tools assume that the diﬀerential equation has ﬁrst been rewritten to be of ﬁrst order. The exception to this is boundary value problems, where the numerical methods allow for higher-order diﬀerential equations. Example 1. The system of second-oder diﬀerential equations can be rewritten as ÿ1 = ẏ1 − y2 + sin(t), (3.2a) ÿ2 = 3ẏ1 + y1 − 4y2 , (3.2b) 74 3 Modeling and Simulation in Scilab ẏ1 = y3 , ẏ2 = y4 , (3.3a) (3.3b) ẏ3 = y3 − y2 + sin(t), ẏ4 = 3y3 + y1 − 4y2 . (3.3c) (3.3d) The diﬀerential equation (3.1) generally has a family of solutions. To perform a simulation, additional information must be provided to make the solution unique. An initial condition is the value of y at a particular value of t. A boundary condition is when more than one value of t is involved in the additional conditions. An example of a boundary condition is y(0) − 2y(π) = 0. We will discuss boundary value problems later. The basic existence and uniqueness result for diﬀerential equations in the form (3.1) says that if we specify y(t0 ), and f and fy = ∂f /∂y are continuous functions of t and y near (t0 , y(t0 )), then there is a unique solution to (3.1) satisfying the initial condition. The solution will continue to exist as long as these assumptions hold. Another important fact is that the more derivatives of f (t, y) that exist and are continuous, the more derivatives y has. This may seem to be an esoteric fact, but it actually plays an important role in simulation. The reason is that most simulation tools try to deliver a requested accuracy and the error estimates are based on assumptions about the number of derivatives of the solutions. Suppose now that we have a controlled diﬀerential equation ẏ = f (t, y, u(t)) (3.4) with u(t) a given function. Then even if f has all the derivatives that one wants, y can have only one more derivative than u. Thus if a control is only piecewise continuous, so that it has jumps, which often occurs in applications, the solution y will not have a continuous ﬁrst derivative at the jumps. This in turn can lead to serious problems with many numerical tools unless the behavior of u is taken into account when one is requesting the simulation. 3.1.2 Boundary Value Problems Boundary value problems (BVP) are diﬀerential equations but with the information given at two or more times rather than at just one time. Two-point boundary value problems take the general form of ẏ = f (t, y), t0 ≤ t ≤ tf , 0 = B(y(t0 ), y(tf )). (3.5a) (3.5b) If y in (3.5a) is n-dimensional, then there usually need to be n equations in (3.5b) in order to uniquely determine a solution. However, the theory is more complicated than that for initial value problems, as illustrated by the next example. Example 2. Suppose that we have the boundary value problem 0 1 0 ẏ = y+ , −1 0 1 y1 (0) 0= . y2 (tf ) The solution of this diﬀerential equation is (3.6a) (3.6b) 3.1 Types of Models y1 = c1 cos(t) + c2 sin(t) + 1, y2 = −c1 sin(t) + c2 cos(t), 75 (3.7a) (3.7b) where c1 , c2 are arbitrary constants. The ﬁrst boundary condition (3.7a) gives c1 = −1. The second boundary condition (3.7b) then gives that 0 = sin(tf ) + c2 cos(tf ). (3.8) But (3.8) has a solution for c2 only if cos(tf ) = 0. Thus we see that the existence of a solution to this boundary value problem depends on the particular value of tf . Here tf cannot be π2 + mπ, where m is an integer. One of the nice features about ODEs is that the given initial conditions are at one point. This means that with the numerical methods given below the computation can be local and move from one step to the next. This makes it easier to solve problems on longer time intervals or with higher state dimension. With BVP, since the information occurs at more than one time, it is often necessary to use a more global algorithm that takes into account the full t interval. This leads to much larger systems of equations that have to be solved. 3.1.3 Diﬀerence Equations The second major class of models is that of diﬀerence equations. These problems occur when there a quantity whose values are of interest only at discrete-time values or the values change only at discrete times. Diﬀerence equations can occur either because the process is naturally discrete and only undergoes changes at isolated times or because it is continuous but our observations occur only at isolated times. Also, many numerical methods for diﬀerential equations actually solve an approximating diﬀerence equation. In diﬀerence equations there is an integer variable, which we denote by k. The solution is a sequence y(k) and it has to solve a diﬀerence equation y(k0 ) = y0 . y(k + 1) = f (k, y(k)), (3.9) Sometimes it is more natural to think of a sequence of times tk , k ≥ k0 , and a diﬀerence equation (3.10) z(tk+1 ) = g(tk , z(tk )), z(tk0 ) = z0 . If the tk are evenly spaced, so that t+1 − tk = h where h is a constant, then we can rewrite (3.10) in the form of (3.9) as follows: v(k + 1) = g(w(k), v(k)), w(k + 1) = w(k) + h, v(k0 ) = z0 , w(k0 ) = tk0 . (3.11a) (3.11b) Note that v(k) of (3.11a) is the same as z(tk ) from (3.10). For the remainder of this section we will assume that the diﬀerence equation is in the form (3.9). The theory for solutions of diﬀerence equations is simpler than that for diﬀerential equations. Given a starting time k0 and a value of y0 that gives the initial condition y(k0 ) = y0 , the values of y(k) for k > k0 are computed recursively using (3.9) and will exist as long as (k, y(k)) is in the domain of f . There is one important diﬀerence between the discrete and continuous theories that is worth mentioning. The uniqueness theorem for diﬀerential equations tells us that if two solutions start at the same time but with diﬀerent initial values and if the continuity assumptions on f, fy are holding, then the solutions can never intersect. This is not true for diﬀerence equations. 76 3 Modeling and Simulation in Scilab Example 3. The diﬀerence equation (3.12) is at the origin after two time steps no matter where it starts at k0 : 1 −1 y(k + 1) = y(k). (3.12) 1 −1 The diﬀerence equations (3.9) and (3.12) are of ﬁrst order since there is maximal diﬀerence of one in the values of k that are present. Just as systems of diﬀerential equations can always be written as ﬁrst-order diﬀerential equations, diﬀerence equations can always be written as ﬁrst-order diﬀerence equations. Example 4. The equation (3.13) is of third order since (k + 3) − k = 3: y(k + 3) = 4y(k + 2) − y(k + 1)y(k). (3.13) The diﬀerence equation (3.13) can be rewritten as a ﬁrst-order diﬀerence equation by introducing additional variables as shown in (3.14): y1 (k + 1) = y2 (k), (3.14a) y2 (k + 1) = y3 (k), y3 (k + 1) = 4y3 (k) − y2 (k)y1 (k). (3.14b) (3.14c) The Scilab tools for working with diﬀerence equations assume that they have been written in ﬁrst-order form. 3.1.4 Diﬀerential Algebraic Equations Many physical systems are most naturally initially modeled by systems composed of both diﬀerential and algebraic equations [11]. These systems take the general form of F (t, y, ẏ) = 0 (3.15) and are often called DAEs. Example 5. One example of a DAE is a model of a constrained mechanical system where the constraint is on the position variables. Such a system takes the general form ẋ = v, (3.16a) v̇ = G(x, v, t) + Q(x, t)λ, 0 = H(x, t). (3.16b) (3.16c) System (3.16) is a DAE in y = (x, v, λ). Equation (3.16c) gives the constraints. Also Q(x, t) = Hx (x, t)T , and Q(x, t)λ gives the force generated by the constraint. Given a DAE model, there are two options. One option is to try to rewrite the DAE into an ODE or a simpler DAE. This is usually done by a mixture of diﬀerentiation and algebraic manipulations. The other option is to try to simulate the DAE directly. The theory for DAEs is much more complex than the theory for ODEs. One problem is that there will exist solutions only for certain initial conditions that are called consistent initial conditions. Considerably more detailed information about DAEs is in [11]. Here it is enough to know two things. First, the structure of the DAE is importan, and second, there is a nonnegative integer called the index. An ODE is is said to have index zero. 3.1 Types of Models 77 Example 6. The simple system ẏ1 = y1 − cos(y2 ) + t, 0 = y13 + y2 + et , (3.17a) (3.17b) is a DAE. From (3.17b) we see that the initial conditions must satisfy y1 (t0 )3 +y2 (t0 )+et0 = 0, so that only some initial conditions are consistent. This particular example can be reduced to an ODE by solving (3.17b) for y2 and substituting into (3.17a) to get an ODE in y1 . Index-one DAE systems occur frequently and are the ones for which Scilab provides tools. They are also the ones that Scicos is able to solve when they occur in the form of implicit blocks. Index-one systems in the form (3.15) have the additional property that {Fẏ , Fy } is an index-one matrix pencil all along the solution and Fẏ has constant rank. Two important types of index-one DAEs are the implicit semiexplicit and semiexplicit index-one systems. The implicit semiexplicit index-one systems are in the form F1 (t, y1 , y2 , ẏ1 ) = 0, (3.18a) F2 (t, y1 , y2 ) = 0. (3.18b) Here ∂F1 /∂ ẏ1 and ∂F2 /∂y2 are both nonsingular. We shall sometimes refer to y1 as a diﬀerential variable and y2 as an algebraic variable. The semiexplicit index-one DAE takes the form ẏ1 = F1 (t, y1 , y2 ) (3.19a) 0 = F2 (t, y1 , y2 ) (3.19b) with ∂F2 /∂y2 nonsingular. The DAE (3.17) is semiexplicit of index one. Index-one DAEs that are not semiexplicit or linear in the derivative terms are called fully implicit. 3.1.5 Hybrid Systems Many systems involve a mixture of discrete- and continuous-time events. These types of systems are generally called hybrid systems. The discrete nature of the system can occur in several diﬀerent ways. These diﬀerences have to be taken into account in the simulation. The times at which a discrete variable changes value are sometimes called events. When an event occurs, there may be a change in the diﬀerential equations. The state dimension may also change. It then becomes important to determine new initial conditions. This is referred to as the initialization problem. Sometimes the event occurs when some quantity reaches a certain value. For example, in the simulation of a mechanical system, if contact is made with an obstacle, then the equations change. It thus becomes necessary for the simulation software to be able to determine when the event occurs. This is known as root-ﬁnding ability. Even when an event does not change the dimension of the state or the form of the equations, it can interfere with the error control of the integrator being used unless care is taken. The Scicos software described in Part 2 of this book was speciﬁcally designed to handle hybrid systems. The new version released with Scilab 3.0 is able to simulate a variety of DAE hybrid systems. 78 3 Modeling and Simulation in Scilab 3.2 Simulation Tools The simulation tools in Scilab generally take three forms. One of these comprises the primary tools used by knowledgeable users on challenging problems. The second is simpliﬁed versions of the primary tools that are easier to use and are designed for simpler problems. The third is tools developed for special cases that occur often in certain areas of science and engineering. We will usually present them in the order of simpliﬁed, primary, specialized. 3.2.1 Ordinary Diﬀerential Equations The primary simulation tool for ordinary diﬀerential equations is ode. In order to simulate a diﬀerential equation (3.1), the least that we can give the integrator is the initial time t0 , the initial value y0 , and a formula for evaluating f (t, y). When we do this, the software picks the method and the error tolerances and adjusts the step size to try to meet the error tolerances. This will often result in many more values of the solution than are needed. Also, sometimes the value of the solution is desired at time values that are not time values used in the numerical simulation. Thus the ﬁnal thing the simpliﬁed version needs is a vector of times at which the value of the solution is desired. The ﬁnal entry of this vector is the ﬁnal time for the integration. Thus the simpliﬁed calling sequence for ode is −→ y=ode(y0,t0,t,f) Here f is an external variable or string providing the function f that is the right-hand side (RHS) of (3.1). If t = [t0 , t1 , . . . , tn ], then y= [y(t0 ), y(t1 ), . . . , y(tn )], where y(ti ) is the estimate for the solution at time ti . If t is a single number, then it is the ﬁnal time of the integration. In earlier versions of Scilab, the function f could be deﬁned either as a .sci ﬁle or on-line using the deff function. With version 3.0 of Scilab, functions may be deﬁned on-line using the same syntax as if they were deﬁned in a ﬁle. Thus if we wish to deﬁne the function f (t, y) = −y + sin(t, y) on-line, we can just write −→ function ydot = f(t,y) −→ ydot=-y + sin(t) −→ endfunction Unfortunately, the simple call of ode does not always work. More direct control of the simulation comes through two methods. One is the expanded or full version of the ode command. The other uses odeoptions. In the full version of ode one can choose the method, specify the integration tolerances, provide the Jacobian if it is needed by the type of method chosen, and set up some other variables required by some methods. ode provides a standard interface to a number of solvers, in particular to those in ODEPACK[28, 34, 13]. In choosing the method, several factors need to be taken into account. For problems with frequent restarts, there are advantages to one step methods such as the Runge–Kutta formulations over the multistep methods such as the BDF methods and Adams methods. The order of a numerical method is an indication of the error relative to the step size. Thus a fourth-order method would be expected to have error proportional to h4 (assuming that a constant step h is used, and the steps are suﬃciently small). On the other hand, multistep methods, for a given order, often require less computation. Some problems are 3.2 Simulation Tools 79 referred to as stiﬀ. There are lots of deﬁnitions of a stiﬀ diﬀerential equation, but we will take stiﬀ to mean that if one had an explicit method, then the step sizes would have to be reduced to ensure stability of the error propagation rather than to get the desired accuracy. Stiﬀness is often caused by large negative eigenvalues. Generally, implicit methods can take bigger time steps than explicit methods on stiﬀ problems. Higher order is usually desirable. However, for implicit methods the higher the order, the smaller the stability region. This means that the higher-order method might have to take smaller steps than expected. This is especially true for problems that have highly oscillatory solutions. In addition, it does not make sense to go to high order if the equations, or input functions, do not have enough continuous derivatives. The previous comments only touch on some of the issues involved in the choice of numerical method. Our experience has been that when the simple call does not work, then it is often better to alter the atol and rtol and use odeoptions, which is described shortly, rather than altering the method. The exception is with DAEs as discussed later. The full calling sequence of ode is either of [y,w,iw]=ode([type],y0,t0,t [,rtol [,atol]],f [,jac] [,w,iw]) [y,rd,w,iw]=ode("root",y0,t0,t [,rtol [,atol]],f [,jac],ng,g [,w,iw]) Any variables inside ode with square brackets are optional. Here type is a character string and refers to the type of method chosen. The available types are as follows: lsoda: This is the default integrator. It is from the package ODEPACK. It automatically selects between a nonstiﬀ predictor-corrector Adams method and the stiﬀ Backward Diﬀerentiation Formula (BDF) method. Both methods are multistep methods. lsoda uses the nonstiﬀ method initially and dynamically monitors data in order to decide which method to use. It also automatically varies the order. adams: This calls the lsode solver from ODEPACK and uses the Adams method. It is for nonstiﬀ problems. stiff: This is for stiﬀ problems. It calls the lsode solver from ODEPACK and uses the BDF method. rk: This is an adaptive Runge-Kutta of order 4 (RK4). Thus it is a one-step method and can usually use larger steps after a restart than a multistep method can use. rkf: This is another Runge–Kutta method by Shampine and Watts and is based on Fehlberg’s Runge–Kutta pair of order 4 and 5 (RKF45). The method is of order 4. It simultaneously carries out a ﬁfth-order method that is used for error prediction. This method is for nonstiﬀ and mildly stiﬀ problems when derivative evaluations are inexpensive. This method should generally not be used when the user demands high accuracy. fix: This is the same solver as rkf, but the user interface is very simple. Only the rtol and atol parameters can be passed to the solver. This is the simplest method to try. root: This is the lsodar solver with root-ﬁnding capabilities from ODEPACK. It is a variant of the lsoda solver. root ﬁnds the roots of a given vector function. We will discuss this more carefully below when we discuss ode_root. discrete: We will discuss this option when we discuss discrete simulation tools below. See also ode_discrete for more details. Two parameters that are especially useful are rtol and atol, which are real constants or real vectors of the same size as y. Their entries are the absolute and relative error tolerances requested for each entry of y. Setting these tolerances too tight can increase computational time and, in some cases, lead to integrator failure when tolerances cannot be met. Setting the tolerances too loose can lead to inaccurate answers. It should be 80 3 Modeling and Simulation in Scilab kept in mind that the step size decisions are based on estimates of the error. The actual error in the solution could be higher. Default values for rtol and atol are rtol=1.d-5 and atol=1.d-7 for most solvers and rtol=1.d-3 and atol=1.d-4 for rfk and fix. The parameter jac is an an external (function or character string or list) that provides a Jacobian for those methods that can utilize analytic Jacobians. These include the BDF based and other implicit methods for stiﬀ problems. If jac is a function, then the syntax should be J=jac(t,y). J should evaluate to the matrix that is ∂f /∂x. Optional arguments w and iw are vectors for storing information returned by the integration routine. When these vectors are provided in the right-hand side of ode the integration restarts with the same parameters as in its previous stop. When ode is called, the ﬁrst thing it does is to check whether the variable %ODEOPTIONS exists. If %ODEOPTIONS does not exist, ode uses the default values of a number of parameters. %ODEOPTIONS can be set directly. Alternatively, these parameters can be changed using the function odeoptions: −→ %ODEOPTIONS=odeoptions() A window then opens in which any desired parameter values can be entered. Parameters will stay at these values throughout the rest of the session unless they are reset or cleared. The variable %ODEOPTIONS has the following entries: %ODEOPTIONS=[itask,tcrit,h0,hmax,hmin,jactyp,mxstep,maxordn,maxords,... ixpr,ml,mu] with default value [1,0,0,%inf,0,2,500,12,5,0,-1,-1]. The parameters in %ODEOPTIONS have the following uses and meaning. itask: Takes on integer values in [1, 5]. 1 is normal computation at speciﬁed times. 2 is computation at mesh points that are given in the ﬁrst row of output of ode. 3 is one step at one internal mesh point and return. 4 is normal computation without overshooting tcrit. 5 is one step, without passing tcrit and return. Options 4 and 5 are used when there is critical time past which the diﬀerential equation changes so that one wants to integrate up to tcrit but not use information past that time. tcrit: Assumes that itask equals 4 or 5 as described above. tcrit is a scalar. h0: This is the ﬁrst step tried by the integrator. It might be used with a Runge–Kutta method where you know that the solution starts out changing slowly and using a small ﬁrst step wastes CPU time. hmax: This is the largest step size that the integrator is allowed to use. One use of this is with simulations where there are long slow stretches and then rapid changes in the solution. Sometimes the integrators will start taking such large steps that they are unable to recover from a rapid change, or even worse, they miss an isolated event completely. This is illustrated in Example 7. If hmax is set small, it may be necessary to increase mxstep. hmin: This is the minimum step size that can be accepted. Setting it greater than zero can make the error estimates less reliable and may result in a less-accurate solution. However, it also has the eﬀect of forcing the integrator to keep going rather that letting the integrator take very tiny steps. This is most useful when there are local conditioning problems or loss of smoothness that is interfering with the error estimates. jactype: This speciﬁes how any nonlinear equations in the integrator will be solved. 0 is functional iterations with no Jacobian used. This is for ’adams’ or "stiff" only. 1 is a user-supplied full Jacobian. 2 is an internally generated full Jacobian. This is done by diﬀerencing and so is an approximate Jacobian. 3 is an internally generated diagonal 3.2 Simulation Tools 81 Jacobian. This is for ’adams’ or "stiff" only. 4 is a user-supplied banded Jacobian (see ml and mu below). 5 is an internally generated banded Jacobian (see ml and mu below). Banded Jacobians naturally occur with some problems, such as the method of lines solutions of PDEs. The solvers can exploit the banded structure if they are informed about it. mxstep: This is the maximum number of time steps the integrator is allowed to take. The default value is 500. Having a ﬁnite value here is important since it avoids having the algorithm spending a very long time on integration without permission. This can happen on longer intervals that require very tiny timesteps, or in integrating until a root is found but the surface is never crossed. 500 is probably a bit low for a default value and is there for historical reasons. maxordn: This is the maximum nonstiﬀ order allowed. It can be at most 12. The higher orders have some advantages, but they also have a number of disadvantages on some problems, so one should not always assume that higher is better. Setting maxordn to be less than 12 is another way to encourage the method to use smaller step sizes. maxords: This is the maximum stiﬀ order allowed. It is at most 5 since BDF methods are not stable at orders higher than 5. Setting maxords=1 gives an implicit Euler’s method. If one is having trouble integrating highly oscillatory systems, then it is often helpful to set maxords to 2 or maybe 3. ixpr: This is the print level and is 0 or 1. ml, mu: If jactype equals 4 or 5, then ml and mu are the lower and upper half-bandwidths of the banded Jacobian. The band is the i,j’s with i-ml ≤ j ≤ ny-1. If jactype equals 4, then the Jacobian function must return a matrix J that is ml+mu+1*ny, where ny is the dimension of ẏ in ẏ = f (t, y). Column 1 of J is made of mu zeros followed by ∂f /∂y1 (1+ml possibly nonzero entries). Column 2 is made of mu-1 zeros followed by ∂f /∂y2 , etc. Example 7. As an illustration of the need for odeoptions(), we consider the problem of simulating ẏ = −0.1y + g(t), y(0) = 1, 0 ≤ t ≤ 600, (3.20) with g(t) zero except between 488.3 and 488.9, where g takes the value of 2. This diﬀerential equation is representative of the situation in which there are sudden events after a long period during which there is little change in the solution of the diﬀerential equation. One way to write g mathematically is g(t) = 0.5(1 + sign(t − 488.3))(1 − sign(t − 488.9)). The following Scilab script runs a simulation of (3.20) with the default settings of %ODEOPTONS. The result is plotted in graphics window 0. It then changes hmax to 0.1 and increases mxstep to 10,000 and plots the new solution in graphics window 1. function z=g(t) z=0.5*(1+sign(t-488.3))*(1-sign(t-488.9)) endfunction function ydot = f(t,y) ydot=-0.1*y+g(t) endfunction 82 3 Modeling and Simulation in Scilab tt=0:0.1:600; %ODEOPTIONS=[1,0,0,%inf,0,2,500,12,5,0,-1,-1]; y=ode(1,0,tt,f); xbasc() plot2d(tt,y); ← Fig. 3.1 %ODEOPTIONS=[1,0,0,0.1,0,2,10000,12,5,0,-1,-1]; xbasc() y=ode(1,0,tt,f); plot2d(tt,y); ← Fig. 3.2 If this script is called longstep.sci and is in the directory /Users/scampbell, then the simulation is carried out by −→ exec("/Users/scampbell/longstep.sci") The simulation using ode with the no limit on hmax gives the graph in Figure 3.1. The eﬀect of g is totally missing. This is because ODE integrators often take large steps during “quiet periods” and then sometimes totally miss events happening over relatively short periods. Changing hmax from %inf to 0.1 and mxstep to 10,000, we get the graph in Figure 3.2. The change in mxstep was needed because the new value of hmax would require more than the default value of 500 steps. 1.1 0.9 0.7 0.5 0.3 0.1 −0.1 0 100 200 300 400 500 600 Figure 3.1. Simulation using default values of hmax and mxstep. A careful discussion of all the factors in choosing between methods is a book in itself. The interested reader is referred to the classic volumes [26, 27]. But one question is, suppose that I am going to use default values. Should I worry about which method I use? The following example addresses the eﬀect of method on time of computation. Actual timings are somewhat machine-dependent. We do not consider the important question of 3.2 Simulation Tools 83 1.2 1.0 0.8 0.6 0.4 0.2 0.0 0 100 200 300 400 500 600 Figure 3.2. Simulation after using odeoptions() to change f, hmax, and mxstep. accuracy here but note that in the computations below, when we had the solutions graphed they appeared the same. Example 8. Listed below is a Scilab script compode.sci, which deﬁnes several functions. The function comparison(a,b,c,d) generates an 8-dimensional linear system ẏ = Ay + g. The eigenvalues of A are a±bi, a±bi, c, c, d, d, and all modes appear in all entries of y. This diﬀerential equation is then integrated using the default method and adams, stiff, rkf, and the DAE integrator dassl discussed later in this chapter. The output is the computational times for each method in seconds. The times are found using tic and toc. function w=g(t) w=[zeros(1,6),cos(t),3*sin(2*t)]’; endfunction function ydot=f(t,y) ydot=A2*y+g(t); endfunction function [r,ires]=res(t,y,ydot) r=ydot-A2*y-g(t) ires=0 endfunction function zz=comparison(a,b,c,d) A=[a,-b,0,0;b, a, 0,0;0,0,c,0;0,0,0,d]; II4=eye(A);Z=zeros(A); AA=[A,zeros(A);zeros(A),A]; Q=[1 3 2 0 -1 4 6 0 10 4 5 7 8 -3 6 -5 ]; Q=[Q,1,3,Q,3,6,8,9,Q,11,7,2,4,0,-3,6,3,9,-3]; QQ=matrix(Q,8,8); A2=inv(QQ)*AA*QQ; tt=[0:0.1:100];t0=0; 84 3 Modeling and Simulation in Scilab y0=[1 1 -1 1 2 0 -1 -1 ]’; ydot0=A2*y0+g(0); tic;yy1=ode(y0,t0,tt,f); t1=toc(); tic;yy2=ode("adams",y0,t0,tt,f); t2=toc(); tic;yy3=ode("stiff",y0,t0,tt,f); t3=toc(); tic;yy4=ode("rkf",y0,t0,tt,f); t4=toc(); xstart=[y0,ydot0]; tic;yy5=dassl(xstart,t0,tt,res); t5=toc(); zz=[t1,t2,t3,t4,t5] xbasc() plot2d(tt,yy1(7,:)) plot2d(tt,yy2(7,:)) plot2d(tt,yy3(7,:)) plot2d(tt,yy4(7,:)) plot2d(tt,yy5(8,:)) endfunction The functions in compode.sci are loaded in Scilab, and to compare the diﬀerent methods on an easy problem we enter −→ comparison(-0.1,2,-3,0.1) ans = ! 0.318 0.197 0.348 0.525 0.533 ! We see that the methods were comparable, with the Adams method the fastest and dassl the fastest of the stiﬀ methods. Adams is often fastest on problems that are not rapidly varying. rkf is second-best. Generally, Runge–Kutta methods like rkf take more work per time step than a multistep of the same oder such as adams. Also, the adams can go to higher order. However, some problems, such as those in electrical engineering and some mechanical systems can be highly oscillatory. In other areas such as chemical reactions we can have very rapidly decaying terms. To examine what happens with a rapidly decaying component we enter −→ comparison(-0.1,2,-5000,0.1) ans = ! 0.471 2.885 0.422 70.131 0.745 ! We see that the implicit methods default, stiff, and dassl are much faster. adams takes 7 times as long as stiff and rkf takes 30 times as long as stiff. The default takes longer than stiff since it starts as a nonstiﬀ method and then has to switch. As is not unusual, dassl is comparable to stiff but sometimes takes a little longer. That is because dassl is designed for fully implicit DAEs and sometimes has to do a bit more work. 3.2 Simulation Tools 85 If a system is highly oscillatory, then some of the methods designed for stiﬀ problems have to reduce their step size or order for stability reasons. To illustrate, we enter −→ comparison(-0.1,100,-3,0.1) ans = ! 11.787 4.678 8.68 5.793 23.37 ! Whereas in the previous problem the methods of stiﬀ type were clearly faster, we see here that the stiﬀ methods were much slower. In the case of dassl it has some heuristics that look out for oscillatory systems and reduces the order to try to increase stability. Sometimes simulation is embedded into design or optimization loops so that the same diﬀerential equations will be integrated many times. As the previous example shows, the computational time can vary greatly with the choice of the method. Accuracy can also vary, so that for complex problems one needs to do a careful examination of which integrator will be fastest and also deliver the desired accuracy. Implicit Diﬀerential Equations In some application areas the models are most naturally initially formulated as diﬀerential equations in the form (3.21) A(t, y)ẏ = g(t, y), y(t0 ) = y0 . If A(t, y) is not invertible for all t, y of interest, then (3.21) is a linear implicit diﬀerential algebraic equation. If A(y, t) is invertible for all t, y of interest, we call (3.21) either a linearly implicit diﬀerential equation or an index-zero DAE. For this section we assume that A(t, y) is an invertible matrix. One could, of course invert A and get the ODE ẏ = A(t, y)−1 g(t, y) y(t0 ) = y0 . (3.22) However, this reformulation is not always desirable. The implicit system (3.21) can often be integrated more eﬃciently and reliably than (3.22). The numerical methods for solving (3.21) are similar to those we discuss later for DAEs of the form F (t, y, ẏ) = 0, which require a way to evaluate F given estimates of y(t), ẏ(t). Traditionally this function F is called a residual since iterative numerical methods are used, and when approximations are substituted in for y and ẏ, the evaluation of F (t, y, ẏ) tells us how far the approximations are from being solutions. One Scilab simulation tool for (3.21) is impl. It is called by y=impl([type],y0,ydot0,t0,t [,atol, [rtol]],res,adda [,jac]) Most of these variables have the same meaning as they do for ode, but there are some important new variables and there are some restrictions on the other variables. If type is present, it is either "adams" or "stiff". The algorithm requires an initial value of ẏ(t0 ) and this is provided in the vector ydot0. The actual diﬀerential equation being integrated enters through the residual res, which is an external, that is, a function with speciﬁed syntax, or the name of a Fortran subroutine or a C function (character string) with speciﬁed calling sequence or a list. If res is a function, its syntax must be r = res(t,y,ydot) and it must return the value of g(t,y)-A(t,y)*ydot. 86 3 Modeling and Simulation in Scilab In the implicit methods there are nonlinear equations that must be solved, and these require Jacobian information. The variable adda tells the integrator what A is. It also is useful within the numerical algorithm in setting up other Jacobians. If adda is a function, its syntax must be r = adda(t,y,p) and it must return r=A(t,y)+p, where p is a matrix to be added to A(t,y). As usual, if adda is a character string, it refers to the name of a Fortran subroutine or a C function. The optional input jac is also an external. If jac is a function, it must have the syntax j = jac(t,y,ydot) and it must return the Jacobian of r= g(t,y) -A(t,y)*ydot with respect to y. If a diﬀerential equation is fully implicit, that is, F (t, y, ẏ) = 0 with ∂F/∂ ẏ nonsingular, then it is integrated using dassl as described in the section on diﬀerential algebraic equations. Linear Systems Since they occur so often in areas such as control engineering, there are a number of specialized functions for working with linear systems of the form ẋ = Ax + Bu, (3.23a) y = Cx + Du, (3.23b) where A, B, C, D are matrices. System (3.23) is called the state-space form. If we let x(0) = 0, take the Laplace transform of (3.23), and let ẑ(s) denote the Laplace transform of z(t), we see that (3.23) can be written as ŷ(s) = (D + C(sI − A)−1 B)û(s) = H(s)u(s), (3.24) where H is a rational matrix function. That is, the entries of H are fractions of polynomials. Further manipulations would allow us to get D(s)ŷ(s) = N (s)û(s) = H(s)u(s), (3.25) where D, N are matrices of polynomials in s. Scilab has functions for working with all three of these ways of describing linear systems. It also easily switches from one representation to another. Linear systems are deﬁned by the syslin function. It may be called in three diﬀerent ways: [sl]=syslin(dom,A,B,C [,D [,x0] ]) [sl]=syslin(dom,N,D) [sl]=syslin(dom,H) The resulting sl is a tlist (“syslin” list) representing the linear system. sl can in turn be used in a number of diﬀerent Scilab functions. dom is a character string (’c’ , ’d’ , or [] ) or a scalar. It deﬁnes the domain of the system. ’c’ is for a continuous-time system, ’d’ is for a discrete-time system, and a scalar n is for a sampled system with sampling period n (in seconds). A, B, C, D are the matrices of the state-space representation (3.23). x0 is the initial state. If x0 is not given, then x0=0. N, D, and H are polynomial and rational matrices respectively corresponding to (3.25) and (3.24). If it is desired to recover the values of A, B, C, D, from sl, this is done by the abcd command, 3.2 Simulation Tools 87 −→ [A,B,C,D]=abcd(sl) The scilab command csim provides a simulation using ode of the linear system (3.23) deﬁned by ls. csim is called by [y [,x]]=csim(u,t,sl,[x0 [,tol]]) u is a function, list, or string that deﬁnes the control u. t provides the times requested for the simulation values. t(1) is the initial time. y is a matrix of y values such that y(i)=[y(t(i)] for i = 1, . . . , n. The optional output x is the corresponding values of the state x. tol is the two-vector [atol rtol] deﬁning absolute and relative error tolerances for the ode solver. In addition to the usual choices for u such as a function, list, or vector of values, there are two special options. If u is the string "impuls", then an impulse response calculation is done. That is, u is a delta function, or unit impulse, at time t = 0. If u is "impuls", then it is assumed that sl is SISO (single input-single output), D = 0, and x0=0. u can also be "step" for a step response calculation. That is, u = 1 for t > 0. "step" has the same assumptions as "impluls". Example 9. Consider the continuous linear system ẋ1 = −2x1 + 7x2 + u, ẋ2 = 8x1 − 2x2 , y = x1 + x2 . (3.26a) (3.26b) (3.26c) When executed, the following ﬁle produces the output for a step input and an impulse input in two diﬀerent windows. A=[-2 7;-8 -2];B=[1;0];C=[1 1 ];D=0; [sl]=syslin("c",A,B,C); t=0:0.01:5; [ys,xs]=csim("step",t,sl); plot2d(t,ys); [yi,xi]=csim("impuls",t,sl); f1=scf(1); xbasc(); plot2d(t,yi) ← Finds step response of system ← Fig. 3.3 (upper graph) ← Finds impuls response ← Fig. 3.3 (lower graph) The graphs are given in Figure 3.3. Root Finding It is often desirable to simulate a diﬀerential equation up to the time something happens. In some cases the time is the quantity of interest, for example in an interception problem. In other cases the time will represent physical change in the dynamics such as when a tank is ﬁlling up and it starts to overﬂow. In all these cases we have a system (3.1) and we wish to integrate it until some quantity g(t, y) is equal to 0. Scilab includes root ﬁnding software for both ODEs and DAEs. We focus on the ODE case here. The Scilab function ode_root is called by 88 3 Modeling and Simulation in Scilab 0.05 0.00 -0.05 -0.10 -0.15 -0.20 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 1.0 0.8 0.6 0.4 0.2 0.0 -0.2 -0.4 -0.6 -0.8 -1.0 0.0 Figure 3.3. Output step response (above) and impulse response (below) for (3.26). [y,rd [,w,iw]]=ode("root",y0,t0,t [,rtol [,atol]],f [,jac],ng,g [,w,iw]) All of these variables have the same meaning as they do for ode, except that there are two new input variables g and ng, and a new output variable rd. g is an an external that deﬁnes g(t, y), and ng is its size. The integration will proceed until the solution of (3.1) crosses the surface g(t, y) = 0. If g is a function, then the syntax should be z=g(t,y). The function g can be vector-valued. In that case the integration stops as soon as any component of g is zero. The output rd is a 1 × k matrix. The ﬁrst entry of rd contains the stopping time. Other entries indicate which components of g have changed sign. k > 2 indicates that more than one surface has been simultaneously traversed. 3.2 Simulation Tools 89 It is worth noting that ode_root does not just stop when it crosses the surface g(t, y) = 0. Rather, once it has crossed the surface, the algorithm iteratively reduces the last step in order to try to end on the surface up to the speciﬁed tolerances. This is an important feature. When the surface-crossing represents a change in the diﬀerential equation, one could try to describe this situation using a single function with if statements. However, then there is the problem that the numerical simulation will step across the event and make the change at a later time. Often it is important to have the change occur at the correct time, and this requires the integrator searching out exactly when the crossing occurs. Example 10. We suppose that we have a 1,000 cubic meter rectangular tank containing water that ﬂows out a hole at the bottom. The tank is 10 meters high and initially full. When the tank is three quarters empty, a sensor causes water to be pumped into the tank at a constant rate. We are interested in the √ amount of water in the tank, which we denote by V . This may be modeled by √ V̇ = −c1 V , V (0) = 1, 000 until V (t) = 250, and then the diﬀerential equation is V̇ = −c1 V +c2 , where c1 , c2 are two positive constants determined by the size of the hole and the pumping rate. We are interested in the ﬁrst 600 time units or until the tank ﬁlls back up, whichever occurs ﬁrst. Listed below is a ﬁle tank.sci, which performs the simulation. It sets itask to 2 so that all mesh points are output. The if-then construction is built to cover the case in which tf occurs before the surface is reached, which does not happen for the particular ﬂow rates and ﬁnal time chosen. This implementation also stops the simulation if the tank ﬁlls back up. The simulation is obtained by executing the next script, and the simulation graph is in Figure 3.4. function z=f1(t,V) z=-0.3*sqrt(V); endfunction function z=f2(t,V) z=-0.3*sqrt(V) +8.0; endfunction ← tank draining ← tank draining with pumping in function z=g(t,V) z=250-V; endfunction ← surface function z=h(t,V) z=1000-V; endfunction ← end of integration %ODEOPTIONS=[2,0,0,%inf,0,2,10000,12,5,0,-1,-1]; ← set itask to 2, increase maxstep tf=600; [vsol,rd]=ode(’root’,1000,0,tf,f1,1,g); m=size(vsol); if rd(1)< tf then [vsol2,rd2]=ode(’root’,vsol(2,m(2)),rd(1),tf,f2,1,h); sol=[vsol,vsol2]; else sol=vsol; end xbasc(); plot2d(sol(1,:),sol(2,:)); ← Fig. 3.4 90 3 Modeling and Simulation in Scilab 1000 900 800 700 600 500 400 300 200 0 100 200 300 400 500 600 700 Figure 3.4. Simulation of Example 10 using ode root. 3.2.2 Boundary Value Problems A number of methods have been developed for solving BVP. The shooting methods take the given initial information and guess the rest of the initial information. They then adjust this initial guess by integrating over the full interval and use how far they miss the terminal boundary condition to correct the initial guess. Shooting methods are easy to program and can solve some problems, but they are not reliable for problems with long intervals or that are stiﬀ in some sense. A more sophisticated approach is multiple shooting, which breaks the time interval into several subintervals and shoots over these. The third approach is to discretize the diﬀerential equation using some numerical discretization, and then solve the large discrete system that results. For example, if one used Euler’s method with a ﬁxed step of h as the discretization, then the BVP (3.5) would become the nonlinear system of equations xi+1 − xi − hf (t0 + ih, xi ) = 0, B(x0 , xN ) = 0. i = 0, . . . , N − 1, (3.27a) (3.27b) Of course, modern computer programs do not use Euler’s method, but (3.27) makes the key point that what must be solved is a large system of equations, and these equations are nonlinear if the diﬀerential equation or the boundary conditions are nonlinear. This means that the BVP solver must deal with all the usual problems of numerically solving a nonlinear system including having a good enough initial guess and needing some sort of Jacobian information. Scilab provides one simulation tool for BVP, which is bvode. It uses the COLNEW code, which is an update of the COLSYS code [6, 7, 20, 8]. Unlike the situation with 3.2 Simulation Tools 91 ode and dassl, which requires the diﬀerential equations to be in ﬁrst-order form, bvode allows the equations to be of higher order. bvode also allows for boundary conditions at several times ζj . It is traditional in parts of the BVP literature to use x instead of t for the independent variable. We will now switch to that notation. bvode assumes that the BVP is in the form dmi −1 u d mi u i du = fi x, u(x), , . . . , mi −1 , 1 ≤ i ≤ nc , (3.28a) dx dx dx dm∗ u (3.28b) gj ζj , u(ζj ), . . . , m∗ = 0, j = 1, . . . , m∗ , dx where ζj are the x values where the boundary conditions are to hold and aL ≤ x ≤ aR . In order to keep the notation down, let m∗ = m1 + m2 + · · · + mnc , d m∗ u du , . . . , m∗ . z(u) = u, dx dx (3.29) (3.30) Then (3.28) becomes dmi u i = fi (x, z(u(x)), 1 ≤ i ≤ nc , aL ≤ x ≤ aR , dx gj (ζj , z(u(ζj )) = 0, j = 1, . . . , m∗ . (3.31a) (3.31b) bvode starts with an initial mesh for the discretization and solves the resulting nonlinear system. It then iteratively reﬁnes the mesh until either it estimates that the requested accuracy has been attained or it reaches the maximum allowable mesh. The call for bvode takes the form [z]=bvode(points,ncomp,m,aleft,aright,zeta,ipar,ltol,tol,fixpnt,fsub1,... dfsub1,gsub1,dgsub1,guess1) The parameters in the bvode call have the following meanings: z: The solution of the ode evaluated on the mesh given by points. points: An array that gives the points for which we want the solution. ncomp: The number of diﬀerential equations. It must satisfy ncomp ≤ 20. m: A vector of size ncomp . The entry m(j) gives the order of the jth diﬀerential equation. The orders must satisfy m(i) ≤ 4. aleft: The left end of the interval on which u is deﬁned. aright: The right end of the interval on which u is deﬁned. zeta: Entry zeta(j) gives the jth side condition point (boundary point). The entries must be ordered so that zeta(j) ≤zeta(j+1). All the side (boundary) conditions must be at mesh points in all meshes used. In particular, they must be part of the initial mesh. Note the description of ipar(11) and fixpnt below. ipar: An integer array with dimension at least 11. A list of the parameters in ipar and their meaning follows. Note that some parameters are renamed in bvode. These new names are given in parentheses. ipar(1): 0 if the problem is linear and 1 if the problem is nonlinear. ipar(2): The number of collocation points per subinterval (= k ) where max m(i) ≤ k ≤ 7. If ipar(2)=0, then bvode sets k = max(maxi m(i) + 1, 5 − maxi m(i)). ipar(3): The number of subintervals in the initial mesh (= n ). If ipar(3) = 0, then bvode sets n = 5. 92 3 Modeling and Simulation in Scilab ipar(4): The number of solution and derivative tolerances. (= ntol) It is required that 0 < ntol ≤ mstar. ipar(5): The dimension of fspace (= ndimf). This is a real work array. Its size provides a constraint on nmax. Choose ipar(5) according to the formula ipar(5) ≥ nmax*nsizef, where nsizef=4+3*mstar +(5+kd)*kdm+(2*mstar-nrec)*2*mstar. ipar(6): The dimension of ispace ( = ndimi ), an integer work array. Its size provides a constraint on nmax, the maximum number of subintervals. Choose ipar(6) according to the formula ipar(6)>=nmax*nsizei where nsizei=3 + kdm with kdm=kd+mstar, kd=k*ncomp, and nrec = the number of right end boundary conditions. ipar(7): Provides output control (= iprint ). It should be set = -1 for full diagnostic printout, = 0 for selected printout, and = 1 for no printout. ipar(8): This is (= iread). Setting it = 0 causes bvode to generate a uniform initial mesh. It should be = 1 if the initial mesh is provided by the user. Other values are not yet implemented in Scilab. The initial mesh is deﬁned in fspace. The mesh will occupy fspace(1),...,fspace(n+1). the user needs to supply only the interior mesh points fspace(j) = x(j), for j = 2, . . . , n. If ipar(8)= 2, then the initial mesh is supplied by the user as with ipar(8)=1, but no adaptive mesh selection is to be done. ipar(9): This is (= iguess). It should be set = 0 if no initial guess for the solution is provided, = 1 if an initial guess is provided by the user in the subroutine guess, = 2 if an initial mesh and approximate solution coeﬃcients are provided by the user in fspace. The former and new mesh are the same. It should be set = 3 if a former mesh and approximate solution coeﬃcients are provided by the user in fspace but the new mesh is to be taken twice as coarse (that is, every second point from the former mesh), and = 4 if in addition to a former initial mesh and approximate solution coeﬃcients, a new mesh is provided in fspace as well. See the description of output for further details on iguess = 2, 3, and 4. ipar(10): This is = 0 if the problem is regular. It is set = 1 if the ﬁrst relaxation factor is =rstart and the nonlinear iteration does not rely on past convergence. Use this value for an extra-sensitive nonlinear problem only. Set = 2 if you want the code to return immediately upon (a) two successive nonconvergences or (b) after obtaining an error estimate for the ﬁrst time. ipar(11): This is the number of ﬁxed points in the mesh other than aleft and aright. (= nfxpnt, the dimension of fixpnt). The code requires that all side condition points other than aleft and aright (see description of zeta) be included as ﬁxed points in fixpnt. ltol: An array of dimension ipar(4). ltol(j) = l speciﬁes that the jth tolerance in tol controls the error in the lth component of z(u). It is also required that 1 ≤ ltol(1) < ltol(2) < $\cdots$ < ltol(ntol) ≤ mstar. tol: An array of dimension ipar(4). tol(j) is the error tolerance on the ltol(j) component of z(u). Thus, the code attempts to satisfy for 1 ≤ j ≤ntol on each subinterval the following bound |z(v) − z(u)|ltol(j) ≤ tol(j)|z(u)|ltol(j) + tol(j), where v(x) is the approximate solution vector and the subscript denotes the tol(j) entry. fixpnt: An array of dimension ipar(11). It contains the points, other than aleft and aright, that are to be included in every mesh. externals: The functions fsub, dfsub, gsub, dgsub, guess are Scilab externals. The Fortrancoded function interface to bvode is speciﬁed in the ﬁle fcol.f. fsub: The name of the subroutine for evaluating f(x,z(u(x))) with ncomp entries at a point x in the interval (aleft, aright). It should have the heading [f]=fsub(x,z) where f is the vector containing the value of f(x,z(u)) and z(u(x))=(z(1),...,z(mstar)). 3.2 Simulation Tools 93 dfsub: Name of the subroutine for evaluating the Jacobian of f(x,z(u)) at a point x. it should have the heading [df]=dfsub(x ,z) where z(u(x)) is deﬁned as for fsub and df is an ncomp× mstar array. The entries should be df(i,j), which are ∂fi /∂zj . gsub: Name of the subroutine for evaluating the ith component of g(x,z(u(x))) = g(zeta(i),z(u(zeta(i)))) at a point x = zeta(i), where 1 ≤ i ≤ mstar. It should have the heading [g]=gsub(i,z). Note that in contrast to f in fsub, here only one value per call is returned in g. dgsub: Name of the subroutine for evaluating the ith row of the Jacobian of g(x,u(x)). It should have the heading [dg]=dgsub(i,z). guess: Name of subroutine to evaluate the initial approximation for z(u(x)) and for dmval(u(x)) = vector of the m(j)th derivatives of u(x). It should have the heading [z,dmval]= guess(x). Note that this subroutine is used only if ipar(9) = 1, and then all mstar components of z and ncomp components of dmval should be speciﬁed for any x in the interval (aleft, aright) . Example 11. One source of boundary value problems is in the necessary conditions for optimal control problems. Other techniques are used for more complicated optimal control problems [10] but solving the necessary conditions works on many simpler problems. To illustrate suppose that we have the nonlinear controlled system ẏ = y 2 + v and we want to choose the control v so that it will steer y from 2 at time 0 to −1 at time 10. Given this requirement, we wish to have v minimize the quadratic cost 10 J(y, v) = 10v 2 + y 2 dt. 0 This cost penalizes v and y, so that it encourages both the control and state to be small. Then the necessary conditions [30] are found by taking the Hamiltonian H = 10v 2 + y 2 + λ(y 2 + v) and forming the boundary value DAE ∂H , dλ ∂H , −λ̇ = ∂y ∂H 0= , ∂v y(0) = 2, y(10) = −1, ẏ = (3.32a) (3.32b) (3.32c) (3.32d) which is ẏ = y 2 + v, (3.33a) λ̇ = −2y − 2λy, 0 = 20v + λ, (3.33b) (3.33c) y(0) = 2, y(10) = −1. Using (3.33c) to eliminate v from (3.33a), we get the BVP (3.33d) 94 3 Modeling and Simulation in Scilab ẏ = y 2 − λ/20, λ̇ = −2y − 2λy, y(0) = 2, y(10) = −1. (3.34a) (3.34b) (3.34c) We will now solve the BVP using bvode. To simplify things, we shall use the defaults whenever possible. In particular, we will not provide Jacobians or an initial guess. The following script sets up all the needed values for the call. A dummy guess is included since the call requires something in the guess position even if not used. After the BVP is solved the optimal y(t) and v(t) are plotted on the same graph as shown in Figure 3.5. The solid line is y(t) and the x-line curve plots v(t). In this problem the origin is unstable. The control has to work harder to get the solution near zero at the start than it does at the end of the interval. function w=f(x,z) w1=z(1)^2-z(2)/20; w2=-2*z(1)-2*z(1)*z(2) w=[w1;w2]; endfunction function w=df(x,z) w=[2*z(1),-1/20;-2-2*z(2),-2*z(1)] endfunction function w=g(i,z) if i==1 then w=z(1)-2, else w=z(1)+1; end endfunction function w=dg(i,z) w=[1 0] endfunction tt=0:0.1:10; ncomp=2;nrec=1; m=[1 1];k=4; nmax=200; ← k=4 since ipar(2)=0 below ← set the maximum number of subintervals mstar=2; ← begin computation for ipar(5), ipar(6) kd=k*ncomp; kdm=kd+mstar; nsizei=kdm+3; ip6=nmax*nsizei; nsizef=4+3*mstar+(5+kd)*kdm+(2*mstar-nrec)*2*mstar; ip5=nmax*nsizef; zeta=[0 10]; ipar=[1 0 0 2, ip5, ip6, er=1.0d-3; ltol=[1 1]; tol=[er,er]; ← aleft=0 and aright=10 0 0 0 0 0]; 3.2 Simulation Tools 95 function w=gs(x) w=[0,0;0,0] endfunction zz=bvode(tt,2,m,0,10,zeta,ipar,ltol,tol,0,f,df,g,dg,gs); plot2d(tt,zz(1,:)); control=-(1/20)*zz(2,:); plot2d(tt,control); plot2d(tt,control,style=-1) ← Fig. 3.5 2 0 -2 -4 -6 -8 -10 0 1 2 3 4 5 6 7 8 9 10 Figure 3.5. Optimal y (line) and control u (x-line) for Example 11. 3.2.3 Diﬀerence Equations In many ways the simulation of discrete systems is easier because there is no choice about the time step and no error is introduced by the approximation of derivatives. Any error is the usual function evaluation and roundoﬀ error. In order to simulate a discrete system of the ﬁrst-order form (3.35) y(k + 1) = f (k, y(k)), y(k0 ) = y0 , we use ode_discrete, which is called by −→ y=ode("discrete",y0,k0,kvect,f) Note that the initial time k0, is an integer and kvect, which contains the times at which we want the solution, is a vector of integers. kvect(1) must be greater than or equal to k0. As an illustration, suppose that we enter the scilab code 96 3 Modeling and Simulation in Scilab function z=f(t,y) z=-y/2 + sin(t/10) endfunction kvect=0:20; y=ode("discrete",1,0,kvect,f); plot2d(kvect,y) ← Fig. 3.6 xbasc() plot2d2(kvect,y) ← Fig. 3.7 If we want to plot the graph, we might use the command plot2d(kvect,y), which results in Figure 3.6. However, this graph is misleading since the actual solution exists only at integer values. A better option is to just plot either the points or to use plotd2d2(kvect,y) as in Figure 3.7. 1.0 0.5 0.0 -0.5 0 2 4 6 8 10 12 14 16 18 20 Figure 3.6. Graph produced by plot2d(kvect,y). Linear Systems As with the continuous case, there are specialized programs for working with linear systems with real matrix coeﬃcients in the form of x(k + 1) = Ax(k) + Bu(k), y(k) = Cx(k) + Du(k). (3.36a) (3.36b) If we just wish to simulate the state equation (3.36a), then we can use ltitr. It is called with either of [X]=ltitr(A,B,U,[x0]) [xf,X]=ltitr(A,B,U,[x0]) 3.2 Simulation Tools 97 1.0 0.5 0.0 -0.5 0 2 4 6 8 10 12 14 16 18 20 Figure 3.7. Graph produced by plot2d2(kvect,y). With ltitr the initial value of k is understood to be zero. If the initial value x0 is not given, it is chosen as the zero vector. The inputs u(i) are the columns of the U matrix, U=[u0,u1,...,un]. X is the matrix of outputs, so that X=[x(0),x(1),x(2),...,x(n)], and xf is the vector of the ﬁnal state xf=X[n+1]. If the system is given by a transfer function, then we can use rtitr with calling syntax [y]=rtitr(Num,Den,u [,up,yp]) Here Num and Den are polynomial matrices of dimensions n × m and n × n that d1are thei Di z numerator and denominator of the transfer function D−1 (z)N (z). If D(z) = i=0 d2 i and N (z) = i=0 Ni z , then D(z)y = N (z)u is interpreted as d1 i=0 Di y(t + i) = d2 Ni u(t + i) i=0 for t = 0, 1, . . . . It is assumed that Dd1 is nonsingular. The columns of u are the inputs of the system at t = 0, 1, ..., T . The outputs at t = 0, 1, . . . , T + d1 − d2 are the columns of the matrix y, that is, y=[y(0), y(1),...., y(T+d1-d2)]. up and yp deﬁne the initial conditions for −d1 < t < 0. The default values of up and yp are zero. The time response, or simulation, of the linear system (3.36) can be done using flts, which is called by either of [y [,x]]=flts(u,sl [,x0]) [y]=flts(u,sl [,past]) Here sl is a syslin list containing A, B, C, D or the information for the transfer function representation, u is the matrix (input vector), x0 is the initial state with default value = 0, past is the matrix giving the past with default value = 0. If one just wants the simulation of the output, then it is often easier to use dsimul, which provides state-space discrete-time simulation with syntax 98 3 Modeling and Simulation in Scilab y=dsimul(sl,u) If [A,B,C,D]=abcd(sl) and x0=sl(’X0’), then dsimul returns y=C*ltitr(A,B,u,x0)+D*u. With dsimul it is assumed that sl is in state-space form. 3.2.4 Diﬀerential Algebraic Equations Before giving the simulation tools for DAEs it is helpful ﬁrst to point out some of the requirements of these tools. With an ODE one had only to supply the initial value of y(t0 ). But a DAE needs information on both y(t0 ) and ẏ(t0 ) in order to uniquely determine the solution and get the integration started. Consider, for example, the implicit ODE tan(ẏ) = −y + g(t). This actually deﬁnes a family of diﬀerential equations ẏ = tan−1 (y + g) + nπ. Without a value for ẏ, the integrator would not be able to determine which family it should be integrating. There are a number of possibilities. A user might have a value of ẏ. Or one may have only an approximate value. Sometimes one has no idea at all. We return to this problem in Example 12. The simulation tools in Scilab use backward diﬀerentiation formulas (BDF). For example, a backward Euler’s method applied to (3.15) would take the form yn+1 − yn = 0. (3.37) F tn+1 .yn+1 , h Given yn , equation (3.37) must be solved by an iterative method. The Jacaobian with respect to yn+1 of (3.37) is Fy + h1 Fy . This accounts for the diﬀerent form that the input jac has in dassl below from what it had in some of the earlier simulation tools. Scilab provides the DAE simulation tool dassl, which is the DASSL code developed by Linda Petzold [11]. DASSL integrates general nonlinear fully implicit index-one DAEs. It can also be used on implicit ODEs. DASSL is called by [r [,hd]]=dassl(x0,t0,t [,atol,[rtol]],res [,jac] [,info] [,hd]) If y is n-dimensional, then r will have 2n + 1 rows. The ﬁrst row is the returned values of t. The next n rows are the corresponding values of y, and the last n rows are the corresponding values of ẏ. info is a list that passes information on to DASSL that is speciﬁc to the solution of index-one DAEs. It also provides some of the information provided by %ODEOPTIONS for ode. It will be described more carefully below. The inputs to dassl have the following meanings: x0: This can can be either y0, in which case ydot0 is estimated by dassl with zero as its ﬁrst estimate of ydot0, or x0 can be the matrix [y0, ydot0]. If ydot0 is provided, then F(t,y0,ydot0) must be equal to zero. If you know only an estimate of ydot0, then set info(7)=1. t0: This is the initial time. t: As with ode, the vector t gives the times that the solution is outputted. If you want the solution at every time step, then set info(2)=1. atol, rtol: As with ode, atol and rtol are real scalars or column vectors of the same size as y giving the absolute and relative error tolerances requested of the solution. res: This is an external as with impl. res computes the value of F(t,y,ydot). If res is a function, its calling sequence must be [r,ires]=res(t,y,ydot). res must return the residue r=g(t,y,ydot) and an error ﬂag ires. ires = 0 if res succeeds in computing r. ires =-1 if the residue is not deﬁned for the given values of (t,y,ydot). ires =-2 if the parameters are out of the admissible range. 3.2 Simulation Tools 99 jac: This is an external (function or list or string) that computes the value of the Jacobian dF/dy+cj*dF/dydot for a given value of the parameter cj. If jac is a function, its calling sequence must be r=jac(t,y,ydot,cj) and it must return r=dF(t,y,ydot)/dy+cj*dF(t,y,ydot)/dydot where cj is a real scalar. info: This is a list that contains 7 elements. Its default value is list([],0,[],[],[],0,0) info(1): real scalar that gives the maximum time for which F is allowed to be evaluated or an empty matrix [] if no limits are imposed for time. info(2): ﬂag that indicates whether dassl returns its intermediate computed values (flag=1) or only the user-speciﬁed time point values (flag=0). info(3): 2-component vector that gives the deﬁnition [ml,mu] of the band matrix computed by jac; r(i - j + ml + mu + 1,j) = "dF(i)/dy(j)+cj*dF(i)/dydot(j)". If jac returns a full matrix, set info(3)=[]. info(4): Real scalar that gives the maximum step size. Set info(4)=[] if no limitation. info(5): Real scalar that gives the initial step size. Set info(4)=[] if not speciﬁed. info(6): Set info(6)=1 if the solution is known to be nonnegative; otherwise set info(6)=0. info(7): Set info(7)=1 if ydot0 is just an estimate of y (t0 ). Set info(7)=0 if F(t0,y0,ydot0)=0. hd: Real vector that allows for storing the dassl information and to resume integration. r: This is a real matrix. Each column is the vector [t;x(t);xdot(t)], where t is the time for which the solution had been computed. Example 12. As an illustration of the use of dassl and also to point out the importance of the full initial conditions, we consider the implicit ODE tan(ẏ) = −y + 10t cos(3t), y(0) = 0. (3.38) Notice that nπ is a consistent initial value of ẏ(0) for any integer value of n. This reﬂects the fact that there are several solutions consistent with (3.38). When executed, the following script uses dassl to solve (3.38) for ẏ(0) = 0, ẏ(0) = π, and ẏ(0) = 2π and then plot them on the same axis. The resulting ﬁgure is Figure 3.8. x0=[0 0];x1=[0, %pi];x2=[0,2*%pi]; t0=0; tt=0:0.1:10; function [r,ires]=res(t,y,ydot) r=tan(ydot)+y-10*t*cos(3*t) ires=0 endfunction r0=dassl(x0,t0,tt,res); r1=dassl(x1,t0,tt,res); r2=dassl(x2,t0,tt,res); plot2d(r0(1,:),r0(2,:)); plot2d(r1(1,:),r1(2,:)); plot2d(r1(1,:),r1(2,:),style=-1); plot2d(r2(1,:),r2(2,:)); plot2d(r2(1,:),r2(2,:),style=-3); ← Fig. 3.8 100 3 Modeling and Simulation in Scilab 60 50 40 30 20 10 0 -10 0 1 2 3 4 5 6 7 8 9 10 Figure 3.8. Simulation of (3.38) using dassl for three values of ẏ(0). The fact that implicit ODEs and fully implicit DAEs can have multiple nearby solutions for the same value of the state has important consequences for the design of these integrators since this behavior shows up in applications. The integrators must try to ensure that when making a step they do not jump to another solution. This is accomplished by being conservative in choosing the size of the step. DAEs and Root-Finding Root-ﬁnding with DAEs is done with dasrt, which is dassl along with the ability to ﬁnd where the solution of the DAE crosses a surface. The calling sequence for dasrt is the same as for dassl except for two inputs, which are the same as for ode using the type “root.” The calling sequence of dasrt is [r,nn,[,hd]]=dasrt(x0,t0,t [,atol,[rtol]],res [,jac],ng, surf [,info] [,hd]) The diﬀerences from the calling of dassl are nn, which is a vector with two entries [times num]. times is the value of the time at which the surface is crossed, and num is the number of the crossed surface. surf is an external (function or list or string) that computes the value of the column vector surf(t,y) with ng components. Each component deﬁnes a surface. If surf is a function, its calling sequence must be surf(t,y). 3.2.5 Hybrid Systems Many systems have both continuous-time and discrete-time components. The simulation tool odedc is designed to integrate some of these models. It is assumed that there are continuous variables yc and discrete variables yd that can change at discrete times tk . yd is considered as a function of continuous time by making it piecewise constant on the intervals [tk , tk+1 [. The system that is integrated then takes the form 3.2 Simulation Tools ẏc (t) = f0 (t, yc (t), yd (t)), t ∈ [tk , tk+1 [, yd (tk+1 ) = f1 (t, yc (tk+1 ), yd (tk ) at t = tk+1 . 101 (3.39a) (3.39b) In a given subinterval [tk , tk+1 ], the software integrates (3.39a) up to time tk+1 using yd (t) = yd (tk ). Then it resets the value of yd using (3.39b) and then proceeds with integration at the next step. The parameters require that the tk be uniformly spaced, but they do not require that the discrete and continuous systems start at the same time. A special example of (3.39) is a sampled data system. In a sample data system the dynamics are given by a diﬀerential equation but the outputs occur at discrete times. A sampled data system could take the form ẏc (t) = f0 (t, yc (t), u(t)), t ∈ [tk , tk+1 [, yd (tk+1 ) = f1 (t, yc (tk+1 ), u(tk )) at t = tk+1 , (3.40a) (3.40b) where u is a control function. The calling sequence for odedc is yt=odedc(y0,nd,stdel,t0,t,f) Here the input terms of odedc have the following meaning: y0: This is a real column vector of initial conditions for both the continuous and discrete variables, y0=[y0c;y0d]. nd: This is the number of components in y0d. t0: This is the initial time for starting the simulation. stdel: This is a real vector with one or two entries, stdel=[h, delta]. If there is one entry, then delta=0. h is tk+1 − tk for k > 0. That is, h is the length of the time interval between events. The variable delta = delay/h, where delay is the length of time after t0 before the ﬁrst discrete event happens. t: This is instants where the solution is calculated and output. f: This is an external. If it is a function, it must have the calling sequence ycd=f(t,yc,yd,flag). This function must return the derivative of the vector yc if flag=0. That is, it evaluates the f0 function in (3.39a). If flag=1, then f returns the update of yd. That is, it provides the function f1 of (3.39b). yp: This must be a vector with the same dimension as yc if flag=0. yp must be a vector with the same dimension as yd if flag=1. y: This contains the values of the solution at times in t. odedc can be called with the same optional parameters as the ode function, provided nd and stdel are given in the calling sequence as second and third parameters. In particu- lar, integration ﬂags and tolerances can be set. Optional parameters can be set by the odeoptions function. As an example we consider the following example of a hybrid system: ẏ1 = −4y2 + (zk − 1)y1 + 2, y1 (0) = 1, ẏ2 = 4y1 + (zk − 1)y2 , y2 (0) = 1, zk+1 = −0.9zk + 1.1sgn(y1 (t)), z0 = 0, (3.41a) (3.41b) (3.41c) where t1 = 3.3 and tk+1 − tk = 3 for k > 0 and we wish to simulate it on the time interval from 0 to 20. We execute the following Scilab script: 102 3 Modeling and Simulation in Scilab function ycd=f(t,yc,yd,flag) if flag==0 then ycd(1)= -4*yc(2)+(yd-1)*yc(1) + 2; ycd(2)=4*yc(1)+(yd-1)*yc(2); else ycd=-0.9*yd+ 1.1*sign(yc(1)); end endfunction t0=0;y0=[1;1;0];nd=1;stdel=[3 1.1]; tt=0:0.1:20; yt=odedc(y0,nd,stdel,t0,tt,f); xbasc(); plot2d(tt’,yt’); ← Fig. 3.9 The result is the plot of the three components in Fig. 3.9. The graph of yd is given by the square wave form. The two components of yc are the wiggly curves that change their behavior each time that yd changes. 1.4 1.2 1.0 0.8 0.6 0.4 0.2 0.0 -0.2 -0.4 -0.6 0 2 4 6 8 10 12 14 16 18 20 Figure 3.9. Simulation of (3.41) using odedc. We conclude with an example of a sampled data system. Example 13. Suppose that we have started with the linear system 0.1 −1 1 ẋ = x+ u. 1 0.1 0 This system is unstable. A feedback control (3.42) 3.2 Simulation Tools u = −2 −1 x 103 (3.43) is designed to stabilize the system. The control is to be applied starting at time t = 15. Sometimes it is possible to measure the state at any time, but suppose that we are only able to determine the state every ρ time units starting at time t = 5. The following script when executed performs three simulations. The ﬁrst is using the continuous version of the control. The second and third are with ρ = 1 and ρ = 0.9. Figure 3.10 shows the eﬀect of the continuous-time feedback. Once the feedback is applied at t = 15 it quickly stabilizes the system. If the sampling is rapid enough, the result is essentially the same as using the continuous feedback. However, as Figure 3.11 shows, once the sampling period is ρ = 0.9, the system is still stabilized, but it takes much longer for the state to go to zero. Once the sampling period reaches 1, the control no longer stabilizes the system, as shown in Figure 3.12. function z=control(t,x) if t<15 then z=0, else z=-2*x(1)-x(2); end endfunction function xdot =f(t,x) xdot=[.1 -1;10.1]*x +[control(t,x);0] endfunction x0=[1;1]; tt=[0:0.1:40]; y=ode(x0,0,tt,f); xbasc(); plot2d(tt,y(1,:)); ← Fig. 3.10 xbasc() xd0=[1;1]; xc0=[0;0]; function ycd =fcd(t,yc,yd,flag) if flag==0 then ycd=[.1 -1;10.1]*yc +[control(t,yd);0], else ycd=yc; end endfunction yy=odedc([xd0;xc0],2,[1,5],0,tt,fcd); plot2d(tt,yy(1,:)); ← Fig. 3.12 xbasc() yy=odedc([xd0;xc0],2,[0.9,5/0.9],0,tt,fcd); plot2d(tt,yy(1,:)); ← Fig. 3.11 104 3 Modeling and Simulation in Scilab 6 4 2 0 -2 -4 -6 -8 0 5 10 15 20 25 30 35 40 Figure 3.10. x1 from simulation of Example 13 using continuous feedback. 6 4 2 0 -2 -4 -6 -8 0 5 10 15 20 25 30 35 40 Figure 3.11. x1 from simulation of Example 13 using sampled feedback with period ρ = 0.95. 3.2 Simulation Tools 105 1500 1000 500 0 -500 -1000 -1500 0 5 10 15 20 25 30 35 40 Figure 3.12. x1 from simulation of Example 13 using sampled feedback with period ρ = 1.0. 4 Optimization Various optimization problems play a fundamental role in modeling and simulation. This chapter will cover some of the optimization utilities available in Scilab. Section 4.1 will present some overview comments about optimization, useful in using the algorithms. Section 4.2 discusses the most general optimization utilities. There is a close relationship between optimization and solving equations in a least squares sense. This is examined in Section 4.4. One application of optimization is parameter ﬁtting. Specialized utilities for this are given in Section 4.5. Finally, some specialized optimization utilities for linear and quadratic programming are given in Section 4.6. 4.1 Comments on Optimization and Solving Nonlinear Equations In this chapter we discuss the problem of solving the minimization problem min f (x), x (4.1) where f is a real-valued function of the vector variable x. Note that maximizing f is the same as minimizing −f , so that all of the utilities given here can also be used with maximization problems. There are a number of characteristics of (4.1) that determine which, of any, of the available utilities will work. They also aﬀect how the optional parameters should be set if a simple call does not work. Constraints The ﬁrst question is whether there are any restrictions on x (4.1). if there are, the problem is said to be constrained. As far as Scilab is concerned there are three types of constraints. Bound, or box, constraints require diﬀerent entries of x to lie in speciﬁed intervals. For example, if x is 3 dimensional, we could have −2 ≤ x(1) ≤ 3 and 2 ≤ x(3) ≤ 4. Linear equality constraints take the form of b x − c = 0 when b and x are written as column vectors. Linear inequality constraints take the form b x − c ≤ 0 when b, x are written as column vectors. Finally there are the more general constraints of the form g(x) = 0. There are not currently any utilities in Scilab for solving general constrained optimal control problems. However, if the problem takes the form 108 4 Optimization minx f (x), g(x) = 0, (4.2a) (4.2b) and f, g are diﬀerentiable, then the solution x∗ of (4.2) satisﬁes the necessary conditions fx (x) + λT gx (x) = 0, (4.3a) g(x) = 0, (4.3b) where gx is the Jacobian of g, which is discussed below. It is sometimes possible to solve (4.3) using fsolve as discussed later in this chapter. The next question is whether f is a diﬀerentiable function and whether we know what its gradient is. If f is diﬀerentiable, then often some type of iterative method is used. Inside the iterative method at each iteration there is the determination of a search direction and a decision on how far to move in that direction. Then there are questions of how long the iteration will continue. Common choices are until f does not change much, until the gradient of f is small, or when the allowed number of iterations has been used. The last criterion can be important if function evaluations are computationally expensive. These types of iterative methods, when they ﬁnd a minimum, usually ﬁnd local minima. That is, they ﬁnd where f is smaller than at nearby values. Such local minima may or may not be a global minimum, that is, the place where f is smallest for all possible x. A diﬀerent type of algorithm is sometimes called a “dart-throwing algorithm.” These algorithms vary in philosophy, but basically they continue to evaluate throughout the region but take more values where the function appears small. While usually much slower than iterative methods when both can be used, the dart-throwing algorithms are more apt to ﬁnd a global minimum and multiple minimum values. Nonlinear Equation Solving A nonlinear equation takes the form f (x) = 0. (4.4) In the simplest case this represents n equations in n unknowns. Such equations are also solved by iterative methods. A key role is played by the Jacobian matrix J(x). If f has m entries and x has n entries, then ⎛ ∂f1 ∂f1 ⎞ ∂x1 · · · ∂xn ⎟ ⎜ J(x) = ⎝ ... . . . ... ⎠ . ∂fm ∂fm ∂x1 · · · ∂xn When m = n, and J(x) is invertible, the simplest iterative method is Newton’s method, which is xj+1 = xj − J(xj )−1 f (xj ). Actual solvers utilize a number of methods to increase the region of convergence and to provide estimates of J. 4.2 General Optimization The general optimization utility in Scilab is optim. This algorithm allows the user to have some control of the optimization process. It can accept bound constraints. optim needs to 4.2 General Optimization 109 know the gradient. If the gradient is unknown, or too complicated to code, then a utility is provided to enable optim to proceed using only the function to be minimized. This utility is NDcost, which stands for numerical diﬀerentiation of the cost. It approximates the gradient using ﬁnite diﬀerences. There are additional software programs whose distribution is maintained by the developer but for which there exist Scilab interfaces. For example, a toolbox providing an interface for the general optimization code can be found at http://www.scilab.org/ under contributions, and then downloads, in the category “Optimization Tools.” In particular, there is an interface to FSQP that can handle more general types of constraints than optim can. We shall ﬁrst present the simpliﬁed use of optim and NDcost. We will then discuss some of the more general options. The simpliﬁed call is −→ [f,xopt]=optim(costf,x0) Here x0 is the initial guess for where the minimum occurs, f is the optimum value, and xopt is where this optimum value occurs. costf is a function that provides the value of the function to be minimized, the value of its gradient, and a variable that is used by the optimization routine that we will explain later. If costf is a Scilab function, it takes the form −→ [f,g,ind]=costf(x,ind) Of course, for a more complicated problem we may not be able to, or prefer not to, provide the gradient. Then we can use the NDcost function. In its simplest form the call to optim then takes the form −→ [f,xopt]=optim(list(NDcost,myf),x0) In using optim and providing the gradient, the initial guess x0 can be either a row or column and then the xopt will be the same. However, in using NDcost, the initial guess must be a column vector. Suppose that we wished to minimize the function f (x, y, z) = (x − z)2 + 3(x + y + z − 1)2 + (x − z + 1)2 . (4.5) Its gradient is ∂f ∂f ∂f , , ∇f = ∂x ∂y ∂z = [2(x − z) + 6(x + y + z − 1) + 2(x − z + 1), 6(x + y + z − 1), −2(x − z + 1) + 6(x + y + z − 1)] , (4.6) and we take an initial guess of x0 = [0, 0, 0]. The following script solves using the gradient and then solves the problem using optim. function z= myf(x) z=(x(1)-x(3))^2+3*(x(1)+x(2)+x(3)-1)^2+(x(1)-x(3)+1)^2 endfunction function z=myg(x) xs=x(1)+x(2)+x(3)-1; z=[2*(x(1)-x(3))+6*xs+2*(x(1)-x(3)+1), 6*xs,... -2*(x(1)-x(3))+6*xs-2*(x(1)-x(3)+1)] 110 4 Optimization endfunction function [f,g,ind]=costf(x,ind) f=myf(x);g=myg(x); endfunction x0=[0 0 0]; [fopt,xopt]=optim(costf,x0); [fopt,xopt]=optim(costf,x0’); [fopt,xopt,gopt]=optim(list(NDcost,myf),x0’); ← initial condition ← x0 a row vector ← x0 a column vector ← x0 must be a column When executed, after listing the function deﬁnitions, the output gives the results: −→ x0=[0 0 0]; −→ [fopt,xopt]=optim(costf,x0) xopt = ! 0.0833333 fopt = 0.3333333 ← initial condition ← x0 a row vector 0.5833333 ! 0.5 −→ [fopt,xopt]=optim(costf,x0’) xopt = ← x0 a column vector ! ! ! 0.0833333 ! 0.3333333 ! 0.5833333 ! fopt = 0.5 −→ [fopt,xopt,gopt]=optim(list(NDcost,myf),x0’) gopt = ← x0 must be a column ! 0. ! ! 0. ! ! - 1.833D-11 ! xopt = ! ! ! 0.0833333 ! 0.3333333 ! 0.5833333 ! fopt = 0.5 In more complicated problems the user often has to set limits on how many iterations are taken in order to control the time of computation. In these cases the value of the gradient is very useful information, since if we are at a local optimum of an unconstrained smooth problem, then the gradient should be zero. Note that in the NDcost we get a numerical estimate of the gradient and it is close to zero. Requesting the value of the gradient at the optimal point is a general option with optim, as we will now show. 4.2 General Optimization 111 For more complicated problems, the user will want to use some of the options of optim, which we will now describe. The full calling format is [f,[xopt,[gradopt,[work]]]]=optim(costf,[contr],x0,[’algo’],.... [df0,[mem]],[work],[stop],[’in’],[imp=iflag]) These parameters are as follows. Using some of them requires knowledge about the iterative numerical methods used. • costf: An external, Scilab function, list, or string (costf is the cost function: see its calling sequence below (Scilab, C, or Fortran)). x0: real vector (initial value of variable to be minimized). • f: Value of optimal cost. • xopt: Best value of x found. • contr: The three entries ’b’,binf,bsup. binf and bsup are real vectors with the same dimension as x0. binf and bsup are lower and upper bounds on x. • "algo": Determines the algorithm. ’qn’ is quasi-Newton, which is the default. ’gc’ is conjugate gradient. ’nd’ stands for nondiﬀerentiable. Use this if there are points where the cost function does not have a derivative. An example is abs, which is not diﬀerentiable at zero. Note that ’nd’ does not accept bounds on x. • df0: Real scalar giving the guessed decrease of f at ﬁrst iteration. df0=1 is the default value. It aﬀects the eﬃciency of the line-search algorithm. • mem: An integer giving number of variables used to approximate the Hessian when algo=’gc’ or ’nd’. Default value is around 6. • stop: A sequence of optional parameters controlling the convergence of the algorithm. stop= ’ar’,nap, [iter [,epsg [,epsf [,epsx]]]], where – "ar": Reserved keyword for stopping rule selection deﬁned as follows: – nap: Maximum number of calls to costf allowed. – iter: Maximum number of iterations allowed. – epsg: Threshold on gradient norm. – epsf: Threshold controlling decreasing of f. – epsx: Threshold controlling variation of x. This vector (possibly matrix) of same size as x0, can be used to scale x. • "in": Reserved keyword for initialization of parameters used when costf is given as a C or Fortran routine. • "imp=iflag": Named argument used to set the trace mode. If iflag=0, then nothing except errors are reported. If iflag=1, then there are initial and ﬁnal reports. If iflag=2, then there is an additional report per iteration. iflag>2 adds reports on linear search. Most of these reports are written on the Scilab standard output, so if there are many iterations involving vectors, there could be a lot of output. • gradopt: Gradient of costf at xopt. • work: Working array for hot restart for quasi-Newton method. This array is automatically initialized by optim when optim is invoked. It can be used as an input parameter to speedup the calculations. The costf format is described above. The variable ind is used by optim. On output, ind<0 means that f cannot be evaluated at x and ind=0 interrupts the optimization. If costf is a character string, it refers to the name of a C or Fortran routine, which must be linked to Scilab. The generic calling sequence for the C or Fortran routine is the function costf(ind,n,x,f,g,ti,tr,td). Details on these additional variables are given in the on-line help. 112 4 Optimization 4.3 Solving Nonlinear Equations The theory and algorithms for solving a nonlinear system of equations (4.7) fct(x)=0 intertwines in a number of ways with those for optimization. Assuming that f is diﬀerentiable, the primary Scilab utility is fsolve. It has the calling sequence [x [,v [,info]]]=fsolve(x0,fct [,fjac] [,tol]) and uses the Powell hybrid method. fsolve is based on the package MINPACK [19]. The simplest calls need only • x0: Real vector that is the initial value of the function argument. • fct: External providing function in (4.7) The optional parameters are • fjac: External providing the Jacobian of fct. • tol: Real scalar, which is a precision tolerance. Termination occurs when the algorithm estimates that the relative error between x and the solution is at most tol. Default value is tol=1.d-10. • x: Final value of function argument. Estimated solution of (4.7). • v: Value of function fct at x. Should be close to zero if a solution has been found. • info: Indicates why termination occurred. – 0: improper input parameters. – 1: algorithm estimates that the relative error between x and the solution is at most tol. – 2: number of allowed calls to fct reached. – 3: tol is too small. No further improvement in the approximate solution x is possible. – 4: iteration is not making good progress. The simplest calling sequence for fct is [v]=fct(x). If fct is a character string, it refers to a C or Fortran routine, which must be linked to Scilab. Fortran calling sequence must be fct(n,x,v,iflag) integer n,iflag double precision x(n),v(n) and the C Calling sequence must be fct(int *n, double x[],double v[],int *iflag) As an illustration of using fsolve we consider the problem of where min f (x, y, z) = (x − z)2 + 3(x + y + z − 1)2 + (x − z + 1)2 , (4.8) g(x, y, z) = 1 − x − 3y − z 2 = 0. (4.9) As noted earlier, the necessary condition for the minimum of this problem is that it be a solution of ∇f + λ∇g = 0, g = 0. If we execute the next script: (4.10) (4.11) 4.4 Nonlinear Least Squares 113 −→ function z=fct(x) −→ xs=x(1)+x(2)+x(3)-1; −→ w1=[2*(x(1)-x(3))+6*xs+2*(x(1)-x(3)+1),6*xs,... −→ -2*(x(1)-x(3))+6*xs-2*(x(1)-x(3)+1)] −→ w2=[-1 -3 -2*x(3)] −→ z=[w1’+x(4)*w2’;1-x(1)-3*x(2)-x(3)^2] −→ endfunction −→ x0=[0 0 0 0]; −→ [x,v]=fsolve(x0,fct); ← initial condition we get −→ v ans ← value of function at x = 1.0D-16 * ! 0.0016752 −→ x ans ! 0.0050255 0.0023359 - 1.6653345 ! ← solution = 0.1972244 0.1055513 0.6972244 - 1.675D-19 ! Since v= 0, we have found a solution given by x. 4.4 Nonlinear Least Squares Suppose that y = f (x) is a function from the space of n-dimensional vectors x to that of m-dimensional vectors y. Sometimes it may not be possible to solve f (x) = 0. The best that we can hope for is to ﬁnd an x that makes mf (x) as small as possible. That is, you want x that minimizes f (x)2 = f (x) f (x) = i=1 fi (x)2 . Such a solution is called a least squares solution. Notice that this allows for m > n. That is, there are more equations than there are unknowns. This situation is important for a number of applications including parameter ﬁtting. Scilab provides two utilities for nonlinear least squares. They are leastsq and lsqrsolve. leastsq The calling format of leastsq is much like that of optim except that we give the function f and optionally its Jacobian. The algorithm then computes the cost function f f and its gradient. The only other diﬀerence is that the parameters are in a diﬀerent order than with optim. Thus the short calling format is [f,xopt]=leastsq([imp,] fun [,Dfun],x0) The complete format is [f,[xopt,[gradopt]]]=leastsq(fun [,Dfun],[contr],x0,[’algo’],... [df0,[mem]],[stop],[’in’]) 114 4 Optimization Here fun is an external, i.e., a Scilab function or string deﬁning the least squares problem. Functions specialized for parameter ﬁtting will be given shortly, but to illustrate the use of leastsq, suppose that we have the data points {(0, 0), (0, 1), (1, 1, ), (2, 1.5), (2, 2)} and we want to ﬁnd parameters a, b, c such that y = aebt + c ﬁts these data as well as possible. If the unknown parameters are called p = (a, b, c) and the data points (ti , yi ), then we can solve in the least squares sense the ﬁve equations given by yi −p1 ep2 ti −p3 = 0. When executed, the following Scilab script ﬁnds p and plots in Figure 4.1 both the data points and the curve. DAT=[0 0;0 1;1 1;2 1.5; 2 2]; function z=fun(p) DAT=[0 0;0 1;1 1;2 1.5; 2 2] z=DAT(:,2)-p(1)*exp(p(2)*DAT(:,1))-p(3) endfunction ← initial value p0=[0 0 0]; ← ff value of the function at p [ff,p]=leastsq(fun,p0) p = ! ff 1.0000000 = 0.4054651 - 0.5000000 ! 0.625 xbasc(); plot2d(DAT(:,1),DAT(:,2),-3,rect=[-0.5,-0.5,2.5,2.5]) t=linspace(-0.5,2.5,50); et=p(1)*exp(p(2)*t)+p(3); plot2d(t,et,rect=[-0.5,-0.5,2.5,2.5]) ← Fig. 4.1 lsqrsolve This program minimizes the sum of the squares of nonlinear functions, using the Levenberg– Marquardt algorithm. The long and short forms of the calling sequence are [x [,v [,info]]]=lsqrsolve(x0,fct,m [,stop [,diag]]) [x [,v [,info]]]=lsqrsolve(x0,fct,m ,fjac [,stop [,diag]]) depending on whether the Jacobian is supplied. Here • • • • x0: Real vector that is initial value of functions argument. fct: An external that deﬁnes the equations. m: Integer that is the number of functions. fjac: External that is the Jacobian of fct. 4.4 Nonlinear Least Squares 115 2.5 2.0 1.5 1.0 0.5 0.0 -0.5 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 Figure 4.1. Data points and curve found by leastsq. • stop: Optional vector [ftol,xtol,gtol,maxfev,epsfcn,factor] whose default value is [1.d-8,1.d-8,1.d-5,1000,0,100]. Controls the iterations. – ftol: A positive real number. Termination occurs when both the actual and predicted relative reductions in the sum of squares are at most ftol. ftol measures the relative error desired in the sum of squares. – xtol: A positive real number. Termination occurs when the relative error between two consecutive iterates is at most xtol. Thus xtol measures the relative error desired in the approximate solution. – gtol: A nonnegative input variable. Termination occurs when the cosine of the angle between fct(x) and any column of the Jacobian is at most gtol in absolute value. gtol measures the orthogonality desired between the function vector and the columns of the Jacobian. – maxfev: A positive integer. Termination occurs when the number of calls to fcn is at least maxfev by the end of an iteration. – epsfcn: A positive real number, used in determining a suitable step length for the forward-diﬀerence approximation. This approximation assumes that the relative errors in the functions are of order epsfcn. if epsfcn is less than the machine precision, it is assumed that the relative errors in the functions are on the order of the machine precision. – factor: A positive real number, used in determining the initial step bound. This bound is set to the product of factor and the Euclidean norm of diag*x if nonzero, or else to factor itself. In most cases factor should lie in the interval (0.1, 100), and 100 is a generally recommended value. • diag: This is an array of length n. diag must contain positive entries that serve as multiplicative scale factors for the variables. • x: Final value of function argument, which is the estimated solution. • v: Value of functions at x, also called the residual. • info: Indicates cause of termination. – 0: improper input parameters. 116 4 Optimization – – – – 1: algorithm estimates that the relative error between x and the solution is at most tol. 2: maximum number of calls to fcn has been reached. 3: tol is too small. No further improvement in the approximate solution x is possible. 4: iteration is not making good progress. The functions describing the equations fct and their Jacobian fjac are of the same form. Letting fn stand for either fct or fjac, the allowable formats are • A Scilab function whose calling sequence is v=fn(x,m) given x and m. • A character string that refers to a C or Fortran routine that must be linked to Scilab. Should return −1 in iflag to stop the algorithm if the function or Jacobian could not be evaluated – Fortran calling sequence should be fct(m,n,x,v,iflag), where m, n, iflag are integers, x is a double-precision vector of size n and v a double-precision vector of size m. – C calling sequence should be fct(int *m, int *n, double x[],double v[],int *iflag) Suppose that we wish not to use leastsq on our previous example, but wish to use lsqrsolve. The format of the function is slightly diﬀerent due to the parameter m, but we can reuse our previous function by using the following script. DAT=[0 0;0 1;1 1;2 1.5; 2 2]; function z=fun(p) DAT=[0 0;0 1;1 1;2 1.5; 2 2] z=DAT(:,2)-p(1)*exp(p(2)*DAT(:,1))-p(3) endfunction p0=[0 0 0]; function z=fct(p,m) z=fun(p) endfunction [p,v]=lsqrsolve(p0,fct,5); xbasc(); plot2d(DAT(:,1),DAT(:,2),-3,rect=[-0.5,-0.5,2.5,2.5]) t=linspace(-0.5,2.5,50); et=p(1)*exp(p(2)*t)+p(3); plot2d(t,et,rect=[-0.5,-0.5,2.5,2.5]) ← Fig. 4.2 Note that we are using the same equations and the same initial guess as before. The value of p is completely diﬀerent from the one we found before. −→ p p = ! - 4154.231 - 0.0001505 4154.706 ! 4.5 Parameter Fitting 117 −→ v’ ans = ! - 0.4749930 0.5250070 - 0.1000375 - 0.2249880 0.2750120 ! However, the value of the norm squared is −→ v’*v ans = ← norm squared 0.6375094 which compares favorably with the 0.625 found by the earlier method. Looking at the graph and the data, we get Figure 4.2. 2.5 2.0 1.5 1.0 0.5 0.0 -0.5 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 Figure 4.2. Data points and curve found by lsqrsolve. The example of this section illustrates that problems can sometimes have several solutions, even when that is not obvious, and that diﬀerent numerical methods, starting with the same starting value, can lead to very diﬀerent solutions. 4.5 Parameter Fitting We gave an example of data ﬁtting in the previous section. But Scilab has some utilities that are especially designed for data ﬁtting. We discuss here datafit, which is an improved version of the older routine fit_dat. For a given function G(p, z), datafit ﬁnds the best vector of parameters p for approximating n G(p, zi ) = 0 for a set of measurement vectors zi . The vector p is found by minimizing i=1 G(p, zi ) W G(p, zi ). That is, p is the solution of a 118 4 Optimization weighted least squares problem. The most common use of W is to make some measurements more important than others. This utility uses optim, so the full calling sequence has many of the same optional variables. The simplest call to datafit requires only the form of the equation G, the data, and a starting guess for the parameters p. In this case the calling sequence takes the form −→ [p,err]=datafit(G,Z,p0) Let ne, np, nz be the number of equations in G, the number of parameters in p, and the size of a single observation (dimension of z) respectively. Then • G is a function describing e=G(p,z). • Z is the matrix of observations [z_1,z_2,...,z_n], where each measurement is a column vector. • p0 is the initial guess for the parameters. For example, if we wished to ﬁt the data of the example in the last section, we could use the following Scilab script: −→ Z=[0 0 1 2 2;0 1 1 1.5 2]; −→ function e=G(p,z) −→ e=z(2)-p(1)*exp(p(2)*z(1))-p(3) −→ endfunction −→ p0=[0 0 0]’; Note that the initial p0 had to be put in as a column vector. When this script is executed we get the output −→ [p,err]=datafit(G,Z,p0); which is the same as that given by leastsq in the last section. We saw in the last section that there can be more than one solution to a parameterﬁtting problem. It can be important to bound the parameters to avoid values that are not physically correct. Also, sometimes some data are more important than other data. In this case one wants to weight the data. Sometimes the default values of parameters do not enable the algorithm to converge. These and other considerations sometimes require access to more of the parameters inside the algorithm. The full calling sequence, which shares a number of variables with optim, is [p,err]=datafit([imp,] G [,DG],Z [,W],[contr],p0,[algo],[df0,[mem]], [work],[stop],[’in’]) These parameters are as follows: • imp: Scalar argument used to set the trace mode. imp=0 means that nothing (except errors) is reported. imp=1 means initial and ﬁnal reports, imp=2 adds a report per iteration and imp>2 adds reports on linear search. Note that most of these reports are written on the Scilab standard output. • DG: Partial of G with respect to p. In the form S=DG(p,z), so that S is ne×np. • W: Weighting matrix of size ne×ne. Default is the identity. 4.6 Linear and Quadratic Programming 119 • "in": Reserved keyword for initialization of parameters used when G is given as a Fortran or C routine. • p: Optimal solution found, given as a column vector. • err: Least squares error. All the remaining parameters are identical to those in the general call to optim. 4.6 Linear and Quadratic Programming 4.6.1 Linear Programs The basic linear programing problem is to minimize p x subject to linear constraints. These linear constraints can be equality or inequality constraints. The inequality constraints can be one-sided or bound constraints. The Scilab function to solve these problems is linpro. The general form of the problem is minx p x, C1 x ≤ b1 , (4.12a) (4.12b) ci ≤ x ≤ cs , C2 x = b2 . (4.12c) (4.12d) The form of the calling sequence depends on how many of these types of constraints are present. The calling sequences are [x,lagr,f]=linpro(p,C,b [,x0]) [x,lagr,f]=linpro(p,C,b,ci,cs [,x0]) [x,lagr,f]=linpro(p,C,b,ci,cs,me [,x0]) [x,lagr,f]=linpro(p,C,b,ci,cs,me,x0 [,imp]) The ﬁrst three of these correspond to having the constraints (4.12b), (4.12b) and (4.12c), (4.12b)–(4.12d) respectively. Note that if there are no constraints at all, then the problem has no ﬁnite solution if p is not the zero vector. These parameters have the following meaning: • p: Real column vector of dimension n that contains the coeﬃcients of the linear cost function. • C: Real (me + md)×n matrix. If no constraints are given, that is, all constraints are of the form (4.12c), you can set C = []. Equality constraints are listed ﬁrst, so that if both (4.12b) and (4.12d) are present, we have C(j,:)x = b(j) for j=1,...,me and C(j,:)x <= b(j), for j=me+1,...,me+md. • b: (me + md)-dimensional column vector that is the right hand of the constraints in (4.12b), (4.12d). If no constraints are given, then you can set b = []. • ci: n-dimensional column vector of lower bounds. If there are no lower bound constraints, put ci = []. If some components of x are bounded from below, set the other (unconstrained) values of ci to a negative number that has very large magnitude. That is, ci(j)=-number_properties(’huge’). • cs: Column vector of upper bounds. See comments for ci. • me: Number of equality constraints. Thus C(1:me,:)*x = b(1:me). • x0: Either an initial guess for x or one of the character strings ’v’ or ’g’. If x0=’v’, then the calculated initial feasible point is a vertex. If x0=’g’, then the calculated initial feasible point need not be a vertex. 120 4 Optimization • imp: This is the verbose option. (Try imp= 7, 8 . . .). Note that the messages are sent to Scilab standard output (can be lengthy if there are many iterations). • x: Optimal solution found by algorithm. • f: Optimal value of the cost function (4.12a). • lagr: This is a vector of Lagrange multipliers. If lower and upper-bounds ci,cs are provided, lagr has n+me+md components and lagr(1:n) is the Lagrange vector associated with the bound constraints and lagr(n+1: n+me+md) is the Lagrange vector associated with the linear constraints. If an upper bound (respectively lower bound) constraint i is active, then lagr(i) is greater than 0 (resp. <0). If no bounds are provided, then lagr has only me+md components. 4.6.2 Quadratic Programs A quadratic program is the same as (4.12) except that the cost function (4.12a) is replaced by the quadratic expression 1 x Qx + p x. (4.13) 2 The utility provided by Scilab is quapro. Since quapro does not require Q to be positive deﬁnite, in fact Q could be zero, quapro can solve linear programming problems. With the exception of the matrix Q, which must be real symmetric, the calling sequence for quapro is identical to that of linpro. Since the variables have the same meanings, we do no repeat their deﬁnitions here. The possible calling sequence for quapro are [x,lagr,f]=quapro(Q,p,C,b [,x0]) [x,lagr,f]=quapro(Q,p,C,b,ci,cs [,x0]) [x,lagr,f]=quapro(Q,p,C,b,ci,cs,me [,x0]) [x,lagr,f]=quapro(Q,p,C,b,ci,cs,me,x0 [,imp]) 4.6.3 Semideﬁnite Programs There is also a Scilab function for solving semideﬁnite programs called semidef: [x,Z,ul,info]=semidef(x0,Z0,F,blck_szs,c,options) However, we shall not discuss it here. The interested reader is referred to the on-line help. 4.7 Diﬀerentiation Utilities The function NDcost was introduced earlier to enable the user to solve some problems without having to specify the gradient. Other utilities described earlier will automatically estimate derivatives or Jacobians numerically when this is requested. However, it is sometimes useful to be able to get numerical estimates of derivatives and Jacobians directly. This capability is provided by numdiff and derivative. The Scilab function numdiff computes a numerical estimate of the Jacobian using ﬁnite diﬀerence methods. If the function has real-number values, then it gives an estimate of the gradient. Its calling sequence is g=numdiff(fun,x [,dx]) if there are no parameters in fun. The parameters in numdiff are 4.7 Diﬀerentiation Utilities 121 • fun: An external describing the function to be diﬀerentiated. The function fun calling sequence must be y=fun(x,p1,p2,...,pn), where the pi are optional parameters. If parameters p1,p2,...,pn exist, then numdiff can be called by g=numdiff(list(fun,p1,p2,...,pn),x). • x: Vector argument of the function fun. • dx: Vector, which is the ﬁnite diﬀerence step. Default value is dx=sqrt(%eps)*(1+1d-3*abs(x)) • g: Vector (matrix), which is the estimated gradient (Jacobian). For example, suppose that we want the Jacobian (derivative) of ⎛ ⎞ 1 2 x1 + 2x2 x3 f (x1 , x2 , x3 ) = at x = ⎝2⎠ . sin(x1 x2 x3 ) 3 (4.14) The following Scilab script, when executed, computes the estimate of the Jacobian given by numdiff, the true Jacobian, and their diﬀerence. function z=myf(x) z=[x(1)+2*x(2)-x(2)^2*x(3);sin(x(1)*x(2)*x(3))] endfunction x=[1;2;3]; J=numdiff(myf,x); TrueJ=[1,2-2*x(2)*x(3), -x(2)^2]; a=cos(x(1)*x(2)*x(3)); TrueJ=[TrueJ;a*[x(3)*x(2),x(1)*x(3),x(1)*x(2)]]; Difference=J-TrueJ; The solution is −→ J J = ! ! 1.0000000 5.7610218 - 10. 2.8805109 - 4. ! 1.9203406 ! - 10. 2.8805109 - 4. ! 1.9203406 ! - 4.045D-08 5.690D-08 3.518D-08 ! - 1.100D-08 ! −→ TrueJ TrueJ = ! ! 1. 5.7610217 −→ Difference Difference = ! - 1.286D-08 ! 1.157D-07 122 4 Optimization 4.7.1 Higher Derivatives Sometimes it is desirable to have higher derivatives. Numerical diﬀerentiation is prone to ill conditioning and error and this is especially true with the higher derivatives. If highaccuracy higher derivatives are required, the best alternatives are symbolic diﬀerentiation using a symbolic language such as Maple or automatic diﬀerentiation using a package such as ADOL-C. [24] For complicated problems the symbolic approach can be very slow and have memory problems. The only choice then may be automatic diﬀerentiation. If the needed derivatives are limited to ﬁrst and second order, then useful approximate derivatives can often by gotten by using the Scilab function derivative. Suppose that we have a function such as (4.15) of an n-dimensional vector x with values that are m-dimensional vectors. Then at a point a it has a Taylor expansion ⎞ ⎞ ⎛ ⎛ f1 (x) (x − a) H1 (a)(x − a) ⎟ ⎟ ⎜ ⎜ .. f (x) = ⎝ ... ⎠ = f (a) + J(a)(x − a) + ⎝ (4.15) ⎠ + ··· . . fm (x) (x − a) Hm (a)(x − a) Thus the ﬁrst derivative is just the m × n Jacobian matrix J. However, the second derivative is now m matrices each of which is n × n. The ﬁrst and second derivatives of f are provided by derivative. This utility provides several choices on how to arrange the information on the second derivative by use of the H_form variable. The full calling syntax is [J [,H]] = derivative(F,x [,h ,order ,H_form ,Q]) • F: Scilab function F or a list(F,p1,...,pk), where F is a Scilab function in the form y=F(x,p1,...,pk), with p1, ..., pk being any Scilab objects such as matrices or lists. • x: real column vector of dimension n. • h: Step size used in the ﬁnite diﬀerence approximations. Usually better to leave to software to choose. • order: Integer that is the order of the ﬁnite diﬀerence formula used to approximate the derivatives. Default is order= 2. May be chosen as 1, 2, or 4. • H_form: String giving the form in which the Hessian will be returned. – H_form=’default’: H is an m×(n^2) matrix. Here the kth row of H corresponds to the Hessian of the kth component of F. – H_form=’blockmat’: In this format H is a (m×n)×n block matrix . Thus, in Scilab notation, H = [H1;H2; ... ;Hm]. – H_form=’hypermat’: H is a n×n matrix for m=1, and a n×n×m hypermatrix otherwise. That is, H(:,:,k) is the classical Hessian matrix of the kth component of F. • Q: Real orthogonal matrix. Default is eye(n,n). The Jacobian is computed by approximating the directional derivatives of the components of F in the direction of the columns of Q. The second derivatives are computed by composition of ﬁrst-order derivatives. Numerical approximation of derivatives is generally an unstable process. The step size h must be small to get a low error, but if it is too small, ﬂoating-point errors will dominate by cancellation. A rule of thumb is not to change the default step size. To work around numerical diﬃculties one may also change the order and/or choose diﬀerent orthogonal matrices Q (the default is eye(n,n)), especially if the approximate derivatives are used in optimization routines. All the optional arguments may also be passed as named arguments. As an illustration suppose that myf is the same function as in the previous example and that it and x are already in the workspace. Then the second derivative will have two 3 × 3 matrices. If we choose ’hypermat’ for the output format, then we will obtain 4.7 Diﬀerentiation Utilities −→ function z=myf(x) −→ z=[x(1)+2*x(2)-x(2)^2*x(3);sin(x(1)*x(2)*x(3))] −→ endfunction −→ x=[1;2;3]; −→ [J,H]=derivative(myf,x,H_form=’hypermat’) H = (:,:,1) ! 0. 0. ! 0. - 6. ! 0. - 4. (:,:,2) 0. ! - 4. ! 0. ! ! ! ! J 10.058956 7.9099883 5.2733256 = 7.9099883 2.5147394 2.6366631 5.2733256 ! 2.6366631 ! 1.117662 ! ! ! 1. 5.7610217 - 10. 2.8805109 - 4. ! 1.9203406 ! 123 5 Examples Up to this point we have deliberately kept most of our examples concise in order to focus on learning Scilab and also to illustrate how simple many types of calculations are with Scilab. However, real scientiﬁc inquiries are often more complicated, involve several kinds of software, and have several goals. In this chapter we provide a few more elaborate application examples to illustrate how Scilab can be used within this more complicated environment of investigation. (x3 , y3 ) y (x2 , y2 ) θ3 g (x1 , y1 ) θ2 θ1 (x0 , y0 ) x Figure 5.1. 3-link pendulum. 5.1 Modeling and Simulation of an N -Link Pendulum Our ﬁrst application will be the modeling and simulation of a pendulum with N links. With this problem we shall illustrate how a symbolic manipulation package, in this case Maple, can be used to derive the equations of motion of a multibody system and simulate the result in Scilab. This is done using the Lagrangian approach. The N -link pendulum is a well-known problem in numerical analysis. For large N it is used as a test problem for software used for molecular dynamics simulation. In this case it is very important to know whether energy is conserved. 126 5 Examples The N -link pendulum also arises as a model for ﬂexible cables and chains. Several years ago there was a collapse of several towers carrying power. It turned out that the power lines were supported by pairs of chains of insulators and one of these chains had failed and the other was unable to continue to support the power line. In the followup studies, the insulator chains were modeled as 16-link pendulums. 5.1.1 Equations of Motion of the N -Link Pendulum In this application we wish to simulate the motion of an N -link pendulum ﬁxed at one extremity and moving without friction in a vertical plane. As an example, and for ﬁxing notation, a picture of a 3-link pendulum is given in Figure 5.1. The equation of motion can easily be deduced from a Lagrangian formulation for a 1- or 2-link pendulum but it can become tedious work for say a 10-link pendulum in the Scilab demos. In order to construct models with large N , we use the computer algebra package Maplein order to derive the equations of motion from a Lagrangian formulation. The computer algebra package generates Fortran code to be used by Scilab for simulation. It also provides LATEX code for symbolic expressions at every step of the computation. The N -link pendulum is considered as N independant connected rigid bodies. The Maple code is separated into two parts. The ﬁrst part provides a general library for Lagrangian modeling of rigid bodies linked with holonomic constraints, and the second part applies the general code to the speciﬁc example of the N -link pendulum. By setting the code up in this manner, we have made it easier for the reader to modify the pendulum problem, say by adding masses at the end or even modeling a completely diﬀerent mechanical system. It should be noted that the kind of constraints we have here are called holonomic. Constraints involving velocities are called nonholonomic and are not considered here. Lagrangian of Multibody Systems with Holonomic Constraints Let q be the generalized coordinates and q̇ the generalized velocities of the system of N rigid bodies. The coordinates q can vary with time t. Then the Lagrangian of the problem can be expressed as N Li (q, q̇), (5.1) L(q, q̇) = i=1 where Li is the Lagrangian of body i. For each component of q we have to compute I(q, q̇, q̈)i , which is deﬁned by the Euler equation d ∂L ∂L − i. I(q, q̇, q̈)i = i dt ∂ q̇ ∂q Readers familiar with the calculus of variations or optimal control will recognize this as the usual term that arises in the necessary conditions for a minimum. This formal expression for I(q) can be rewritten in matrix notation as I(q, q̇, q̈) = M (q)q̈ + R(q, q̇). (5.2) If there are no constraints or outside forces, then (5.2) is set equal to zero, and we get the usual second-order equation of motion, which says that mass times acceleration is equal to the total force. 5.1 Modeling and Simulation of an N -Link Pendulum 127 So far we have assumed that the rigid bodies were independent. We now have to deal with the mechanical constraints of holonomic type expressing the fact that we have a linked pendulum. These constraints can be written as A(q) = 0. Note that the time derivative of the constraints gives the equation B(q)q̇ = 0, where B(q) = dB dq , that is, B(q)i,j = ∂A(q)i ∂qj . Thus q̇ has to be in the kernel of B(q). We can then try to compute a formal basis of the kernel of B(q). This yields a matrix S(q) whose columns span the kernel of B(q). Then we can introduce a time function η such that q̇ = S(q)η. Note that by deﬁnition we have B(q)S(q)η = 0. (5.3) We return now to the Euler equations. Using the constraints and their associated Lagrange multipliers, the equations can be expressed as I(q, q̇, q̈) = B(q)T λ. (5.4) We use S(q) to eliminate the right-hand side of (5.4) by left multiplying by S(q)T and simplifying the result using q̇ = S(q)η and its time derivative, which can be expressed as q̈ = W (q, η, η̇). We ﬁnally obtain 0 = S(q)T I(q, q̇, q̈) = M1 (q)η̇ + R1 (q, η). The dynamical system with state (q, η) to be simulated is then M1 (q)η̇ + R1 (q, η) = 0, q̇ − S(q)η = 0. (5.5a) (5.5b) Note that in this last equation the constraint A(q) = 0 is seen only through its time derivative. This raises the possibility that during simulation there may be numerical errors in the constraints. In the numerical analysis literature this is called constraint drift. However, we will see that in the example of the N -link pendulum, we can simplify the equations in order to avoid the numerical drift in the constraint A(q) = 0. When it is not possible to ﬁnd a formal basis of the null space of B(q), it is often possible to diﬀerentiate the holonomic constraint twice, which leads to the constraint B(q)q̈ + Bq (q)q̇ = 0. Thus we have A(q) = 0, B(q)q̇ = 0, and B(q)q̈ + Bq (q)q̇ = 0. These three constraints can be replaced by a unique constraint [9] −B(q)q̈ + R2 (q, q̇) = 0 with R2 (q, q̇) = −Bq (q)q̇ + α (B(q)q̇) + βA(q) = 0. This replacement process is known as stabilized diﬀerentiation or Baumgarte stabilization. The new system to be simulated is then M (q)q̈ − B(q)T λ + R(q, q̇) = 0, −B(q)q̈ + R2 (q, q̇) = 0. (5.6a) (5.6b) 128 5 Examples At each integration step we need to solve a linear system to get (q̈, λ), and α and β are to be properly chosen so as to keep the error stabilized. A similar problem has been considered in [21] where the control of a bicycle was considered. Note that neither (5.5) nor (5.6) is an explicit ordinary diﬀerential equation. Depending on the invertibility of M1 and B they are either implicit ordinary diﬀerential equations or diﬀerential algebraic equations (DAE). There are two options. One is to try to integrate with a DAE solver such as dassl. Alternatively, one can try to reduce the system to an ordinary diﬀerential equation. We shall do the latter. The N -Link Pendulum as N Rigid Bodies with Holonomic Constraints We consider here an N -link pendulum that is ﬁxed at one end and moves without friction in a vertical plan as described in Figure 5.1. In this section we derive, with specialized Maple code, the equations of motion of the N -link pendulum. We will follow the steps of the previous section. In order to keep the output simple, we will use the simpler 2-link pendulum. But the reader should keep in mind that the equations are not manually written but rather are derived by a computer program. We ﬁrst generate the Lagrangian of the N -link pendulum by adding the Lagrangian of each rigid body. Let ri = li /2. For the 2-link pendulum this gives L(q, q̇) = J θ̇2 1 1 1 m1 r1 2 sin(θ1 )2 θ̇12 + r1 2 cos(θ1 )2 θ̇12 + − m1 g r1 sin(θ1 ) 2 2 2 2 J θ̇2 1 2 2 ẋ1 − r2 sin(θ2 ) θ̇2 + ẏ1 + r2 cos(θ2 ) θ̇2 + + m2 2 2 − m2 g (y1 + r2 sin(θ2 )) . Then we obtain the following expression for I(q, q̇, q̈): ⎛ ⎞ m2 ẍ1 − m2 r2 cos(θ2 ) θ̇22 − m2 r2 sin(θ2 ) θ̈2 ⎜ ⎟ m2 ÿ1 − m2 r2 sin(θ2 ) θ̇22 + m2 r2 cos(θ2 ) θ̈2 + m2 g ⎜ ⎟ 2 ⎜ ⎟ m1 r1 θ̈1 + J1 θ̈1 + m1 g r1 cos(θ1 ) ⎜ ⎟, ⎜ ⎟ 0 ⎜ ⎟ ⎝ ⎠ 0 −m2 r2 sin(θ2 ) ẍ1 + m2 r2 2 θ̈2 + m2 r2 cos(θ2 ) ÿ1 + J2 θ̈2 + m2 g r2 cos(θ2 ) which is rewritten as in (5.2) with ⎛ ⎞ m2 0 0 00 −α2 ⎜ 0 m2 ⎟ 0 00 β2 ⎜ ⎟ ⎜ 0 0 m1 r1 2 + J1 0 0 ⎟ 0 ⎟, M (q) = ⎜ ⎜ 0 0 ⎟ 0 00 0 ⎜ ⎟ ⎝ 0 0 ⎠ 0 00 0 2 0 0 0 m2 r2 + J2 −α2 β2 ⎛ ⎞ 2 −m2 r2 cos(θ2 )θ˙2 ⎜ ⎟ ⎜ m2 g − m2 r2 sin(θ2 )θ˙2 2 ⎟ ⎜ ⎟ ⎜ ⎟ m1 g r1 cos(θ1 ) R(q, q̇) = ⎜ ⎟, ⎜ ⎟ 0 ⎜ ⎟ ⎝ ⎠ 0 m2 g r2 cos(θ2 ) 5.1 Modeling and Simulation of an N -Link Pendulum 129 where βk = mk rk cos(θk ) and αk = mk rk sin(θk ). We have simpliﬁed the system by eliminating (x0 , y0 ) and (x˙0 , y˙0 ), since one end of the ﬁrst link is at a ﬁxed position. The constraints are x1 − 2r1 cos(θ1 ) = 0, y1 − 2r1 sin(θ1 ) = 0, (5.7a) (5.7b) x2 − x1 − 2r2 cos(θ2 ) = 0, y2 − y1 − 2r2 sin(θ2 ) = 0. (5.7c) (5.7d) In general we would have 2N constraints for a nN -link pendulum, which are given by A(q) = 0. Symbolic diﬀerentiation of A(q) by Maple leads to B(q): ⎞ ⎛ 0 1 0 2r1 sin(θ1 ) 0 0 ⎟ ⎜ 0 1 −2r1 cos(θ1 ) 0 0 0 ⎟. B(q) = ⎜ ⎝ −1 0 0 1 0 2r2 sin(θ2 ) ⎠ 0 −1 0 0 1 −2r2 cos(θ2 ) A symbolic computation for a basis of the kernel of B(q) gives ⎛ ⎞ −2 r1 sin(θ1 ) 0 ⎜ 2 r1 cos(θ1 ) ⎟ 0 ⎜ ⎟ ⎜ ⎟ 1 0 ⎜ ⎟. S(q) = ⎜ ⎟ sin(θ ) −2 r sin(θ ) −2 r 1 1 2 2 ⎟ ⎜ ⎝ 2 r1 cos(θ1 ) 2 r2 cos(θ2 ) ⎠ 0 1 As pointed out in the previous section we can thus introduce a time function η such that q̇ = S(q)η. Using the particular form of S(q), we note that the η variable has a physical interpretation as η = θ̇. Note also that S(q) is not unique and we have selected the S(q) for which we have η = θ̇. We use S(q) to eliminate the right-hand side of this equation by left multiplying by S(q)T and simplifying the result using q̇ = S(q)η. Using basic symbolic algebra, the obtained equation can be rewritten as M1 (q)η̇ + R1 (q, η) = 0, where M1 (q) = R1 (q, η) = m1 r1 2 + J1 + 4 r1 2 m2 2 r1 m2 r2 cos(θ1 − θ2 ) m2 r2 2 + J2 2 r1 m2 r2 cos(θ1 − θ2 ) , r1 cos(θ1 ) g (2 m2 + m1 ) + 2 r1 m2 r2 sin(θ1 − θ2 )η22 m2 g r2 cos(θ2 ) − 2 r1 m2 r2 sin(θ1 − θ2 )η12 . The dynamical system with state (q, η) to be simulated is then M1 (q)η̇ + R1 (q, η) = 0, q̇ − S(q)η = 0. Note that M1 , R1 , and S depend only on θ and η. Thus we can drop (xi , yi )i=1,2 from the system equations and just keep (θ, η) as the state, giving us 130 5 Examples M1 (θ)η̇ + R1 (θ, η) = 0, θ̇ = η. (5.8a) (5.8b) Notice that (5.8) has the expected form with θ being the angular position and η being the angular velocity. When a solution is found for (θ, η), it is easy to recompute the (xi , yi )i=1,2 positions using the constraints (5.7). This calculation works in general and we have shown that the dynamics of the N -link depend only on the state variables (θ, θ̇). 5.1.2 Generated Code and Simulation Using the Maple computer algebra system we can code as a Maple script the steps described in the previous section. This script was used to generate the three Fortran ﬁles: npend.f (dynamics), np.f (number of links), and ener.f (total energy), and LATEX code of the equations that were placed, using cut and paste, in the previous paragraph. We provide in listing (5.1.2) a verbatim input of the Fortran ﬁle npend.f containing npend(neq,t,th,ydot), which codes the dynamics given by (5.1.2). The equations given by (5.1.2) are in implicit form, and we numerically solve a linear system in npend (M1 (q)η̇ = −R1 (q, η) in order to compute η̇. Note that this ﬁle is 59 lines long for a 2-link pendulum. For a 10-link pendulum the generated ﬁle, which can be found in the Scilab distribution at demos/simulation/npend/Maple, is 477 lines long! c SUBROUTINE npend subroutine npend(neq,t,th,ydot) parameter (n=2) implicit doubleprecision (t) doubleprecision t,th(2*n),eta(n),ydot(2*n),r(n), doubleprecision me3s(n,n),const(n,1),j(n),m(n) doubleprecision w(3*n),rcond integer i,k,neq,ierr data g / 9.81/ data r / n*1.0/ data m / n*1.0/ data j / n*0.3/ c do 1000, i =1,n ,1 ydot(i) = th(i+n) 1000 continue c c do 1001, i =1,n ,1 eta(i) = th(i+n) 1001 continue c t1 = r(1)**2 t8 = cos(th(1)−th(2)) t11 = 2*r(1)*m(2)*r(2)*t8 t12 = r(2)**2 me3s(1,1) = m(1)*t1+4*t1*m(2)+J(1) me3s(1,2) = t11 me3s(2,1) = t11 me3s(2,2) = m(2)*t12+J(2) 5.1 Modeling and Simulation of an N -Link Pendulum 131 t1 = m(2)*r(2) t2 = eta(2)**2 t4 = sin(th(1)−th(2)) t8 = cos(th(1)) t16 = eta(1)**2 t20 = cos(th(2)) const(1,1) = r(1)*(2*t1*t2*t4+2*t8*m(2)*g+m(1)*g*t8) const(2,1) = t1*(−2*r(1)*t16*t4+g*t20) c do 1002, i =1,n ,1 const(i,1) = −const(i,1) 1002 continue c c solving M z = const to obtain ydot((n+1). .2*n) call dlslv(me3s,n,n,const,n,1,w, rcond,ierr,1) if (ierr.ne.0) then write(6,2000) 2000 format(’Matrix is badly conditioned’) endif c do 1003, i =1,n ,1 ydot(n+i) = const(i,1) 1003 continue c return end As described in Section 2.5.1 on interfacing, it is now possible to link the function npend (which conforms to the internal calling syntax accepted by ode), and numerically integrate the N -pendulum equation using ode. The np function, which is also dynamically linked in Scilab, returns the number of links of the pendulum. The reader can take a look at the SCI/demos/simulation/npend to check the details. The Fortran code is compiled and dynamically linked in a running Scilab using the previously described ilib_for_link function. −→ npend_build_and_load() ; generate a loader file generate a Makefile: Makelib running the makefile compilation of npend compilation of np compilation of ener compilation of dlslv building shared library (be patient) shared archive loaded Link done −→ n=np(); r=ones(1,n); m=ones(1,n); j=ones(1,n); g=9.81; −→ y0=0*ones(2*n,1);tt=0:0.05:10; −→ yt=ode(y0,0,tt,’npend’); −→ draw_chain_from_angles(yt(1:$/2,:),r,1); ← calling ilib_for_link ← (θ, θ̇)(0) = 0 ← calling ode ← Figure˜5.2 132 5 Examples Then the N -link pendulum is easily simulated using ode given initial values for (θ, θ̇). Using the scilab demo ﬁles, we now show the result (See Figure 5.2) of a simulation of a 10-link pendulum. Figure 5.2. 10-link pendulum. Dotted line represents the trajectory of the endpoint of the pendulum. The result of the ode simulation is a matrix with N rows. Each column of the matrix gives the value of (θ, θ̇) at a speciﬁc time. Using this matrix a graphics simulation is obtained by ﬁrst computing the (xi , yi )i=0,N and then using the function draw_chain_from_angles to provide an animation. There is an option in the animation to keep track of the trajectory of the endpoint of the pendulum. This curve can be seen as the dashed line in the static Figure 5.2. We provide here a slightly modiﬁed version of draw_chain_from_angles, which gives an example of an animation using the new graphics mode. Moreover, this function uses the Scilab functions realtimeinit and realtime to control the animation speed. function draw_chain_from_angles(a,r,job,rt) // a the angles , a(i,j) is the angle of node i at time t(j) // r the segments half length if argn(2)<3 then job=0, end if argn(2)<4 then rt = 0.01; end n2=size(a,2); x=[0*ones(1,n2);cumsum(2*diag(r)*cos(a),1)]; y=[0*ones(1,n2);cumsum(2*diag(r)*sin(a),1)]; draw_chain_from_coordinates(x,y,job,rt) endfunction function draw_chain_from_coordinates(x,y,job,rt) // x(i,j), y(i,j) is the coordinate of node i at time t(j) 5.1 Modeling and Simulation of an N -Link Pendulum 133 // r the segments half length if argn(2)<3 then job=0, end if argn(2)<4 then rt = 0.01; end [n1,n2]=size(x); b = maxi(maxi(abs(x)),maxi(abs(y))); xbasc();set figure_style new; ← new graphics mode a=gca() ← we want to set a.data_bounds=[-1,-1;1,0]*b; ← the graphics boundaries a.isoview="on"; ← and use an isoview mode xset(’pixmap’,1); ← double buﬀer mode drawlater() ← wait for graphics display colors= 1:n1-1; colors(8)=n1; xsegs([x(1:$-1,1)’;x(2:$,1)’],[y(1:$-1,1)’;y(2:$,1)’],colors) ← draw p=gce();p.thickness=4; ← p is used to keep track of xsegs data if job==1 then xpoly(x($,1)*ones(2,1),y($,1)*ones(2,1),’lines’); ← endpoint trajectory t=gce();t.line_style=2; end drawnow() ← perform drawing xset(’wshow’) ← update display with double buﬀer ind=[1;(2:n1-1)’.*.ones(2,1);n1] realtimeinit(rt); ← set time unit: smaller values will accelerate the animation for j=1:n2, realtime(j) ← wait to time j before continuing drawlater() p.data = [x(ind,j),y(ind,j)]; ← update xsegs data if job==1 then t.data=[t.data;[x($,j),y($,j)]], end ← update xpoly data drawnow() ← since objects are updated, graphics is cleared and redrawn xset(’wshow’) ← update display with double buﬀer end endfunction 5.1.3 Maple Code General Code Dealing with Euler Equation We provide here part of the ﬁle Euler.map, which contains generic functions for Euler equation computation from Lagrangian formulation. For simplicity we have removed from the Maple code functions devoted to LATEX code generation, but the full ﬁle content can be found in the Scilab distribution. #--------------------------------------------------------------# Functions for computing Euler equations. L is the Lagrangian, # q,qd,qdd are the generalized coordinates and their derivatives #--------------------------------------------------------------with(‘linalg‘): euler_equations:=proc(L,q,qd,qdd) local k,m: m:=nops(q); v:=matrix(m,1,0); for i to m do 134 5 Examples v[i,1]:=LL(q[i])=simplify(time_diff(diff(L,qd[i]),q,qd,qdd) -diff(L,q[i])); od; eval(v): end: #--------------------------------------------------------------# Time derivative computation of an expression # depending on q,qd,qdd. Used to compute Euler equations #--------------------------------------------------------------ttvar:=proc(xx) if type(xx,‘indexed‘) then cat(op(0,xx),‘d‘)[op(xx)] else cat(xx,‘d‘) fi end: time_diff:=proc(phi,q,qd,qdd) local phi_copy,k,diff_phi: # substitution to specify that q,qd ,qdd depends on time phi_copy:=phi: phi_copy:=subs(map( xx-> xx=xx(t),[op(q),op(qd)]),phi_copy): diff_phi:=diff(phi_copy,t): # substitution to come back to our variables diff_phi:=subs(map(xx->diff(xx(t),t)=ttvar(xx),[op(q),op(qd)]), diff_phi): diff_phi:=subs(map(xx->xx(t)=xx,[op(q),op(qd),op(qdd)]),diff_phi): end: #----------------------------------------------------# Rewriting the Euler equations to have a canonical form # .. . # El= ME(q) q + RE(q,q) # Computation of ME # CEuler returns a list [ME,RE]; #----------------------------------------------------CEuler:=proc(E,q,qd,qdd) local Me,Ce,Re: Me:=MME(E,q,qd,qdd): Re:=RRE(E,Me,q,qd,qdd): [eval(Me),eval(Re)]: end: MME:=proc(E,q,qd,qdd) local E1: E1:=eval(E): genmatrix([seq(E1[i,1],i=1..nops(qdd))],qdd): end: #----------------------------------------------------# . # Extract the RE(q,q) matrix El= ME(q) qdd + RE(q,qd) 5.2 Modeling and Simulation of a Car 135 #----------------------------------------------------RRE:=proc(E,ME,q,qd,qdd) local MM: MM:=matadd( E,multiply(ME,matrix(nops(q),1,qdd)),1,-1); MM:=map((x)-> simplify(x),eval(MM)): end: #----------------------------------------------------# FORTRAN GENERATION #----------------------------------------------------Gener:=proc(filename,fortranlist) global optimized; init_genfor(); optimized:=true: writeto(filename): genfor(flist): writeto(terminal): end: 5.2 Modeling and Simulation of a Car This example is presented to illustrate the use of the zero-crossing option in the ode function. is also particularly interesting because it is hybrid in nature. This means that it has diﬀerent behaviors in diﬀerent “states.” In this example the diﬀerent states are characterized by whether or not the wheels of the car are on the ground. Since our 2D model has only two wheels, the system has four states, since each wheel can be on or oﬀ the ground. The term “state” is being used two ways in this section. If we wanted to be more precise, we would say that the state of the system is composed of both discrete and continuous states. The discrete part takes on four values depending on the contact of the wheels. For each value of the discrete state we have continuous state variables that describe the car. In the model that follows there are the same number of continuous state variables for each value of the discrete state variable. That need not be true in general. We consider a very simple planar mechanical model so that the equations do not become very complex. We will make several simplifying assumptions in order to reduce the complexity of the model, yet keep the essence of the application so that the model’s dynamic behavior remains realistic. It would be easy to modify this example to make it even more realistic. 5.2.1 Basic Model The simple 2D model of the car that we consider here includes (see Figure 5.3): • two parallel suspensions which are massless but have internal damping, • massless wheels of size zero rolling on the ground with no loss of energy due to friction, • a mass m with rotational inertia J in the middle of the massless rod connecting the top of the suspensions. The length of the rod connecting the top of the suspensions is a constant denoted by l. 136 5 Examples y-axis m y θ η y1 y0 ξ y = F (x) λ3 λ1 λ4 λ2 x0 x x1 x-axis Figure 5.3. Simple model of the 2D car. The model uses the following variables: • θ: angle the car body makes with the horizonal. This is equal to the angles of the suspensions with the vertical. • ξ and η: length of the two suspensions. • λ1 and λ2 (respectively λ3 and λ4 ): the horizontal and vertical forces exerted by the ground on the rear (respectively front) wheel. • ground modeled by the function y = F (x). • (x0 , y0 ) (respectively (x1 , y1 )), which represent the coordinates of the rear (respectively front) wheel. They may or may not be on the ground (i.e., on the curve F (x)). • (x, y): coordinate of the center of gravity of the mass m (the only object with mass in the model). The two suspensions are assumed to have the parameters k, γ, and d. The forces produced by the suspensions are k(ξ − d) + γ ξ˙ for the rear suspension and k(η − d) + γ ξ˙ for the front suspension. 5.2.2 Equations of Motion The equations of motion are obtained from basic principles of physics. We calculate the sum of external forces in the x and y directions to obtain the acceleration of the mass m in each of these directions. We also compute the torque around its center of gravity to obtain an equation for θ̈. The remaining equations are obtained from the dynamics of the suspensions. Since in our model each wheel can be on or above the ground, normally we should derive four diﬀerent sets of equations describing the motion of the car. However, it turns out that once we have the model with both wheels on the ground, the other models can be obtained from it very easily. So we start with the assumption that both wheels are on the ground. The equations of motion for the system are easily obtained from basic principles: 5.2 Modeling and Simulation of a Car mẍ = λ1 + λ3 , mÿ = −mg + λ2 + λ4 , 0 = k(ξ − d) + γ ξ˙ − λ1 sin(θ) + λ2 cos(θ), 0 = k(η − d) + γ η̇ − λ3 sin(θ) + λ4 cos(θ), J θ̈ = λ1 φ0 − λ2 ψ0 + λ3 φ1 + λ4 ψ1 , 137 (5.9a) (5.9b) (5.9c) (5.9d) (5.9e) where φ0 = ξ cos(θ) + l cos(θ)/2, φ1 = η cos(θ) − l sin(θ)/2, (5.9f) (5.9g) ψ0 = −ξ sin(θ) + l cos(θ)/2, ψ1 = η sin(θ) + l cos(θ)/2. (5.9h) (5.9i) System (5.9) gives a complete characterization of the dynamics of the system if the λ’s are known. In the absence of friction, the ground can apply only an orthogonal force to the wheels (the car does not accelerate or brake in our model). Thus λ1 = −λ2 Fx (x0 ), λ3 = −λ4 Fx (x1 ), (5.10) (5.11) where ∂f (x). (5.12) ∂x This means that the only values we need to determine are λ2 and λ4 . Note that if a wheel is not in contact with the ground, then the corresponding λ’s are simply zero. If the wheels touch the ground, we have Fx (x) = y0 = F (x0 ), y1 = F (x1 ). (5.13) (5.14) The state of the car is completely characterized in terms of the variables x, y, θ, their derivatives, and ξ and η. Thus the wheel positions can be obtained from them: x0 = x − ψ0 , (5.15) y0 = y − φ0 , x1 = x + ψ1 , (5.16) (5.17) y1 = y − φ1 . (5.18) The values of λ2 and λ4 can be obtained from (5.13)–(5.18) and the derivatives of (5.9c) and (5.9d): λ2 = γ(Fx (x0 )ẋ − w0 k(ξ − d)/γ + (Fx (x0 )φ0 + ψ0 )θ̇ − ẏ)/w02 , λ4 = γ(Fx (x1 )ẋ − w1 k(η − d)/γ + (Fx (x1 )φ1 − ψ1 )θ̇ − ẏ)/w12 , (5.19) (5.20) wi = Fx (xi ) sin(θ) + cos(θ), i = 0, 1. (5.21) where 138 5 Examples 5.2.3 Simulation Model To use the Scilab function ode to simulate this system, we need to put the system into a ﬁrst order ODE. This is done by introducing variables vx , vy , and ω to represent the derivatives of x, y and θ. The ﬁrst-order ODE can now be expressed as follows: ẋ = vx , v˙x = −(λ2 Fx (x0 ) + λ4 Fx (x1 ))/m, (5.22) (5.23) ẏ = vy , v˙y = −(mg − λ2 − λ4 )/m, ξ˙ = −(k(ξ − d) + λ2 w0 )/γ, (5.24) (5.25) η̇ = −(k(η − d) + λ4 w1 )/γ, θ̇ = ω, ω̇ = −(λ2 (Fx (x0 )φ0 + ψ0 ) + λ4 (Fx (x1 )φ1 − ψ1 ))/J, (5.26) (5.27) (5.28) (5.29) where λ2 and λ4 are either zero (wheel not on the ground) or given by λ2 = γ(Fx (x0 )vx − w0 k(ξ − d)/γ + (Fx (x0 )φ0 + ψ0 )ω − vy )/w02 , λ4 = γ(Fx (x1 )vx − w1 k(η − d)/γ + (Fx (x1 )φ1 − ψ1 )ω − vy )/w12 . (5.30) (5.31) The w’s, φ’s, and ψ’s are deﬁned as before. Discrete States Note that the system can be in 4 diﬀerent states: 1. 2. 3. 4. both wheels on the ground, only rear wheel on the ground, only front wheel on the ground, no wheel on the ground. The simulation must detect when the state changes. This is done by zero-crossing tests (the root option in ode). If the system is in state 1 (both wheels on the ground), then the zero-crossing surfaces are λ2 . (5.32) s= λ4 The reason is that as long as the wheels are on the ground, the vertical forces exerted on them from the ground are positive. As soon as any of them goes negative, the corresponding wheel leaves the ground. If the system is in state 2 (only rear wheel on the ground), then the test on the rear wheel (λ2 ) remains the same, but the other zero-crossing consists in ﬁnding whether the front wheel has come to contact with the ground. So in this case the zero-crossing surfaces are λ2 s= . (5.33) y1 − F (x1 ) Similarly, for state 3 the zero-crossing surfaces are y0 − F (x0 ) s= , λ4 (5.34) 5.2 Modeling and Simulation of a Car and for state 4, s= y0 − F (x0 ) . y1 − F (x1 ) 139 (5.35) 5.2.4 Scilab Implementation Now that the ﬁrst-order dynamics and the zero-crossing surfaces are explicitly deﬁned, the implementation in Scilab is straightforward using the ode function. The four states of the system are coded in two variables ST0 and ST1, which can take the values 0 and 1. The value 0 for ST0 indicates rear-wheel contact with the ground, while the value 0 for ST1 indicates front-wheel contact. A value of one means that the wheel is not in contact with the road. Since the root option in ode does not allow the system to start oﬀ a zero-crossing surface, the simulation starts (and restarts after each zero-crossing detection) with a call to ode without the root option for a short period of time. Subsequently, ode with root option is called to perform the simulation up to the next zero-crossing. This also eliminates multiple zero-crossing detections for the same surface due to numerical errors. The Scilab function simul car, given below, implements the main simulation routine. This function receives the initial state X and a vector of time instances TT. It computes the solution XX of the system over the instances T. T is just TT to which the zero crossing instances have been added in order to capture important positions of the car. function [XX,T]=simul_car(TT,X) Tt=TT; // remaining time instances XX=[];T=[]; // intialization of outputs tc=0; //current time delt=1d-3; // time interval without root while 1 t=tc+delt; It=find(Tt<t); // find indices of output points if It==[] then xt=ode(X,tc,t,car); // no root simulation else Xx=ode(X,tc,Tt(It),car); T=[T,Tt(It)];XX=[XX,Xx];xt=Xx(:,$);Tt(It)=[]; end [Xx,rd]=ode(’root’,xt,t,Tt,car,2,carg); if rd==[] then // no root found, update outputs XX=[XX,Xx];T=[T,Tt]; break; end tc=rd(1); // root found at time tc if size(rd,’*’)==3 then // two roots crossed ST0=1-ST0;ST1=1-ST1; // change both states else if rd(2)==1 then ST0=1-ST0; end // rear wheel if rd(2)==2 then ST1=1-ST1; end // front wheel end T=[T,Tt(find(Tt<rd(1))),rd(1)];XX=[XX,Xx];X=Xx(:,$); // update outputs, time of zero-crossing added to TT Tt=Tt(find(Tt>rd(1))); // update remaining time instances end endfunction 140 5 Examples The car model car and the zero-crossing surfaces carg functions are deﬁned as follows: function Xd=car(t,X) x=X(1);xd=X(2);y=X(3);yd=X(4);th=X(5);thd=X(6);xsi=X(7);eta=X(8); [lam2,lam4,x0,x1,mu0,mu1,w0,w1]=fun(X); if ST0 then lam2=0; end if ST1 then lam4=0; end xdd=-(lam2*Fx(x0)+lam4*Fx(x1))/m ydd=-(m*g-lam2-lam4)/m thdd=-(lam2*mu0+lam4*mu1)/J xsid=-(k*(xsi-d)+lam2*w0)/gam etad=-(k*(eta-d)+lam4*w1)/gam Xd=[xd;xdd;yd;ydd;thd;thdd;xsid;etad] endfunction function s=carg(t,X) x=X(1);xd=X(2);y=X(3);yd=X(4);th=X(5);thd=X(6);xsi=X(7);eta=X(8); [lam2,lam4,x0,x1,mu0,mu1,w0,w1]=fun(X); if ST0 then s1=y-xsi*cos(th)-l2*sin(th)-F(x0) else s1=lam2 end if ST1 then s2=y-eta*cos(th)+l2*sin(th)-F(x1) else s2=lam4 end s=[s1,s2] endfunction function [lam2,lam4,x0,x1,mu0,mu1,w0,w1]=fun(X) x=X(1);xd=X(2);y=X(3);yd=X(4);th=X(5);thd=X(6);xsi=X(7);eta=X(8); x0=x+xsi*sin(th)-l2*cos(th) x1=x+eta*sin(th)+l2*cos(th) mu0=Fx(x0)*(xsi*cos(th)+l2*sin(th))-xsi*sin(th)+l2*cos(th) mu1=Fx(x1)*(eta*cos(th)-l2*sin(th))-eta*sin(th)-l2*cos(th) w0=Fx(x0)*sin(th)+cos(th) w1=Fx(x1)*sin(th)+cos(th) lam2=(Fx(x0)*xd-w0*k*(xsi-d)/gam+mu0*thd-yd)*gam/w0^2 lam4=(Fx(x1)*xd-w1*k*(eta-d)/gam+mu1*thd-yd)*gam/w1^2 endfunction The road proﬁle (the function F (x) and its derivative) are deﬁned below. The parameters a, b, f are available to vary the road surface. function y=F(x) if x<0 then y=-a*cos(f*x) else y=a*(-2+cos(b*f*x)) 5.2 Modeling and Simulation of a Car 141 end endfunction function y=Fx(x) if x<0 then y=a*f*sin(f*x) else y=-a*f*b*sin(b*f*x) end endfunction We can avoid having to deﬁne Fx (x) by using automatic diﬀerentiation, which is available as a plug-in toolbox for Scilab. 5.2.5 Simulation Result The simulation is run with the following Scilab script: A=30;a=5.2;f=%pi/A;B=3;b=1; ← curve parameters XI=A; ← used to deﬁne boundaries of display del=.02;TT=[del:del:15]; ← instances for animation // model parameters l=6;l2=l/2;m=1;d=4;g=10;gam=.5; J=m*l2*l2/4;k=12;r2=d/4; // compute reasonable initial state x=-XI+.7; y=d-m*g/2/k+F(x);xsi=y-F(x-l2)-0.3;eta=y-F(x+l2)-0.4; X=[x;0;y;0;0;0;xsi;eta]; ST1=1;ST0=1; [XX,T]=simul_car(TT,X); ← simulation play(T,X,XI) ← animation The initial condition is selected so that the car starts oﬀ with zero speed close to the top of a hill. To avoid having to compute an initial condition in which both wheels are on the ground, we start oﬀ with both wheels slightly above the ground. The car lands and start rolling immediately. The animation is performed by the function play, which can be found in Appendix C. During this animation, the system changes state a number of times. Some of the changes can be seen in Figure 5.4, which shows the position of the car at certain points during the simulation. To make the animation more realistic, wheels are drawn even though the model assumed zero-sized wheels. It turns out that the simulation is valid (assuming of course that wheels have zero mass) if the function F (x) describes not the proﬁle of the ground but a function drawn by the center of a wheel if the wheel would roll on the ground. This is true provided the radius of curvature of the curve F (x) is always larger than the radius of the wheel. In the function play, the proﬁle of the ground drawn (see Figure 5.4) is not F (x), but it is obtained from F (x) and Fx (x), and, of course, the radius of the wheel. This is done for a set of x’s to allow plotting the curve with enough detail. The mathematical expression of this curve is in general diﬃcult to obtain. 142 5 Examples Figure 5.4. Snapshots of the car during animation. Note also that in play we use the xor mode to erase previous positions of the car. In the xor mode, drawing an object for a second time erases it. This provides a very eﬃcient way of moving objects, in particular when some objects (for example the road in this case) do not move because these latter objects need not be redrawn. 5.3 Open-Loop Control to Swing Up a Pendulum We consider here the classical control problem of erecting a free swinging pendulum. The set up consists of a pendulum mounted on a motor-driven cart traveling horizontally. 5.3.1 Model The model is a special case of the model considered later in the part dedicated to Scicos; see Figure 11.14 and equations (11.3)–(11.4). Here we consider the case that the angle φ is zero. The model is then (M + m)z̈ + mlθ̈ cos(θ) − mlθ̇2 sin(θ) = u(t), lθ̈ + z̈ cos(θ) − g sin(θ) = 0. The state of this system can be deﬁned as follows: ⎛ ⎞ z ⎜θ ⎟ ⎟ x=⎜ ⎝ż ⎠ . θ̇ (5.36) (5.37) (5.38) The system dynamics can then be expressed as follows: ẋ = f (x, u), (5.39) where f is easily obtained from (5.36)–(5.37). 5.3.2 Control Problem Formulation The control problem considered is that of ﬁnding an input u(t) to move the pendulum from an initial position, hanging at rest with the cart at the origin, to a ﬁnal position with the pendulum upright with the cart restored to its original position. We look only for an open-loop control; in a real application such a control must be supplemented with a feedback control to stabilize the system around the open-loop trajectory. 5.3 Open-Loop Control to Swing Up a Pendulum 143 The solution to this open-loop control problem is not unique: there are many control functions u(t) over any interval [0, T ] that would move the state x(0) = [0, −π, 0, 0] to x(T ) = [0, 0, 0, 0]. Clearly there is a compromise between the interval length T and the size of u: the smaller T is, the bigger u is going to be. A common formulation is to consider u to be bounded: |u(t)| ≤ Umax , 0 ≤ t ≤ T, and look for the shortest control interval (smallest T ). In this formulation, since u enters the system equations linearly and there is no cost associated with it, from the optimality principle, it can be shown that the optimal open-loop control is bang-bang. This means that the optimal open-loop trajectory consists of a piecewise constant function taking values +Umax and −Umax . So the control starts oﬀ with u(t) equal to +Umax for a period of τ1 (which could be zero). This pushes the state to x1 . Then the control switches to −Umax and remains there over a period τ2 ; at the end of this period the state is at x2 , and so on until the end of the interval T is reached. See Figure 5.5. τ1 x0 Umax τ2 x1 ......... −Umax x2 Umax τn xn Figure 5.5. Controls and states over the interval [0, T ]. So the problem reduces to ﬁnding the number of “switchings” and their “times.” To do so, we ﬁx the number of switchings and optimize over the switching times. We can run the optimization problem for diﬀerent numbers of switchings. We shall see the consequence of overestimating this number in an example. 5.3.3 Optimization Problem The control problem is formulated as an optimization problem by deﬁning the following cost function: J(τ ) = h(τ ) + x(h(τ ))T W x(h(τ )), (5.40) where τ = (τ1 , . . . , τn ) and h(τ ) = n τi . (5.41) (5.42) i=1 The problem is then to minimize J over the τ ’s subject to the dynamics of the system. We shall use optim to solve the problem. The computation of J requires the computation of the ﬁnal state xn given the control. This can be done with the Scilab function ode. But optim also requires the gradient of J with respect to τ (= (τ1 , τ2 , . . . , τn )). The gradient can also be obtained using ode and the linearized model of the system. The linearized model can readily be obtained from (5.36)–(5.37): 144 5 Examples (M + m)δz̈ − mlθ̈ sin(θ)δθ + ml cos(θ)δ θ̈ − mlθ̇2 cos(θ)δθ − 2mlθ̇ sin(θ)δ θ̇ = δu, lδ θ̈ + cos(θ)δz̈ − z̈ sin(θ)δθ − g cos(θ)δθ = 0. By straightforward algebra, it can be shown that the linearized model can be expressed as follows: ˙ = A(ẋ, x, u)δx, δx (5.43) where A(ẋ, x, u) = where 0 I V −1 F V −1 G (5.44) 0 ml sin(θ)θ̈ + mlθ̇2 cos(θ) , 0 z̈ sin(θ) + g cos(θ) 0 2mlθ̇ sin(θ) G= , 0 0 M + m ml cos(θ) V = . cos(θ) l F = Note that z, θ, and their derivatives are all functions of u. Note also that ∂h ∂J ∂x(h) = + x(h)T W . ∂τ ∂τ ∂τ (5.45) (5.46) (5.47) (5.48) is simply a vector of ones. ∂x(h) is the variation in the ﬁnal state xn due to a variation ∂τ in τ . Thus we can compute it by computing δxn due to variations δτ . Let Ψ (t) be the fundamental solution of the linear system (5.43), i.e., the solution of the following equation: Ψ̇ = A(ẋ, x, u)Ψ, Ψ (0) = I. (5.49) ∂h ∂τ It is then straightforward to see that δx1 = f (x1 , Umax )δτ1 , (5.50) δx2 = Ψ (τ2 )δx1 + f (x2 , −Umax )δτ2 = Ψ (τ2 )f (x1 , Umax )δτ1 + f (x2 , −Umax )δτ2 δτ1 = Ψ (τ2 )f (x1 , Umax ) f (x2 , −Umax ) . (5.51) δτ2 By continuing the same way, we can show that for all i > 0, ⎛ ⎞ δτ1 ⎜ .. ⎟ δxi = Gi ⎝ . ⎠ (5.52) δτi where G is obtained from the following recursion: Gi+1 = Ψ (τi )Gi f (xi , (−1)i+1 Umax ) , G0 = [ ]. (5.53) So at the end we have δxn = Gn δτ, which allows us to evaluate ∂J ∂τ (5.54) thanks to (5.48): ∂J = 1 + xT n WGn , ∂τ where 1 denotes a row vector of ones of size n. (5.55) 5.3 Open-Loop Control to Swing Up a Pendulum 145 5.3.4 Implementation in Scilab As we have seen in the previous section, the evaluation of the cost and the gradient of the cost require the solution of the system and the fundamental solution of the linearized system. These two systems cannot be solved separately because the linear system depends on the trajectory of the original system. The two systems are thus solved simultaneously by considering as state X= xΨ . (5.56) This matrix diﬀerential equation can be solved directly by ode. The associated Scilab functions are deﬁned in the following ﬁle: function Xd=simul(t,X) x=X(:,1);dx=X(:,2:$) [xd,dxd]=full_model(x,dx) Xd=[xd,dxd]; endfunction function [xd,dxd]=full_model(x,dx) [lhs,rhs]=argn(0); z=x(1);th=x(2);zd=x(3);thd=x(4); Mati=inv([M+m,m*l*cos(th);cos(th),l]); ydd=Mati*[m*l*sin(th)*thd^2+U;g*sin(th)]; zdd=ydd(1);thdd=ydd(2); xd=[zd;thd;zdd;thdd]; F=[0,m*l*thdd*sin(th)+m*l*thd^2*cos(th);0,zdd*sin(th)+g*cos(th)]; G=[0,2*m*l*thd*sin(th);0,0]; if rhs == 2 then dxd=[zeros(2,2),eye(2,2);Mati*F,Mati*G]*dx; end endfunction The cost and its gradient, to be used by optim, are then computed by the following function function [f,gg,ind]=cost(Ti,ind) xc=x0; dxdt=[]; N=size(Ti,2) tc=0; for i=1:N U=(-1)^(i-1)*Umax; if Ti(i)>1d-9 then XX=ode([xc,eye(4,4)],tc,tc+Ti(i),1d-11,1d-12,simul) else XX=[xc,eye(4,4)] end xc=XX(:,1);tc=Ti(i)+Ti(i) Psi=XX(:,2:$); dxdt=[Psi*dxdt,full_model(xc)]; end f=sum(Ti)+xc’*W*xc/2 gg=ones(Ti)+xc’*W*dxdt endfunction 146 5 Examples The main Scilab script which uses optim is the following −→ W=100*eye(4,4); // weighing matrix −→ M=1;m=.2;l=.6;g=10; // model parameters −→ N=5; // number of tau’s −→ delt=1.7;T0=ones(1,N)*delt/N; // initial guess −→ x0=[0;-%pi;0;0]; // intial state −→ Umax=9.7; −→ [J,Topt,gr]=optim(cost,’b’,ones(T0)*delt*0,delt*ones(T0),T 0,’ar’,300); −→ Topt Topt = ! 0. 0.2817765 0.6133068 0.5066828 0.1752807 ! Note that we have let n be 5 (maximum of four switchings) but the optimal solution obtained by optim sets τ1 to zero (Scilab variable Topt contains the vector τ ). So this solution starts oﬀ with u = Umax and switches three times over the interval. Both the simulation result and the animation show that the solution is correct; see Figures 5.6 and 5.7. 0.4 0.2 0.0 −0.2 −0.4 −0.6 −0.8 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 −0.0 −0.5 −1.0 −1.5 −2.0 −2.5 −3.0 −3.5 −4.0 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 10 8 6 4 2 0 −2 −4 −6 −8 −10 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 8 6 4 2 0 −2 −4 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 Figure 5.6. The ﬁrst two ﬁgures illustrate the evolution of z and θ. The third ﬁgure is the control. The last ﬁgure contains the derivatives of z and θ showing that the pendulum comes to rest at the end of the interval as required. 5.4 Parameter Fitting and Implicit Models 147 Figure 5.7. Snapshots of the pendulum during animation. 5.4 Parameter Fitting and Implicit Models In many applications the scientist knows the general form of the equations that model the process, but speciﬁc parameter values are not known. Also, in many application areas the models are often diﬀerential algebraic equations (DAE). In this section we shall give an illustration of parameter ﬁtting with a DAE model. The general procedure will be the following. Suppose that the model that is to be found is a DAE F (ẏ, y, t, p) = 0, (5.57) where p is a vector of unknown parameters. We suppose that the model is to be deﬁned over the time interval [0, T ] and suppose that measurements are taken of the actual physical process for some function of y at times ti for i = 0, . . . , N . That is, we have the measured quantities (5.58) zi = h(y(ti ), ti ), i = 0, . . . , N. We suppose that J of these trials are done and let zi,j be the result of the jth trial at time ti . The initial condition for (5.57) could be ﬁxed or it could be viewed as more parameters to be determined. We shall take the initial condition ﬁxed, but that does not change the procedure. We wish to determine p such that the solution of (5.57) ﬁts the observation data as closely as possible. There are a couple of ways to do this in Scilab. One way would be to use optim. Another is to use the function datafit. The function datafit will ﬁt a function e = G(p, z) to data. We found that datafit could solve the problem, but convergence could be slow and the accuracy was somewhat lower. Here we shall use directly optim with a cost function of the same form that datafit uses except that by using optim we can control the step used for the ﬁnite diﬀerence computation of the gradients. We do not want to call for the integration of (5.57) for every zij . It is more eﬃcient to do just one integration for each value of j. Thus we will take the z in G(p, z) to be the vector of all zi,j for a ﬁxed j. 148 5 Examples 5.4.1 Mathematical Model Many applications use polynomial models. In some cases these are argued for on ﬁrst principles. In other applications they occur because if linear equations do not suﬃce, then the next natural thing to try is a polynomial equation. If a linear approximation is not adequate, then the next level of Taylor approximation uses quadratic functions. The DAE we consider arises as the model of a batch reactor and is sometimes used as a test problem in the chemical engineering literature [17, 31]. The equations are ẏ1 = −p3 y2 y8 , ẏ2 = −p1 y2 y6 + p2 y10 − p3 y2 y8 , (5.59a) (5.59b) ẏ3 = p3 y2 y8 + p4 y4 y6 − p5 y9 , ẏ4 = −p4 y4 y6 + p5 y9 , (5.59c) (5.59d) ẏ5 = p1 y2 y6 − p2 y10 , ẏ6 = −p1 y2 y6 − p4 y4 y6 + p2 y10 + p5 y9 (5.59e) (5.59f) 0 = −0.0131 + y6 + y8 + y9 + y10 − y7 , 0 = p7 y1 − y8 (p7 + y7 ), (5.59g) (5.59h) 0 = p8 y3 − y9 (p8 + y7 ), 0 = p6 y5 − y10 (p6 + y7 ). (5.59i) (5.59j) In its original form this problem is known to be very stiﬀ and is used for a test problem for parameter sensitivity software. We shall take a slightly modiﬁed set of initial conditions. 5.4.2 Scilab Implementation To illustrate the procedure we shall proceed as follows. Our observational data will be given by a simulation followed by the addition of random perturbations. We begin with a ﬁxed value of p, which we denote by ptrue. Assuming that p is given in the current Scilab environment, the system is coded as follows: function [r,ires]=reactor(t,y,ydot) ires=0; r(1)=ydot(1)+p(3)*y(2)*y(8); r(2)=ydot(2)+p(1)*y(2)*y(6)-p(2)*y(10)+p(3)*y(2)*y(8); r(3)=ydot(3)-p(3)*y(2)*y(8)-p(4)*y(4)*y(6)+p(5)*y(9); r(4)=ydot(4)+p(4)*y(4)*y(6)-p(5)*y(9); r(5)=ydot(5)-p(1)*y(2)*y(6)+p(2)*y(10); r(6)=ydot(6)+p(1)*y(2)*y(6)+p(4)*y(4)*y(6)-p(2)*y(10)-p(5)*y(9); r(7)=-0.0131+y(6)+y(8)+y(9)+y(10)-y(7); r(8)=p(7)*y(1)-y(8)*(p(7)+y(7)); r(9)=p(8)*y(3)-y(9)*(p(8)+y(7)); r(10)=p(6)*y(5)-y(10)*(p(6)+y(7)); endfunction Since the model (5.59) is a DAE, only some initial conditions will be consistent. We shall assume that {p6 , p7 , p8 } are known and ﬁxed at the values ptrue(6:8). In this case the space of consistent initial conditions is independent of the remaining values of pi and we do not have to worry about ﬁnding new consistent initial conditions as the parameters are varied. 5.4 Parameter Fitting and Implicit Models 149 We take ﬁxed initial values of the ﬁrst 6 components of the initial condition y(0). The remaining values of y(0) are found by solving the last four equations in (5.59) using fsolve. This ensures that our y(0) is consistent to high order. −→ ptrue=[21.893;2.14e3;32.318;21.893;1.07e3;6.65e-3;4.03e-4;5.e-5/32]; −→ function z=constraints(y2) −→ r = reactor(0,[y1;y2],0*ones(10,1)); −→ z = r(7:10); −→ endfunction −→ function [y,v]=initial_condition(y1) −→ [y2,v]=fsolve(zeros(4,1),constraints); −→ y =[y1;y2]; −→ endfunction −→ y01=[1.5776;8.32;0;0;0;0]; −→ p=ptrue; −→ [y0,v]=initial_condition(y01); −→ norm(v) ans = ← compute constraints for y=[y1,y2] ← compatible initial value given y1 ← initial value of ﬁrst 6 components ← a consistent initial condition ← test consistency 2.262D-17 Simulations with dassl using y(0) and ptrue show that the solutions are close to steady state by around t = 0.5. Accordingly we will take our observations on the interval [0, 0.5]. These simulations, which are indicated by the solid unmarked lines in Figs. 5.8–5.13, also show that the components of yi vary greatly in size. A given experiment will consist of observing the value of the ﬁrst six components of y every 0.05 seconds for a total of 10 time points. Hence the observation times are tts=0.05:0.05:0.5. We generate the experimental outcomes by taking a simulation using ptrue and then perturbing the entries by a uniformly generated random vector. The perturbations are scaled so that they are of the same relative size for each entry and each observation time. We take 9 such observations so that J = 9. −→ t0=0; tt=0:0.05:0.5; −→ y=dassl(y0,t0,tt,reactor); −→ y=y(2:7,2:11);y(4:6,:)=100*y(4:6,:); −→ rtrue=y(:); ← integration with p=ptrue ← rescale y(4:6,:) −→ rand(’seed’,20); −→ N=9; ← generate 9 observations perturbed with noise −→ obs= rtrue*ones(1,N); −→ obs= obs.*(1+ 0.1*(rand(60,N,’u’)-0.5)); ← perturbations are to be rela← tive to the size of what is being perturbed We then make an intial guess p0 for the ﬁrst 5 parameters and use optim to ﬁnd the values of {p1 , . . . , p5 } to best ﬁt the data. The ﬁnal estimate is denoted by pest. This last step is carried out now by −→ function e=G(p1) ← error function to be minimized 150 5 Examples −→ p=[p1;ptrue(6:$)] ← p(6:8) is ﬁxed and ﬁt using p(1:5) −→ rr=dassl(y0,t0,tt,1.e-10,1.e-12,reactor); −→ rr=rr(2:7,2:11);rr(4:6,:)=100*rr(4:6,:); −→ rr=rr(:); −→ e=0; −→ for i=1:size(obs,’c’) −→ gg = (rr-obs(:,i)); −→ e = e + gg’*gg; −→ end −→ endfunction −→ function [f,g,ind]=costf(p,ind) −→ if ind==2|ind==4 then −→ f=G(p); −→ else −→ f=0; −→ end; −→ if ind==3|ind==4 then −→ g=0*p; −→ pa=sqrt(%eps)*10*abs(p); −→ f=G(p); −→ for j=1:size(p,’*’) do −→ v=0*p;v(j)=pa(j), −→ g(j)=(G(p+v)-f)/v(j); −→ end; −→ else −→ g=0*p; −→ end −→ endfunction ← cost function for optim −→ p0=ptrue(1:5)*0.5; −→ [err,pest]=optim(costf,p0,’ar’,100,100,1.e-9,1.e-9,imp=2); ← initial guess for p −→ [p0,pest,ptrue(1:5)] ans = ← ﬁtted parameters ! ! ! ! ! 10.9465 1070. 16.159 10.9465 535. 10.614308 1105.6571 33.346866 5.8412889 946.85234 21.893 2140. 32.318 21.893 1070. ! ! ! ! ! −→ [G(p0),G(pest),G(ptrue(1:5))] ans = ! 453.75872 6.1442786 ← cost minimized by datafit 5.2443056 ! The ﬁrst thing one notices is that pest is not the same as ptrue(1:5), nor is it even very close. However, if we examine the answer we see that we have in fact found a good approximation. Running the following script will graph the diﬀerent yi for p0, ptrue, and pest. What we see is that the new value pest is providing a good ﬁt to the data. This example shows the importance of using parameter bounds whenever they are available 5.4 Parameter Fitting and Implicit Models 151 especially in nonlinear problems. Observe how the ﬁfth component of pest has moved much farther from the ﬁfth component of ptrue than the ﬁfth component of p0 was. p=[ptrue]; krt=dassl(y0,t0,tt,reactor); p=[p0;ptrue(6:$)];kr0=dassl(y0,t0,tt,reactor); ttt=[0.01:0.02:0.5]; p=[pest;ptrue(6:$)];krf=dassl(y0,t0,ttt,reactor); scale_r=[ones(1,3),100*ones(1,3)]; for ii=2:11 f1=scf(ii-1); plot2d(ttt,krf(ii,:)); plot2d(ttt,krf(ii,:),-1); plot2d(tt,krt(ii,:)); plot2d(tt,kr0(ii,:)); plot2d(tt,kr0(ii,:),-2); xtitle(’y(’+string(ii-1)+’)’); nz=size(obs,’c’); if nz <>1 & ii<=7 then for k=1:N obsk=obs(:,k);obsk=matrix(obsk,6,10); plot2d(tt(2:$)’,obsk(ii-1,:)./scale_r(ii-1),-3); end end end Figures 5.8–5.13 illustrate what happens for several components for y. In all cases the solid unmarked line gives the graph of yi using ptrue. The graphs using pest and p0 are indicated by + and ×’s respectively. In the cases where there are observations, those are indicated by circles. y(1) 2.0 1.5 1.0 0.5 0.0 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 Figure 5.8. Graph of observational data and y1 for ptrue (solid), pest (+++), and p0 (xxx). 152 5 Examples y(2) 8.4 8.2 8.0 7.8 7.6 7.4 7.2 7.0 6.8 6.6 6.4 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 Figure 5.9. Graph of observational data and y2 for ptrue (solid), pest (+++), and p0 (xxx). y(3) 1.6 1.4 1.2 1.0 0.8 0.6 0.4 0.2 0.0 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 Figure 5.10. Graph of observational data y3 for ptrue (solid), pest (+++), and p0 (xxx). Often there are bounds on reasonable parameter values which come either from physical arguments or practical experience. In this case the bounds can be incorporated into the call for datafit or optim. Suppose, for example, that we knew that the value of pi was in the interval [0.8 ∗ ptrue(i), 1.2 ∗ ptrue(i)]. If we replace the last line of the ﬁrst script with −→ ps=ptrue(1:5);pinf=ps;psup=1.2*ps;p0=1.1*ps; −→ [err,pest]=optim(costf,’b’,pinf,psup,p0,’ar’,100,100,1.e-9,1.e-9,imp=2); we get the new parameter estimate of −→ [p0,pest,ps] 5.4 Parameter Fitting and Implicit Models 153 y(4) 0.050 0.045 0.040 0.035 0.030 0.025 0.020 0.015 0.010 0.005 0.000 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 Figure 5.11. Graph of observational and y4 for ptrue (solid), pest (+++), and p0 (xxx). y(5) 0.012 0.010 0.008 0.006 0.004 0.002 0.000 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 Figure 5.12. Graph of observational data and y5 for ptrue (solid), pest (+++), and p0 (xxx). ans ! ! ! ! ! = 10.9465 1070. 16.159 10.9465 535. 24.172324 2349.0207 32.318 24.364568 1083.1877 21.893 2140. 32.318 21.893 1070. ! ! ! ! ! 154 5 Examples y(6) 0.040 0.035 0.030 0.025 0.020 0.015 0.010 0.005 0.000 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 Figure 5.13. Graph of observational data and y6 for ptrue (solid), pest (+++), and p0 (xxx). Comments optim calls an iterative solver. Each time G is evaluated there is a call to dassl. For more complicated problems with more parameters or longer time intervals these calls can be computationally expensive. As implemented in the previous illustration there would be N calls for each value of p. If these calls are taking too much time, one can directly formulate the cost as i eTi ei , use optim, and have to do only one call to dassl for each value of p. Parameter estimation is an important part of many engineering and scientiﬁc procedures. The example given illustrates two important facts. One is that since one is using the data to construct a model that predicts the data, it is important to have enough data to cover the region in which one wishes to use the model. Secondly, the criterion is the ability to predict the observations. If the parameter estimates are of interest themselves, then considerable more analysis and care may be needed to determine how good the estimate is. Notice in this example that the graphs using the estimated parameters appear to be close to the graphs for the true parameter values. So the numerical algorithm is doing a good job of giving us parameter values that give the desired behavior. However, the estimated parameter values are not always close to the true parameter values. A similar behavior was observed in Section 3.4 in the discussion of lsqrsolve. In the nonlinear case there are the added problems of local minima when one is solving for parameter estimates. But even for the linear case y = Ax, it is important to keep in mind that the following three problems are distinct and can produce diﬀerent estimates Â for A: 1. Given data {xi , yi } ﬁnd the value Â that gives the best prediction law for y given an additional value of x. 2. Given data {xi , yi } ﬁnd the value Â that gives the best prediction law for x given an additional value of y. 3. Given data {xi , yi }, which we assume come from a model y = Ax, ﬁnd the value of Â that gives the best estimate of A. 5.4 Parameter Fitting and Implicit Models 155 The functions optim, datafit, and lsqrsolve as described here are being used for the ﬁrst type of problem since the error in the value of G is being minimized. The function G needs to be formulated diﬀerently if either problem 2 or 3 is to be solved. Finally, this example illustrates an important aspect of solving optimization problems. Finding the optimal solution even when we know it exists can be diﬃcult. When we initially solved this problem we used more of the default settings of optim and dassl. The result was not nearly as good as reported above. After some experimentation, it was determined that one factor was that the gradient approximation was not accurate enough. Since the function being diﬀerenced was the result of an integration by dassl, we both increased the lower bound on the diﬀerence in optim and tightened the error tolerances in dassl. Another problem was that the large diﬀerence in magnitude of the components was leading to inaccuracy. So we put a factor of 100 in the function evaluation, which helps prevent stagnation and increased accuracy of gradients. In particular, we rescaled y(4:6) to give these variables a range similar to those in y(1:3). This was necessary to increase the part dedicated to y(1:3) in the global cost. We also gave optim some guidance. In [err,pest]=optim(costf,p0,’ar’,100,100,1.e-9,1.e-9,imp=2) the 1.e-9 sets the thresholds on the gradient norm and the decrease in f. Here we are forcing the change in f and the gradient to be rather small at termination since they need to be below these thresholds. The imp=2 gives a verbose reporting on the iterations, which was used in examining the behavior of the iteration while deciding where to set the thresholds. In trying to solve a parameter problem in practice when the solution is not known it is often helpful to try ﬁrst what we have done here. That is, generate observations by simulation with known parameters and then try ﬁrst to ﬁnd the known parameters. This helps in checking that the problem is properly coded and gives insights into the problem’s behavior. Thus the problem solved in this section is not as academic as it appears and is representative of part of the solution of real problems. Part II Scicos 6 Introduction Scicos is a Scilab toolbox for modeling and simulation of dynamical systems. Scicos is particularly useful for modeling systems where continuous-time and discrete-time components are interconnected. Such models can be programmed directly in Scilab using the ode and dassl functions, studied previously, but that requires programming the discrete-time dynamics in Scilab. These programs are often complex and diﬃcult to debug. Moreover, there is no simple systematic way of programming them in a modular fashion. Scicos provides a modular way to construct complex dynamical systems using a block diagram editor. Scicos diagrams are compiled and simulated eﬃciently by a single click. Scicos handles, in particular, the interaction between continuous-time dynamics and system events including events associated with the timing of a discrete-time clock. Such events aﬀect the way the numerical solver, which integrates the continuous-time dynamics, should be called. Handling eﬃciently such matters by hand for complex dynamical systems can be extremely diﬃcult. Using Scicos, the user can construct a library of reusable modules (blocks) that can be used in diﬀerent models in diﬀerent projects. This is particularly useful when a large model is composed of modules designed by diﬀerent development teams. The Scicos formalism, which must be respected in designing blocks and submodels, guarantees that modules constructed separately and interconnected can work harmoniously together. A large number of blocks are already available in Scicos palettes. These blocks provide elementary operations needed to construct models of many dynamical systems. Users seldom need to construct a new block from scratch. Scicos is more than a modeling and simulation program. It contains many functionalities to help the designer optimize model parameters, validate models, generate C code, etc. In this part of the book, we present Scicos and some of these useful functionalities. We shall give the reader a general picture of the Scicos environment and the type of applications that can be treated in it. Numerous examples are used to illustrate the subjects covered. Enough information is provided to allow the user to understand the examples. The examples are also included in the book’s web site. We start by providing a tutorial in Chapter 7 on how to use the editor to construct models and how to simulate these models. We cover important editor functionalities and illustrate them using simple examples. Chapter 8 is devoted to a description of the formalism behind Scicos. This chapter provides detailed information on how blocks are activated by activation signals, how data is exchanged between blocks, and what synchronism means in this environment and how it can be used for conditional subsampling. 160 6 Introduction Even if in many cases Scicos diagrams can be constructed using existing blocks, sometimes it is more convenient, or even becomes necessary, to develop new blocks. This is the subject of Chapter 9. Sometimes a new block is not absolutely necessary but can simplify the diagram and/or its use can make the simulation of the diagram more eﬃcient. Various ways of constructing new blocks are discussed in this chapter. The content of this chapter, however, is fairly technical, and most users may skip it on a ﬁrst reading. In Chapter 10, through a number of examples, we show what types of systems can be modeled in Scicos and propose solutions for modeling certain behaviors that are particularly diﬃcult to realize outside of Scicos. Often the objective for modeling and simulation is parameter optimization. The parameter can be a system parameter to be tuned in an identiﬁcation process, or a controller or ﬁlter parameter to be adjusted to a given system. The procedure for parameter optimization consists in associating a cost (obtained by simulation) to a set of parameters. This cost is then optimized by standard optimization techniques. This procedure, which usually requires many simulation runs, can easily be implemented in the Scilab-Scicos environment by batch processing. In Chapter 11, we present Scilab functions used for batch simulation of Scicos diagrams in Scilab and present a number of examples illustrating typical applications. Code generation is an important functionality in Scicos. A typical application is to a situation in which the diagram models a controlled system. The control part of the diagram, after validation of the control law through simulation, is to be implemented in an embedded system. For that, C code generation can be performed for a part of the diagram designated by the user. Code generation is the subject of Chapter 12. Examples are provided and some pitfalls are discussed. A number of debugging functionalities are available in Scicos. They are explained in Chapter 13. Debugging is particularly useful when diagrams become complex and when user-deﬁned blocks are used. A few examples are provided to show how debugging can be done eﬃciently. A recent addition to Scicos is the capacity to use “implicit” blocks in a Scicos diagram. This allows the construction of systems using blocks modeling physical components in a natural way. These blocks have no explicit inputs and outputs but rather have ports. Connecting these ports by links deﬁnes constraints. For example, in this environment we can deﬁne an electrical circuit with implicit blocks modeling resistors, capacitors, diodes, and other electrical components, and with links imposing Kirchoﬀ’s laws of current and equality of voltages at the two connected ports. The possibility to mix implicit and normal blocks in the same environment provides a powerful modeling environment that is presented in Chapter 14. It should, however, be noted that the implementation of implicit blocks is at the beta testing stage. Appendix A contains an introduction on how Scicos is implemented and, in particular, what data structures it uses. Understanding implementation issues is very useful not only for advanced users who want to customize the tool, but also for anybody who encounters subtle modeling problems and wants to take full advantage of debugging facilities. This chapter also gives a lot of information on where important Scicos programs can be found. Most of these programs (except for the simulator part) are Scilab programs, so they are easy to read and customize. In this book we present functionalities of Scicos distributed with Scilab version 3.1 or higher. Even though most of the discussions apply also to Scicos in Scilab 3.0, some of the examples may not run on this earlier version. Note also that running some of the examples presented here requires a C compiler. This is usually no problem under Linux, MacOSX, and most Unix workstations. But under the 6 Introduction 161 Windows operating system, it requires the installation of Visual C++. Visual C++ is automatically instantiated and usable by Scilab if Scilab is run by clicking on its icon. 7 Getting Started 7.1 Construction of a Simple Diagram Scicos contains a graphical editor that can be used to construct block diagram models of dynamical systems. The blocks can come from various palettes provided in Scicos or can be user-deﬁned. In this section, we describe how the editor can be used to construct simple models and how these models can be simulated. 7.1.1 Running Scicos Scicos is a Scilab toolbox included in the Scilab package. The Scicos editor can be opened by the scicos command −→ scicos; This command opens up an empty Scicos diagram named by default Untitled. Called with an argument, it can open up an existing diagram: −→ scicos my_diagram.cos; The Scicos main window displaying an empty diagram is illustrated in Figure 7.1. The look of the main window may be slightly diﬀerent under diﬀerent window managers and operating systems. Editor functionalities are available through pull-down menus placed at the top of the editor window. The manual page of each functionality is displayed by selecting Help in the Misc menu and then the item of interest in the menu. Some of the editor functionalities can also be accessed by clicking the right mouse button. Finally, keyboard shortcuts can be used for various operations. For example, typing an “r” activates the operation Replot, which centers and redraws the diagram. Keyboard shortcuts can be deﬁned by the user. 7.1.2 Editing a Model To construct a model, we need to access Scicos blocks. Scicos provides many elementary blocks organized in diﬀerent palettes that can be accessed using the operation Palettes in the Edit menu. This operation opens up a dialog box that includes the list of available palettes. By selecting a palette in the list, a new Scicos window opens up displaying the blocks available in this palette. Figure 7.2 shows the blocks in the Sources palette. Blocks from palettes can be copied into the main Scicos window by clicking ﬁrst on the desired block and then at the location where the block is to be copied in the Scicos window. 164 7 Getting Started Figure 7.1. Scicos editor main window. sinusoid generator 1 1 1 1 square wave generator Counter Modulo 3 read from C binary file read from .au sound file random generator sawtooth generator read from input file Figure 7.2. Blocks in the Sources Palette. The Sources and Sinks palettes contain respectively blocks generating signals without any inputs and blocks without any outputs such as scopes and right-to-ﬁle blocks. We start by copying from these palettes three blocks as illustrated in Figure 7.3. In this ﬁgure we have copied a sinusoid signal generator, a scope, and an event generator (Event Clock). The ﬁrst block generates on its unique output port a sine function. We wish to display this signal using the scope. This can be done by connecting the output of the signal generator to the input of the scope by clicking ﬁrst on the output port and then near the input port of the scope. The Align operation in the Edit menu can be used to align the two ports beforehand in order to make sure the link becomes horizontal. 7.1 Construction of a Simple Diagram 165 sinusoid generator Figure 7.3. Scicos diagram in construction. Blocks are copied from the palettes. The Event Clock is used to activate the scope block periodically with the desired frequency. Every time the scope is activated, it reads the value of the signal on its input port (which is nothing but the value of sin(t) generated by the signal generator). This value is then used to construct the curve that is displayed in the scope window. To specify that the scope block is activated by the event generator, the activation output of the event generator should be connected to the activation input of the scope. The result then looks like the diagram in Figure 7.4. This diagram is now complete. sinusoid generator Figure 7.4. Completed Scicos diagram. Note that Scicos diagrams contain two diﬀerent types of links. The regular links transmit signals and the activation links transmit activation timing information. By default, the activation links are drawn in red and the regular links in black, but this can be changed by the user. Also note that regular ports are placed on the sides of the blocks whereas activation inputs and outputs are respectively on the top and at the bottom of the blocks. Most blocks follow this convention, but the user can deﬁne new blocks with ports arbitrarily placed. The editor provides, through pull-down menus, many functionalities to change the look of the diagram and that of the blocks such as changing color, size, and font. 7.1.3 Diagram Simulation To simulate a diagram, it suﬃces to select the Run operation from the Simulate menu. Simulation parameters can be set by the Setup operation in the same menu. There we can, for example, adjust the ﬁnal simulation time. Running the simulation for the system in Figure 7.4 leads to the opening of a graphics window and the display of a sinusoidal signal. This window is opened and updated by the 166 7 Getting Started scope block. The simulation result is given in Figure 7.5. In this case, the ﬁnal simulation time is set to 30. The default value of the ﬁnal simulation time is very large. A simulation can be stopped using the stop button on the main Scicos window, subsequent to which the user has the option of continuing the simulation, ending the simulation, or restarting it. 10 5 0 −5 −10 + 0 3 6 9 12 15 18 21 24 27 30 Figure 7.5. Scope window for the system in Figure 7.4. A Scicos diagram can be modiﬁed and simulated again. Let us consider adding an integrator to the diagram in Figure 7.4 as illustrated in Figure 7.6. The integrator block gives as an output the integral of its input. This block comes from the Linear palette. We also replace the scope with a multi-input scope. Note that to create a split on a link, in particular here to create a link going to the integrator, oﬀ the link connecting the signal generator to the scope, the user must click ﬁrst on the existing link at the position where the split is to be placed. Also note that to create a broken link, the user can click on intermediary points before clicking on the destination, which is necessarily an input port. The simulation result for this new diagram is given in Figure 7.7. 7.1.4 Changing Block Parameters The behavior of a Scicos block may depend on parameters that can be modiﬁed by the user. These parameters can be changed by clicking on the block. This action opens up a dialog box showing current values of the block parameters and allowing the user to change them. For example, the dialog box associated with the integrator block is illustrated in Figure 7.8. The other blocks also have parameters. For example, in the sinusoid generator, we can set the frequency, the amplitude, and the phase. In the same way, certain properties of the scope window can be set by the parameters of the scope block. The value of a parameter can be deﬁned by any valid Scilab instruction. For example, the frequency of the sinusoid generator can be set to 2*%pi/10. The Scilab instruction can also include Scilab variables, but these variables must have been previously deﬁned 7.1 Construction of a Simple Diagram sinusoid generator 167 MScope 1/s Figure 7.6. Modiﬁed Scicos diagram. 1.0 0.5 0.0 −0.5 −1.0 + 0 3 6 9 12 15 18 21 24 27 30 3 6 9 12 15 18 21 24 27 30 5.0 2.5 0.0 −2.5 −5.0 + 0 Figure 7.7. Scope window after simulation of the modiﬁed diagram. in the “context” of the diagram. Such variables are called symbolic parameters. Symbolic parameters and the context will be discussed in the next section. Let us now consider more complicated examples in which the systems we want to model are given by diﬀerential equations. An integrator block is used to deﬁne each state of the system. SIR Model for Spread of Disease We consider a simple model of how a disease can spread in a community [5]. Let s(t) be the fraction of the population susceptible to getting infected as a function of time t. Also let i(t) be the infected (and infectious) fraction of the population and r(t) the recovered, and thus immune, fraction of the population. Then the SIR model can be expressed as follows: 168 7 Getting Started Figure 7.8. Dialog box of the integrator block. ṡ = −as(t)i(t), (7.1a) i̇ = as(t)i(t) − bi(t), ṙ = bi(t), (7.1b) (7.1c) where a and b are positive parameters. To model this system in Scicos, we set the state of one integrator to s, another one to i, and a third one to r. It is then straightforward to construct the inputs of the integrators from their outputs, as can be seen in Figure 7.9. Here we have set a to 1 and b to 0.3. For the reader less familiar with block diagrams, the × and Σ blocks multiply and add inputs respectively. Since we are calling the outputs of the integrator blocks s, i, and r, the Scicos implementation of the diﬀerential equation in (7.1a) can be thought of as the integral equation s(t) = − t s(τ )i(τ )dτ + s(t0 ), t0 where s(t0 ) is the initial condition of s at time t0 . Similarly (7.1a), (7.1c) are implemented as integral equations in the Scicos diagram. However, they are integrated as diﬀerential equations by Scicos using a numerical ODE solver. The simulation result for this SIR model is given in Figure 7.10. The variable s is initialized to 0.999, and i to 0.001. Chaotic Dynamics of a Rössler Attractor The Rössler system [35] given below has chaotic behavior for certain values of the parameters a, b and c: ẋ = −(y + z), ẏ = x + ay, ż = b + z(x − c). This system is modeled in Figure 7.11 with a = b = 0.2 and c = 5.7. The initial conditions are set to zero. The 2D scope is used to plot y against x. The result is given in Figure 7.12. 7.2 Symbolic Parameters and Context 169 −1 S − − I Mux .3 R Figure 7.9. Scicos implementation of the SIR model. 1.00 0.75 0.50 0.25 0.00 + 0 3 6 9 12 15 18 21 24 27 30 Figure 7.10. The simulation shows, as expected, that the percentage of the recovered population is increasing and that of the susceptible population is decreasing. The percentage of population infected reaches its maximum at a time called the peak of the epidemic. 7.2 Symbolic Parameters and Context Often it is useful to use symbolic parameters to deﬁne block parameters. This is particularly the case if the same parameter is used in more than one block or if the parameter is computed as a function of other parameters. Symbolic parameters are simply Scilab variables that must be deﬁned in the context of the diagram before being used in the deﬁnition of block parameters. Each Scicos diagram contains a context. The context is simply a Scilab script used to deﬁne Scilab variables, which can then be used to deﬁne block parameters. To access the context of the diagram, use the Context button in the Edit menu. This opens up 170 7 Getting Started .2 + + Y − − .2 X + + + − Z 5.7 Figure 7.11. Scicos implementation of the Rössler attractor. 15 12 9 6 3 0 −3 −6 −9 −12 + −15 −15 −12 −9 −6 −3 0 3 6 9 12 15 Figure 7.12. Output of the 2D scope for the Rössler model. 7.2 Symbolic Parameters and Context 171 an editor. The user can use this editor to write a Scilab script, that is, a set of Scilab commands that are executed by Scilab after a click on the ok button. For example, if the command C=1 is placed in the context, then C can be used to deﬁne a block parameter. If the value of C is changed in the context, the parameter of the block is automatically updated accordingly. Let us now consider an example in which the context is very useful for deﬁning a generic diagram. We consider the construction of a sampled-data observer for a linear system. The system is considered to be modeled as a continuous-time state-space linear system: ẋ = Ax + Bu, (7.2) y = Cx, (7.3) where A, B, C are constant matrices. In developing controls for a system like this, it is often easier to develop them in terms of the state x. State feedback controls are one example. But the only thing that may be available in practice is the output y. A state observer is another dynamical system, which accepts as input u and y, and gives as an output x̂, which has the property that x − x̂ goes to zero independent of the initial conditions of either (7.2) or the observer. Observers have the nice property of being able to return to giving good estimates after disturbances. The speed with which the error goes to zero can be speciﬁed to give good estimation but avoid being overly sensitive to disturbances. A continuous-time observer can be constructed as follows: x̂˙ = Ax̂ + Bu + K(y − C x̂). (7.4) The matrix K must be chosen so that the eigenvalues of A − KC have negative real parts. This ensures that the estimation error x̃ = x̂ − x satisfying x̃˙ = (A − KC)x̃ will go to zero. The discrete-time (sampled data) observer is obtained by ﬁrst constructing the corresponding continuous-time observer using the method of pole placement, and then discretizing it. We begin by taking ﬁxed dimensions and randomly generated matrices. We will then explain how to make this a generic diagram. The Scilab script to perform this procedure with random matrices, placed in the context of the diagram, is the following: n=5;m=2;dt=.2; A=rand(n,n);A=A-max(real(spec(A)))*eye() B=rand(n,1);C=rand(m,n);D=zeros(m,1); x0=rand(n,1); K=ppol(A’,C’,-ones(x0))’; Ctr=syslin(’c’,A-K*C,[B,K],eye(A),zeros([B,K])) Ctrd=dscr(Ctr,dt) [Ad,Bd,Cd,Dd]=abcd(Ctrd) The original system is constructed with random matrices, and the A matrix is modiﬁed to make sure the system is not exponentially unstable. The Scilab pole placement (eigenvalue assignment) function ppol is used to obtain the observer gain matrix K, and dscr is used to discretize the observer. dt is the sampling period. The Scicos diagram in Figure 7.13 is used to simulate the observer performance. The input u is set to sin(t) and the estimation error is displayed using a scope. The continuoustime and discrete-time state-space linear system blocks are used to model the original system and the observer. The parameters of the ﬁrst are set as illustrated in Figure 7.14 and the second as in Figure 7.15. The ﬁrst block also outputs its internal state to be used 172 7 Getting Started sinusoid generator x+=Ax+Bu y=Cx+Du xd=Ax+Bu y=Cx+Du Mux + − Demux Figure 7.13. Scicos diagram including the system and the discrete-time observer. Figure 7.14. Dialog box of the block realizing the original system; the output of the block contains also its state. Figure 7.15. Dialog box of the discrete-time linear system realizing the observer. 7.3 Hierarchy 173 for generating the estimation error displayed by the scope. dt is used to deﬁne the period of the Event Clock. The use of symbolic parameters is particularly useful in this case because it allows us to construct a generic system observer diagram. To change the system matrices A, B, and C, the size of the system, the size of the outputs, or the discretization time, we only have to modify the deﬁnition of the m, n, and system matrices. No change is made to the diagram. In fact, by rewriting the context as follows we can make the diagram completely generic: load(’datafile’) K=ppol(A’,C’,-ones(x0))’; Ctr=syslin(’c’,A-K*C,[B,K],eye(A),zeros([B,K])) Ctrd=dscr(Ctr,dt) [Ad,Bd,Cd,Dd]=abcd(Ctrd) We just have to make sure that the data ﬁle datafile contains variables A, B, C, D, x0, and dt before launching Scicos. This ﬁle can be created in Scilab as follows: −→ save(’datafile’’,A,B,C,x0,dt) Remark If the context contains many lines of Scilab code, it is convenient to place the code in a separate script ﬁle and execute it with a single exec command in the context. However, if the ﬁle being executed by the exec command is changed when the diagram is already open, and the user wants the modiﬁcation to be taken into account, he or she should do an Eval because Scicos has no way of knowing whether the ﬁle has been changed. It should also be noted that using a separate script ﬁle implies that the Scicos diagram is not self-contained and this script ﬁle must always accompany the diagram. 7.3 Hierarchy It is not good modeling practice to place too many blocks in a diagram at the same level because the diagram becomes incomprehensible and diﬃcult to read and debug. For large systems, it is useful to use the Super block facility to construct a hierarchical model. A Super block looks like any other block: it can be moved, copied, resized, etc., but its behavior is deﬁned by the Scicos submodel within it. 7.3.1 Placing a Super Block in a Diagram There are two ways to place a Super block in a Scicos diagram. Region-to-Super-block If the submodel the user wants to place in a Super block already exists in the model, then the Region-to-Super-block operation of the Diagram menu can be used to place it in a Super block. This is done by specifying the region of the diagram containing the submodel concerned. This region is automatically replaced with a Super block with the appropriate number of input and output ports. Let us consider a simple example. In the diagram of Figure 7.13, we used a linear system block to model the original system, but since we also needed its internal state, we 174 7 Getting Started had to concatenate it to the output, which we then had to split using the Demux1 block in order to generate separately the state x and the output y. The diagram would have been much clearer if the linear system block had two output ports generating x and y. Unfortunately, that is not the way this block works. But the combination of the linear system block and Demux does exactly what we want. So it is natural to consider constructing a Super block out of these two blocks. This can be done using the Region-to-Super-block functionality and selecting a region containing these two blocks. The result is illustrated in Figure 7.16. u x+=Ax+Bu y=Cx+Du y Mux x Kc x_hat Figure 7.16. The linear system and Demux blocks are placed in a Super block. Super Block in Palette A Super block can also be placed in a diagram by copying the empty Super block provided in the Others palette. The desired submodel can then be constructed inside the Super block. 7.3.2 Editing a Super Block To edit the content of a Super block, it suﬃces to click on it to “open” it. This opens up a new Scicos editor window resembling the main Scicos window displaying the content of the Super block. For example, by opening the Super block in Figure 7.16, the model illustrated in Figure 7.17 is displayed in the new editor. Note that new blocks have been added to the submodel that were not present in the original model. These are the input, output port blocks. These blocks deﬁne the connection from the inside of a Super block to the outside world. There are exactly as many input (respectively output) port blocks inside the Super block as there are input (respectively output) ports on the Super block. 1 Demux is network terminology for demultiplex and it means to separate channels. 7.4 Save and Load 1 xd=Ax+Bu y=Cx+Du System 175 1 Demux 2 Figure 7.17. Inside the Super block. The Super block can be edited just like the main Scicos diagram, and when the editing is complete, closing the diagram will commit the changes to the main diagram. If the number of input or output port blocks is modiﬁed, after closing the Super block, the number of ports of the Super block in the main window is adjusted accordingly. Note that the port blocks must be numbered consecutively and the corresponding ports on the Super block are also numbered counting consecutively from the top to the bottom. Each Super block has a context. The values of symbolic parameters used in the blocks of a Super block are normally deﬁned in the context of the Super block or in the context of the diagram. 7.4 Save and Load 7.4.1 Scicos File Formats Scicos diagrams can be saved in two diﬀerent formats. The most common format is a binary ﬁle for which by convention the extension .cos is used. This ﬁle is simply a binary Scilab data ﬁle and can be loaded in Scilab (assuming its name is myfile.cos) using the command −→ load myfile.cos There are three Scilab variables inside .cos ﬁles: • scs m: this Scilab structure contains all the information concerning the diagram • %cpr: this Scilab structure contains the result of the compilation. It is an empty structure if the diagram had not been compiled before being saved (diagram compilation will be discussed later) • scicos ver: a string indicating the version of Scicos that has produced the diagram. Normally, the user never needs to load this ﬁle in Scilab directly except for batch processing, which is discussed in Chapter 11. The loading is done in the Scicos environment using the Load button in the File menu. The ﬁle can also be loaded when launching Scicos as follows: −→ scicos myfile.cos; Scicos diagrams can also be saved in text format. The extension of text ﬁles is .cosf. These ﬁles are in fact Scilab scripts that can be executed using the exec command, provided Scicos libraries are loaded in the environment. Note that Scicos libraries are not loaded by default into Scilab. They are loaded by the function scicos. Just as in the binary case, the loading should be done by Scicos. The expert user may be able to modify a diagram by editing the corresponding text ﬁle. But in most cases, it is much easier to use the Scicos graphical editor to do the editing. Note that the name of the saved ﬁle, except the extension, corresponds to the name of the diagram. New diagrams are named by default Untitled. The name of the diagram 176 7 Getting Started can be changed using the Rename button in the File menu. The name is also changed if the ﬁle is saved, using the Save As button, under a diﬀerent name. In that case, the name of the diagram becomes the name of the ﬁle (without the extension). 7.4.2 Super Block and Palette The contents of all the Super blocks present in a diagram are saved when the diagram is saved. So there is no need to save the content of a Super block unless we want to use it in the construction of another diagram. Super blocks can be saved just like any diagram in both .cos and .cosf formats. The saved ﬁle is identical to that of a main diagram. It can be loaded into a main Scicos diagram or inside a Super block. A palette is also a standard Scicos diagram. Any Scicos diagram can be loaded as a palette. This means that blocks can be copied from it but the diagram itself cannot be edited. The button Load as Palette can be used to load any existing Scicos diagram as a palette. To avoid searching for palette ﬁles often used, Scicos provides the possibility of deﬁning a list of palettes with their names and locations (see Pal editor in the menu File). These palettes can then be loaded simply by clicking on their names when the button Palettes is clicked. The default Scicos palettes are originally placed in this list. 7.5 Synchronism and Special Blocks When two blocks are activated with the same activation source (for example the same Event Clock), we say that they are synchronized. In that case, they have the same activation times, and if the output of one is connected to the input of the other, the compiler makes sure the blocks are executed in the correct order. Synchronism is an important property. Two blocks activated by two independent clocks exactly at the same time are not synchronized. Even though their activations have identical timing, they can be activated in any order by the simulator. On the other hand, two activations can be synchronous but not have exactly the same timings. For example, an activation can be a subset of another activation. Consider two activation sources, one generating a sequence of events with frequency 2 and another with frequency 1. In this case, half the events generated by the fast clock are simultaneous with the events of the slow clock. If these two activations are generated by two independent clocks, then they are not synchronized. So even when the two events are simultaneous, the blocks they activate can be activated in any order by the simulator. To enforce synchronism in this case, we have to make sure the two activations have the same source (same Event Clock for example). In this example this means that the slow activation is obtained from the fast activation. There are two very special blocks in Scicos: the If-then-else and the event select blocks. They are in the Branching Palette. Even though they look like standard Scicos blocks and can be manipulated by the editor as such, strictly speaking they are not blocks. We shall give a detailed presentation of these blocks and the notion of synchronism in Chapter 8. Here, it suﬃces to say that these blocks are the only blocks that generate events synchronous with the incoming event that had activated them. These blocks, which can be considered as counterparts of conditional statements If-then-else and Switch in the C language, are used for conditional subsampling of activation signals. Consider the diagram in Figure 7.18. The If-then-else block redirects the event it receives toward its then output port, activating the delay block 1/z if its regular input is positive. If not, the activation goes out through the else port, which is not connected to 7.5 Synchronism and Special Blocks 177 If in>0 square wave generator then + else 1/z + Figure 7.18. A synchronous Scicos diagram. anything. The square wave generator generates a series of alternating ones and minus ones. Every time the 1/z block is activated, it adds the output of the generator to its content (initially set to zero). Since the activation takes place only when the output of the generator is positive, the content of 1/z goes up by one at every other activation of the clock. This is conﬁrmed by the result of the simulation illustrated in Figure 7.19. The period of the clock in this simulation is set to 1. 12 ♦ 9 ♦ ♦ ♦ 6 ♦ ♦ ♦ 3 ♦ ♦ 0 + ♦ 0 ♦ ♦ ♦ ♦ ♦ ♦ ♦ ♦ ♦ ♦ 2 4 6 8 10 12 14 16 18 20 Figure 7.19. Simulation of Figure 7.18 showing that the counter goes up by one at every other activation of the clock. 8 Scicos Formalism We have seen, in the previous chapter, how simple Scicos models can be constructed and simulated. To be able to construct more complex models, in particular models involving conditional and unconditional subsampling, it is important to understand the formalism on which Scicos is based. This is the subject of this chapter. The information in this chapter is also essential for reading the next chapter, which discusses the construction of new blocks. 8.1 Activation Signal A simulation function is associated to each Scicos block. This function is called when the block is activated. The activation times for a given block are speciﬁed by the activation signals received on its activation input ports. 8.1.1 Block Activation Memoryless Case Consider the diagram in Figure 8.1. In this diagram, the activation signal (event) activates synchronously the three blocks Source, Func.1, and Func.2. This synchronous activation occurs because they are activated by the same activation signal. The order in which the associated functions are called is determined by the Scicos compiler. If we assume that the Func blocks are memoryless immediate functions, Func.1 needs the value of the output of the Source block before being called, and similarly Func.2 needs the output of Func.1, then the compiler orders the blocks accordingly. In this example, the activation times of the three blocks were the same. However, having the same activation time does not necessarily imply synchronism. Consider the diagram in Figure 8.2. In this case the two event generators are identical and the events they generate have the same timing but are not synchronized. When a diagram contains more than one activation source, the compiler computes an order for each activation separately. During the simulation, it is the timing of the activations that determines the order in which the events are ﬁred, and consequently the blocks are activated. If, as is the case here in Figure 8.2, two events have identical timing, then the order of ﬁring is arbitrary. So Func.2 could be executed before or after the other two blocks. Depending on which one occurs, the result of the simulation can be completely diﬀerent. 180 8 Scicos Formalism Event at time t Source Func. 1 Func. 2 Figure 8.1. A synchronous diagram. Source Event at time t Event at time t Func. 1 Func. 2 Figure 8.2. An asynchronous diagram. Discrete Blocks with Internal State If a block has an internal state, then when the block is activated, the simulation function corresponding to the block may be called more than once. In particular, if the block has an internal state and an output, then it will be called twice. Consider the diagram in Figure 8.3. The delay block is simply a memory block holding in its internal state the value of its input at the time of activation. Its output corresponds to the previous value of the state. At its activation, this block is called ﬁrst with flag 1 so that it can compute its output (in this case a simple copy of the state), and then it is called with flag 2 to compute its state (copy of its input). Event at time t Source 1/z Func. 2 Figure 8.3. A synchronous diagram with a memory (delay) block. The order in which block functions are called with flag 2, contrary to the case of flag 1, has no importance because the result has no bearing on the outputs of the blocks. 8.1 Activation Signal 181 But in this case, even the order with which the blocks are called with flag 1 is not the same as in the case of the diagram in Figure 8.1. Here, the 1/z block does not require the value of its input when it computes its output. This means that this block can very well be activated before the Source block. But in any case, Func.2 comes after the 1/z block. Event at time t Func. 1 1/z Func. 2 Figure 8.4. Diagram with feedback but without algebraic loop. The blocks whose outputs do not depend directly on their inputs must be speciﬁed. Otherwise the compiler cannot compute the correct order of execution. In the example of Figure 8.3 even if 1/z is not speciﬁed as such a block, the blocks can be ordered. However, in the example in Figure 8.4, the compiler cannot compute the order of block activations due to the presence of an algebraic loop. This is why the dep u property of the 1/z block is set to false and the compiler ﬁnds that the block functions must be called with flag 1 in the following order: 1/z followed by Func.2 followed by Func.1. And then 1/z is called with flag 2. General Case A Scicos block can be more complex than just an internal state and an output. The event generator, for example, has an output activation port. We have seen in the previous chapter blocks with continuous-time internal states such as the integrator block. A general Scicos block can be fairly complex and will be discussed in the next chapter. 8.1.2 Activation Generation The event generator in diagrams of Figures 8.1 through 8.4 generates an activation signal at a given time. This output activation is preprogrammed at the output activation port of this block. The block itself is never activated. Since only one activation can be preprogrammed at any output activation port, a Scicos block cannot serve as a clock event as we have seen in the previous chapter. In fact, the event clock is not a basic block; it is a Super block containing an event delay block with a feedback, as illustrated in Figure 8.5. Initially an activation is programmed on the output activation port of the delay block. This activation, when ﬁred, activates the delay block itself. Since this block has no internal state and no (regular) output, the corresponding function is not called with flags 1 and 2. Instead, it is called with flag 3 because it has an output activation port. In this case, the function schedules a new activation on its output activation port by returning the delay (in this case T), after which the scheduled activation must be ﬁred. When this activation is ﬁred, it schedules in turn a new activation, and so on. This way, an event clock with period T is constructed. 182 8 Scicos Formalism Delay T 1 Figure 8.5. An event clock. 8.2 Inheritance In the examples of the previous chapter, we had encountered many blocks without activation input ports. Consider the diagram in Figure 8.6. Normally the Func.1 block should not be activated because it receives no activation signal. But by convention, in the absence of input activation ports, a block inherits its activation from its regular input. Event at time t Source Func. 1 Figure 8.6. The block Func.1 is activated by inheritance. The inheritance mechanism is implemented at a precompilation phase. For example, in the case of the diagram in Figure 8.6, the precompiler adds the missing activation port and link as illustrated in Figure 8.7. The user never sees this diagram; the precompilation phase is completely transparent to the user. If the block has more than one regular input, the inheritance mechanism places as many activation input ports on the block as the block has regular input ports. See the diagram in Figure 8.8 and the diagram obtained after the precompilation phase illustrated in Figure 8.9. The + block outputs an activation signal with activation times corresponding to the union of input activation times. We see here that a block can have more than one activation input port. In this case, the block is activated when it receives an event on one or the other of its input activation ports. Thus the block is activated at the union of the two activation times. This explains how Func.2 inherits its activation. 8.3 Always Active Blocks 183 Event at time t Source Func. 1 Figure 8.7. The diagram after the precompilation phase. Event at time t Event at time s Source Func. 1 Func. 2 Source Figure 8.8. Blocks Func.1 and Func.2 are activated by inheritance. 8.3 Always Active Blocks There is another way a block can be active, and that is by being declared always active. Consider the sinusoid generator in the diagram of Figure 7.4 in Chapter 7. This block is not explicitly activated, and it clearly does not inherit. But it is active because it is declared always active. This is done by setting the block’s dep t property to true. An always active block is, at least as far as the formalism is concerned, always active. But during the simulation, the block is activated only when needed. For example, in the diagram of Figure 7.4, the output of the sinusoid generator should evolve continuously. But since this value is used only by the scope at its activation times, during the simulation, the function associated with the sinusoid generator is only called at these times. In the diagram of Figure 7.6, the integrator block is also declared always active. But even if it were not, it would not have made any diﬀerence because it would have inherited always activation from the sinusoid generator. In this case, since the output of the sinusoid generator aﬀects a diﬀerential equation, the times that the function associated with the sinusoid generator is called, in addition to the activation times of the scope, are the times (integration time steps) imposed by the numerical solver. 184 8 Scicos Formalism Event at time t Event at time s Source Func. 1 Func. 2 Source Figure 8.9. The diagram after the precompilation phase. 8.4 Constant Blocks A block without input, not explicitly activated and not being always active, is a constant block. A constant block’s outputs do not evolve as a function of time. During the simulation, the function associated with a constant block is called only at the initialization phase. The same holds for blocks inheriting from constant blocks. See, for example, the diagram in Figure 8.10. If Source is not always active, then it is a constant. If Func.1 is not always active, it inherits its activation, or more speciﬁcally its absence of activation, from the Source block. The Func.1 block’s function is called just once to compute its output after the initialization phase. This is, of course, reasonable because the output of Func.1 does not evolve. Source Func. 1 Figure 8.10. Inheritance from a constant block. 8.5 Conditional Blocks In many applications, in particular in signal processing, signals with diﬀerent frequencies interact in the same model. A common situation is the decimation operation, which consists in generating a signal B from a signal A by taking one out of every n values of A. The frequency of signal B is then that of signal A divided by n. To implement this operation, one might consider using two independent clocks to ﬁx the two frequencies. This is done in the diagram of Figure 8.11 using a sample and hold block. 8.5 Conditional Blocks Fast Clock Slow Clock Source S/H 185 Func. Figure 8.11. Incorrect way of implementing decimation. But we have seen that two activation sources generate asynchronous events. So even if we set the period of the slow clock to exactly n times that of the fast clock, at times when the two events have the same time, the order of block execution is not predictable. We see through this example that there is a need for being able to deﬁne synchronism between two nonidentical activation signals. The events generated by the fast clock do not always have the same timing information as that of the slow clock. However, when they do (one out of every n times), they must be considered synchronized. This is particularly important when the fast and slow signals are used in common operations later such as being added together. This type of synchronous signal can be constructed in Scicos using two special blocks: the If-then-else and the event select, which can be found in the Branching palette. Strictly speaking, these blocks are not Scicos blocks. They do not generate new activation signals. Rather they redirect the activation they receive. Clock If in>0 then Source else S/H Func. Figure 8.12. Conditional subsampling. Consider the diagram in Figure 8.12. The sample and hold block, when activated, copies its input on its output. The If-then-else block activates the sample and hold block when the value of its input is positive. The output of this block is then a signal with an activation that depends on values of the signal generated by the Source block. 186 8 Scicos Formalism We say then that this signal is obtained by conditional subsampling. In this case, since the Source and S/H have identical activation sources (remember that the If-then-else block only redirects activations), they are considered synchronous by the Scicos compiler and ordered properly. In particular, in this case we can be sure that the output of the S/H block is always positive. Now we return to the decimation problem previously discussed. The diagram in Figure 8.11 did not function properly because the Source and the S/H blocks were not synchronized. Using the If-then-else block, we can implement the decimation operation correctly as illustrated in Figure 8.13. The Counter Modulo block counts up to n − 1 and then returns to zero. The S/H block is activated only when the output of the counter block is zero (activation through the else branch), that is, one out of every n times. And in this case, the Source and S/H blocks are synchronized. Fast Clock Counter Modulo n Source If in>0 then S/H else Func. Figure 8.13. A correct way to implement decimation. The (Super) block freq div can replace the Counter Modulo and If-then-else blocks. The combination Counter Modulo and If-then-else provides a versatile mechanism for implementing frequency division. The division factor can be set by ﬁxing the value of n, and the phase by the initial state of the counter. In fact, that is the way the freq div (Super) block, available in the Events palette, is constructed. Conditional blocks work also for “always active” activation. Consider the diagram in Figure 8.14. The If-then-else block is always active (inheriting its activation from the sine generator) but activates the S/H block only when its input is positive. So the integrator receives on its input sin(t) if it is positive, and zero otherwise. Zero is the last value output by S/H before deactivation. When a block is not active, its output remains constant. The simulation result is given in Figure 8.15. 8.5 Conditional Blocks 187 If in>0 then sinusoid generator else S/H MScope 1/s Figure 8.14. Conditional blocks function also with continuous-time activation. 10.0 7.5 5.0 2.5 0.0 + 0 2 4 6 8 10 12 14 16 18 20 2 4 6 8 10 12 14 16 18 20 2 1 0 −1 −2 + 0 Figure 8.15. Simulation result. At the bottom we have the output of the sinusoid generator Rt sin(t) and on top 0 max(sin(τ ), 0)dτ . 9 Scicos Blocks A block is a basic module from which Scicos diagrams are constructed. A block corresponds to an operation, and by interconnecting blocks through links, we construct (implement) an algorithm. So in a sense, blocks in Scicos are the counterpart of functions in a programming language, and as in programming languages, some blocks are provided through standard libraries, while others are developed by the user. Most Scicos users never need to develop a new block because standard libraries contain a large number of blocks providing most of the elementary functionalities needed in the construction of Scicos diagrams; moreover, blocks such as Mathematical Expression allow the user to deﬁne block functionality in terms of Scilab expressions without loss of eﬃciency (the Scilab expression in this case is compiled). That is why readers can skip this chapter on a ﬁrst reading and come back to it when they want to learn about block construction. 9.1 Block Behavior A Scicos block can be a complex entity. It can have multiple vector inputs and outputs, a continuous-time vector state, a discrete-time vector state, zero-crossing surfaces, etc., as shown in Figure 9.1. But most Scicos blocks do not contain all of these features simultaneously. The behavior of a block is primarily speciﬁed by the way it is activated. We have seen in the previous section that a block is activated in three diﬀerent ways. The way the block learns about the way it is activated is via an activation index nevprt. 9.1.1 External Activation A block is activated when it receives activations on its activation input ports. As we have seen in the previous section, such activations can also be inherited when the block has no activation input ports, in which case the corresponding activation ports are added by the Scicos compiler and the activations are considered to be external. In the case of external activation, the activation index nevprt is a positive integer obtained by the following formula: nevprt = n j=1 ij 2j−1 , (9.1) 190 9 Scicos Blocks input activations i2 continuous-time state i1 state derivative discrete-time state inputs outputs mode vector zero-crossing surfaces tevo activation outputs Figure 9.1. Scicos block: an inside look. where n is the number of input activation ports and ij is equal to one or zero depending on whether an activation has been received on port j. Note that nevprt corresponds to the binary coded number in in−1 · · · i1 . In some cases, block behavior does not depend on nevprt. For example, the summation block performs an addition regardless of the value of nevprt. But the Selector block uses nevprt to select the input that should be copied into the output. Event Activation When a block is activated in this way by an event, say at time te , it can update its outputs: − y(te ) = f1 (te , x(t− e ), z(te ), u(te ), μ(te )). (9.2) − In the above, te denotes the event time, and x(t− e ) and z(te ) denote respectively the continuous-time and discrete-time states just before the occurrence of the event. y(te ) denotes the outputs of the blocks (which can be more than one and each a vector of arbitrary size). u(te ) denotes similarly the inputs. The function f1 depends also on nevprt , which is part of μ: μ = (nevprt , m, p). Here m and p represent respectively the mode and the simulation phase. We shall discuss them later. If the block contains output activation ports, upon activation it can program events on them by providing the delay time to event ﬁring on each activation output port: − tevo = f3 (te , x(t− e ), z(te ), u(te ), μ(te )). (9.3) Finally, if it has internal states, it can update them (both continuous-time and discretetime): − (9.4) [z(te ), x(te )] = f2 (te , x(t− e ), z(te ), u(te ), μ(te )). The vector tevo is generated at block activation (if the block has any output activation ports). If the jth entry is positive, then an event is programmed on the jth output activation port of the block. The activation date of this event is obtained by adding te to this entry. 9.1 Block Behavior 191 Continuous-Time Activation If a source of block activation is “always activation,” then activation occurs over time intervals and not at speciﬁc times as in the case of event activation. See, for example, the diagram in Figure 8.14. In this case, the output calculation given in equation (9.2), thanks to the continuity assumption of all the signals over a continuous-time activation period, can be simpliﬁed as follows: y(t) = f1 (t, x(t), z(t), u(t), μ(t)). (9.5) The output activation programming and state updates do not apply in this case, but if the block has a continuous-time state, it evolves according to the following diﬀerential equation, (9.6) ẋ = f0 (t, x(t), z(t), u(t), μ(t)) over the activation period, which we call the simulation phase 2, as opposed to event activation instances which are referred to as simulation phase 1 . Note that z(t), like nevprt , is constant over this period. So is the mode m if there is one. Mode and Zero-Crossing The mode parameter is not part of the Scicos formalism. It is introduced to facilitate the implementation of the numerical solver. Most blocks that a user may have to construct don’t need any mode or zero-crossing, and the user can use existing blocks with mode and zero-crossing without a complete understanding of the way they work. For the sake of completeness, however, we give a short presentation below. More details can be found in the examples provided later in this chapter. Numerical integrators can experience step size control and other problems at places where the functions being integrated are not continuously diﬀerentiable. If f1 or f0 is not smooth (continuously diﬀerentiable) at some points, then mode is used in such a way as to make sure the numerical solver never encounters these points of discontinuity inside an integration interval. Consider the following simple example: y = u if u ≥ 0, and y = −u otherwise. This function realizes the absolute-value function and is not diﬀerentiable at 0. In this case, a mode can be deﬁned to specify at the start of the integration period whether u is positive. To make sure the integration stops when the sign of u changes, a zero-crossing surface is introduced at zero. During the integration period (which could end because of the zerocrossing), the output y is computed as follows: y = u if m = 1, and y = −u otherwise. After the zero-crossing the mode is recomputed and the integration continues. The computation of the mode and the zero-crossing surface are performed by the block. If the simulation phase is 1, we have (9.7) [m(t), s(t)] = f9 (t, x(t), z(t), u(t), μ(t)). The zero-crossing surfaces s(t) may be present even if the block has no mode. When a zero-crossing occurs, the block is internally activated, which means that (9.3) and (9.4) are called with nevprt = −1. The blocks in the Threshold palette, for example, have zero-crossings but no modes. 9.1.2 Always Activation A block can be declared always active. This is, for example, the case of the sine generator block. In this case the nevprt index is zero. Always activation can be thought of as 192 9 Scicos Blocks an activation received on a ﬁctitious activation input port 0 that would correspond to nevprt = 0. Always activation is a special case of continuous-time activation where the activation period is equal to the period of simulation. So the behavior of the block is speciﬁed again by equations (9.5) and (9.6) with nevprt set to zero. 9.1.3 Internal Zero-Crossing When a zero-crossing occurs inside a block, this block is activated with nevprt equal to −1 at the time of crossing. If this time is denoted by te , then the state update formula is given by (9.4) with nevprt = −1. This type of activation is used, for example, in the bouncing balls demo block of Scicos to change the direction of balls (characterized by the continuous-time state of the block) after a collision. 9.2 Blocks Inside Palettes Scicos libraries of blocks are called palettes. There are a number of standard palettes available in Scicos providing many elementary blocks. The available palettes are the following: • Sources library. It includes signal generators such as the square-wave, sawtooth, and sine-wave generators, and also read from ﬁle blocks. The read from ﬁle blocks generate signals from data stored on ﬁles. • Sinks library. It includes blocks that store signals on a ﬁle or display signals on a screen. These blocks have no output ports. • Branching library. This palette contains switches, multiplexers, selectors, and a relay. It also includes two very special blocks that redirect activation signals: the If-then-else and the events select blocks. • Non linear library. The blocks in this palette realize memoryless nonlinear operators such as trigonometric functions, multiplication, logarithm, inversion, and saturation. The palette also includes interpolation and table lookup blocks, and a special block called Mathematical Expression used to express a nonlinear operator in terms of a Scilab expression. The constraint on the scilab expressions is that only scalar expressions can be used, but the evaluation is very eﬃcient. • Events library. This library includes blocks for generating and manipulating events (activation signals). It also includes the block Stop, which halts the simulation if activated. • Threshold library. The blocks included in this palette can generate events when a signal crosses zero. • Others library. This palette includes a number of blocks useful for the construction of new blocks: Cblock2, Scifunc, GENERIC,. . . . These blocks are discussed later in this chapter. It also includes logical and relational blocks. It also includes the Text block, which is not a real block, but a way to place text inside a diagram. • Linear library. This palette contains blocks realizing linear operators such as summation, gain, and many dynamical linear systems in both continuous and discrete time. Delay blocks and jump linear systems are also included in this palette. The above palettes provide general-purpose blocks. Specialized palettes can be added by the user. Two such palettes are included. One is an electrical palette. The other is a Thermo-Hydraulic palette. 9.4 Super Block and Scifunc 193 9.3 Modifying Block Parameters Most blocks have parameters that must be changed by the user for speciﬁc applications. Block parameters can be changed by “opening” a block (clicking at the center of the block). This action opens a dialog box, which can be used to deﬁne new parameter values. Blocks parameters are stored in two diﬀerent ways: 1. Symbolically: the exact expression entered for each parameter in the dialog box is stored as Scilab text. For example, the user can enter 2*%pi, and this expression is saved and shown the next time the dialog box is opened. 2. Numerically: the expression is evaluated and the numerical value is saved. It may seem redundant to store parameter values in two diﬀerent ways but there are good reasons for doing this. One reason is that it is more convenient to see 2*%pi the next time the dialog box is opened than it is to see 6.2831853. But the main reason is that the values of the parameters can be deﬁned in terms of Scilab variables already deﬁned in the context of the diagram. For example, a parameter can be set to sin(x)*y. The numerical value of this parameter is evaluated based on the values of x and y deﬁned in the context of the diagram. Keeping the symbolic expression allows Scicos to update the value of the parameter when x or y is modiﬁed in the context. In this case, x and y are symbolic parameters. 9.4 Super Block and Scifunc In addition to the blocks available in Scicos palettes, the user can create and use custom blocks by adapting existing blocks. There are various ways of doing this. • using the Super block facility, • using Scifunc blocks, which allow block functionality to be deﬁned on-line by Scilab expressions. • using C or Scilab programs, dynamically linked with, or loaded in, Scilab. The third method, which will be presented in the next section, gives the best results as far as simulation performance is concerned, provided a C program is used. 9.4.1 Super Blocks To construct a Super block, the user should copy the “Super block” block from the Others palette into the Scicos window and click on it. This will open up a new Scicos window in which the Super block should be deﬁned. Super blocks can also be deﬁned using the Region-to-Super-block facility, which creates a Super block from existing blocks in a speciﬁed region of the diagram. The construction of the Super block model is done in the same way as the earlier diagrams, that is, by copying and pasting blocks from various palettes into the Super block’s editor window and connecting them. Input and output ports of the Super block should be speciﬁed by input and output block ports available in the Sources and Sinks palettes. Super blocks can, of course, be used within Super blocks. A Super block can be saved in various formats. If the Super block is of interest in other constructions, it can be converted into a block and placed in a user palette. This can be done in various ways: 194 9 Scicos Blocks • The Save-as-Interf-Func facility can save the Super block in two diﬀerent ways: as a block or as a Super block. The diﬀerence is that in the block case, the content of the Super block is no longer visible, whereas in the Super block case, the saved Super block would have exactly the same behavior as the Super block being saved. Save-as-Interf-Func should be used inside the Super block of interest. By default, in the block case, the dialog menus of the new block are generated automatically using the dialog menus of all the blocks within the Super block. This is often cumbersome, and most users need to edit the new interfacing function. This Scilab function is easily customizable. Once the Super block, or more speciﬁcally the corresponding Scilab interfacing function, is saved, either as a block or as a Super block, it can be used in Scicos. This can be done using Add-new-block in the Edit menu. The block can also be placed in a palette for future use. See Section 9.6. • Using Code-Generation. Under certain conditions, the dynamics of the content of a Super block can be expressed using a single C program. Code-Generation generates this program automatically; see Section 12. If the Super block is used only in a particular construction for one Scicos diagram, then the Super block need not be saved. A click on the Exit will close the Super block window and activate the main Scicos window (or another Super block window if the Super block was being deﬁned within another Super block). Saving the block diagram automatically saves the content of all Super blocks used inside of it. A Super block, like any other block, can be duplicated using the Copy button. In that case all the contents of the Super block are duplicated. 9.4.2 Scifunc The Scifunc block enables using the Scilab language for deﬁning, on-line, new Scicos blocks. Block information, such as the number of inputs and outputs, initial states, the initial ﬁring vector, and information regarding the behavior of the block during simulation have to be entered by the user. This input consists essentially of Scilab expressions deﬁning functions fi (presented in Section 9.1). This is done interactively by clicking on the Scifunc block once it is copied into the Scicos window. The main disadvantage of Scifunc is that the dialog for updating block parameters cannot be customized. In order to have a customized dialog box, a Scilab function called the interfacing function must be developed. We shall see how this is done in Section 9.5.1. It should be noted that in the case of Scifunc, like all older types of Scicos blocks, the function f3 must return the time of the outgoing events and not the delay with respect to the activation time of the block. 9.5 Constructing New Basic Blocks In this section we show how a new basic block can be constructed and used in Scicos. Each Scicos basic block is deﬁned by two functions. The ﬁrst one, which must be in the Scilab language, handles the interactions with the editor. It is this function that speciﬁes what the geometry of the block should be, how many inputs and outputs it should have, what the block type is, etc. It is also this function that handles the user interface (updating block parameters and initializing the states). This function is referred to as the interfacing function. 9.5 Constructing New Basic Blocks 195 The second function, which is called the computational function, is normally written in C, but can also be a Scilab function. This function deﬁnes the behavior of the block during the simulation. For simple blocks, this amounts to coding the fi functions which have to be evaluated by the program depending on the value of an input ﬂag. The i in fi corresponds to the value of the ﬂag. We shall come back to this in Section 9.5.2. In order to get the best performance, this part of the Scicos block should be deﬁned by C programs, but Scilab functions are often used in the early phases of development for testing and debugging purposes since they are easier to write. Even though each basic block requires both an interfacing function and a computational function, the user often needs to be developed only one. For example, a computational function can be used with the GENERIC block available in the Others palette. The GENERIC block is a generic interfacing function. It suﬃces to specify the “name of the computational function” in the dialog box of the GENERIC block along with information concerning the input/output numbers and sizes, initial states, etc. The name of the program, in the case of C programs, is the name of the entry point when the C program is incrementally linked with Scilab (see Section 9.5.2). In the case of a Scilab program, it is simply the name of the Scilab function. Scilab functions must, of course, be loaded as usual into Scilab before Scicos is launched. This can be done using either getf or load if the function is contained in a library. See Section 2.5. The drawback of using the GENERIC block, as in the case of Scifunc, is the generic dialog box. In most cases, it is desirable to customize both the block’s dialog box and its look. For that, a custom interfacing function must be used. This Scilab function should provide information on the geometry of the block and handle the dialog for deﬁning and updating block parameters and states. The interfacing function is used by the graphical editor, as opposed to the computational function, which is used by the simulator. Section 9.5.1 describes how the interfacing function and the underlying programs should be developed. On the other hand, in some cases a new block may consist simply of a new interfacing function using an existing computational function. Consider, for example, the two continuous-time linear dynamical system blocks in the Linear palette. One is in state-space form and the dialog requests the A, B, C, D matrices. The other uses transfer functions and the user should enter the transfer function of the system. But both of these blocks use the same computational function, namely csslti. The reason is that the transfer function is converted to a state-space representation in the corresponding interfacing function. So two blocks may look very diﬀerent but have a common computational function. A new block realizing a low-pass ﬁlter should have a dialog where information such as the cut-oﬀ frequency, order, overshoot, etc., are speciﬁed. But once the ﬁlter is designed in the interfacing function (using specialized Scilab functions), the simulation can very well be performed with csslti. The reason is that the ﬁlter is nothing but a linear system characterized by A, B, C, D matrices. 9.5.1 Interfacing Function The interfacing function is used only by the Scicos editor. In particular, it is used to initialize, draw, connect the block, and to modify its parameters. In addition, the interfacing function initializes block states and speciﬁes certain block properties. It also speciﬁes the name of the computational function to be used by the simulator. What the interfacing function should return depends on an input ﬂag job. Syntax [x,y,typ]=block(job,arg1,arg2) 196 9 Scicos Blocks Parameter job The parameter job can take on the following values: • ’plot’: the function draws the block and its label. arg1 is the data structure of the block. arg2,x,y,typ are unused. In general, the standard draw function can be used. It draws a rectangular block. • ’getinputs’: the function returns the position and type of input ports (regular and event). arg1 is the data structure of the block. arg2 is unused. x is the vector of x coordinates of input ports. y is the vector of y coordinates of input ports. typ is the vector of input ports types. In general, the standard input function can be used. • ’getoutputs’: returns position and type of output ports (regular and event). arg1 is the data structure of the block. arg2 is unused. x is the vector of x coordinates of output ports. y is the vector of y coordinates of output ports. typ is the vector of output ports types . In general, the standard output function can be used. The standard input and standard output functions place regular input/outputs on the side of the block and the activation inputs on top and activation outputs at the bottom of the block. • ’getorigin’: returns coordinates of the lower left point of the rectangle containing the block’s shape. arg1 is the data structure of the block. arg2 is unused. x is the vector of x coordinates of output ports. y is the vector of y coordinates of output ports. typ is unused. In general, the standard origin function can be used. • ’set’: This function opens a dialog for block parameter acquisition. arg1 is the data structure of the block. arg2 is unused. x is the new data structure of the block. y is unused. typ is unused. • ’deﬁne’: provides initialization of the block’s data structure. It sets block’s initial type, number of inputs, number of outputs, etc. arg1, arg2 are unused. x is the data structure of the block. y is unused. typ is unused. Example: the Interfacing Function of the Absolute Value Block function [x,y,typ]=ABS_VALUE(job,arg1,arg2) x=[];y=[];typ=[] select job case ’plot’ then standard_draw(arg1) case ’getinputs’ then [x,y,typ]=standard_inputs(arg1) case ’getoutputs’ then [x,y,typ]=standard_outputs(arg1) case ’getorigin’ then [x,y]=standard_origin(arg1) case ’set’ then x=arg1; graphics=arg1.graphics;exprs=graphics.exprs model=arg1.model; while %t do [ok,zcr,exprs]=.. getvalue(’Set block parameters’,.. 9.5 Constructing New Basic Blocks 197 [’use zero_crossing (1: yes) (0:no)’],.. list(’vec’,1),exprs) if ~ok then break, end graphics.exprs=exprs if ok then if zcr<>0 then model.nmode=-1;model.nzcross=-1; else model.nmode=0;model.nzcross=0; end x.graphics=graphics;x.model=model break end end case ’define’ then nu=-1 model=scicos_model() model.sim=list(’absolute_value’,4) model.in=nu model.out=nu model.nzcross=nu model.nmode=nu model.blocktype=’c’ model.dep_ut=[%t %f] exprs=[string([1])] gr_i=[’txt=[’’ABS’’];’; ’xstringb(orig(1),orig(2),txt,sz(1),sz(2),’’fill’’)’] x=standard_define([2 2],model,exprs,gr_i) end endfunction The data structures used in the interfacing function are explained in Appendix A. The only hard part of deﬁning an interfacing function is in the ’set’ case. Note that the function getvalue is used to create a dialog for the block. This should be done in all user-deﬁned blocks as well, since this function is overloaded, when Eval is performed, to read and evaluate block parameters. getvalue is a generic dialog box with built-in type checking facilitating the implementation of Scicos blocks’ dialog boxes. 9.5.2 Computational Function The computational function is called in various ways by the simulator. The way the block is called (the calling sequence) is characterized by the type of the interfacing function. It is strongly recommended that type 4 and 5 computational functions now be used. The other types are now obsolete. Type 4 is used for programs written in C and type 5 for Scilab computational functions. Type 4 C functions receive two arguments: a structure containing block information and a ﬂag: #include "scicos_block.h" #include <math.h> void my block(scicos block *block,int ﬂag) 198 { } 9 Scicos Blocks ... ﬂag The ﬂag indicates the job that the computational function must perform. It usually consists in updating some of the ﬁelds of the block structure. Speciﬁcally, the following jobs exist: • Initialization: If the function is called with ﬂag=4, then the continuous and discrete states can be initialized, or more speciﬁcally, reinitialized (if necessary) because they are already initialized by the interfacing function. The block output can also be initialized. This option is not used in most blocks. It is used by the ones that read and write data from ﬁles, for opening the ﬁle, or by scope for initializing the graphics window. It is also used by blocks, which require dynamically allocated memory; the allocation is done under this ﬂag. Each block is called once and only once at the beginning of the simulation with ﬂag 4. • Output update: When ﬂag=1, the simulator is requesting the outputs of the block. The computational function should use the needed information within the block’s structure (inputs, states, nevprt, which codes the way by which the activation has arrived, etc.) to compute the outputs and place them at the addresses given in the block structure. Note that if the block contains diﬀerent modes, then the way the output is computed depends on the simulation phase. The simulation phase indicates whether the call is made in the numerical integration phase or is due to an event. • State update: When the simulator calls the function with ﬂag=2, it means that an event has activated the block (nevprt= 0). The activation may also be due to an internal zero-crossing event, in which case nevprt is equal to −1. With ﬂag= 2, the simulator is asking the block to update its states x and z. Scicos provides the addresses of the states, where old values of x and z reside, and the block should update them in place. This avoids useless and time-consuming copies, in particular when part or all of x or z is not to be changed. If a temporary workspace is needed to perform the computations, it can either be statically allocated in the C routine or dynamically allocated when the block is called with ﬂag 4. If nevprt= −1, indicating that the activation is due to an internal zero-crossing, then the vector jroot speciﬁes which surfaces have been crossed and in which direction. In particular, if the ith entry of jroot is 0, then the ith surface (counting from zero) has not crossed zero. If it is +1 (respectively −1), then it has crossed zero with a positive (respectively negative) slope. • Integrator calls: During the integration, the solver calls the function for the value of ẋ. This is done with ﬂag=0. The computational function must compute ẋ and place it in the address provided in the block structure. • Mode and zero-crossing: Scicos solvers assume model smoothness. This means that the behavior of blocks, which contain or aﬀect continuous-time states of the system must be smooth, or at least piecewise smooth. In this latter case, the points of nonsmoothness must be speciﬁed in such a way that the solver can, if needed, issue a cold restart when going through such points. An example of such a block is the ABS block. The absolute value function is smooth except at zero, where it is not even diﬀerentiable. In this case, we say that the absolute value block has two modes: one where the output equals the input, and one where the output is the negative of the input. A block may have many diﬀerent modes, which are stored in the block structure. The point of 9.5 Constructing New Basic Blocks 199 nonsmoothness is represented by a zero-crossing, and the mode is updated after such a crossing occurs. ﬂag= 9 is used both for evaluating the zero-crossing function and setting the mode. Mode setting is done only in the simulation phase 1. Note that the block may have more zero-crossings than modes. For example, the zcross block, which generates an event when its input crosses zero, has one zero-crossing surface but no mode. • Event scheduler: To update the event scheduler table, the simulator calls the function with ﬂag=3. This happens if the block has output activation ports. In this case the block has access to jroot. • Ending: Once the simulation is done or at user request (by responding End in the Run menu after a Stop), the simulator calls each computational function with ﬂag=5, once. This is useful, for example, for closing ﬁles that have been opened by the block at the beginning or during the simulation, to ﬂush buﬀered data, and to free allocated memory. Table 9.1 summarizes the role of each flag. Note that in most cases the computational functions are not called with all these ﬂag values. For example, if a block has no output port, its associated computational function is never called with flag 1. If it has no continuous-time state, then it is not called with flag 0, etc. Also note that in this table we have not speciﬁcally stated as input time-invariant quantities such as the parameters rpar and ipar. The function clearly has access to these parameters and can use them in computing its outputs. Finally, the workspace is not mentioned in the table because it can be used in various ways. If needed, the workspace w is in general allocated when flag= 4 and freed when flag= 5. The workspace can be used as storage and in many diﬀerent ways. ﬂag inputs 0 t, nevprt, x, z, inptr, mode, phase 1 t, nevprt, x, z, inptr, mode, phase 2 t, nevprt>0, x, z, inptr 2 3 4 5 6 7 9 9 outputs xd description compute the derivative of continuous time state outptr compute the outputs of the block x, z update states due to external activation t, nevprt=-1, x, z, inptr, x, z update states due to interjroot nal zero-crossing t, x, z, inptr, jroot evout program activation output delay times t, x, z x, z, outptr initialize states and other initializations x, z, inptr x, z, outptr ﬁnal call to block for ending the simulation not needed only used for internally implicit blocks t, phase=1, nevprt, x, z, g, mode compute zero-crossing surinptr faces and set modes t, phase=2, nevprt, x, z, g compute zero-crossing surinptr faces Table 9.1. This tables illustrates the jobs that the computational function must perform for diﬀerent ﬂags. 200 9 Scicos Blocks The computational function receives most of its inputs from the block structure. The rest is obtained from the following functions: • double get scicos time(): returns the current time t. • int get phase simulation(): returns the simulation phase (1 or 2). • int get block number(): returns the block number in the structure %cpr. These functions are deﬁned in scicos block.h. Other useful functions in this ﬁle are: • void set block error(int): used by the block to signal an error to the simulator. • void do cold restart(): used to force a cold restart of the numerical solver (almost never used since Scicos determines automatically when cold restart is needed). • void set pointer xproperty(int* pointer): used only for internally implicit blocks to designate which states are algebraic and which ones are diﬀerential. • void *scicos malloc(size t ): used to allocate memory for workspace if needed. • void scicos free(void *p): used to free allocated memory. Block Structure The block structure is a C struct deﬁned as follows: typedef struct { int nevprt; /* binary coding of activation inputs, -1 if internally activated */ voidg funpt; /* pointer: pointer to the computational function */ int type; /* type of interfacing function, current type is 4 */ int scsptr; /* not used for C interfacing functions */ int nz; /* size of the discrete-time state */ double *z; /* vector of size nz: discrete-time state */ int nx; /* size of the continuous-time state */ double *x; /* vector of size nx: continuous-time state */ double *xd; /* vector of size nx: derivative of continuous-time state */ double *res; /* only used for internally implicit blocks. vector of size nx */ int nin; /* number of inputs */ int *insz; /* input sizes */ double **inptr; /* table of pointers to inputs */ int nout; /* number of outputs */ int *outsz; /* output sizes */ double **outptr;/* table of pointers to outputs */ int nevout; /* number of activation output ports */ double *evout; /* delay times of output activations */ int nrpar; /* number of real parameters */ double *rpar; /* real parameters of size nrpar */ int nipar; /* number of integer parameters */ int *ipar; /* integer parameters of size nipar */ /* number of zero-crossing surfaces */ int ng; double *g; /* zero-crossing surfaces */ int ztyp; /* boolean, true only if block MAY have zero-crossings */ int *jroot; /* vector of size ng indicating the presence and direction of crossings */ char *label; /* block label */ void **work; /* pointer to workspace if allocation done by block */ int nmode; /* number of modes */ int *mode; /* mode vector of size nmode */ } scicos block; 9.5 Constructing New Basic Blocks 201 Examples The simplest computational functions are those that simply compute an output as a smooth function of an input. One such function is the computational function of the SUMMATION block: #include "scicos_block.h" #include <math.h> void summation(scicos block *block,int ﬂag) { int j,k; if (ﬂag==1){ if (block−>nin==1){ block−>outptr[0][0]=0.0; for (j=0;j<block−>insz[0];j++) { block−>outptr[0][0]=block−>outptr[0][0]+block−>inptr[0][j]; } } else { for (j=0;j<block−>insz[0];j++) { block−>outptr[0][j]=0.0; for (k=0;k<block−>nin;k++) { if (block−>ipar[k]>0){ block−>outptr[0][j]=block−>outptr[0][j]+block−>inptr[k][j]; }else{ block−>outptr[0][j]=block−>outptr[0][j]−block−>inptr[k][j]; } } } } } } This block can have one or more vector inputs. If it has one input, then the output is a scalar corresponding to the sum of the entries of the unique input vector. If not, the output is a vector of the same size as that of all the input vectors. In this case, the output is simply obtained by the adding and subtracting (in the vector sense, i.e., elementwise) of the input vectors. The choice of addition and subtraction is made based on the values of the integer parameter vector block->ipar. The test on the number of inputs determines which algorithm should be used. This number is available in block->nin. The size of the input vector i (counting from zero) is in block->insz[i]. The address of the jth entry of the kth input (respectively output) can be found in block->inptr[k][j] (respectively block->outptr[k][j]). Another example of a simple block is the Gain block. This block has a vector input and a vector output. The output is the input multiplied either by a scalar or a matrix. Here is the corresponding computational function: #include "scicos_block.h" #include <math.h> #include ". ./machine.h" extern void C2F(dmmul)(); 202 9 Scicos Blocks void gainblk(scicos block *block,int ﬂag) { int i,un=1; if (block−>nrpar==1){ for (i=0;i<block−>insz[0];++i){ block−>outptr[0][i]=block−>rpar[0]*block−>inptr[0][i]; } }else{ C2F(dmmul)(block−>rpar,&block−>outsz[0],block−>inptr[0], &block−>insz[0],block−>outptr[0],&block−>outsz[0], &block−>outsz[0],&block−>insz[0],&un); } } Note that depending on the value of block->nrpar, which represents the number of real parameters, two diﬀerent algorithms are used. If block->nrpar equals one, then the output is obtained by multiplying each entry in the input vector by the unique real parameter contained in block->rpar[0]. If not, a matrix multiplication is performed. Note that Scilab’s multiplication routine is used. C2F is used because the routine dmmul is in Fortran. The following is the computational function associated with the ABS block. : #include "scicos_block.h" #include <math.h> void absolute value(scicos block *block,int ﬂag) { int i,j; if (ﬂag==1){ if ( block−>ng>0){ for(i=0;i<block−>insz[0];++i){ if (get phase simulation()==1) { if (block−>inptr[0][i]<0){ j=2; } else{ j=1; } }else { j=block−>mode[i]; } if (j==1){ block−>outptr[0][i]=block−>inptr[0][i]; } else{ block−>outptr[0][i]=−block−>inptr[0][i]; } } }else{ for(i=0;i<block−>insz[0];++i){ if (block−>inptr[0][i]<0){ block−>outptr[0][i]=−block−>inptr[0][i]; }else{ block−>outptr[0][i]=block−>inptr[0][i]; 9.5 Constructing New Basic Blocks } } 203 } } }else if (ﬂag==9){ for(i=0;i<block−>insz[0];++i){ block−>g[i]=block−>inptr[0][i]; if (get phase simulation()==1) { if (block−>g[i]<0){ block−>mode[i]=2; }else{ block−>mode[i]=1; } } } } This example is not very simple because this block is nonsmooth and thus uses modes. Note, however, that the modes are used only if the block generates continuous-time signals aﬀecting the continuous-time state. The compiler determines whether this is the case, so the computational function must be prepared to function both with and without modes. It is the value of block->ng (or equivalently in this case block->nmode) that determines whether modes should be used. Note that if block->ng is zero, then a simple absolute value operation is performed in ﬂag 1 and the block is never called with ﬂag 9. If, on the other hand, block->ng is not zero, then modes must be used. An important function to use in this case is get phase simulation. It returns 1 or 2 to specify the simulation phase. If the numerical solver is at work advancing the time, then the computational function must produce a smooth signal and thus must use the mode to generate its output. When get phase simulation returns 1, then the output must be computed normally by computing the absolute value of the input. In the ﬂag 9 case, the zero-crossing surface is computed all the time, but the mode is set only in simulation phase 1. Note that using modes in this case means that during the simulation, the output of the ABS block can become negative because at zero the solver has to step back and forth for pinpointing the zero-crossing. In some cases, this could be a problem, for example, if the ABS block is followed by a SQRT block that computes the square root. It is for this reason that in the ABS block we have the option of using, or not using, zero-crossings (modes). This is true for most blocks using modes. The following example shows how output activations can be programmed when a block has output activation ports: #include "scicos_block.h" void evtvardly(scicos block *block,int ﬂag) { if (ﬂag==3){ block−>evout[0]=block−>inptr[0][0]; } } evtvardly is the computational function associated with the variable Event Delay block in the Events palette. The value of the input determines the delay between the activation time of the block and the generation of the output activation. 204 9 Scicos Blocks Let us now consider an example in which the zero-crossing is used to generate a jump in the state of the system. This example corresponds to a bouncing ball. This model includes a gravitational force and a nonlinear air resistance. The diﬀerential equations modeling this system are ḣ = v, v̇ = −9.8 − αv 3 , (9.8a) (9.8b) where h is the height of the ball, v is its speed, and α is the coeﬃcient of friction. When the ball hits the ﬂoor (at h = 0), it bounces back without any loss of energy. Thus, at h = 0, v becomes −v. The computational function of a block realizing this dynamical system is the following: #include <scicos/scicos block.h> #include <math.h> void Bounceball(scicos block *block,int ﬂag) { if (ﬂag==1){ block−>outptr[0][0]=block−>x[0]; }else if (ﬂag==0){ block−>xd[0]=block−>x[1]; block−>xd[1]=−9.8−block−>rpar[0]*block−>x[1]*block−>x[1]*block−>x[1]; }else if (ﬂag==2&block−>nevprt==−1&block−>jroot[0]<0){ block−>x[1]=−block−>x[1]; }else if (ﬂag==9){ block−>g[0]=block−>x[0]; } } Note that the test on nevprt does not accomplish anything because this function is called only with flag equal to 2 when nevprt is equal to −1 (nevprt indicates the way in which the block is executed and since this block has no activation port and does not inherit any activations because it is always active, it is only internally activated). Note also the test on jroot. This test means that we switch the sign of speed only when the ball crosses the zero level going downward. In a perfect world, the ball should never cross zero upward, but due to numerical errors such a situation may come up. In such a case, if a test on the sign of jroot is not performed, the ball could bounce oﬀ the zero line and go downward thereafter. To use this function, we should ﬁrst link it with Scilab. This can be done with the following Scilab commands (assuming that the corresponding ﬁle is in the current directory): ilib_for_link(’Bounceball’,’bounceball.o’,[],’c’) exec loader.sce The ﬁrst command compiles the program and creates a shared library. The second command links the shared library with Scilab. This computational function can be easily tested with the GENERIC interfacing function provided in the Others palette. The name of the simulation function is set to Bounceball, the function type to 4, the initial continuous-time state to [10;5], the real parameter to 0.001, the number of zero-crossings to 1, and the block is declared time-dependent (always active). The block is placed in the diagram in Figure 9.2. The simulation result is shown in Figure 9.3. 9.5 Constructing New Basic Blocks 205 GENERIC Figure 9.2. Scicos diagram for the bouncing ball example. 20 15 10 5 0 + 0 3 6 9 12 15 18 21 24 27 30 Figure 9.3. Simulation result of the bouncing ball example. Scilab Computational Functions A computational function of type 5 is a Scilab function. Its calling sequence is block=func_name(block,flag) where block is a Scilab structure similar to the C structure used in the C computational function of type 4. The ﬁelds of block are scicos_block nevprt funpt type scsptr nz z nx x xd nin insz inptr nout outsz outptr nevout evout nrpar nipar ipar ng g ztyp jroot label work nmode mode res rpar Note that block.z, in case of computational functions of type 5, can be any Scilab object and not just a vector of reals. The user, however, should make sure that the size of block.z does not change during the simulation. For example, it can be a Scilab list of constant-size vectors, an integer matrix, or a character string, but not a Scilab polynomial because the memory used by a Scilab polynomial depends on the number of nonzero 206 9 Scicos Blocks coeﬃcients and is very likely to change if the polynomial changes. block.nz gives the size of the memory used by the Scilab object. So even for a simple vector, block.nz does not give the size of the vector, but rather the size of the vector plus 2. The overhead is due to the coding of the type and dimensions that Scilab places on top of each object. In the case of computational functions of type 5, the size information is not really necessary. If block.z is a vector, its size can always be obtained using Scilab’s size function: nz=size(block.z) The variable flag is a scalar and has exactly the same role as flag in the C case. The following Scilab functions can be used to obtain additional information and perform other operations: • • • • • curblock(): returns the current block number in the structure %cpr. scicos time(): gives the current time. phase simulation(): returns the simulation phase. set blockerror(i): set the error ﬂag if the computational function encounters an error. pointer xproperty: used for internally implicit blocks. Example The following is a simple type 5 computational function for a Scicos block realizing the sine function: function block=sin5(block,flag) if flag==1 then for j=1:block.insz(1) block.outptr(1)(j)=sin(block.inptr(1)(j)); end end endfunction The ﬁelds of the block structure are similar to their C counterpart. But note that the numbering starts from 1 and not zero in referring to input/output numbers. That is because in Scilab, unlike C, indices start counting from 1. Other examples of a type 5 computational functions can be found in Appendix B. Using the Computational Function Once you have written your computational function, you have to compile it and incrementally link it with Scilab. All this can be done within Scilab. Suppose the name of your C routine is My prog and it is deﬁned in the ﬁle Files.c in the current directory. Then the following Scilab commands can be used to compile and link: −→ −→ ilib_for_link(’My_prog’,’Files.o’,[],’c’); exec loader.sce These commands works under all operating systems (Linux, Unix, Windows, MacOSX) provided an appropriate C compiler is available on the system. 9.6 Constructing and Loading a New Palette 207 CBlock2 As we have seen in the previous section, if you are developing a new C computational function, in order to use it in Scicos, you have to compile it and incrementally link it with Scilab before launching Scicos. You also need an interfacing function (or you could use the GENERIC block). The CBlock2 block provides an alternative to this. It allows you to write your computational function on-line. The text is stored in the block structure and the compilation and linking are done automatically. The block also provides a generic interfacing function. The main advantage of using CBlock2 is that the C computational function is included in the block and thus in the diagram structure. This means that the Scicos diagram is self-contained and can be used on diﬀerent operating systems. The only drawback is that the diagram works only if there is an appropriate C compiler available on the operating system being used. If the computational function is used directly without CBlock2, then dll libraries can be provided along with the diagram and the block will function even if no compiler is available. It also means that the source of the computational function need not be made available. This could be of interest for commercial toolboxes. 9.5.3 Saving New Blocks New blocks can be used in a Scicos diagram using the AddNewBlock menu by giving the name of the corresponding interfacing function. However, it is a lot more convenient to place new blocks in user palettes. A palette is simply a Scicos diagram from which the user can copy blocks into the Scicos editor. To edit a palette, for example to add a new block, the palette should be loaded as a diagram, edited, and saved. 9.6 Constructing and Loading a New Palette A palette can be deﬁned if the computational and interfacing functions associated to all of its blocks are available. Suppose we want to construct a palette named foo and we have placed inside the directory /home/basile/foo the C ﬁles containing the computational functions foo blk1 through foo blk5 and Scilab interfacing functions associated to the blocks to be placed in the palette. Each interfacing function is supposed to be in a ﬁle with extension .sci bearing its name. The C and Scilab ﬁles can be compiled and the corresponding libraries generated by the appropriate commands (usually ilib for link for C functions and genlib for Scilab functions). These commands can be placed in a ﬁle called builder.sce. The following is the content of a builder.sce for compiling the C functions: −→ comp_fun_lst=[’foo_blk1’,’foo_blk2’,’foo_blk3’,’foo_blk4’,’foo_blk5’]; −→ c_prog_lst=listfiles(’*.c’); //list of C programs in the directory −→ prog_lst=strsubst(c_prog_lst,’.c’,’.o’); −→ ilib_for_link(comp_fun,prog_lst,’c’); // compile and generate loader −→ genlib(’lib_foo’,pwd()); // compile macros and generate lib Note that to execute this ﬁle, the current directory must be foo. The Scilab function genlib can be used to construct lib associated to Scilab functions as usual. This can be done by the builder.sce function as well. This script needs to be executed only once. It generates the libraries but does not load them. Loading of the interfacing functions can be done with the command load(’lib’) and linking the computational functions done with exec(’loader.sce’). 208 9 Scicos Blocks At this point, the palette foo can be edited and saved in foo.cos or foo.cosf. Blocks are placed in the diagram using the Add New Block functionality. The palette can also be constructed using the create palette Scilab function. For that, all the ﬁles containing the interfacing functions associated with the blocks in the palette should be placed in a directory. Each ﬁle must bear the name of the function with the extension .sci, as usual. Then the function create palette, called with argument the path to this directory, creates the Scicos diagram of the palette. For example, if the directory /home/basile/foo contains the interfacing function ﬁles, the command −→ create_palette(’/home/basile/foo’); creates a Scicos diagram (palette) foo.cosf and places it in /home/basile/foo. To load the palette, we can now construct a loader.sce script as follows −→ load(’lib’) // loads the interfacing functions −→ scicos_pal($+1,1)=’foo’; //adding the palette to the list −→ scicos_pal($+1,2)=’/home/basile/foo/foo.cosf’; It is good programming practice to provide with each palette the builder and the loader scripts. These scripts may also be used for creating and loading manual pages for the blocks. 10 Examples A typical Scicos model used in a real industrial application includes hundreds of blocks and does not constitute a good example for illustrating Scicos functionalities. That is why in this chapter we use small academic examples. Each example is chosen speciﬁcally to put forward a diﬀerent feature of modeling. 10.1 Predator Prey Model In this section we consider a simple predator prey model of sardine and shark populations. The dynamics of this model can be expressed as follows: ẋ = ax − bxy + F (t)x, (10.1a) ẏ = cxy − dy, (10.1b) where x denotes the sardine population and y the shark population. The xy terms represent the eﬀect of shark predation of the sardines which is assumed proportional to the frequency of encounters between the two species. These encounters, of course, have a positive eﬀect on the shark population and a negative eﬀect on the sardine population. The a, b, c, and d are positive constants. The F (t)x term represents the consequence of ﬁshing with F (t) ≤ 0, so that F (t) = 0 means no ﬁshing. F (t) may be thought of as a varying harvesting rate. The Scicos model corresponding to this system of equations is given in Figure 10.1. The ﬁshing eﬀort is modeled by a Super block. With F set to zero, the simulation results are given in Figure 10.2 and Figure 10.3. As we can see, the solution to these equations with F = 0 (in fact with F any constant larger than −a) is periodic. With the parameters and initial conditions chosen for this simulation, the two populations undergo large variations. If there is no ﬁshing, then there is a stationary value of this system, which is x = d/c, y = a/b, obtained by setting the derivatives in (10.1) to zero. Suppose we wish to use ﬁshing regulation to get the sardine population to stay close to the stationary value d/c. One type of regulation, which is practical to implement, is to adjust the ﬁshing eﬀort when populations reach certain threshold levels. So we assume the regulation is done by setting F (t) to 0 (no ﬁshing) when the sardine population is low and F (t) = f < 0 when it is high. This is the control law: 0 if x falls below xmin , F (t) = f if x goes above xmax . We set xmin and xmax to 5 percent below and 5 percent above d/c. This control law is implemented in the fishing effort Super block as illustrated in Figure 10.4. We could 210 10 Examples have used the HYSTERESIS block to implement this block, but we wanted to illustrate the way Threshold blocks function. −bxy −b cxy c xy ax a x 1/s fishing F effort MScope Mux y 1/s dy d Figure 10.1. The predator prey model with ﬁshing. The simulation is done for the parameters deﬁned in the context as follows: a=2;b=1;c=.3;d=-1; perc=5/100; xmin=-(1-perc)*d/c;xmax=-(1+perc)*d/c; f=-.5; The simulation results are given in Figure 10.5 and Figure 10.6. As we can see, after a transient period, as desired the sardine population stays much closer to its stationary value than in Figure 10.2. 10.2 Control Application One of the most common uses of Scicos is in control applications. Here we give a simple example in which a discrete-time controller is designed and used to control a continuoustime system. We start with the observer system constructed in Figure 7.13 of Chapter 7 and construct an observer-based controller. This is done by adding a feedback control to obtain 10.2 Control Application 211 10.0 7.5 5.0 2.5 0.0 + 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1.0 0.5 0.0 −0.5 −1.0 + 0 Figure 10.2. The simulation shows wide swings in the two populations for the given initial conditions when the ﬁshing eﬀort (lower graph) is set to zero. 10 9 8 7 6 5 4 3 2 1 0 + 0 1 2 3 4 5 6 7 8 9 10 Figure 10.3. The output of the 2D scope displays here the shark population as a function of the sardine population showing clearly the periodicity of the solution. 212 10 Examples −xmax − to + 1 −xmin + to − 0 Selector 1 f Figure 10.4. Scicos diagram inside the fishing effort Super block. 10.0 7.5 5.0 2.5 0.0 + 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1.0 0.5 0.0 −0.5 −1.0 + 0 Figure 10.5. The simulation shows the stabilization of the two populations. The lower graph is the ﬁshing eﬀort F . a generic observer-based controller as illustrated in Figure 10.7. The feedback gain Kc is computed as follows: Kc=-ppol(A,B,-ones(x0)) This statement is, of course, placed in the context of the diagram. In this case, in contrast to what we did in Chapter 7, we can let the matrix A be unstable. 10.3 Signal Processing Application 213 10 9 8 7 6 5 4 3 2 1 0 + 0 1 2 3 4 5 6 7 8 9 10 Figure 10.6. The output of the 2D scope shows the solution moving close to the desired stationary point. u xd=Ax+Bu y=Cx+Du System x+=Ax+Bu y=Cx+Du y Demux Mux x Kc x_hat Figure 10.7. Generic Scicos diagram realizing an observer-based controller. 10.3 Signal Processing Application In signal processing applications, very often down-sampling and segmentation are used. These operations can easily be implemented in Scicos. We illustrate this through an example of a simple monitoring scheme, where we track the frequency components of an input signal. We do this by implementing a sliding window over which we apply the FFT (fast Fourier transform) algorithm. The result of the FFT is animated on a graphics window. Moreover, we compute and display on a scope the two dominant frequencies in the signal. Such monitoring is often used in failure-detection applications. 214 10 Examples random generator freq_div + Mathematical Expression − Buffer n=200 Scifunc MScope Figure 10.8. A signal processing application. In the diagram of Figure 10.8, we have used a Scifunc block to implement the computation of the FFT and the display of the result. The output of the Scifunc block is computed using the following instructions: nz=size(u1,1)/2 f=fft(u1,-1) f=abs(f(1:nz)) xset(’window’,3) xbasc() plot2d(0:nz-1,f,1,"011"," ",[0,0,nz,240]) [j,I]=gsort(f) I=I(1:rpar(2)) y1=rpar(1)*(I-1)/(2*nz) Note that we only use half the spectrum. Since the input signal is real, the other half is redundant. Note also that the display of the spectrum is done here using the Scilab graphics function plot2d. The Mathematical Expression block is used to generate a test signal as follows: sin(2*%pi*20*u1*(1+cos(u1/20)))+cos(2*%pi*14*u1)*2 Here u1 is the block’s input, which is just the time. The output is then the sum of a ﬁxed amplitude and frequency cosine and a time-varying frequency sine. Random noise is added to create a more realistic situation. The Buffer is realized using a CBlock2 block, which allows an on-line deﬁnition of its computational function in C. The buﬀer simply outputs the last n values of its input on its output vector of size n. This is realized as follows: #include <math.h> #include <stdlib.h> #include <scicos/scicos block.h> void toto(scicos block *block,int ﬂag) 10.3 Signal Processing Application 215 50.0 37.5 25.0 12.5 + 0.0 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 4 2 0 −2 + −4 5.0 Figure 10.9. Simulation shows the evolution of the two dominant frequencies (top graph) and the original signal (bottom graph). 240 216 192 168 144 120 96 72 48 24 0 0 10 20 30 40 50 60 70 80 90 100 Figure 10.10. The animation of the frequency proﬁle. { if (ﬂag == 4) { /* initialization */ toto bloc init(block,ﬂag); } else if (ﬂag == 1) { /* output computation*/ set block error(toto bloc outputs(block,ﬂag)); } else if (ﬂag == 5) { /* ending */ set block error(toto bloc ending(block,ﬂag)); } 216 10 Examples } int toto bloc init(scicos block *block,int ﬂag) { return 0; } int toto bloc outputs(scicos block *block,int ﬂag) { int n=block−>outsz[0]; int i; for (i=0;i<n−1;i++){block−>outptr[0][n−i−1]=block−>outptr[0][n−i−2];} block−>outptr[0][0]=block−>inptr[0][0]; return 0; } int toto bloc ending(scicos block *block,int ﬂag) { return 0; } The FFT operation can, of course, be implemented in a C block to speed up the simulation. However, in this example, the FFT is not computed at every step, so the overhead of calling the Scilab interpreter is not signiﬁcant. The simulation results are illustrated in Figure 10.9 and Figure 10.10. The latter contains a snapshot of the animation at time t = 20. 10.4 Queuing Systems Queuing systems can conveniently be modeled in Scicos using the event mechanism. Let us consider a simple queuing system with a Poisson arrival process and an exponential service time. This would model a queue at the checkout counter of a store. Customers arrive following a Poisson process law, and the time each customer spends at the counter is assumed to have an exponential law. To generate the arrival process, we use the model in Figure 10.11. The random number generator outputs a uniformly distributed random number between 0 and 1. By taking the logarithm and multiplying by −1/λ, we generate a random number with an exponential law with parameter λ. By feeding the output of the Event Delay block to its input activation port, we generate a sequence of events where the time between two events is an independent random variable with exponential law. The result is a Poisson process with parameter λ. The input activation to the Super block starts the process (no event ﬁring is initially programmed on the output of the event delay block). Generating the departure process (process of serving a customer) is more complicated. As long as the queue is not empty, the time between two events is an independent random variable. But when the queue is empty, no departure event should be generated. The process should be inactive. To implement this process, we have to allow the restart of the process by an external event (the arrival of a customer in an empty queue). This is the reason why the model of the departure events in Figure 10.12 has two input activations. One is for initialization and the other is for restart. Note the use of the Mathematical Expression block to allow for modeling more general waiting laws. But in this example we have used -log(u1)/mu, which results in an exponential law with parameter μ. The complete model is given in Figure 10.13, where the state of the queue is stored in the 1/z block. Its value goes up or down by one, depending on the event. The simulation 10.4 Queuing Systems random generator Log Event Delay −1/lam 1 1 Figure 10.11. Super block generating arrival events based on Poisson’s law. 1 If in>0 then else random generator Mathematical Expression Event Delay 2 1 1 1 − + If in>0 then else Figure 10.12. Super block generating departure events from the queue. 217 218 10 Examples result is given in Figure 10.14, where we see the evolution of the queue, and in Figure 10.14, where the events are displayed (dark lines correspond to arrival events). The parameters are deﬁned in the context as follows: lam=.3;mu=.35;z0=6 where z0 is used as the initial state of the 1/z block. Event at time 0 Arrival event Depart. event 1 −1 Selector + + 1/z 0 Figure 10.13. Scicos diagram realizing the queuing system. 10.5 Neuroscience Application We consider here a model of the luteinizing hormone-releasing hormone pulse generator. We are not going to discuss the signiﬁcance of the model. The interested reader is referred to [12]. We focus on the mathematical aspects. This model is chosen because it presents, from the modeling point of view, some nice features. The model is a continuous-time nonlinear dynamical system with state jumps. The jumps are due to perturbations of variable magnitude occurring at random times. These times are constrained by a minimum interevent period ta and are modeled as a displaced exponential distribution. The magnitude and timing of the perturbations are modeled as in Figure 10.16. The top random generator block generates uniformly distributed random variables between 0 and exp(−ta λ), so that the input to the event delay block is a displaced exponential distribution. The second random generator block generates a normal distribution with mean μ and variance σ 2 . The magnitude of the perturbation is then the exponential of this value. 10.5 Neuroscience Application 219 20 15 10 ◊ ◊ 5 ◊◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ 0 ◊ ◊ ◊ ◊ ◊ 9 12 ◊ ◊ + 0 ◊ ◊ ◊ 3 6 ◊ ◊ 15 18 ◊ ◊ 21 24 27 30 Figure 10.14. Simulation result shows the evolution of the queue. 1.0 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.0 + 0 3 6 9 12 15 18 21 24 27 30 Figure 10.15. Arrival and departure events. Since the perturbation aﬀects only the ﬁrst state out of the three, two zero signals are concatenated to the perturbation so that the result can directly be added to the state. In the absence of perturbation, the dynamics of the system are given by the following equations: where v̇ = s(−v(v − c)(v − 1) − k1 g + k2 a), ġ = b(v)(k3 a + k4 v − k5 g), (10.2) (10.3) ż = p(v) − k6 z, (10.4) 220 10 Examples 1 random generator Log Event Delay −1/lambda 1 random generator a^u 1 Mux [0;0] Figure 10.16. Scicos diagram for perturbations. b2 1 + exp(−b3 (v − b4 )) (10.5) p1 1 + exp(−p2 (h(v) − p3 )) (10.6) b(v) = b1 − and p(v) = with h(v) = v if v > 0 and h(v) = 0 otherwise. The s, a, ki , pi and, bi are known constants. To allow for a jump in the state, the Jump linear system block is used. In the absence of activation on its input activation port, this block realizes a standard continuous-time linear system with u corresponding to the ﬁrst input of the block. When the block is activated by an external event, the state jumps to the value speciﬁed on the second input. So the second input port must have the same dimension as the state. In this case the state is set to x = (v, g, z) and the parameters are chosen so that the block realizes a simple integrator. This is done by setting A = 0, B = I, C = I, and D = 0. Expressions (10.2) to (10.4) are realized with three Mathematical Expression blocks. See Figure 10.17. Note that the last expression is nonsmooth due to the if statement in the deﬁnition of h. This expression is realized in the Mathematical Expression block as follows: (u1>0)*(p1/(1+exp(-p2*(u1-p3)))-k6*u3)+(1-(u1>0))*(p1/(1+exp(p2*p3))-k6*u3) The zero-crossing mode must be set to on. The simulation result is given in Figure 10.18. 10.6 A Fluid Model of TCP-Like Behavior Our next example is a nonlinear time-delay system representing a ﬂuid model for a TCP/AQM network. Once again we do not address the signiﬁcance of the model but simply focus on the dynamics and the way it can be implemented in Scicos. We consider 10.7 Interactive GUI 221 Mathematical Expression Mathematical Expression Mux Mathematical Expression Pulse generator + + Jump (A,B,C,D) Demux MScope Figure 10.17. Scicos diagram realizing the luteinizing hormone-releasing hormone pulse generator. this system to show how delay diﬀerential systems can be modeled. We use one of the models studied in [29]: Ẇ (t) = Q̇(t) = 1 W (t)W (t − R) − KQ(t − R), R 2R N WR(t) − C, Q > 0, max(N WR(t) − C, 0), Q = 0. (10.7) (10.8) The Scicos diagram realizing this system is given in Figure 10.19. The continuous-time ﬁxed delay blocks are used to implement the delay. The content of the Mathematical Expression block is (u2==0)*max(N*u1/R-C,0)+(u2>0)*(N*u1/R-C) The simulation result is displayed in the 2D scope, where W is plotted as a function of Q. Note that the result is compatible with that obtained in [32]. 10.7 Interactive GUI In some applications, it is desirable to have the possibility of adjusting certain system parameters during the simulation without having to stop and restart the simulation process. We consider here a simple example to illustrate how this can be done. The technique used here can be used to construct sophisticated control panels for controlling simulations. 222 10 Examples 1.0 0.5 0.0 −0.5 + −1.0 0.0 0.2 0.1 0.0 −0.1 + −0.2 0.0 20 15 10 5 + 0 0.0 0.8 1.6 2.4 3.2 4.0 4.8 5.6 6.4 7.2 8.0 0.8 1.6 2.4 3.2 4.0 4.8 5.6 6.4 7.2 8.0 0.8 1.6 2.4 3.2 4.0 4.8 5.6 6.4 7.2 8.0 Figure 10.18. Simulation result gives the evolution of the state (v, g, z). 1/2/R W − 1/R * * * Continuous Fix Delay + 1/s Product Q Continuous Fix Delay K 1/s Mathematical Expression Figure 10.19. The delay diﬀerential model. An on-screen representation of a control that may be manipulated by the user, is called a widget as are parts of such a representation. Scroll bars, buttons, sliders, menu bars, title bars, and text boxes are all examples of widgets. Programmatically, widgets are often expressed as data structures. 10.7 Interactive GUI 223 1.60 1.44 1.28 1.12 0.96 0.80 0.64 0.48 0.32 0.16 + 0.00 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 2.50 Figure 10.20. Chaotic behavior of the delay diﬀerential system. We consider here a source block where its output can be interactively set using a scale widget by moving up and down a scrollbar. The ﬁrst thing to consider is the type of widget to use. In order to make sure the application remains machine-independent, a good choice is to use the Tcl/Tk language. Tcl/Tk is included and interfaced in Scilab, so widget construction and data exchange can be carried out with Scilab commands. This implies, however, using Scicos blocks of type 5. This may not be a problem, since user interaction is in general of low frequency, and thus the source block can be activated with a slow clock. Otherwise, Tcl/Tk routines must be called directly from the C language, which requires familiarity with the internal implementation of Tcl/Tk, to have optimal performance. The source block we are considering must construct and display the widget at initialization (ﬂag 4) and then read the position of the scrollbar and return it when activated with ﬂag= 1. All this is done, of course, by the computational function, which here will be of type 5, that is, written in Scilab. The interfacing function is the following: function [x,y,typ]=TKSCALE(job,arg1,arg2) //Source block; output defined by tk widget scale x=[];y=[];typ=[]; select job case ’plot’ then standard_draw(arg1) case ’getinputs’ then [x,y,typ]=standard_inputs(arg1) case ’getoutputs’ then [x,y,typ]=standard_outputs(arg1) case ’getorigin’ then [x,y]=standard_origin(arg1) case ’set’ then x=arg1; graphics=arg1.graphics;exprs=graphics.exprs 224 10 Examples model=arg1.model; [ok,a,b,f,exprs]=getvalue(’Set scale block parameters’,.. [’Min value’;’Max value’;’Normalization’],.. list(’vec’,1,’vec’,1,’vec’,1),exprs) // tk widget returns a scalar, the value is divided by // Normalization factor if ok then graphics.exprs=exprs model.rpar=[a;b;f] x.graphics=graphics;x.model=model end case ’define’ then a=0;b=100;f=1;// default parameter values model=scicos_model() model.sim=list(’tkscaleblk’,5) model.out=1 model.evtin=1 model.rpar=[a;b;f] model.blocktype=’d’ model.dep_ut=[%f %f] exprs=[sci2exp(a);sci2exp(b);sci2exp(f)] gr_i=[’xstringb(orig(1),orig(2),’’TK Scale’’,sz(1),sz(2),’’fill’’)’] x=standard_define([3 2],model,exprs,gr_i) end endfunction Note that the block has three parameters: the ﬁrst two are the minimum value and the maximum value of the scale. The last is a normalization factor. The output of the block is normalized by dividing the scale value by this factor. This allows, in particular, the display of only integer scale values inside the widget. The computational function is the following: function block=tkscaleblk(block,flag) blknb=string(curblock()) if flag==4 then cur=%cpr.corinv(curblock()) if size(cur,’*’)==1 then // open widget only if the block // is in main Scicos editor window o=scs_m.objs(cur).graphics.orig; sz=scs_m.objs(cur).graphics.sz pos=point2pixel(1000,o) pos(1)=pos(1)+width2pixel(1000,sz(1)) // widget position geom=’wm geometry $w +’+string(pos(1))+’+’+ string(pos(2)); title=block.label if title==[] then title="TK source", end tit=’wm title $w Scale’+blknb // write block label bounds=block.rpar(1:2) bnds=’-from ’+string(bounds(1))+’ -to ’+string(bounds(2)) cmd=’-command ""f’+blknb+’ $w.frame.scale""’ lab=’-label ""’+title+’""’; L=’-length 100’ I=’-tickinterval ’+string((bounds(2)-bounds(1))/4) scale=strcat([’scale $w.frame.scale -orient vertical’,.. 10.7 Interactive GUI 225 lab,bnds,cmd,L,I],’ ’) initial=mean(bounds) // initial value is the mean txt=[’set w0.vscale’+blknb; ’set y’+blknb+’ 0’; ’catch destroy $w’; ’toplevel $w’; tit geom ’frame $w.frame -borderwidth 10’; ’pack $w.frame’; scale ’frame $w.frame.right -borderwidth 15’; ’pack $w.frame.scale -side left -anchor ne’; ’$w.frame.scale set ’+string(initial); ’proc f’+blknb+’ w height global y’+blknb+’;set y’+blknb+’ $height’ ]; TCL_EvalStr(txt) // call TCL interpretor to create widget block.outptr(1)=mean(block.rpar(1:2))/block.rpar(3); end elseif flag==1 then // evaluate output during simulation block.outptr(1)=evstr(TCL_GetVar(’y’+blknb))/block.rpar(3); end endfunction The block number (returned by the function curblock) is used to customize Tcl variable names to make sure that if two of these blocks are used in the same diagram, they create diﬀerent widgets. The position of the widget is computed so that it is placed next to the corresponding block in the Scicos diagram (it can be moved subsequently by the user). The complete Tcl text for the construction of the widget is sent to the Tcl interpreter using the TCL EvalStr command. The Tcl procedure deﬁned in the program sets the variable yN (where N is the block number) to the value designated by the position of the scrollbar in the scale. This variable is returned from the Tcl environment to Scilab using the command TCL GetVar in the case of ﬂag= 1. It is then normalized and returned as the output of the block. The source block TKSCALE can be used, for example, to adjust manually the parameters of a PD controller. This is done in the diagram illustrated in Figure 10.21. The clock period activating the square-wave generator is set to half that of the refresh period of the scope so that the output of the generator, displayed as the ﬁrst curve on the scope, remains always the same, that is, −1 up to the middle of the screen and then 1. The output of the plant is changed as the user moves up and down the scrollbars in the GUI boxes. See the simulation result in Figure 10.22. Various Tcl/Tk widgets can be used to display simulation results and even to construct a scope using Tk Canvas. It is not diﬃcult to imagine the construction of sophisticated control panels, for example, the instrumentation panel of a car in Tcl/Tk to be used with a simulation model of the car. This can be done even if the model is simulated in batch mode. 226 10 Examples Figure 10.21. Screen capture of the diagram shows the widgets during simulation. 2 1 0 −1 + −2 7980 7982 7984 7986 7988 7990 7992 7994 7996 7998 8000 Figure 10.22. Simulation result after adjusting PD parameters by hand using the widgets. 11 Batch Processing in Scilab Having Scicos live within Scilab is very useful because it allows the interaction of the two in many diﬀerent ways, thereby mutually enriching their functionalities. We have already seen that Scicos relies heavily on the Scilab language. The Scicos editor is, for example, fully written in Scilab, making it easy for the user to add new functionalities or customize existing ones. We have also seen that the interfacing functions of all Scicos blocks are Scilab functions using only standard Scilab commands. For example, to deﬁne a block’s icon we use standard Scilab graphics commands. But perhaps the most important aspect of having access to Scilab in Scicos is the ability to use Scilab commands from specialized toolboxes, such as Signal Processing, Control and Optimization, in the construction of Scicos blocks. In this chapter we look at the ways Scicos simulations can be performed and used in Scilab. We have already seen that Scilab can be used for postprocessing Scicos simulation results, but here we look at ways of piloting Scicos functionalities through Scilab commands. In doing modeling and simulation work, very often one needs to perform multiple simulations either for adjusting or optimizing model parameters, or for testing the model on multiple data sets, or for performing simulation-based statistical analyses. Monte Carlo techniques are one example. In such cases, Scicos simulations should be carried out in batch mode. In the Scilab/Scicos environment, this can be done within the Scilab language, which not only can handle repetitive calls to the Scicos simulator by changing the value of a parameter or the name of a ﬁle each time, but can also perform complicated analyses and make decisions on how to proceed with additional simulation runs. 11.1 Piloting Scicos via Scilab Commands Having the Scicos editor written entirely in Scilab simpliﬁes a great deal the piloting of Scicos commands from Scilab. Note, however, that the Scicos library is not loaded by default into the Scilab environment. So if you want to use any Scicos routines within Scilab, you must ﬁrst load them as follows: −→ load SCI/macros/scicos/lib 228 11 Batch Processing in Scilab if you are writing a Scilab function, you can place this instruction on top of it. Scicos blocks’ interfacing functions associated with the blocks inside Scicos palettes are not loaded automatically either. If they are needed, they can be loaded as follows: −→ exec (loadpallibs,-1) You can now use Scicos commands associated with each editor button. The name of the function is obtained from the text inside the button from which empty spaces, slashes, dots and dashes have been removed and an underscore has been added to the end. For example, the Scilab function associated with the button Save As is named SaveAs . It is deﬁned in the directory scilab/macros/scicos, like all other editor functions, and inside the ﬁle SaveAs .sci. Note, however, that using editor commands requires a good understanding of the data structures and global variables used in the Scicos editor. Some of these functions require that these variables be deﬁned properly. In order to see how the editor functions should be called, we examine the scicos function. This function, which can be found in scilab/macros/auto/scicos.sci, is the main Scicos editor program and contains the main loop from which editor functions are called when the Scicos editor is active. Some data structures used in this function are explained in Appendix A. In many applications, however, it turns out that there is no need to access directly every editor command. The reason is that often when doing batch processing, we are only interested in being able to change system parameters and running the simulation with these new parameters. The Scilab function scicos simulate provides a generic way of performing batch simulation. This function is described in Section 11.1.2. But ﬁrst we describe scicosim, the basic Scilab function interfacing the Scicos simulator. This function can be used only if the Scicos diagram is compiled. 11.1.1 Function scicosim The Scilab function scicosim is an interface to the Scicos simulator. To use this function, the Scicos diagram must be compiled because we need the compilation result %cpr. The user, however, need not be familiar with the compilation stage (information on that can be found in Appendix A). scicosim is called in three diﬀerent ways. First it is called to initialize the simulation. This is done usually by the following Scilab command: [state,t]=scicosim(state,0,tf,sim,’start’,tol) The string start indicates that only the initialization must be performed. To run the simulation and ﬁnish the simulation, the following corresponding calls are performed: [state,t]=scicosim(state,t,tf,sim,’run’,tol) and [state,t]=scicosim(state,t,tf,sim,’finish’,tol) The argument state initially contains the initial state of the system to be simulated (see Section A.2.3 for details). It contains, in particular, initial values of all the continuoustime and discrete-time states of all the blocks, the preprogrammed events, etc. state is both an input and an output because it changes as the simulation goes on. 11.1 Piloting Scicos via Scilab Commands 229 The argument sim contains the scheduling tables needed by the simulator. These tables are generated by the compiler and do not evolve during the simulation (see Section A.2.3 for details). The argument tf is the time up to which the simulation must run (not used with options start and finish). When scicosim is called with the run option, the simulation is performed up to time tf unless a pause or an error is encountered earlier. The output argument t gives the actual ending time of the simulation. In most cases we have t equal to tf. The input argument t indicates the current time and is used only in the run case. Its value is, in general, 0 because Scicos simulations by default start at time 0. But if a simulation is to be continued by making other calls to scicosim with option run without starting over, then the ﬁnal time of the previous simulation (the output argument t of previous call to scicosim) must be used. The argument tol is a vector containing various simulation parameters. It includes 1. atol: absolute tolerance (see Section 3.2.1). 2. rtol: relative tolerance (see Section 3.2.1). 3. ttol: minimum spacing in time between two events for the continuous-time solver to be called. If two events are spaced closer than deltat in time, the solver is not called and the continuous-time variables are supposed to remain constant over that period. 4. deltat: maximum time interval length used for each call to the continuous-time solver. If the next event is further in the future than deltat, the continuous-time solver is called more than once. 5. scale: sets the correspondence between simulation time and real time. If set to 1, one unit of Scicos simulation time corresponds to one second. The solver is slowed down to achieve the desired correspondence. The default value 0 indicates that no slowing down is to be performed and simulation runs at maximum speed. 6. hmax: maximum time step taken by the solver (see Section 3.2.1). The default value 0 indicates no maximum being set. To use scicosim, the variables state and sim must be available. The initial value of state and the value of sim are computed by the Scicos compiler from the Scicos diagram. It is possible to call the compiler from Scilab to construct these variables, but the simpler solution is to use the Scicos editor to construct them and save them to a ﬁle for future use in Scilab. This can be done as follows: 1. Use the Scicos editor to construct a block diagram and compile it. 2. Save the compiled diagram in binary form (as a .cos file) using the Save or Save As menu. 3. In Scilab, load the saved ﬁle (say myfile.cos) using the load function: load myfile.cos You obtain Scilab variables scicos ver, scs m, and %cpr. scicos ver contains the version number of Scicos that has created the diagram, scs m contains the Scicos diagram, and %cpr contains the result of the compilation. It is this latter that is of interest to us for calling scicosim. In particular, %cpr is a Scilab structure containing state, sim, cor, and corinv. Note that if the diagram is not compiled before being saved, then %cpr is an empty structure. 4. Extract state out of %cpr: state=%cpr.state; 5. Deﬁne tol and tf, for example, 230 11 Batch Processing in Scilab tf=1; tol=[1.d-4,1.d-6,1.d-10,tf+1,0,0]; then call scicosim for initialization: [state,t]=scicosim(state,0,tf,%cpr.sim,’start’,tol); 6. Then run the simulation from 0 to tf: [state,t]=scicosim(state,t,tf,%cpr.sim,’run’,tol); Successive such calls may be performed changing the ﬁnal time tf before each call. 7. End the simulation: [state,t]=scicosim(state,t,tf,%cpr.sim,’finish’,tol); Running Scicos simulations in Scilab in batch mode is useful if the user wants to perform multiple simulations by changing a parameter or an initial condition each time. All the parameters of the blocks in the diagram are in %cpr.sim. The initial conditions are in state. Note, however, that the change in the parameter should not require recompilation. For example, if one is testing the performance of a controller by changing its parameters, the order of the controller should not be modiﬁed, because this implies a change in the size of a state, which in turn requires a partial recompilation since %cpr is no longer valid. To change the parameter of a block, one must know the block number in the compiled structure. This information can be obtained in the Scicos editor using the Get Info menu in the “Compiled structure index” ﬁeld. This number speciﬁes the order in which blocks are placed in %cpr ﬁelds. Note, however, that this number may change if the diagram is edited and recompiled. This number can also be obtained in Scilab if a “label” has been associated with the block of interest. Labels are available in %cpr.sim.labels as a vector. So, for example, if the label toto has been assigned to a block, the block number can be obtained using the command k=find(%cpr.sim.labels==’toto’) k is then the block number and can be used to examine and modify block parameters. Suppose the block labeled toto is a GAIN block. The parameters of the block that are the entries of the gain matrix, like all real parameters of all the blocks, are in %cpr.sim.rpar. The entries of the vector %cpr.sim.rpar that contain the parameter of block k can be obtained from %cpr.sim.rpptr, which is a vector of pointers to %cpr.sim.rpar. In particular, let idx=%cpr.sim.rpptr(k):%cpr.sim.rpptr(k+1)-1 Then idx contains the indices of %cpr.sim.rpar corresponding to the GAIN block: gain_param=%cpr.sim.rpar(idx) So if the objective is to run simulations for various gains, then before starting each simulation, the parameter must be set as follows: %cpr.sim.rpar(idx)=new_gain_param To change integer parameters, the procedure is identical except that the two vectors to use are %cpr.sim.ipar and %cpr.sim.ipptr. For initial continuous-time and discretetime states, the pointer vectors are respectively %cpr.sim.xptr and %cpr.sim.zptr, and the values are found in %cpr.state.x and %cpr.state.z. 11.1 Piloting Scicos via Scilab Commands 231 Example of Batch Simulation sinusoid generator num(s) den(s) write to output file Figure 11.1. Scicos diagram for batch simulation. Consider the Scicos diagram illustrated in Figure 11.1. The sinusoid generator block has been labeled toto, the transfer function in the linear system block is 1/(1+s+s2 ), and the output ﬁle name is chosen to be o file. Suppose we want to simulate this diagram for diﬀerent values of the frequency parameter in the sinusoid generator block and compare the results in Scilab by plotting them in the same plot. This can be done by changing the second parameter of the sinusoid generator (the three parameters of this block are respectively the amplitude, the frequency, and the phase) before each simulation. Note that the ﬁle name is not changed from one simulation to the other, so at the end of each simulation the ﬁle o file must be stored in a diﬀerent location. The following Scilab script does exactly that if batch1.cos has been saved after being compiled. load batch1.cos freq_list=[.1 1 2 4]; ← list of frequencies to consider k=find(%cpr.sim.labels==’toto’); ← block number idx=%cpr.sim.rpptr(k):%cpr.sim.rpptr(k+1)-1; tf=100; ← simulation parameters tol=[1.d-4,1.d-6,1.d-10,tf+1,0,0]; for i=1:size(freq_list,2) ← main loop over the list of frequencies %cpr.sim.rpar(idx(2))=freq_list(i); state=%cpr.state; [state,t]=scicosim(state,0,tf,%cpr.sim,’start’,tol); [state,t]=scicosim(state,t,tf,%cpr.sim,’run’,tol); [state,t]=scicosim(state,t,tf,%cpr.sim,’finish’,tol); // renaming o_file o_file1, o_file2,.... if MSDOS then host(’move o_file o_file’+string(i)); else host(’mv o_file o_file’+string(i)); end end // and plotting on a single graph 232 11 Batch Processing in Scilab xbasc() for i=1:size(freq_list,2) tx=read(’o_file’+string(i),-1,2); plot2d(tx(:,1),tx(:,2),i) end ← reading back the results of the simulation ← and plotting on a single graph In this example, we can avoid using the host command by changing the ﬁle name in the write to output file block. The ﬁle name is coded as an integer in the integer parameter vector of the block. To do so, however, we must know exactly where the name appears in the vector. This information can be obtained by examining the interfacing function of the block. The integer coding of the name can be done using the Scilab function str2code. The function retrieving the name from the code is code2str. Note, however, that if we are to use this method to write into diﬀerent ﬁles, all ﬁle names must have the same length, unless %cpr.sim.ipptr is reconstructed. This is not recommended, especially since there is a more convenient way of handling such modiﬁcations, as we shall see in the next section. Finally, note that in this example the diagram contained no graphics displays such as a scope. This is often the case when one is doing batch processing. But sometimes it could be interesting to follow simulation results visually for debugging purposes. If a graphics display is used in the diagram, then make sure to switch to the old graphics style in Scilab before calling scicosim, because for the moment, Scicos displays use the old graphics routines. This can be done with the Scilab command set(’old_style’,’on’) 11.1.2 Function scicos simulate The scicos simulate function provides a convenient way of running Scicos simulations in Scilab using the diagram’s data structure scs m. Unlike scicosim, scicos simulate does not require %cpr. The function scicos simulate handles automatically all the necessary compilations in a transparent way. In this case, model changes must be done by changing formal parameters deﬁned normally in the context; see Section 11.2.1 for information on formal parameters and the use of context. Changing formal parameters not only is more convenient than directly changing block parameters, it also allows for more general changes. For example, it is more natural in the case of a linear system block, speciﬁed in terms of a transfer function, to specify the new polynomials representing the transfer function’s numerator and denominator than it is to change directly the matrices of the state-space representation. Moreover, by changing the transfer function, the order of the linear system and thus the size of its system matrices can vary. In this case the scicos simulate function performs automatically the necessary (partial) recompilation. The calling sequence of this function is as follows: Info=scicos_simulate(scs_m,Info[,%scicos_context][,flag]); The arguments %scicos context and flag are optional. • scs m: scicos diagram, obtained for example by the instruction load mydiag.cos. Note that the diagram mydiag need not be compiled because we do not make use of %cpr. The Scicos version of mydiag.cos must be the current version. If not (i.e., if the diagram has been saved by an old version of Scicos), you must use the Scicos editor to load and save. The save will be done automatically in the current version. 11.2 Data Sharing 233 • %scicos context: a Scilab structure containing values of symbolic variables used in the context and Scicos blocks. This is often used to change a parameter in the diagram context. In that case, in the diagram context, the variable must be deﬁned such that it can be modiﬁed. Say a variable a is to be deﬁned in the context having value 1, and later in batch mode it is to be changed. In that case, in the context of the diagram one can use the following command to deﬁne a: if ~exists(’a’) then a=1,end This way we deﬁne the default value of a to be 1 but we allow its modiﬁcation. For example, to run a simulation in batch mode using the value a= 2, it suﬃces to set a to 2 in the %scicos.context variable. This can be done as follows: %scicos_context.a=2 • Info: a list. It must be list() at the ﬁrst call, then output Info can be used as input Info for the next call. Info contains compilation and simulation information and is used to avoid recompilation when not needed. Normally the user should not, and need not, modify Info. There are, however, a few exceptional cases in which expert users may be able to edit this variable. Info contains the following ﬁelds (when it is not empty): 1. %tcur: starting time of simulation. It is always zero except if the simulation had been halted before reaching the end time, for example by an event to the block STOP. In this case the simulation is not terminated but only halted at time %tcur. 2. %cpr: compilation result as we have previously seen. 3. alreadyran: a boolean indicating whether the simulation is halted. 4. needstart: a boolean indicating whether the simulation is to be restarted from zero. This applies only if the simulation is halted. If false, the halted simulation proceeds without reinitialization. 5. needcompile: An integer indicating the level of compilation. It should always be zero unless there are errors in the diagram leading to the failure of the compilation process. But it can be used by the user to force a compilation. It should be set to 4. 6. %state0: Initial state. • flag: a string. If it equals nw (no window), then blocks using graphics windows are not activated. In most batch simulation tasks, graphical outputs are not useful and only slow down the simulation. Note that the list of deactivated blocks must be updated as new blocks are added. The current list is coded in the following vector: [’cscope’,’cmscope’,’scope’,’mscope’,’scopexy’,’evscpe’,’affich’] We shall see an example of the usage of scicos simulate in Section 11.3. 11.2 Data Sharing When running a Scicos simulation in Scilab, exchanging data between Scilab and Scicos becomes an important issue. In previous examples we have seen a few methods. In particular, we have seen how to communicate information from Scilab to Scicos using the %scicos context variable. We have also seen how Scicos simulation results can be retrieved in Scilab through writing into a ﬁle. We shall examine in more detail these methods and propose new ones for exchanging data. 234 11 Batch Processing in Scilab 11.2.1 Context Variables The variable %scicos context is used to store all the variables deﬁned in the context. As we have seen in previous chapters, Scicos blocks’ parameters can be made to depend on formal parameters. These formal parameters (which are just Scilab variable names) must be deﬁned in the context of the diagram before being used. All the formal variables deﬁned in a context are placed by the Scicos editor in the Scilab structure %scicos context. So, for example, if the context contains the instruction a=3 then the ﬁeld a is added to %scicos context as if the instruction %scicos_context.a=3 had been executed. After that, a can be used in block dialog boxes to set block parameters. If a variable is not deﬁned in the context, then it cannot be used, unless it is placed initially in %scicos context. The way context works is to ﬁrst extract all the variables from %scicos context and then execute context commands and ﬁnally place all the variables deﬁned by these commands in %scicos context. Variables already in %scicos context can thus be used and updated, if needed, by the context. In most cases, when one is using the Scicos editor, %scicos context is initially empty, and all formal parameters must be deﬁned by the context commands. But a user can initialize %scicos context before launching Scicos (editor) by placing certain parameters in it. The user can then use these parameters without having to deﬁne them in the context. The user just should make sure that the initialization is done every time Scicos is started. But the real usage of %scicos context occurs when the Scicos simulator is to be executed by a Scilab command. Being able to change formal parameters is an extremely ﬂexible way of changing system parameters. We shall see that in Section 11.3 through an example. 11.2.2 Input/Output Files Another way of exchanging data between Scilab and Scicos is through the usage of external ﬁles. We have seen an example of that previously. Files are particularly useful if the data to be exchanged is not just a value but a timed sequence of values. For example, an input signal to a Scicos diagram performing signal processing, is best placed in a ﬁle. Similarly, simulation results in the form of output signals can be placed in output ﬁles. These ﬁles can then be read into Scilab for further processing. A simple example of such exchange is the following. Consider the Scicos diagram in Figure 11.2. In this diagram, the read block generates a piecewise constant signal by reading the values and the timing of the signal from the ﬁle foo. The dialog box of this block is illustrated in Figure 11.3. The signal generated by the read block goes through a continuous-time linear system the output of which, along with the input and the current time, are fed to a write block that writes the data to the ﬁle goo at the frequency speciﬁed by the parameters of the event clock. The “period” of this latter is set to dt deﬁned in the context along with the parameters of the linear system as follows: if ~exists("num") then num=1,end if ~exists("den") then den=1+%s,end if ~exists("dt") then dt=0.1,end 11.2 Data Sharing read from C binary file write to C binary file num(s) den(s) Mux Figure 11.2. Using ﬁles for exchanging data between Scicos and Scilab. Figure 11.3. Parameters of the read block. 235 236 11 Batch Processing in Scilab In order to run this diagram in batch mode from Scilab, we deﬁne a function which receives the description of the input signal and the deﬁnition of the linear system, generates the ﬁle foo, sets the parameters of the linear system in the Scicos diagram, runs the simulation, reads the content of the ﬁle goo, and outputs it: function [t,u,y]=syslinsim(Sys,tf,dt,ui,T,U) // Define symbolic parameters %scicos_context.num=Sys.num %scicos_context.den=Sys.den %scicos_context.dt=dt // Add initial input and a fake end point UT=[[0,T,tf+1];[ui,U,0]]; mopen("foo","wb") mput(UT,"d") mclose() load("bg.cos") scs_m.props.tf=tf // run simulation in batch mode scicos_simulate(scs_m,list(),%scicos_context) // read back result in Scilab mopen("goo","rb") YT=mget(3*tf/dt+1000,"d") mclose() t=YT(3:3:$);u=YT(1:3:$);y=YT(2:3:$) endfunction The following script shows how the function syslinsim can be used: // Example Sys=(1-2*%s)/(1+%s+%s^2); tf=20;dt=.01; ui=0; T=[ 2 4 7 9 ]; U=[ 2 -2 2 0 ]; [t,u,y]=syslinsim(Sys,tf,dt,ui,T,U); // Display result xset("window",1) xbasc() plot2d(t,u,2) plot2d(t,y,5) Figure generated by this script is given in Figure 11.4. 11.2.3 Global Variables A very convenient way of exchanging data between Scilab and Scicos is through the use of global variables, which were discussed in Section 2.2.3. However, this is possible only if the computational function of the Scicos block doing the exchange is written in Scilab (for example, it is a Scifunc block). In general, the use of such blocks is not recommended because of eﬃciency concerns. However, if such a block is not executed very often and 11.3 Examples 237 6 4 2 0 −2 −4 −6 0 2 4 6 8 10 12 14 16 18 20 Figure 11.4. Simulation result displayed by Scilab. executes fast enough, then its use is justiﬁable. In Section 11.3 we shall see an example in which Scifunc is not activated at all by any event in the diagram; it is simply the last call by the Scicos simulator (flag= 5), made to all blocks regardless of whether they are activated, which is used to copy the ﬁnal value of the input of the block into a Scilab global variable. This global variable is subsequently read and used in Scilab. 11.3 Examples Often the purpose of batch processing Scicos simulations is to optimize system parameters. In the following examples, we shall consider the problem of optimizing the parameters of a PI (proportional-integral) controller. These are toy examples presented to illustrate modeling techniques and are not necessarily practical methods for PI controller construction. Consider the Scicos diagram in Figure 11.5. There are two linear systems in this diagram. The one on top has transfer function 1/(1 + s), and the other has transfer function (a + bs)/s. The latter is the PI controller, and a and b are the parameters to be optimized. These parameters are initialized in the context of the diagram as follows: if ~exists(’a’) then a=1;end if ~exists(’b’) then b=1;end The optimality criterion used here is the integral of the square of the diﬀerence between the reference input (a step function in this case) and the output of the system over the interval [0, 10]. The input step occurs at time t = 1. The square is computed using the block u^ a and integrated. The scifunc block returns the ﬁnal value of the integration to Scilab. Note that the scifunc block has an input activation port but this port is not connected. This means that this block is never activated except once at the beginning and once at the end. But this is exactly what is needed here because we only need to read the ﬁnal value at the input of this block, and we want to activate this block as little as possible to avoid loss of performance. To use the scifunc block to return the value of its input to Scilab, the following instructions are placed in the “ﬁnishing” section: 238 11 Batch Processing in Scilab + − u^a Scifunc 1/s + num(s) den(s) − num(s) den(s) Mux Figure 11.5. Scicos diagram for batch simulation. global AA AA=u1 This means that at the end of the simulation, u1, which is the value of the input, is copied into the Scilab global variable AA. Then if in Scilab we declare a global variable with the same name, we can get back the value of u1. Assuming that the diagram is saved in the ﬁle batch11.cos (not necessarily compiled), the following Scilab script runs the simulation for various values of the parameters a and b, stores the value of the diﬀerence between the reference input and the output, and ﬁnally plots in 3D this diﬀerence as a function of the parameters. load batch11.cos A=.04:.004:.06;B=-0.3:.05:-0.1; ← values of a and b global AA ← declared global to read value in scifunc Res=[]; Info=list(); for i=1:size(A,’*’) %scicos_context.a=A(i); ← change a for j=1:size(B,’*’) %scicos_context.b=B(j); ← change b Info=scicos_simulate(scs_m,Info,%scicos_context); Res(i,j)=AA; ← store result end end xbasc() plot3d(A,B,Res) ← visualize the diﬀerence The simulation can be made to run faster if the optional argument nw is used when calling scicos simulate. It is also more eﬃcient to set initially Res to a matrix of appropriate size. As it is programmed here, Res often changes dimensions and thus Scilab performs numerous useless memory allocations and copies. To avoid this, replace 11.3 Examples 239 Res=[]; with, for example (the values of the entries of the matrix don’t matter, it is just the size that counts here), Res=A’*B; The result of running this script is a 3D plot, which is given in Figure 11.6. 0.67 0.65 0.63 0.61 0.59 Z 0.57 0.55 0.53 0.51 0.49 0.47 -0.10 -0.14 -0.18 -0.22 -0.26 Y -0.30 40e-3 44e-3 48e-3 52e-3 X 56e-3 60e-3 Figure 11.6. PI controller performance as a function of its parameters. Scifunc + num(s) den(s) − num(s) den(s) Scifunc Mux Figure 11.7. Scicos diagram for batch simulation. 240 11 Batch Processing in Scilab Consider now the Scicos diagram in Figure 11.7. The problem here is again that of ﬁnding the optimal values of a and b, the PI controller parameters. But in this case we will use an optimization algorithm. For input, we use a square wave, and the diﬀerence between the input and output is no longer computed using an integrator but is evaluated in Scilab using sampled values. Finally, the system to control, used to test the program, is 1/(1 + s + s2 ). Note that the main diﬀerence between the way the block scifunc is used in this example and in the previous case is its activation. The block scifunc is used to exchange data between Scicos and Scilab. In the previous example, scifunc was used to send a Scicos result back to Scilab only once at the end of the simulation. Here, the two scifunc blocks exchange data at every event time generated by the Event Clock. The parameters of the Event Clock are set to 0 for the initial time and 0.01 for the period. Thus the scifunc on the left side of Figure 11.7 generates a new value every 0.01 unit of time. This is done by using the following instructions in the ﬁeld dedicated to the computation of the output: global i global ZZ y1=ZZ(i) i=i+1 Here the variable ZZ contains the proﬁle of the input signal and i is a counter. The scifunc on the right side of Figure 11.7 contains the following instructions in the same ﬁeld: global AA global i AA(i)=u1 y1=u1 Note that we have deﬁned an output for this block even though the output port is not connected and thus the y1 is not used. The reason is that if the block does not have an output, the scifunc does not propose to deﬁne a ﬁeld for the computation of the output, and worse, the Scicos simulator never calls the block, because even though the block is activated, if it has no output to compute or state to update, Scicos considers calling the block to be pointless. The underlying diﬃculty is that the block does have an output, one that goes to Scilab through a global variable, but Scicos is not aware of it. Another way to get around this problem would have been to deﬁne a ﬁctitious discrete-time state. In that case, the block would have been called for state update. This is, for example, how the scope block works except that the state is not ﬁctitious, but rather contains a data buﬀer. Setting the PI controller parameters to zero (a = b = 0), we obtain the simulation result in Figure 11.8. The distance between the input and output in this case is around 21. The optimization is then performed using the following Scilab script: function e=eval_cost(p,z) global(’Info’,’AA’,’ZZ’,’i’) i=1;ZZ=z;AA=zeros(ZZ); %scicos_context.a=p(1) %scicos_context.b=p(2) Info=scicos_simulate(scs_m,Info,%scicos_context,’nw’) e=norm(AA-ZZ) ← distance between input and output 11.3 Examples 241 2 1 0 −1 −2 + 0 1 2 3 4 5 6 7 8 9 10 Figure 11.8. Simulation result without PI controller. //disp(e) endfunction ← to monitor the progress of optimization load Untitled0.cos global(’Info’,’AA’,’ZZ’,’i’) Z=sign(sin([1:1002]/100))’; ← generate a square wave p0=[0.;0.]; Info=list(); %scicos_context.a=p0(1); %scicos_context.b=p0(2); ZZ=Z; i=1;AA=zeros(Z); scicos_simulate(scs_m,Info,%scicos_context); // datafit is an optimization function that // uses optim but does not require the gradient [p,err]=datafit(eval_cost,Z,p0,’ar’,30); ← initial values of a and b ZZ=Z; i=1;AA=zeros(Z); %scicos_context.a=p(1); %scicos_context.b=p(2); // final simulation to visualize the solution scicos_simulate(scs_m,list(),%scicos_context); The function datafit reduces the distance to below 9; the simulation result is depicted in Figure 11.9. The optimal values of the parameters obtained by the program are a = −0.94255, b = 0.02047. datafit is discussed in Chapter 4. The scifunc block used to generate the desired input trajectory from within Scilab can be used to construct complex parameter optimization scenarios. For example, suppose 242 11 Batch Processing in Scilab 2 1 0 −1 −2 + 0 1 2 3 4 5 6 7 8 9 10 Figure 11.9. Simulation result with optimal PI controller. the parameters a and b are to be adjusted not to just one input signal, but we want the PI controller to have good tracking capabilities for a step input and a ramp input. This is done in the following Scilab script: function e=eval_cost(p,z) global(’Info’,’AA’,’ZZ’,’i’) i=1;ZZ=z;AA=zeros(ZZ); %scicos_context.a=p(1) %scicos_context.b=p(2) scicos_simulate(scs_m,Info,%scicos_context,’nw’) e=norm(AA-ZZ) // disp(e) endfunction load Untitled0.cos global(’Info’,’AA’,’ZZ’,’i’) Z=[ones(1002,1),[1:1002]’/500]; p0=[0.;0.]; Info=list(); %scicos_context.a=p0(1); %scicos_context.b=p0(2); for ZZ=Z i=1;AA=zeros(ZZ); scicos_simulate(scs_m,Info,%scicos_context); end [p,err]=datafit(eval_cost,Z,p0,’ar’,30); Info=list(); 11.4 Steady-State Solution and Linearization 243 for ZZ=Z i=1;AA=zeros(ZZ); %scicos_context.a=p(1); %scicos_context.b=p(2); scicos_simulate(scs_m,Info,%scicos_context); end Note that the optimized cost in this case is the sum of the two distances obtained for a step and a ramp input. It is, of course, fairly straightforward to weigh the distances diﬀerently. The simulation results without PI controller (parameters a and b set to zero) are given in Figure 11.10 and Figure 11.11. The simulation results using the optimal PI controller are given in Figure 11.12 and Figure 11.13. 2 1 0 −1 −2 + 0 1 2 3 4 5 6 7 8 9 10 Figure 11.10. Simulation result for step input without PI controller. 11.4 Steady-State Solution and Linearization Scicos is primarily used to construct nonlinear models for simulation purposes. For analysis and controller or ﬁlter synthesis, it is often necessary to have a linearized version of the model, or more speciﬁcally, of the part of the model that represents the system being analyzed. Scicos provides some facilities for performing linearization of such systems. The method presented in this section concerns only continuous-time components of the system described by a Scicos model or submodel (Super block). These components represent a ﬁnite-state, in general nonlinear, dynamical system that can be expressed as follows: ẋ = f (x, u, t), (11.1) y = g(x, u, t). (11.2) 244 11 Batch Processing in Scilab 2 1 0 −1 −2 + 0 1 2 3 4 5 6 7 8 9 10 Figure 11.11. Simulation result for the ramp input without PI controller. The two curves are almost identical. 2 1 0 −1 −2 + 0 1 2 3 4 5 6 7 8 9 10 Figure 11.12. Simulation result for step input with optimal PI controller. Here x is the continuous-time state vector; u and y are the input and output vectors. For simplicity, we assume here after that f and g do not depend explicitly on time and drop t from their list of arguments. An equilibrium point of system (11.1) is any pair (x0 , u0 ) satisfying f (x0 , u0 ) = 0. The state x0 is the steady-state solution associated with the constant input u0 . This type of equilibrium is also sometimes called a setpoint. Clearly if the system is at an equilibrium point and u is the corresponding constant u0 , then the state remains at x0 indeﬁnitely. 11.4 Steady-State Solution and Linearization 245 2 1 0 −1 −2 + 0 1 2 3 4 5 6 7 8 9 10 Figure 11.13. Simulation result for ramp input with optimal PI controller. The two curves coincide almost perfectly. In most control problems, the objective of the controller is to keep the system near an equilibrium point. Under mild regularity assumptions on the functions f and g, the behavior of the system near the equilibrium point can be approximated with a linear model. Let δx = x − x0 , δu = u − u0 , be the deviation from the equilibrium. Then the following linear model approximates the continuous-time behavior of the nonlinear system: ˙ = Aδx + Bδu, δx δy = Cδx + Dδu, where matrices A, B, C, and D are given by ∂f (x0 , u0 ), ∂x ∂g (x0 , u0 ), C= ∂x A= ∂f (x0 , u0 ), ∂u ∂g (x0 , u0 ). D= ∂u B= To illustrate how linearization can be done and used in Scicos, we consider an example. In particular, we consider the classical problem of the control of an inverted pendulum on a cart. The system consists of a cart of mass M traveling on a ramp and subject to a force u(t) with an inverted pendulum hinged on its side. For simplicity, all friction and the mass of the cart wheels are neglected. Moreover, it is assumed that the mass of the pendulum is concentrated at the tip and has value m. It is also assumed that the slope of the ramp, φ, is known. The setup is illustrated in Figure 11.14. The dynamics of this mechanical system can be obtained easily from the Euler Lagrange equation, and is given below: 246 11 Batch Processing in Scilab m θ u(t) M z φ Figure 11.14. Inverted pendulum on a cart. (M + m)z̈ + mlθ̈ cos(θ − φ) − mlθ̇2 sin(θ − φ) = u(t) − (M + m)g sin(φ), 2 ml θ̈ + mlz̈ cos(θ − φ) − mgl sin(θ) = 0. C block2 1/s 1 (11.4) zd zdd thdd 1 1/s thd 1/s (11.3) 2 1/s Figure 11.15. Scicos diagram for inverted pendulum on a cart problem. To start, we need to construct a Scicos diagram to model these dynamics. We can do this in many diﬀerent ways, both using existing blocks and constructing new blocks. To use existing blocks, we can reduce considerably the complexity of the diagram by using the Mathematical Expression block. The model can be reduced to a single block if a new block is used. This can be done with both C and Scilab blocks. Here we have chosen to use a new C block but leaving out the integrators; see Figure 11.15. The Cblock2 is used to evaluate z̈ and θ̈ as functions of θ, θ̇, and u. The expressions for z̈ and θ̈ are readily obtained from (11.3) and (11.4), and are coded in the block as follows: 11.4 Steady-State Solution and Linearization 247 #include <math.h> #include <scicos/scicos block.h> void inv pend(scicos block *block,int ﬂag) { double M,m,l,g,ph,th,thd,zdd,thdd,u,delta; M=block−>rpar[0]; m=block−>rpar[1]; l=block−>rpar[2]; ph=block−>rpar[3]; g=9.81; th=block−>inptr[0][0]; thd=block−>inptr[1][0]; u=block−>inptr[2][0]; delta=M*m*l*l+m*m*l*l*pow(sin(th−ph),2); zdd=(m*l*l*(m*l*thd*thd*sin(th−ph)+u− (M+m)*g*sin(ph))−m*m*l*l*g*sin(th)*cos(th−ph))/delta; thdd=(−m*l*cos(th−ph)*(m*l*thd*thd*sin(th−ph)+u− (M+m)*g*sin(ph))+(M+m)*m*g*l*sin(th))/delta; block−>outptr[0][0]=zdd; block−>outptr[1][0]=thdd; } Note that the block’s parameter vector rpar contains M , m, l, and φ, which can be given numerical values or deﬁned symbolically, for example as [M m l ph], in the dialog box of the block. In this latter case, Scilab variables M, m, l, and ph must have been previously deﬁned in the context of the diagram. The model non lin can be used to construct a linearized model of the nonlinear system and subsequently a controller. It can also be used in the construction of a complete Scicos diagram for testing the controller. 11.4.1 Scilab Function steadycos Computing an equilibrium point can be done using the Scilab function steadycos. The calling sequence is the following: [X,U,Y,XP]=steadycos(scs_m,X,U,Y,Indx,Indu,Indy [,Indxp [,param ] ]) where • • • • • • • • scs m: a Scicos diagram data structure. X: column vector. Continuous state. On input, it can be set to [] if zero. U: column vector. Input. On input, it can be set to [] if zero. Y: column vector. Output. On input, it can be set to [] if zero. Indx: indices of entries of X that are not ﬁxed. If all can vary, must be set to 1:$. Indu: indices of entries of U that are not ﬁxed. If all can vary, set to 1:$. Indy: indices of entries of Y that are not ﬁxed. If all can vary, set to 1:$. Indxp: indices of entries of XP (derivative of x) that need not be zero. If all can vary, it must be set to 1:$. The default value is []. • param: list with two elements (default list(1.d-6,0)) – param(1): scalar. Perturbation level for linearization. – param(2): scalar. Time t. 248 11 Batch Processing in Scilab The equilibrium point is, in general, not unique, and parts of the vectors x0 and u0 can be set a priori to desired values. Vector indices are used to specify parts of the state and the input that are free to be adjusted by the program. Even by specifying properly these indices, the equilibrium point may not be unique and the input arguments X and U can be used to direct the search toward a desired solution since they are used as initial guess values for the search algorithm. Often the problem of ﬁnding an equilibrium point is that of ﬁnding a steady-state solution for a given input, in which case Indu is set to [] and Indx to 1:$. But in the example we are considering in this section, the equilibrium point we are looking for has an unknown u0 and a known x0 . The desired objective is to keep the cart at the center with the pendulum pointing upward. The force u0 is what is needed to prevent the cart from moving; that is, it compensates for the force of gravity. It is not diﬃcult to see that u0 = (M + m)g sin(φ), (11.5) but we let the program ﬁnd this by itself. This is done using the following commands: −→ load non_lin.cos −→ [X,U,Y,XP]=steadycos(scs_m,[],[],[],[],1,1:$) XP = 1.0E-16 * ! 0.3830958 ! ! 0. ! ! - 0.3811819 ! ! 0. ! Y = ! 0. ! ! 0. ! U = 12.731756 X = ! 0. ! ! 0. ! ! 0. ! ! 0. ! Here it is assumed that the Scicos diagram non lin has been saved in the current directory. Loading non lin.cos provides the variable scs m, which contains all the information regarding the diagram. This variable is then used as the argument to the function steadycos. 11.4.2 Scilab Function lincos Computing the linearized system around an equilibrium point can be done using the Scilab function lincos. The calling sequence is the following: sys= lincos(scs_m [,x0,u0 [,param] ]) where • scs m: a Scicos diagram data structure. • x0: column vector. Continuous-time state around which linearization is to be done (default value is 0). • u0: column vector. Input around which linearization is to be done (default is 0). 11.4 Steady-State Solution and Linearization 249 • param: list with two elements (default is list(1.d-6,0)). – param(1): scalar. Perturbation level for linearization. – param(2): scalar. Time t. • sys: linear state-space system. The linear system obtained from the linearization is coded in a “linear system” Scilab object. This is a tlist including the A, B, C, D matrices. Applied to our example, we obtain −→ sys= lincos(scs_m,X,U) sys = sys(1) (state-space system:) !lss A B C D X0 dt ! ! ! ! ! 0. 1. 0. 0. sys(2) = A matrix = 0. - 2.993E-08 - 2.9195682 0. 0. 0. 0. 2.978E-08 12.714983 0. 1. 0. sys(3) ! 0.0997019 ! 0. ! - 0.0992038 ! 0. ! ! ! ! = B matrix = ! ! ! ! sys(4) = C matrix = 1. 0. 0. ! 0. 0. 1. ! ! ! 0. 0. ! ! sys(5) = D matrix = 0. ! 0. ! ! ! ! ! 0. 0. 0. 0. sys(6) = X0 (initial state) = ! ! ! ! sys(7) = Time domain = c The linear system can now be used to construct a controller. Many methods are coded and available in Scilab for designing controllers including pole placement, LQG, H∞ , and LMI. Here we use a simple pole placement method: Kc=-ppol(sys.A,sys.B,[-1,-1,-1,-1]); Kf=-ppol(sys.A’,sys.C’,[-2,-2,-2,-2]);Kf=Kf’; Contr=obscont(sys,Kc,Kf); The “linear system” Contr contains the system matrices of the controller. To test and validate the controller’s use in the original nonlinear model, a Scicos simulation must be performed. This is done using the Scicos diagram in Figure 11.16. The Super block non lin contains the diagram in Figure 11.15. An animation block is used to show in real time the movement of the cart and the pendulum. This block is developed 250 11 Batch Processing in Scilab U MScope z + + θ non_lin xd=Ax+Bu y=Cx+Du Mux Figure 11.16. Complete Scicos diagram for testing the controller and validating the result by animation. with a type-5 computational function. The interfacing and the computational functions are given in Section B.2. Note that the control value (input to the Super block) is obtained by adding U, which is the value of u corresponding to the equilibrium point, denoted by u0 earlier, and the output of the controller that generates δu. To simplify the procedure for using this diagram, all the controller design steps are included in the context of the diagram as follows: M=10;m=3;l=3;ph=0.1; scs_m=scs_m.objs(27).model.rpar; [X,U,Y,XP]=steadycos(scs_m,[],[],[],[],1,1:$); sys= lincos(scs_m,X,U); Kc=-ppol(sys.A,sys.B,[-1,-1,-1,-1]); Kf=-ppol(sys.A’,sys.C’,[-2,-2,-2,-2]);Kf=Kf’; Contr=obscont(sys,Kc,Kf); clear(’scs_m’,’X’,’Y’,’XP’,’Kc’,’Kp’,’sys’) z0=-4;th0=.02; Here we see that the scs m corresponding to non lin is extracted directly from the scs m of the diagram, which is always available within the Scicos environment. For this, the object number of the Super block in scs m must be known. In this case, it is the 27th. This information can be obtained using the Get Info button in the Object menu. The context deﬁnes Scilab variables M, m, l, ph, z0, th0, U, and Contr, which are used as formal parameters of various blocks in the diagram. But there are intermediary variables such as Kc, Kf, which are not needed outside the context and are removed with the clear statement to avoid the needless storage of their values in the %scicos context. The removal of scs m is particularly important. If it is not removed, then it would clash with the main diagram’s scs m. 11.4 Steady-State Solution and Linearization 251 Figure 11.17. Dialog box of the controller block. To see how the Scilab variables deﬁned in the context are used, consider for example the dialog box of the linear system block representing the controller illustrated in Figure 11.17. The variable U is used in the constant block feeding the summation block. The real parameters of the CBlock2 block are set to [M m l ph], and z0 and th0 are used as initial conditions for integration blocks in the non lin diagram. The simulation results conﬁrm the correctness of the design procedure as shown in Figure 11.18. The system state and control converge to the desired steady-state values. The correctness of the model itself can be veriﬁed by examining the animation. 10 5 0 −5 + −10 0 1.0 0.5 0.0 −0.5 + −1.0 0 70 50 30 10 + −10 0 3 6 9 12 15 18 21 24 27 30 3 6 9 12 15 18 21 24 27 30 3 6 9 12 15 18 21 24 27 30 Figure 11.18. Simulation results. The displayed functions correspond to u, z, and θ. 12 Code Generation Scicos provides a code-generation facility that can be used to create C code for a model deﬁned in Scicos. Code generation has many applications. It can be used to improve simulation performance. Even though Scicos diagrams are compiled in the sense that all the scheduling tables are precomputed, there is still some overhead in using the Scicos simulator that is eliminated by running the code that has been generated. Even if the performance gain is not signiﬁcant, replacing a Super block with a basic block may be of interest if the designer of the model does not want to reveal the content of the Super block. If the Super block is converted to a basic block, the designer can then simply distribute a compiled version of the generated code. But the most important application of code generation is the creation of standalone applications (programs that run independently of Scilab), which can be used, in particular, in real-time applications. This chapter discusses how the code-generation facility can be used and what the limitations are. Some examples are provided. 12.1 Code Generation Procedure The Code Generation item in the Object menu can be used to generate C code for the submodel deﬁned within a Super block. In addition, once invoked, Code Generation compiles and links incrementally the generated code with Scilab and replaces the Super block with a basic block having for computational function the generated code. Except in some special situations that we shall discuss later, the replacement of the Super block with this new block should not alter simulation results in any way. Code Generation also creates a main C program implementing a standalone application. Consider the Scicos diagram in Figure 12.1. In this model, a random number is generated by the random generator block at every clock event. The block parameter is set so that the random number can be both positive and negative. If the number is positive, the If-then-else block activates the memory block 1/z, which is updated by adding its previous value to the (positive) value of the random number. Finally, the result is displayed in the scope alongside the values of the random signal. The simulation result shows, as expected, that the output of the memory block increases only when the random number is positive. The output remains constant otherwise. See Figure 12.2. Suppose now that we want to isolate the part of the diagram that receives a number and, depending on its sign, adds it to a counter, in order to perform code generation for 254 12 Code Generation If in>0 then random generator + else 1/z + MScope Figure 12.1. Original Scicos diagram. 10 + 5 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + −5 −10 + 0 3 6 9 12 15 2 ⊕ ⊕ ⊕ ⊕ ⊕ ⊕ ⊕ ⊕ 24 0 ⊕ ⊕ ⊕ ⊕ ⊕ ⊕ ⊕ 6 30 ⊕ ⊕ ⊕ 3 ⊕ ⊕ + 27 ⊕ ⊕ −1 −2 21 ⊕ 1 0 18 ⊕ ⊕ ⊕ ⊕ ⊕ ⊕ 9 12 15 18 21 24 27 30 Figure 12.2. Simulation result for the Scicos diagram in Figure 12.1. Output is the top graph. The random numbers generated are shown in the bottom graph. that part of the diagram. The ﬁrst step is to place this part in a Super block. This can be done using the Region to Super Block facility. See Figure 12.3. To generate C code for this Super block, it suﬃces to select the Code Generation item in the Object menu and then click on the Super block. A dialog box is then opened so that the user can specify the name of the new block and the directory in which the generated code should be placed. Once the answers are validated, the following lines are displayed in Scilab, indicating that the code has been successfully generated, compiled, and linked: building shared library (be patient) shared archive loaded 12.1 Code Generation Procedure 255 random generator MScope Figure 12.3. Scicos diagram in Figure 12.1 after isolating the part for which code generation is to be done. Link done Link done Subsequently, the Super block is converted into a block; see Figure 12.4. Simulation can be performed to verify that this operation has not altered system behavior. Example random generator MScope Figure 12.4. Scicos diagram after code generation. This code generation operation creates a number of ﬁles in the speciﬁed directory. Since we have named our new block Example, all of the generated ﬁle names start with Example. The main ﬁle is Example.c, which is the computational function of the new 256 12 Code Generation block (this ﬁle is already compiled and linked with Scilab), and Example Cblocks.c, Example standalone.c, and Example act sens events.c, which are used for standalone application. The ﬁle Example Cblocks.c contains the main routines which are also found in Example.c. The ﬁle Example standalone.c is the main standalone program. The ﬁle Example act sens events.c contains the routines corresponding to the input/output ports. In an embedded application, they would be the routines handling communications with sensors and actuators. That is why the routines are named Example sensor for the inputs and Example actuator for the outputs. The ﬁle also contains the routine Example events, which is used to specify how the program should be executed by setting the event dates and ensuring real-time pacing if needed. This ﬁle is generated the ﬁrst time code generation is performed, for a given example, and contains default routines, which consist of sensors and actuators that read from and write to the shell, and event dates regularly spaced in time with no real-time pacing. Since this ﬁle is very likely to be edited by the user in subsequent code generations for the same example, the user has the option of not regenerating it. To create the standalone version, the following shell command should be used inside the directory where the ﬁles have been created: under Unix, Linux, and MacOSX make -f Example_Makefile standalone under Windows (inside an MSDOS shell) nmake /f Example_Makefile standalone Note that under Windows, Scilab.dll and Atlas.dll must be added to the library path of the system, or copied into the current directory. These ﬁles are available in the scilab/bin directory. The compilation may generate some warnings concerning unused variables. This is normal. If everything goes well, this command creates the executable ﬁle standalone (standalone.exe under Windows). Running the program, for example, under Unix, Linux, and MacOSX, by typing ./standalone the user is asked to enter an input value (followed by carriage return). In response, the program computes and displays the corresponding output value. Then the user is asked again to enter an input value, and so on. The following is what is displayed on the terminal. Note that the user has entered 23, 2 and 3; the rest is displayed by the program. Require outputs of sensor number 1 time is: 0.100000 size of the sensor output is: 1 Please set the sensor output values 23 Actuator: time=0.100000, u(0) of actuator 1 is 0.000000 Require outputs of sensor number 1 time is: 0.200000 size of the sensor output is: 1 Please set the sensor output values 2 Actuator: time=0.200000, u(0) of actuator 1 is 23.000000 Require outputs of sensor number 1 time is: 0.300000 size of the sensor output is: 1 Please set the sensor output values 12.2 Limitations 257 3 Actuator: time=0.300000, u(0) of actuator 1 is 25.000000 Of course this sequence continues until the ﬁnal time tf is reached. Note that sensor output is the input to the program (block) and input (u) of the actuator is the output of the program. These actuator sensor routines are, of course, useful only for debugging purposes. They should be adapted to the application at hand. The standalone program can also be called with optional arguments: standalone [-h] [-v] [-i arg] [-o arg] [-d arg] [-s arg] [-e arg] [-t arg] where • • • • • • -h: prints a short help page, -v: prints the Scilab version used to create the program, -i: to deﬁne input ﬁle name; by default, terminal is used, -o: to deﬁne output ﬁle name; by default, terminal is used, -d: sets discrete-time clock period; default is 0.1, -s: to chose the ﬁxed-step integration method among ﬁrst-order Euler (1), Heun (2), and fourth order Runge Kutta (3), • -e: sets discretization time period used by ode solver in case of continuous-time states, default is 0.001, • -t: deﬁnes ﬁnal simulation time, default is 30. The options s and e concern only diagrams containing continuous-time states. 12.2 Limitations We have seen that C code can be generated to realize the behavior of a Super block in Scicos simply by designating the Super block after selecting the Code Generation menu item. There are, however, limitations on the Scicos models that can be inside the Super block. This section presents these limitations. 12.2.1 Continuous-Time Activation In most applications, even if the Scicos model contains continuous-time blocks, the part of the model for which code generation is to be performed contains only discrete-time components. Typically, this part represents a controller or a ﬁlter that is to be implemented in a real-time application on digital hardware. Scicos simulations in such cases are used to evaluate the controller/ﬁlter performance when applied to a continuous-time model of the environment. It is for this reason that in Scilab version 3.0, Scicos proposed code generation only for discrete-time systems. In Scilab version 3.1, the code generator has been extended and allows code generation even if the Super block contains continuously activated blocks including blocks with continuous-time states. There are, however, some limitations, in particular concerning zerocrossing events. The standalone code in this case contains ﬁxed-step solvers paticularly appropriate for real-time applications. This recent extension of the CodeGeneration function should be considered to be at the beta testing stage. 258 12 Code Generation 12.2.2 Synchronicism We have seen that code generation is done by ﬁrst converting the Super block for which code generation is done into a basic block. This is done, in particular, for testing the generated code. But we have also seen that a basic Scicos block cannot be self activated. Its activation must come from the outside. This sets some limitations on the class of Super blocks that can be handled by Code Generation. Consider the Super block model depicted in Figure 12.5. 1 Event Delay 1 1/z S/H 1 Figure 12.5. Asynchronous Super block diagram. In this example, the 1/z and the S/H blocks are not synchronous. The reason is that S/H is activated by the output of the Event Delay block and the events corresponding to this output are not synchronous with the input events of this block that activate the 1/z block. In fact, no Scicos basic block can produce output events synchronous with its input events, the only exceptions being the If-then-else and event select block,s which, strictly speaking, are not blocks. So a hard constraint on the blocks within the Super block is that none should have an output activation port unless it is an If-then-else or an event select block. 12.3 A Look Inside Code generation is done by the Scilab function CodeGeneration , which can be found in the directory scilab/macros/scicos. This function takes a Scicos model (model inside the designated Super block) and converts it into a regular block by generating the corresponding computational function in the C language. It also generates a standalone C program. The code generation is done in ﬁve steps: diagram construction, compilation, code generation, block construction, and substitution. Diagram Construction The subdiagram within the designated Super block is extracted and used to construct a new Scicos diagram. This requires modifying the subdiagram because of the presence of input/output ports that are not Scicos blocks. These ports, used inside Super blocks, are eliminated at a precompilation stage when the original diagram is compiled. 12.3 A Look Inside 259 The replacement of regular input ports is fairly straightforward because each input port is simply replaced with a regular block representing a sensor block and each output port is replaced with an actuator block. In a standalone application, it is up to the user to deﬁne the computational functions of these blocks. The Scicos code generator, by default, generates computational functions that read from and write to the terminal where the standalone application is launched. This facility is useful for debugging purposes. 1 1 num(z) den(z) 1 num(z) den(z) Figure 12.6. Diagram inside the Super block designated for code generation. The activation of the sensor blocks must be done through the addition of activation links. Let us, consider the simple case illustrated in Figure 12.6. We see the diagram inside the Super block designated for code generation. To be able to use the Scicos compiler, this diagram is transformed into the one illustrated in Figure 12.7. This transformation is transparent to the user, who never sees this diagram. We see that the sensor block replacing the input port has received an explicit activation from the block replacing the activation input port. The actuator block operates on inheritance and does not require explicit activation (placing an explicit activation link would not aﬀect its behavior in this case). If the Super block contains more than one input port, then each port is turned into a sensor block. And each sensor block is explicitly activated. But if the block contains more than one activation input port, the situation becomes more complicated because the events received through these ports may or may not be synchronous. For example, if the Super block contains two activation input ports, three events must be considered: event received on port 1, event received on port 2, synchronous events received on ports 1 and 2. That is why, in this example, the two activation input ports are replaced with a single block having 3 output activation ports. The activations are then used in such a way as to preserve the functionality of the Super block. Compilation The modiﬁed diagram corresponds to a valid Scicos diagram and it is compiled with the standard compiler. The result of the compilation contains all the scheduling tables needed to generate the simulation C code. 260 12 Code Generation Event Source Sensor num(z) den(z) Actuator num(z) den(z) Figure 12.7. Diagram inside the Super block after transformation. Code Generation Two programs are generated: one to deﬁne a new block to substitute for the Super block in the Scicos diagram and one to be used for standalone applications. In the ﬁrst case, the sensor and actuator blocks’ computational functions are replaced with dummy programs. The inputs and outputs to the block are written directly on their outputs and inputs during simulation by the new computational function. For the standalone applications, default computational functions are generated in a separate ﬁle, which can be customized by the user. Block Construction The C code representing the computational function of the new block is generated at the “Code generation” stage. Here the Scilab interfacing function is generated. Substitution The new block replaces at this stage the Super block in the Scicos diagram. The compilation and linking of the associated computational function is done automatically so that after the substitution, the new Scicos diagram can be simulated. 12.4 Some Pitfalls In most cases, code generation does not alter the behavior of the system. The Super block and the block replacing it have identical behavior. There is, however, one situation in which the two behaviors may diﬀer. This diﬀerence is due to the way inheritance works in Scicos. Consider the Scicos diagram illustrated in Figure 12.8. In this diagram, we use the 1/z block, available in the Linear palette, with the inheritance option. The behavior of this block is exactly the same as the usual 1/z except that since it does not have an activation input port, it inherits its activation. Even though it is 12.4 Some Pitfalls sinusoid generator 261 If in>0 then else square wave generator 1/z S/H MScope Figure 12.8. Scicos diagram posing problems for the code generator. not recommended in Scicos to use blocks with discrete states that inherit their activations, there is nothing in the formalism that forbids it, and in some cases, it could be a useful facility. Suppose now that we want to generate code for the part of the diagram including this new block, the Summation and the S/H (sample and hold) block. This is done by encapsulating the region of interest in a Super block using the Region-to-Superblock facility. The result is given in Figure 12.9. sinusoid generator If in>0 then else square wave generator MScope Figure 12.9. Diagram after encapsulation by Super block. 262 12 Code Generation By examining Figure 12.8, we see that the 1/z and Summation, which form a discrete integrator, are activated by the output of the If-then-else block, that is, by a subsampled version of the activations generated by the Event Clock. In fact, the activation takes place only if sin(t) is positive. On the other hand, the S/H block is activated directly by the Event Clock. Since this block simply performs a copy of the input on the output, its more frequent activation does not change the result of the simulation in any way. The encapsulation process does not alter, of course, the simulation result either, and for both Diagrams 12.8 and 12.9, the simulation result is given in Figure 12.10. 2 1 ♦ ♦ ♦ ♦ ♦ ♦ ♦ 0 ♦ −1 −2 ♦ ♦ + 0 1 2 3 4 5 6 7 8 9 10 2 ♦ 1 0 ♦ ♦ ♦ ♦ ♦ ♦ ♦ 3 4 5 6 ♦ ♦ −1 −2 + 0 1 2 7 8 9 10 Figure 12.10. Simulation result before code generation (the output of the MScope display). After code generation, we obtain the diagram in Figure 12.11, which results in the simulation given in Figure 12.12. Clearly the simulation results are diﬀerent from those in Figure 12.10. To explain the discrepancy, note that after the encapsulation step (Figure 12.9), the Super block contains both blocks activated through inheritance and blocks activated directly through the activation signal received on its activation input port. But when the Super block is converted into a basic block by code generation, the new block cannot exhibit both behaviors. It can work either by inheritance or by explicit activation, but not both. In this case, since the Super block has an explicit activation port, it has no choice but to consider this port as the source of activation for all blocks inheriting their activations with the Super block. In a sense, the Super block is considered a functional encapsulation by the code generator and not just a graphical commodity. Another situation in which such a problem occurs is when a block inherits from the new generated block. Consider the Scicos diagram in Figure 12.13. In this diagram, the scope inherits its activation from the S/H block, which means that it displays the sample values of sin(t) only if this latter is positive, as can be seen from the simulation result illustrated in Figure 12.14. If the subsampling part of the diagram is converted into a block using code generation as illustrated in Figure 12.15, the simulation result changes because now the scope inherits 12.5 Applications 263 If in>0 sinusoid generator then else square wave generator MScope Untitled Figure 12.11. Diagram after the code generation step. 2 1 ♦ ♦ ♦ ♦ ♦ ♦ ♦ 0 ♦ −1 −2 ♦ + 0 1 2 3 ♦ 2 ♦ 1 0 ♦ 4 5 6 7 8 9 10 5 6 7 8 9 10 ♦ ♦ ♦ −1 −2 + 0 1 2 3 4 Figure 12.12. Simulation result after code generation.The output of the Untitled block diverges. from the new block and thus it is activated every time. The simulation result is illustrated in Figure 12.16. 12.5 Applications Hardware in the loop and real-time embedded control are the main applications of code generation in Scicos. Hardware in the loop applications can be developed by constructing speciﬁc Scicos blocks for interfacing control and measurement devices. Under Linux, for 264 12 Code Generation If in>0 then else sinusoid generator S/H Figure 12.13. Original diagram; the scope inherits from the S/H block. 1.50 0.75 0.00 +++++ ++ + + ++ + + + + + + + + + + + + + + + + + + + + ++++ ++ ++ + + + + + + + + + + + + + + + + + + + + + + + + ++++++ ++ + + ++ + + + + + + + + + + + + + + + + + + + ++++++ ++ + + ++ + + + + + + + + + + + + + + + + + + + + +++++ ++ ++ + + + + + + + + + + + + + + + + + + + + + + −0.75 −1.50 + 0 3 6 9 12 15 18 21 24 27 30 Figure 12.14. Simulation result shows that scope is active only if the value to be displayed is positive. Sample sinusoid generator Figure 12.15. Diagram after code generation; the scope inherits from the Sample block. 12.5 Applications 265 1.50 0.75 0.00 ++++ +++++ +++++ ++++++ ++++++ ++ ++ ++ + ++ ++ ++ + ++ + + + + ++ + + + ++ + ++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ++++++++++++++++++++++++++++++++++ ++++++++++++++++++ + +++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++ −0.75 −1.50 + 0 3 6 9 12 15 18 21 24 27 30 Figure 12.16. Simulation result is diﬀerent from the original simulation, as expected. example, this has been done using the COMEDI tool (http://www.comedi.org). The Scicos COMEDI toolbox can be downloaded from the contribution section of the Scilab home page. Direct real-time code generation for Linux RTAI is available through the RTAI-Lab project; see http://a.die.supsi.ch/~bucher/rtai.html. An adapted version of the Scicos code generator is used in this project. RTAI-Lab relies on Scicos for the control system code design and generation, with only the addition of some speciﬁc blocks and building options. The generated code is then embedded in an RTAI framework to be executed in soft/hard real time and monitored by a generic graphical user interface. For hard real-time embedded applications in multiprocessor environments, an interface between Scicos and SynDEx (http://www.syndex.org) is being developed. SynDEx is system-level CAD software for rapid prototyping and optimizing the implementation of distributed real-time embedded applications in multiprocessor architectures. Information about this project can be obtained from http://www.criltechnology.com/eclipse. 13 Debugging When a large Scicos diagram does not yield the expected simulation results, it can be fairly complicated to identify the problem. To help the user, debugging tools are provided in Scicos. We shall present these tools later in this chapter, but we will ﬁrst explain Scicos error messages. 13.1 Error Messages When something goes wrong during the simulation, the simulation stops and an error message is displayed. Problems encountered by the simulator can be of a number of diﬀerent types. 13.1.1 Block Errors An error could occur within a block, or more speciﬁcally the computational function of the block. These errors must be signaled by the block itself using the set block error function, which should be called with a negative value depending on the type of error encountered. The following error ﬂag values can be used: • −1: This means that a block has been called with input out of its domain. In general, this means that one of the input arguments of the computational function does not have the expected value. The block can print out, in a Scilab menu, more details using the scifunc function. • −2: This error is signaled if the block encounters a singularity, that is, an operation such as division by zero. • −16: This error corresponds to the case that a block wants to allocate memory (for which it has to use the scicos malloc function) but it cannot. Another block-related error, not detected by the block itself but by the simulator, happens when the block tries to program an event on an output activation port but the event previously programmed on the same activation port has not been ﬁred. This happens, for example, if an event delay block is activated with an event clock and the period of the clock is smaller than the delay period in the delay block. Finally, it could happen that Scicos simply cannot ﬁnd the block’s computational function. This could happen with a user block if the computational function is not linked in Scilab before simulation or its name does not match the name in the model data structure of the block (set by the interfacing function). The message in this case is unknown block. 268 13 Debugging 13.1.2 Errors During Numerical Integration Scicos uses LSODAR in the explicit case to simulate the continuous-time dynamics of the model. When an error is signaled by lsodat, Scicos displays the error message integration problem and speciﬁes the value of the error ﬂag istate returned by LSODAR. The following values of istate are relevant in Scicos. • −1: This means that too many calls have been made by the solver over the integration period. This often happens when the system is very stiﬀ or nonsmooth and/or the integration period is too long. The integration period is the period between two event,s which often correspond to the activations generated by an event clock to drive a scope or read-from/write-to ﬁle blocks. In that case, to reduce the integration period it suﬃces to reduce the period of the event clock. It is also possible to set a maximum for the integration period in the Setup menu. • −2: This means that excess accuracy has been requested (tolerances are too small). The tolerances can be set by the Setup menu. • −5: This means that repeated convergence failures have occurred, probably due to the requested tolerances being too small. In the implicit case, the solver DASKR can return additional error messages: • −7: The nonlinear system solver in the time integration could not converge. • −8: The matrix of partial derivatives appears to be singular. • −9: The nonlinear system solver in the integration failed to achieve convergence, and there were repeated error test failures in this step. • −12: Failure to compute the initial state (x, ẋ). If in>0 then else −1 Selector 1 1/s MScope Figure 13.1. A simple system exhibiting sliding mode behavior. Another error condition related to continuous-time dynamics, which is not always detected by Scicos, is the presence of a sliding mode. When a model exhibits sliding mode behavior, the modes of one or more blocks cannot be set properly and the simulation may cease to advance. Consider, for example, the system ẋ = −1 if x ≥ 0 and ẋ = 1 if x < 0 modeled in Diagram 13.1. For this system the dynamics on either side of the zero-crossing surface move toward the surface. If a zero-crossing is used in the If-then-else block, then simulation halts as soon as the state of the integrator reaches zero. If zero-crossing (and 13.2 Debugging Tools 269 thus mode) is not used, the simulation may proceed in some cases, but in most cases the numerical solver returns an error. For well-chosen simulation parameters, simulation can proceed and the result is given in Figure 13.2. This is a reasonable result from a practical point of view, but such sliding mode conditions usually imply a modeling error and should be avoided. 4 2 0 −2 + −4 0.0 0.4 0.8 1.2 1.6 2.0 2.4 2.8 3.2 3.6 4.0 0.4 0.8 1.2 1.6 2.0 2.4 2.8 3.2 3.6 4.0 4 2 0 −2 + −4 0.0 Figure 13.2. Simulation result without zero-crossing in the If-then-else block. The simulation gets stuck at t = 3 if zero-crossing is used. Top graph is the state. Bottom graph is ẋ. 13.1.3 Other Errors Initial Conditions not Converging At the start of the simulation, after all the blocks have been called with ﬂag 4, Scicos calls all the blocks with ﬂag 6 until the states and output values reach a stationary point. This is done by performing a ﬁxed-point iteration, and the error is declared if this iteration does not converge. Few blocks need to use speciﬁcally ﬂag 6. Cannot Allocate Memory The Scicos simulator needs to perform dynamic memory allocation when it is called. This error is encountered when the operating system cannot furnish the requested memory. This error is extremely unlikely on modern computers even for very large diagrams. 13.2 Debugging Tools Information can be displayed during Scicos simulation on the Scilab window. How much information is displayed depends on the debugging level. At level 1, only event number at activation times and calls to the ODE or DAE solver are displayed. At level 2, in addition 270 13 Debugging to the information displayed at level 1, the block number and activation time of every activated block is displayed. The debugging level can be set to i by the Scilab command scicos_debug(i) It can also be set by a Scicos menu. If i is zero, the debugging is turned oﬀ. In most cases, the display of information is not enough to eﬃciently debug Scicos diagrams. That is why Scicos provides a powerful debugging facility through the Debug block (in the Others palette). This block should be placed in the diagram being debugged. The Debug block has a Scilab computational function of type 99. This type of block is not activated itself but it impersonates all the other blocks in the diagram, once before and once after their activations, provided the debug level is set to 2. The debug level is automatically set to 2 when the dialog box of the Debug block is opened and validated. In the dialog box of the Debug block, it is possible to place Scilab instructions to be executed when the computational function of the Debug block is called. This means once before and once after each block activation. The default instruction in the dialog box of the Debug block is pause. This corresponds to a simulation in single-step mode. Under pause, all the block parameters can be inspected and even modiﬁed. To stop the debugging, it suﬃces to set the debugging level back to zero as follows: -1-> scicos_debug(0) Having the possibility to use any Scilab instructions provides a powerful debugging environment. For example, the pause statement can be made conditional: if flag==1&scicos_time()>3&curblock()==2&block.inptr(1)>0 then pause end This way, the user can choose exactly where and when the pause should occur. But many other Scilab instructions can be used, as we shall see in the examples later. In these instructions, we have access to the environment available to any Scilab computational function of type 5. We have seen in the above example the use of functions scicos time and curblock. The other functions available to type 5 blocks presented on page 206 can also be used, in addition to the function scicos debug count, which returns the number of times the block Debug has been called. In addition to these functions, which provide information about the block, the Scilab function getscicosvars can be used to obtain information about the global state of the model, in particular x, z, and outtb. The continuous-time state x is obtained by the command getscicosvars(‘‘x’’), etc. Other information that can be obtained from getscicosvars is xptr, zptr, rpar, rpptr, ipar, ipptr, inpptr, outptr, inplnk, outlnk, and lnkptr. These variables are part of %cpr.sim and do not evolve as a function of time. They are essential in determining which parts of x, z, and outtb correspond to which block. For more details, see Appendix A. 13.3 Examples The Debug block can be used in various ways to supervise, analyze, and debug a Scicos diagram. Here we present two elementary examples to illustrate the possibilities. Typical applications are, of course, in general a lot more complex. 13.3 Examples 271 13.3.1 Log File The Debug block can be used to create a log ﬁle including information concerning every single call to every block. The information recorded in the log ﬁle can be more or less detailed. Here we record the time, the block number, the values of inputs, and the computed values of the outputs. We do not consider the states or the way they are updated. It is for this reason that we consider only the case of ﬂag equal to 1 in simulation phase 1. The following statements are placed inside the Debug block: if flag==1&phase_simulation()==1 then unit=mopen(’scicoslog.dat’,’a’) if scicos_debug_count()==1 then mfprintf(unit,’SIMULATION DATE ’+date()+’\n’),end if scicos_debug_count()-int(scicos_debug_count()/2)*2>0 then mfprintf(unit,’Block number: %3d at time %5.3f \n’,curblock(),scicos_time()) for i=1:size(block.inptr) IN=strcat(string(block.inptr(i)’),’;’) mfprintf(unit,’ Input number ’+string(i)+’ is [’+IN+’]\n’) end else for i=1:size(block.outptr) OUT=strcat(string(block.outptr(i)’),’;’) mfprintf(unit,’ Output number ’+string(i)+’ is [’+OUT+’]\n’) end end mclose(unit) end Note that the information concerning the inputs is written before the block is called, and the information concerning the outputs is written after the block is called. This is done by testing whether or not scicos debug count is odd. Since the Debug block is called twice for each block execution (once before and once after), if scicos debug count is odd, then it corresponds to a call before block activation. The information, at each call, is appended to the end of the ﬁle scicoslog.dat. This ﬁle typically contains SIMULATION DATE 11-Jan-2005 Block number: 1 at time 0.000 Input number 1 is [-0.0761649] Output number 1 is [-2;1;2] Block number: 3 at time 0.000 Input number 1 is [0] Input number 2 is [-5.1904856;-2.9075105;4.7414013] Output number 1 is [0;0;0] Block number: 8 at time 0.000 Output number 1 is [0] Block number: 2 at time 0.000 Input number 1 is [-2;1;2] Output number 1 is [3;8] and so on. 13.3.2 Animation Here we use the Debug block to animate the block executions on the Scicos editor. This is done by highlighting a block when it is activated. 272 13 Debugging xset(’window’,1000) // Main Scicos editor window k=%cpr.corinv(curblock()) if scicos_debug_count()-int(scicos_debug_count()/2)*2>0 then hilite_obj(scs_m.objs(k(1))) else xpause(50000) unhilite_obj(scs_m.objs(k(1))) end Note that k gives the path to the block to be highlighted, but if the block is inside a Super block, only the ﬁrst value of k is used. This means that only the Super block on the main window containing the block is highlighted. The xpause command is used to make sure that the period of time when the block remains highlighted is suﬃciently long so that it can be seen on screen. 14 Implicit Scicos and Modelica 14.1 Introduction Standard Scicos is not well suited for physical component-level modeling. For example, when modeling an electrical circuit, it is not possible to construct a Scicos diagram with a one-to-one correspondence between the electrical components (resistor, diode, capacitor,. . . ) and the blocks in the Scicos diagram. In fact, the Scicos diagram does not look anything like the original electrical circuit. In this chapter we will discuss extensions to Scicos that make physical component-level modeling possible. But ﬁrst we will take a simple circuit and model it in standard Scicos for comparison purposes. Consider the electrical circuit depicted in Figure 14.1. This circuit contains a voltage source, a resistor, and a capacitor. 0.01 ~ 220 0.01 Figure 14.1. A simple electrical circuit. To model and simulate this diagram in Scicos, we can use Kirchoﬀ’s law, which states that the voltage drops (or increases) around a circuit loop must add up to zero. This gives us 1 Vs + Ri + i dt = 0, (14.1) C where Vs is the voltage across the voltage source and i the current through the circuit. If we are interested in the voltage V across the capacitor, we can compute it as follows: 1 V =− i dt. (14.2) C 274 14 Implicit Scicos and Modelica We can now implement these two equations in Scicos by using an integrator block receiving i as an input. The resulting Scicos diagram is depicted in Figure 14.2. Note that this Scicos diagram does not look anything like the original electrical circuit in Figure 14.1. sinusoid generator − 1/R 1/R/C − 1/s −1/C Figure 14.2. Scicos diagram realizing the dynamics of the electrical circuit in Figure 14.1. Recently an extension of Scicos has been developed to allow modeling of physical components directly within the standard Scicos diagrams. This has been done, in particular, by lifting the causality constraint on Scicos blocks and by introducing the possibility of describing block behaviors in the Modelica language. This extension allows us to model naturally not only systems containing electrical components but also mechanical, hydraulic, thermal, and other systems. For example, the electrical circuit in Figure 14.1 can be modeled and simulated by constructing the Scicos diagram in Figure 14.3. The electrical components come from the Electrical palette. 220 ~ R=0.01 + − C= 0.01 Figure 14.3. Scicos diagram using Modelica component. 14.3 Implicit Blocks 275 The user should be aware that this extension is still being developed and should not be considered a stable product. In this chapter we provide some information on how to use this facility, but we do not provide detailed documentation because things may change in the future. We also assume that the reader is familiar with Modelica; if not, consult www.modelica.org for documentation. 14.2 Internally Implicit Blocks The ﬁrst step in extending Scicos to accept implicit blocks has been the introduction of internally implicit blocks. These blocks are identical to standard Scicos blocks except for the fact that the internal continuous-time dynamics are allowed to be implicit. This means that (9.6) for internally implicit blocks changes to e(t) = f0 (t, x(t), ẋ(t), z(t), u(t), nevprt , μ(t)), (14.3) where e(t) is the residual, which in theory equals zero, and the solver tries to keep it close to zero during numerical integration. Internally implicit blocks have explicit input-output ports and are used in Scicos exactly the same way as standard blocks are. This means, in particular, that the editor and, except for some minor modiﬁcations, the compiler were not aﬀected by the introduction of these blocks. But the simulator had to be extended because a diagram containing even a single internally implicit block results in a system of diﬀerential algebraic equations (DAE) that cannot be solved with an ODE solver. It is for this reason that the DAE solver DASKR had been interfaced with the Scicos simulator. DAEs were discussed earlier, in Chapter 3 where the solver dassl was presented. DASKR is a relatively new member of the family; it is DASPK with root-ﬁnding added [14]. Solving DAE systems poses speciﬁc problems [11], in particular, that of ﬁnding consistent initial conditions. In the case of a DAE, and in particular an internally implicit block, the state is not just x but the pair (x, ẋ), and not all pairs (x, ẋ) are consistent with the system equations. Finding consistent initial conditions can be a complex problem in some cases. This is particularly diﬃcult in the multimode case, which is often encountered in the Scicos environment. To help the DAE solver ﬁnd consistent initial conditions, an internally implicit block must be able to furnish information about the nature of each state variable. It should specify in particular which state variables are algebraic and which ones are diﬀerential [15]. This is done by the computational function of the block when it is called with ﬂag 7. We do not give detailed information here on how to construct internally implicit blocks. It suﬃces to say that the computational function is of type 10005, that (x, ẋ) must be placed in the continuous-time state slot in the model, that the computational function must return the residual e(t) when it is called with ﬂag 0 and should provide additional information when called with ﬂag 7. The reason we do not insist on the details is that one rarely needs to construct such an internally implicit block. These blocks are generated automatically when the diagram contains implicit blocks, as will be discussed in the next section. 14.3 Implicit Blocks Implicit blocks, contrary to internally implicit blocks, have implicit ports. An implicit port is diﬀerent from an input or an output port in that connecting two such ports imposes a 276 14 Implicit Scicos and Modelica constraint on the values at these ports but does not imply the transfer of information in an a priori known direction, as is the case when we connect an output port to an input port. For example, the implicit block Capacitor used in the diagram of Figure 14.3 has current and voltage values on its ports but there is no way a priori, without analyzing the full diagram, to designate the ports as input or output. Implicit blocks and the construction of diagrams based on these blocks is fully in the spirit of the object-oriented Modelica language [22]. The description of block behavior in Scicos is done in Modelica.1 14.3.1 Scicos Editor Implicit and regular Scicos blocks can be used in the same diagram. The way implicit blocks are handled by the Scicos editor is similar to the way regular blocks are handled. Blocks can be copied from palettes or inserted using the Add New Block functionality. Two elementary palettes are provided by Scicos. Super blocks can be used to structure diagrams with implicit blocks. Note that Super block implicit ports are available in the Sources and Sinks palettes for that. A Super block can have both explicit and implicit ports. Links are constructed as in the regular case. It is, of course, not possible to connect an implicit port to an explicit port (it would be meaningless anyway). The connection to the explicit world can come only from the implicit blocks having one or more explicit ports such as the voltage sensor in Diagram 14.3. 14.3.2 Scicos Compiler The compilation with implicit blocks is again transparent to the user; however, the compiler performs a ﬁrst stage compilation by grouping all the implicit blocks into a single internally implicit block. This is done by generating a Modelica program for the implicit part of the diagram and generating the computational function of the new internally implicit block. The C program is compiled (this requires a C compiler) and linked with Scilab, and the new internally implicit block replaces the implicit part of the diagram. The last stages are similar to the way code generation works, as we have seen in Chapter 12, except that the user does not see the implicit blocks being replaced. After this ﬁrst compilation stage, the compilation proceeds as with standard Scicos diagrams, and subsequently, so does the simulation. 14.3.3 Block Construction As in the case of standard blocks, we need to provide two functions for each new block: an interfacing function, in Scilab, and a computational function, in Modelica. The interfacing function is similar to that of a regular block. The only diﬀerence is an additional ﬁeld in the model structure and two additional ﬁelds in the graphics structure of the block. model.equations is in fact present in all blocks. But in regular blocks, it is set to an empty list and not used. In the VoltageSensor block, for example, model.equations is deﬁned as follows: mo=modelica() mo.model=’VoltageSensor’ 1 For the moment only a subset of the language is implemented. 14.4 Example 277 mo.inputs=’p’; mo.outputs=[’n’;’v’] model.equations=mo Implicit blocks can be classiﬁed either as inputs or outputs. Here the Modelica variable in the Modelica class VoltageSensor p is deﬁned as input and n and v as outputs. But in fact, p and n correspond to implicit ports. The output v is explicit. It measures the voltage across the block, i.e., between p and n. The implicit/explicit nature of the input-output ports is characterized in two new ﬁelds in the graphics structure. In the case of VoltageSensor, we have graphics.in_implicit=[’I’] graphics.out_implicit=[’I’;’E’] The interfacing function of this block, as with the rest of the blocks in the Electrical palette, can be found in the directory scilab/macros/scicos blocks/Electrical. This directory contains also the Modelica sources of the blocks. The Modelica program deﬁning the behavior of the VoltageSensor block can be found in the ﬁle VoltageSensor.mo: class VoltageSensor Pin p; Pin n; Real v; equation p.i = 0; n.i = 0; v = p.v - n.v; end VoltageSensor; For a Modelica class to be usable in the deﬁnition of an implicit block in Scicos, the corresponding .mo ﬁle must be precompiled using the Modelica compiler modelicac, available in the directory scilab/bin/modelicac. The result is a ﬁle with extension .moc. For example, if the class is named foo and is deﬁned in the ﬁle foo.mo, the command modelicac -c foo.mo produces a ﬁle named foo.moc. To use foo in Scicos, the Scilab variable modelica libs must include the path to the directory where foo.moc can be found. Initially, the paths to the directories Electrical and Hydraulics are placed in modelica libs. Paths to user-deﬁned *.moc ﬁles must be added by the user to modelica libs before launching Scicos. 14.4 Example Here we consider the construction and the use of a new implicit block in Scicos. This block is an ideal operational ampliﬁer (inﬁnite gain) to be used with the electrical components already available in the Electrical palette. The Modelica model of this operational ampliﬁer is very simple and is taken from the Modelica library Electrical.Analog.Ideal, with small modiﬁcations to make it compatible with modelicac: class IdealOpAmp3Pin Pin in_p "Positive pin of the input port"; Pin in_n "Negative pin of the input port"; 278 14 Implicit Scicos and Modelica Pin out "Output pin"; equation in_p.v = in_n.v; in_p.i = 0; in_n.i = 0; end IdealOpAmp3Pin; The Modelica program indicates that there are no currents ﬂowing through the pins in p and in n, and that their voltages are equal. The pin out, on the other hand, can have any voltage and produce any current. The interfacing function is deﬁned as follows: function [x,y,typ]=IdealOpAmp3Pin(job,arg1,arg2) x=[];y=[];typ=[] select job case ’plot’ then standard_draw(arg1) case ’getinputs’ then [x,y,typ]=standard_inputs(arg1) case ’getoutputs’ then [x,y,typ]=standard_outputs(arg1) case ’getorigin’ then [x,y]=standard_origin(arg1) case ’set’ then x=arg1; case ’define’ then model=scicos_model() model.in=[1;1];model.out=1; model.sim=’IdealOpAmp3Pin’ model.blocktype=’c’ model.dep_ut=[%t %f] mo=modelica() mo.model=’IdealOpAmp3Pin’ mo.inputs=[’in_p’;’in_n’]; mo.outputs=[’out’] model.equations=mo exprs=[string([1])] gr_i=[’txt=[’’OpAmp’’];’; ’xstringb(orig(1),orig(2),txt,sz(1),sz(2),’’fill’’)’] x=standard_define([2 2],model,exprs,gr_i) x.graphics.in_implicit=[’I’;’I’] x.graphics.out_implicit=[’I’] end endfunction Note that this block has no parameter (thus no dialog box) and all of its ports are implicit. To use this block, we ﬁrst construct the ﬁle IdealOpAmp3Pin.moc using the shell command modelicac -c IdealOpAmp3Pin.mo and in Scilab, we add the directory where this ﬁle resides to the Scilab variable modelica libs. Note that the shell command can be executed from within Scilab using the host command: 14.4 Example 279 host(SCI+’/bin/modelicac -c IdealOpAmp3Pin.mo’) Finally we load the interfacing function in Scilab: getf(’IdealOpAmp3Pin.sci’) The block IdealOpAmp3Pin is now available in Scicos and can be placed in a block or a palette using the AddNewBlock facility. This is done for the Scicos diagram in Figure 14.4. We have used the block IdealOpAmp3Pin to implement a voltage ampliﬁer. The simulation result is given in Figure 14.5. 0.1 ~ Voltage Source R=2 + − Voltmeter C= 0.1 V OpAmp Voltmeter R=0.2 MScope V R=2 Input/Output voltages Figure 14.4. Scicos diagram containing the new operational ampliﬁer block. 280 14 Implicit Scicos and Modelica 0.10 0.05 0.00 −0.05 + −0.10 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 1.0 0.5 0.0 −0.5 + −1.0 0.0 Figure 14.5. Simulation result. A Inside Scicos In this chapter, we will give an overview of how the Scicos compiler and editor are implemented. This information is useful for advanced users who want to customize Scicos or develop exotic functionalities. It is also useful for developers of interfaces to other software. But it can also be of interest to the average user, in particular, for eﬃciently using the debugging facilities. Our objective here is not to give a detailed description of the underlying code but rather to give enough information so that the code (which of course is available in the source version of Scilab) becomes comprehensible. This requires explaining the data structures used and giving the structure of the main programs. The data structures used in Scicos are all Scilab variables. Thus they can be examined and altered using standard Scilab functions. There are in particular two variables associated with a Scicos diagram that are of great importance: • scs m contains all the information concerning the Scicos diagram. It is constructed by the editor. • %cpr contains the result of the compilation; it is used by the simulator. We start with a brief presentation of the editor and the way scs m is constructed. We then look at the compilation phase, where scs m is used to obtain %cpr. %cpr is used by the simulator to perform simulation. A.1 Scicos Editor The Scicos editor is written entirely in the Scilab language, including the graphical user interface, which uses standard Scilab graphics. This facilitates customization of the editor by the user. This section provides the necessary information. A.1.1 Main Editor Function The main Scilab function implementing the Scicos editor is scicos, which can be found in ﬁle SCI/macros/auto/scicos.sci. This function takes as input argument a Scicos diagram (scs m) and opens it. Invoked without any argument, it opens an empty diagram. This function is called recursively when a Super block is opened. The Scicos editor can be parameterized by initializing the following variables: 282 A Inside Scicos • scicos pal: list of palettes. An n × 2 matrix of strings, where the ﬁrst column contains the names of the palettes and the second the ﬁle (.cos or .cosf) in which the palette is deﬁned. In fact, a palette is nothing but a Scicos diagram. • %scicos menu: Scicos menus. A Scilab list including vectors of strings, where the ﬁrst element of each vector contains the name of the menu, and the rest, the corresponding items of the menu. Each item corresponds to an operation (such as move, copy, save) and has a corresponding Scilab function. • %scicos short: keyboard shortcuts. An n × 2 matrix of strings, where the ﬁrst column contains the character to be used for the shortcut (only lower-case characters can be used) and the second, the name of an operation, which must be an item in one of the menus. • %scicos help: manual pages associated with operations. A Scilab tlist where for each operation, a vector of strings contains the corresponding manual page. • %scicos display mode: this scalar is used to specify whether backstoring for graphical display of the diagrams should be used by the editor. • modelica libs: a vector of strings containing the list of directories (full path), where implicit Modelica blocks are deﬁned. • scicos pal libs: a vector of strings containing the list of directories inside SCI/macros/scicos blocks to be loaded. These directories contain the interfacing functions of the blocks in Scicos palettes. The user should not, in general, edit this variable. These variables are initialized by the function initial scicos tables, which can be found in the directory SCI/macros/util. This function is executed as a script by scilab.star, which is executed at the startup of Scilab. They can be modiﬁed by the user subsequently. The function scicos contains the main event loop of the editor. When an item in one of the menus is invoked to perform an operation, scicos calls the corresponding Scilab function. The name of the Scilab function is derived from the name of the menu item by removing special characters “/”, “.”, and “-”, and adding a trailing “ ”. For example, the function corresponding to the item Open/set in the Object menu is OpenSet . Functions associated with editor operations have no input-output arguments and are executed as scripts. Scicos comes with default menus providing many elementary editor operations. The associated functions can be found in the directory SCI/macros/scicos. Example Here we give an example of adding a new operation to the Scicos editor. This is just a toy example and consists in analyzing the diagram and displaying the number of blocks, the number of Super blocks, and the number of links in the diagram. We start by adding a new item, which we call Block Info. We place it in the last position. For that, the Scilab variable %scicos menu should be modiﬁed. This should be done before Scicos is launched. Default Scicos menus are Diagram, Edit, Simulate, Object, and Misc. So if we want to place this item in Misc, we should add it to the vector %scicos menu(5). For example, it can be placed at the end of this menu. By doing so, the item Diagram Info is added to the Misc menu in Scicos, and when the user selects this item, Scicos attempts to execute the function DiagramInfo . So we need to deﬁne this function as well. To implement fully the debugging menu, the following script can be used. These instructions must be executed before Scicos is launched. A convenient place for them is in the .scilab ﬁle. A.1 Scicos Editor 283 %scicos_menu($)($+1)="Diagram Info"; function [block_num,link_num,sup_num]=.. do_diagram_Info(scs_m,block_num,link_num,sup_num) for i=1:length(scs_m.objs) if typeof(scs_m.objs(i))==’Block’ then if or(scs_m.objs(i).model.sim==[’super’,’csuper’]) then sup_num=sup_num+1; [block_num,link_num,sup_num]=.. do_diagram_Info(scs_m.objs(i).model.rpar,block_num,link_num,sup_num) elseif ~or(scs_m.objs(i).model.sim==[’input’,’output’]) then block_num=block_num+1; end elseif typeof(scs_m.objs(i))==’Link’ then link_num=link_num+1; end end endfunction function DiagramInfo_() [block_num,link_num,sup_num]=do_diagram_Info(scs_m,0,0,0); txt=["Number of basic blocks: "+string(block_num); "Number of Super blocks: "+string(sup_num); "Number of links: "+string(link_num)]; message(txt) Cmenu="Open/Set" endfunction Note that in order to explore the scs m structure, we examine each item in the objs ﬁeld and increment the corresponding counters. When a Super block is encountered, the function do diagram Info is called recursively. This way we can explore the content of the hierarchical structure scs m. The Cmenu variable sets the next operation to be performed. By default in Scicos, the editor is in Open/Set operation mode. Editor operations are, in general, more complex and involve Scilab variables deﬁned and used in scicos. For example, in most cases, an operation modiﬁes the variable scs m, which codes the current status of the diagram. The best way to learn about these variables is by examining source codes of the functions associated with default operations. A.1.2 Structure of scs m scs m is a Scilab object of type diagram having two entries: • props: Diagram properties. Scilab object of type params, • objs: list of objects included in the Scicos diagram. Diagram Properties params The diagram properties are: 284 A Inside Scicos • wpar: This vector is not currently used. It may be used in the future to code window sizes of the editor. • title: A string containing the name of the diagram. The default value is "Untitled" • tol: A vector containing simulation parameters including various tolerances used by the solver: – atol: Integrator absolute tolerance for the numerical solver. – rtol: Integrator relative tolerance for the numerical solver. – ttol: Tolerance on time. If an integration period is less than ttol, the numerical solver is not called. – deltat: Maximum integration time interval. If an integration period is larger than deltat, the numerical solver is called more than once in such a way that for each call the integration period remains below deltat. – scale: Real-time scaling; the value 0 corresponds to no real-time scaling. It associates a Scicos simulation time to the real time in seconds. A value of 1 means that each Scicos unit of time corresponds to one second. – solver: Choice of numerical solver. The value 0 implies LSODAR and 100 implies DASKR. – hmax: Maximum step size for the numerical solver. 0 means no limit. The default value is [0.0001,1.000E-06,1.000E-10,100001,0,0]. • tf: Final integration time. The simulation stops at this time. The default value is 100000. • context: A vector of strings containing Scilab instructions deﬁning variables to be used inside block GUIs. All valid Scilab instructions can be used but not comments. • void1: Not used. • options: Scilab object of type scsopt deﬁning graphical properties of the editor such as background color and link color. The ﬁelds are the following: – 3D: A list with two entries. The ﬁrst one is a boolean indicating whether or not blocks should have 3D aspect. The second entry indicates the color in the current colormap to be used to create the 3D eﬀect. The default is 33, which corresponds to gray added by Scicos to the standard colormap, which contains 32 colors. The default value is list(%t,33). – Background: Vector with two entries: background and foreground colors. The default value is [8,1]. – Link: Default link colors for regular and activation links. These colors are used only at link construction. Changing them does not aﬀect already constructed links. The default value is [1,5], which corresponds to black and red if the standard Scilab colormap is used. – ID: A list of two vectors including font number and sizes. The default value is list([5,1],[4,1]). – Cmap: An n×3 matrix containing RGB values of colors to be added to the colormap. The default value is [0.8,0.8,0.8], i.e., the color gray. • void2: Not used . • void3: Not used. • doc: Used for documenting the diagram. Diagram Content The ﬁeld objs contains a Scilab list of the objects within the diagram. The objects can be of type Block, Link, or Text. A Block can be a basic block or a Super block. A.1 Scicos Editor 285 Scicos Block It is a structure including the following ﬁelds. • graphics: Scilab object of type graphics including graphical information concerning the features of the block. The ﬁelds are: – orig: Vector [xo,yo], where xo is the x coordinate of the block origin and yo is the y coordinate of the block origin. – sz: Vector [w,h], where w is the block width and h the block height. – flip: Boolean indicating block orientation. It is used to switch the ports on the lef- hand side and those on the right-hand side of the block. – exprs: A vector of strings including formal expressions (usually including numbers and variable names) used in the dialog of the block. – pin: Vector with pin(i), the number of the link connected to the ith regular input port (counting from one), or 0 if this port is not connected. – pout: Vector with pout(i), the number of the link connected to the ith regular output port (counting from one), or 0 if this port is not connected. – pein: Vector with pein(i), the number of the link connected to the ith event input port (counting from one), or 0 if this port is not connected. – peout: Vector with peout(i), the number of the link connected to the ith event output port (counting from one), or 0 if this port is not connected. – gr i: Vector of strings including Scilab graphics expressions for drawing block’s icon. – id: A string including an identiﬁcation for the block. The string is displayed underneath the block in the diagram. – in implicit: A vector of strings including "E" and "I". E and I stand respectively for explicit and implicit port, and this vector indicates the nature of each input port. For regular blocks (not implicit), this vector is empty or contains only E’s. – out implicit: Similar to in implicit but for the output ports. • model: Scilab object of type model including the following ﬁelds. – sim: A list containing two elements. The ﬁrst element is a string containing the name of the computational function (C, Fortran, or Scilab). The second element is an integer specifying the type of the computational function. Currently type 4 and 5 are used, but older types continue to work to ensure backward compatibility. – in: A vector specifying the number and sizes of regular inputs. – out: A vector specifying the number and sizes of regular outputs. – evtin: A vector specifying the number and sizes of activation inputs. Currently activation ports can be only of size one. – evtout: A vector specifying the number and sizes of activation outputs. – state: Vector containing initial continuous-time state. – dstate: Vector containing initial discrete-time state. – rpar: Vector of real parameters passed to associated computational function. – ipar: Vector of integer parameters passed to associated computational function. – blocktype: It can be set to c or d indiﬀerently for regular blocks. x is used if we want to force the computational function to be called during the simulation phase even if the block does not contribute to computation of the state derivative. – firing: Vector of initial event ﬁring times of size equal to the number of activation output ports. A value ≥ 0 programs an activation (event) at the corresponding port to be ﬁred at the speciﬁed time. – dep ut: Boolean vector [timedep udep]. 286 A Inside Scicos · · timedep boolean: true if block is always active. udep boolean: true if block has direct feed-through, i.e., at least one of the outputs depends directly (not through the states) on one of the inputs. In other words, when the computational function is called with ﬂag 1, the value of an input is used to compute the output. – label: A string. The label can be used to identify a block in order to access or modify its parameters during simulation. – nzcross: Number of zero-crossing surfaces. – nmode: Number of modes. Note that this gives the size of the vector mode and not the total number of modes in which a block can operate in. Suppose a block has 3 modes and each mode can take two values, then the block can have up to 23 = 8 modes. – equations: Used in case of Implicit blocks; see Chapter 14. • gui: The name of the Scilab GUI function associated with the block. • doc: Used for documentation of the block. Scicos Link. It is a Scilab list including the following ﬁelds: • xx: A vector. A link is deﬁned as a polyline line. xx deﬁnes the x-coordinate of the points characterizing the polyline. • yy: A vector having the same size as xx. It deﬁnes the y-coordinate of the points characterizing the polyline. • id: A string corresponding to the name of the function drawing the link. Default value is "drawlink". • thick: Vector of size two deﬁning line thickness. • ct: A vector. The ﬁrst entry designates the color, and the second, the nature. The second entry is 1 for a regular link, −1 for an activation link, and 2 for an implicit link. • from: Vector of size three including the block number, port number, and port type (0 for output, 1 for input) at the origin of the link. Note that the third entry may be 1 if the link is implicit; otherwise it is zero. • to: Vector of size three including the block number, port number, and port type at the destination of the link. Once the diagram has been successfully edited, scs m can be passed on to the compiler. A.2 Scicos Complier Scicos diagram compilation is done in two stages (there is an additional precompilation phase if the diagram contains implicit blocks; see Chapter 14). These two stages are implemented by the Scilab functions c pass1 and c pass2. A.2.1 First Compilation Stage The ﬁrst stage of the compilation consists in removing the hierarchy from the diagram and constructing a ﬂat description. This is done by c pass1, which has the following calling sequence: [blklst,cmat,ccmat,cor,corinv,ok]=c_pass1(scs_m) A.2 Scicos Complier 287 • blklst is a list of blocks present in the diagram. It contains block information relevant to simulation. Block properties such as color, icon, size, and location, which are not useful for simulation, have been stripped. • cmat is an n × 6 matrix. Each row corresponds to a regular link and contains the block number, port number, and port type (explicit or implicit) of the source block and the same information regarding the destination block. • ccmat is an n × 4 matrix. Each row corresponds to an activation link and contains the block number and port number of the source block and the block number and port number of the destination block. • cor and corinv are correspondence tables (coded as lists) used to ﬁnd the correspondence between blocks in blklst and scs m. A.2.2 Second Compilation Stage The second stage is done by the main compilation function c pass2, which constructs all the scheduling tables and other information needed for simulation and code generation. The calling sequence is as follows: %cpr=c_pass2(blklst,connectmat,ccmat,cor,corinv) The input arguments are all generated by c pass1. connectmat is simply cmat, but the third and last columns have been removed. The output %cpr is a Scilab structure containing all the information needed by the simulator. We will not explain how c pass2 computes %cpr. Instead we give a detailed description of %cpr in the next section. A.2.3 Structure of %cpr The Scilab object %cpr contains the result of the compilation. The simulator uses only %cpr. It is thus important for an advanced user to understand how compilation results are coded in %cpr. • state: Scilab object of type xcs. It contains all the states of the model, that is, everything than can evolve during the simulation. It contains in particular – x: The continuous-time state, which is obtained by concatenating the continuoustime states of all the blocks. – z: The discrete-time state, which is obtained by concatenating the discrete-time states of all the blocks. – iz: Vector of size equal to the number of blocks. If a block needs to allocate memory at initialization, the associated pointer is saved here. – tevts: Vector of size equal to the number of activation sources. It contains the scheduled times for programmed activations. – evtspt: Vector of size equal to the number of activation sources. It is an event scheduler. – pointi: The number of the next programmed event. – outtb: Vector containing all the link memories. Link memories hold block output values. • sim: Scilab object of type scs. It contains information that does not evolve during the simulation. – funs: A vector containing the name of the computational functions. – xptr: A vector pointer to the continuous time state x. The continuous-time state of block i is %cpr.state.x(%cpr.sim.xptr(i):%cpr.sim.xptr(i+1)-1). 288 A Inside Scicos inpptr input port # outptr block number output port # inplnk outlnk link # link # lnkptr outtb Figure A.1. outtb contains the link registers. The memory allocated to link l is outtb([lnkptr(l):lnkptr(l+1)-1]). The link number of the link connected to input i of block j is l=inplnk(inpptr(j)+i-1). Similarly, the link number of the link connected to output i of block j is l=outlnk(outptr(j)+i-1). A.2 Scicos Complier – – – – – – – – – – – – – – – – – – – – – – – – – – – – 289 zptr: A vector pointer to the discrete-time state z, which is similar to xptr. zcptr: A vector pointer to the zero-crossing surfaces. inpptr: A vector pointer used to ﬁnd the link number and consequently the part of outtb corresponding to a given input port. See Figure A.1. outptr: Similar to inpptr but for output ports. inplnk: Similar to inpptr. See Figure A.1. outlnk: Similar to inplnk but for output ports. lnkptr: A vector pointer to outtb indicating the region corresponding to a given link. rpar: Vector of real parameters that is obtained by concatenating the real parameters of all the blocks. rpptr: A vector pointer to real parameters rpar. The real parameters of block i are %cpr.sim.rpar(%cpr.sim.rpptr(i):%cpr.sim.rpptr(i+1)-1). ipar: Vector of integer parameters, similar to rpar. ipptr: A vector pointer to integer parameters, similar to rpptr. clkptr: A vector pointer to output activation ports. ordptr: A vector pointer to ordclk designating the part of ordclk corresponding to a given activation. execlk: Not used. ordclk: An n × 2 matrix associated to blocks activated by output activation ports. The ﬁrst column contains the block number, and the second, the event code by which the block should be called. See Figure A.2. cord: An n×2 matrix associated to always active blocks. The ﬁrst column contains the block number, and the second, the event code by which the block should be called. oord: Subset of cord, which aﬀects the continuous-time state derivative. zord: Subset of cord, which aﬀects the computation of zero-crossing surfaces. critev: A vector of size equal to the number of activations and containing zeros and ones. The value one indicates that the activation is critical in the sense that the continuous-time solver must be cold restarted. nb: Number of blocks. Note that the number of blocks may diﬀer from the original number of blocks in the diagram because c pass2 may duplicate some conditional blocks. ztyp: A vector of size equal to the number of blocks. A 1 entry indicates that the block may have zero-crossings, even if it doesn’t in the context of the diagram. Usually not used by the simulator. nblk: Not used. Set to nb. ndcblk: Not used. subscr: Not used. funtyp: A vector of size equal to the number of blocks indicating the type of the computational function of the block. Block type can be 0 through 5. Currently only type 4 (C language) and type 5 (Scilab language) computational functions should be used. But older blocks can also be used. iord: An n × 2 matrix associated to blocks that must be activated at the start of the simulation. This includes blocks inheriting from constant blocks and always active blocks. labels: A string vector of size equal to the number of blocks containing block labels. modptr: A vector pointer to the block modes. 290 A Inside Scicos • cor: Scilab list with a hierarchy identical to that of the original diagram and including block numbers in the compiled structure. It allows ﬁnding block number in %cpr from position in scs m. • corinv: Scilab list containing vectors. It allows ﬁnding position in scs m from block number in %cpr. ordclk ordptr event number activated event blocks code block event number code Figure A.2. The table ordptr is a pointer to ordclk, which contains the blocks that are activated by any given activation. For activation i, the associated blocks are found in ordclk([ordptr(i):ordptr(i+1)-1],1). A.2.4 Partial Compilation For large diagrams including many conditional blocks, the compilation time can be noticeable. That is why Scicos does not do a complete recompilation if minor modiﬁcations are made in a compiled diagram. In most cases (but not always), changing a block parameter does not aﬀect scheduling tables in the compiled structure. In such a case, only a partial compilation is performed, which means that some elements of %cpr such as %cpr.sim.rpar are updated without recomputing scheduling tables %cpr.sim.ordclk, which can be timeconsuming. Anytime a block parameter is modiﬁed, Scicos determines the eﬀect of the modiﬁcation on the compiled structure by setting the level of compilation required. This level is coded in the Scilab variable needcompile. If a diagram is not compiled, needcompile is 4. For a freshly compiled diagram, needcompile is zero. In many cases, changing a block parameter, for example the gain value of the Gain block, does not aﬀect needcompile. The block is A.3 Scicos Simulator evtspt 291 tevts event times pointi last event 0 Figure A.3. The table evtspt is used to store the programmed events. The variable pointi contains the next programmed event number. i=evtspt(pointi) is the number of the event programmed next; j=evtspt(i) is next and so on. evtspt of the last programmed event is zero. Event times for each event are stored in table tevts. simply placed in the variable newparameters so that the new parameters of the block can be copied from scs m into %cpr before simulation. This is done by the Scilab function modipar. If changing a parameter changes link sizes too, then needcompile goes up to one. In this case, pointer vectors may need to be recomputed. However, scheduling tables remain valid and are not recomputed. If the modiﬁcation is more complex and involves, for example, preprogrammed activation signals, then it still may not be necessary to recompute scheduling tables. In this case, needcompile becomes 2 and Scilab function c pass3 recomputes all the entries from scs m, except for scheduling tables, before simulation. Finally, when it is absolutely necessary, for example following any operation modifying the diagram (adding or deleting a block for example), needcompile becomes 4 and a recompilation is performed before simulation. The Scilab function do update handles the partial compilation. Note that a recompilation can always be imposed by the user by choosing the menu item Compile. A.3 Scicos Simulator The interface to the Scicos simulator in Scilab is the function scicosim. We have already encountered this function in Section 11.1.2. This function is used to initialize, run, and end Scicos simulations. These operations are done by the function do run (in the SCI/macros/scicos directory), which is called when the user runs a simulation in Scicos. The Scicos simulator is a complex C program. We do not study it here. The simulation functions are in the ﬁle SCI/routines/scicos/scicos.c. The main function is scicos, which calls the routines cosini, cossim, and cosend depending on whether initialization, simulation, or ending has been requested. B Scicos Blocks of Type 5 We present here two examples of Scicos blocks using type 5 computational functions. B.1 Type 5 Block for the Bouncing Ball Example This section gives the interfacing and the computational functions of a type 5 block realizing the bouncing ball example presented on page 204. In this case, the full dynamics is included in a basic block. Interfacing Function The interfacing function for the block realizing the dynamics of the bouncing ball problem is given below. The user can change the initial position and velocity of the ball. function [x,y,typ]=BouncingBall5(job,arg1,arg2) x=[];y=[];typ=[]; select job case ’plot’ then standard_draw(arg1) case ’getinputs’ then [x,y,typ]=standard_inputs(arg1) case ’getoutputs’ then [x,y,typ]=standard_outputs(arg1) case ’getorigin’ then [x,y]=standard_origin(arg1) case ’set’ then x=arg1 graphics=arg1.graphics; exprs=graphics.exprs; model=arg1.model; while %t do [ok,xx,rpar,exprs]=.. getvalue(’Set BOUNCEBALL block parameters’,.. [’Initial ball position and speed’;.. ’Parameter alpha’],.. list(’vec’,2,’vec’,1),exprs) if ok then 294 B Scicos Blocks of Type 5 model.rpar=rpar;model.state=xx graphics.exprs=exprs x.graphics=graphics;x.model=model end break end case ’define’ then model=scicos_model() model.sim=list(’bounceball5’,5); model.out=1 model.state=[2;0] model.rpar=1 model.nzcross=1; model.blocktype=’c’; model.dep_ut=[%t %t]; exprs=[sci2exp(model.state);sci2exp(model.rpar)] gr_i=[’xstringb(orig(1),orig(2),’’BOUNCEBALL’’,sz(1),sz(2),’’fill’’);’] x=standard_define([3 2],model,exprs,gr_i) end endfunction Note that the ﬁrst entry of dep ut, set to true, is not used because the block has no input, but the second entry speciﬁes that the block is always active. Computational Function This computational function should be compared to the C function Bounceball deﬁned on page 204. function block=bounceball5(block,flag) if flag==1 block.outptr(1)(1)=block.x(1) elseif flag==0 block.xd(1)=block.x(2); block.xd(2)=-9.8-block.rpar(1)*block.x(2)^3 elseif flag==2&block.nevprt==-1&block.jroot(1)<0 block.x(2)=-block.x(2) elseif flag==9 block.g(1)=block.x(1) end endfunction B.2 Animation Block for the Cart Pendulum Example Animation of simulation results is often helpful both in understanding the results and in presenting them to others. In this section we give the interfacing and the computational functions associated with the animation block of the cart pendulum problem presented in Section 11.4. The computational function of this block is type 5; that is, a Scilab program is used to perform the animation during the simulation. This block receives the position of the cart z and the angle of the pendulum θ as input. The angle φ, which is a constant, is a block parameter as are the sizes of the cart and B.2 Animation Block for the Cart Pendulum Example 295 the pendulum and the coordinate of the viewing window. These parameters are initialized and adjusted by the interfacing function below: function [x,y,typ]=PENDULUM_ANIM(job,arg1,arg2) // Animation of the cart-pendulum problem x=[];y=[];typ=[] select job case ’plot’ then standard_draw(arg1) case ’getinputs’ then [x,y,typ]=standard_inputs(o) case ’getoutputs’ then x=[];y=[];typ=[]; case ’getorigin’ then [x,y]=standard_origin(arg1) case ’set’ then x=arg1; graphics=arg1.graphics;exprs=graphics.exprs model=arg1.model;dstate=model.dstate while %t do [ok,plen,csiz,phi,xmin,xmax,ymin,ymax,exprs]=getvalue(.. ’Set Scope parameters’,.. [’pendulum length’;’cart size (square side)’;’slope’; ’Xmin’;’Xmax’; ’Ymin’; ’Ymax’; ],.. list(’vec’,1,’vec’,1,’vec’,1,’vec’,1,’vec’,1,’vec’,1,’vec’,1),exprs) if ~ok then break, end mess=[] if plen<=0|csiz<=0 then mess=[mess;’Pendulum lenght and cart size must be positive.’;’ ’] ok=%f end if ymin>=ymax then mess=[mess;’Ymax must be greater than Ymin’;’ ’] ok=%f end if xmin>=xmax then mess=[mess;’Xmax must be greater than Xmin’;’ ’] ok=%f end if ~ok then message(mess) else rpar=[plen;csiz;phi;xmin;xmax;ymin;ymax] model.rpar=rpar; graphics.exprs=exprs; x.graphics=graphics;x.model=model break end end case ’define’ then plen=2; csiz=2; phi=0; xmin=-5;xmax=5;ymin=-5;ymax=5 model=scicos_model() model.sim=list(’anim_pen’,5) 296 B Scicos Blocks of Type 5 model.in=[1;1] model.evtin=1 model.dstate=0 model.rpar=[plen;csiz;phi;xmin;xmax;ymin;ymax] model.blocktype=’d’ model.dep_ut=[%f %f] exprs=string(model.rpar) gr_i=[’thick=xget(’’thickness’’);xset(’’thickness’’,2);’; ’xx=orig(1)+sz(1)*[.40.60.60.40.4]’ ’yy=orig(2)+sz(2)*[.20.20.40.40.2]’ ’xpoly(xx,yy,’’lines’’)’ ’xx=orig(1)+sz(1)*[.50.6]’ ’yy=orig(2)+sz(2)*[.40.8]’ ’xpoly(xx,yy)’ ’xset(’’thickness’’,thick);’] x=standard_define([3 3],model,exprs,gr_i) end endfunction Note that this block has 2 regular scalar inputs and an activation input. The activation input receives during the simulation the information concerning the time instants when the display has to be updated. Note also that the block has a discrete-time state, set to zero. The reason for the presence of this state is to make sure that during the simulation, the block is called with flag equal to 2. Since this block has no output (from the point of view of Scicos; there is indeed an output to the screen), the block is never called with flag equal to 1. So in the absence of a state, the block is never called during simulation. Adding a state is one way to overcome this problem. The other is to add outputs to the block. This latter method works even if these outputs are left unconnected. Here we have chosen to add a state, which turns out to be useful for other reasons. The computational function of this block is deﬁned in PENDULUM ANIM to be anim pen. This function is given below. function [blocks] = anim_pen(blocks,flag) win=20000+curblock() if flag<>4 then H=scf(win), end xold=blocks.z rpar=blocks.rpar plen=rpar(1);csiz=rpar(2);phi=rpar(3); if flag==4 then xset("window",win) set("figure_style","new") H=scf(win) clf(H) H.pixmap=’on’ Axe=H.children Axe.data_bounds=rpar(4:7) Axe.isoview=’on’ S=[cos(phi),-sin(phi);sin(phi),cos(phi)] XY=S*[rpar(4),rpar(5);-csiz/2,-csiz/2] xsegs(XY(1,:),XY(2,:)) x=0;theta=0; x1=x-csiz/2;x2=x+csiz/2;y1=-csiz/2;y2=csiz/2 B.2 Animation Block for the Cart Pendulum Example 297 XY=S*[x1 x2 x2 x1 x1;y1,y1,y2,y2,y1] xpoly(XY(1,:),XY(2,:),"lines",1) XY=S*[x,x+plen*sin(theta);0,0+plen*cos(theta)] xsegs(XY(1,:),XY(2,:)) elseif flag==2 then Axe=H.children x=blocks.inptr(1)(1) theta=blocks.inptr(2)(1) XY=Axe.children(2).data’+.. [cos(phi)*(x-xold);sin(phi)*(x-xold)]*ones(1,5) Axe.children(2).data=XY’ x1=x*cos(phi);y1=x*sin(phi) XY=[x1,x1+plen*sin(theta);y1,y1+plen*cos(theta)] Axe.children(1).data=XY’ blocks.z=x show_pixmap() end endfunction This block uses Scilab’s new graphics (in contrast to Scicos, which still uses the old graphics). The window number of the graphical display is set to 20000 plus the block number. This is to make sure that if two such blocks are used in the same diagram, they use diﬀerent windows for their displays. The discrete-time state blocks.z, which has been introduced simply to make sure the block is called during simulation, is also used to store the previous position of the cart. This information is then used to update the associated graphics data structure in a slightly more eﬃcient way. This is done by computing the change in the position by subtracting the old position from the new position. The isoview property is used to make sure the horizontal and vertical scales remain proportional on the screen after resizing the window. The pixmap mode is used to create an eﬃcient and smooth animation. In particular, the display is refreshed only after all the changes have been done. Note that the block does not ensure real-time simulation. This must be done by adjusting the corresponding simulation parameter of the diagram. C Animation Program for the Car Example This section provides the Scilab program used to construct the animation of the car example in Section 5.2. This program can be used as an example for developing animation programs in Scilab using the new graphics mode. Note also the use of the Scilab functions realtime and realtimeinit used to make the animation run in real time. function play(T,X,XI) scf(1);clf();h=gcf(); h.pixmap = "on"; h.figure_size = [1400 600]; h.children.axes_visible=[’off’,’off’,’off’] ; h.children.isoview=’on’; h.children.tight_limits = "on" h.children.margins = [0,0,0,0]; XXX=[-XI-l2:0.2:3*XI+l2]; FY=[];XC=[]; for x=XXX, th=atan(Fx(x)); XC=[XC,x+sin(th)*r2] FY=[FY,F(x)-cos(th)*r2]; end h.children.data_bounds=[min(XC),min(FY);max(XC),max(FY)+d]; xfpoly([XC(1),XC,XC($)],[min(FY)-d,FY,min(FY)-d]); hfp = gce(); hfp.foreground = 5; drawlater() h.pixel_drawing_mode=’xor’ l3=1.15*l2; // car body length (half) for animation realtimeinit(1); // intialize real time operation for i=1:size(T,’*’) t=T(i); X=XX(:,i); x=X(1);xd=X(2);y=X(3);yd=X(4);th=X(5);thd=X(6);xsi=X(7);eta=X(8); x0=x+xsi*sin(th)-l2*cos(th); x1=x+eta*sin(th)+l2*cos(th); y0=y-xsi*cos(th)-l2*sin(th); y1=y-eta*cos(th)+l2*sin(th); xp=[x0,x-l2*cos(th);x+l2*cos(th),x1]’; yp=[y0,y-l2*sin(th);y+l2*sin(th),y1]’; xpp=[x-l3*cos(th);x+l3*cos(th)]; 300 C Animation Program for the Car Example ypp=[y-l3*sin(th);y+l3*sin(th)]; W1=[x0-r2,y0+r2,2*r2,2*r2,0,360*64]; W2=[x1-r2,y1+r2,2*r2,2*r2,0,360*64]; if i==1 then xpoly(xpp,ypp); hp1 = gce(); hp1.thickness=10; xpolys(xp,yp); hp2 = gce();hp21=hp2.children(1);hp22=hp2.children(2) xfarcs([W1’,W2’]); ha = gce();ha1=ha.children(1);ha2=ha.children(2) realtime(t); draw([hp1,hp2,ha])//draw car else draw([hp1,hp2,ha])//erase car hp1.data=[xpp,ypp]; hp21.data=[xp(:,1),yp(:,1)]; hp22.data=[xp(:,2),yp(:,2)]; ha1.data=W1; ha2.data=W2; realtime(t); draw([hp1,hp2,ha])//draw car end show_pixmap() end h.pixel_drawing_mode=’copy’ drawnow() show_pixmap() endfunction D Extraction Program for the LATEX Graphic Example This function exports to a ﬁle in mixed LATEX and Postscript code the content of a Scilab graphic window. This ﬁle has been in particular used in Section 2.4.4 to produce the two ﬁles figps-ps.eps and figps-tex.tex. function xs2latex(win,fname) base=basename(fname); I=strindex(base,’.’); if I==[] then fname_eps= fname+’.eps’; fname= fname+’.ps’; else suf=part(fname,I($):length(fname)); if suf<>".ps" then error(’xs2latex: fname must be ended by0.ps’); return; end fname_eps= part(fname,1:I($-1))+’.eps’; end; xs2ps(win,fname) unix(SCI+’/bin/Blatexpr 1 1 ’+fname); txt=mgetl(fname_eps); I=grep(txt,"%Latex:"); txt1=txt(I); I=grep(txt1,"nan"); txt1(I)=[]; txt1=strsubst(txt1,"%Latex:",""); mputl(txt1,base+’-tex.tex’); txt1=txt; I=[]; for i=1:size(txt,’*’) i, l=length(txt(i)); if l-4 >= 1 then if part(txt(i),(l-4):l) == " Show" then I=[I,i]; end end end txt1(I)=[]; mputl(txt1,base+’-ps.eps’); 302 D Extraction Program for the LATEX Graphic Example endfunction E Maple Code Used for Modeling the N -Link Pendulum Here we give the Maple code for the N -link pendulum presented in Section 5.1. We have removed from the Maple code functions devoted to LATEX code generation and to energy computation (ener.f). read(‘Euler.map‘); #--------------------------------------------------------------------------------# Lagrangian for the N-link pendulum ( l[i]: length of links r[i]:=l[i]/2;) #--------------------------------------------------------------------------------# number of links in the pendulum n:=2; # [x,y,theta] is of size three mm:=3: LLi:=proc(i) (1/2)*m[i]*( ( xd[i-1]- r[i] *sin(th[i])*thd[i])**2 + ( yd[i-1]+ r[i]*cos(th[i])*thd[i])**2 ) + 1/2*J[i]*(thd[i])**2 -m[i]*g*(y[i-1]+r[i]*sin(th[i])): end: # The point zero is fixed x[0]:=0:xd[0]:=0: y[0]:=0:yd[0]:=0: L:=sum( LLi(j),j=1..n): # Lagrangian variables: q := [ seq (op([x[i],y[i],th[i]]),i=1..n)]: qd := [ seq (op([xd[i],yd[i],thd[i]]),i=1..n)]: qdd:= [ seq (op([xdd[i],ydd[i],thdd[i]]),i=1..n)]: # Lhs of Euler equations EL:=euler_equations(L,q,qd,qdd): EE:=map((i)->rhs(i),EL): #----------------------------------------------------# Rewriting the Euler equations to have a canonical form # used only for output 304 E Maple Code Used for Modeling the N -Link Pendulum #----------------------------------------------------XX:=CEuler(EE,q,qd,qdd): ME1:=XX[1]: ME1:=subs(seq(m[k]*r[k]*cos(th[k])=mrc[k],k=1..n), seq(m[k]*r[k]*sin(th[k])=mrs[k],k=1..n),eval(ME1)): #-----------------------------------------------# Constraints on the N-link Pendulum #-----------------------------------------------ncont:=2*n; cont:=[ seq(op([ x[i]-x[i-1] - 2*r[i]*cos(th[i]), y[i]-y[i-1] - 2*r[i]*sin(th[i])]),i=1..n)]: # time derivative of constraints; cont1:=map ( (exp)->( time_diff(exp,q,qd,qdd)),cont): # derivatives of constraints are of type Aprim qd = 0 Aprim:=genmatrix(cont1,qd): #---------------------------------------------# Computing S(q); #---------------------------------------------SS:=linsolve(Aprim,matrix(ncont,1,0)): #-------------------------------------------------------------# Since the indices can be mixed we have to change SS # so that the correspondence between the eta[i] and the thd[i] # will be the identity. This is convenient, but not essential, # in order to have a simple interpretation of eta #-------------------------------------------------------------permut:=seq(SS[mm*i,1]=t_s[i],i=1..n): SS:=subs(permut,eval(SS)): permut:=seq(t_s[i]=eta[i],i=1..n): SS:=subs(permut,eval(SS)): S:= genmatrix(convert(convert(SS,vector),list),[seq( eta[i],i=1..n)]): #----------------------------------------------------# Left multiplication in the Euler equations. #----------------------------------------------------E1:=multiply(transpose(S),EE): #----------------------------------------------------# . # q= S(q) eta ; here eta = [eta1,eta2,...] # .. # we use this equation to compute q #----------------------------------------------------qt := [ seq (eta[i] ,i=1..n)]: qtd := [ seq (etad[i] ,i=1..n)]: qtdd:= [ seq (etadd[i],i=1..n)]: qqdd:=map((x,y,z,t)-> time_diff(x,y,z,t),eval(SS), E Maple Code Used for Modeling the N -Link Pendulum [op(q),op(qt)],[op(qd),op(qtd)],[op(qdd),op(qtdd)]): #----------------------------------------------------# .. . # Using q= d/dt [ S(q) eta] and q= S(q) eta # we can substitute these expressions in E1 #----------------------------------------------------E2:=subs(seq(qdd[i]=qqdd[i,1],i=1..nops(qdd)),eval(E1)): E3:=subs(seq(qd[i]=SS[i,1],i=1..nops(qd)),eval(E2)): #----------------------------------------------------# The global system is now # . # E3 = 0 and q= S(q) eta #----------------------------------------------------E3:=map((x)-> simplify(x),E3): #-----------------------------------------------------------------# Canonical representation for the simplified Euler equations # . # El= ME(q) eta + RE(q,eta). # We use CEuler with a little trick in the parameter call qt,qt,qtd #-----------------------------------------------------------------XX1:=CEuler(E3,qt,qt,qtd): MM3:=map((i)->factor(combine(i,trig)),XX1[1]): RR3:=map((i)->factor(combine(i,trig)),XX1[2]): #----------------------------------------------------# FORTRAN GENERATION # First routine npend(neq,t,th,ydot) #----------------------------------------------------flist:=[subroutinem,‘npend‘,[‘neq‘,‘t‘,‘th‘,‘ydot‘], [ [ parameterf,[‘n=‘.n]], [ declaref,‘implicit doubleprecision‘,[‘(t)‘] ], [ declaref,doubleprecision,[‘t,th(2*n),eta(n),ydot(2*n),r(n)‘]], [ declaref,doubleprecision,[‘me3s(n,n),const(n,1),j(n),m(n)‘]], [ declaref,doubleprecision,[‘w(3*n),rcond ‘]], [ declaref,integer,[‘i,k,neq,ierr‘]], [ declaref,‘data g‘,[‘/ 9.81/‘]], [ declaref,‘data r‘,[‘/ n*1.0/‘] ], [ declaref,‘data m‘,[‘/ n*1.0/‘] ], [ declaref,‘data j‘,[‘/ n*0.3/‘] ], [ dom , ‘i ‘ ,1,‘n ‘,1,[ equalf,‘ydot(i)‘,‘th(i+n)‘]], [ dom , ‘i ‘ ,1,‘n ‘,1,[ equalf,‘eta(i)‘,‘th(i+n)‘]], [ matrixm,‘me3s‘,MM3 ] , [ matrixm,‘const‘, RR3 ] , [ dom , ‘i ‘ ,1,‘n ‘,1,[ equalf,‘const(i,1)‘,‘-const(i,1)‘]], [commentf,‘ solving M z = const to obtain ydot((n+1)..2*n)‘], [ callf , ‘dlslv‘,[‘me3s,n,n,const,n,1,w, rcond,ierr,1‘]], 305 306 E Maple Code Used for Modeling the N -Link Pendulum [ if_then_m,ierr<>0,[ [writef,6,ff_w,[]], [formatf,ff_w,[‘’Matrix is badly conditioned’‘]]]], [ dom , ‘i ‘ ,1,‘n ‘,1,[ equalf,‘ydot(n+i)‘,‘const(i,1)‘]], [returnf]]]: Gener(‘npend.f‘,flist): # New np.f flist:=[subroutinem,‘np‘,[‘i ‘], [[ declaref,integer,[‘i ‘]],[ equalf,‘i ‘,n],[returnf]]]: Gener(‘np.f‘,flist): References 1. Y. Alain. Théorie et analyse du signal. Cours et initiation pratique via Matlab et Scilab. Ellipses, Paris, 1999. 2. G. Allaire and S. Kaber. Algèbre linéaire numérique. Cours et exercices. Ellipses, Paris, 2002. 3. G. Allaire and S. Kaber. Introduction á Scilab - Exercices pratiques corrigés d’algèbre linéaire. Ellipses, Paris, 2002. 4. E. Anderson, Z. Bai, C. Bischof, S. Blackford, J. Demmel, J. Dongarra, J. D. Croz, A. Greenbaum, S. Hammarling, A. McKenney, and D. Sorensen. LAPACK Users’ Guide, Third Edition. SIAM, Philadelphia, 1999. 5. R. M. Anderson and R. M. May. Infectious diseases of humans: dynamics and control. Oxford University Press, Oxford, UK, 1991. 6. U. Ascher, J. Christiansen, and R. D. Russell. A collocation solver for mixed order systems of boundary value problems. Math. Computation, 33:659–679, 1979. 7. U. Ascher, J. Christiansen, and R. D. Russell. Algorithm 569: Colsys: Collocation software for boundary-value ODEs. ACM Transactions on Mathematical Software (TOMS), 7(2):223–229, 1981. 8. U. Ascher, J. Christiansen, and R. D. Russell. Collocation software for boundary-value ODEs. ACM Transactions on Mathematical Software (TOMS), 7(2):209–222, 1981. 9. J. Baumgarte. Stabilization of constraints and integrals of motion in dynamical systems. Comp. Math. Appl. Mech. Eng., 1972. 10. J. T. Betts. Practical Methods for Optimal Control Using Nonlinear Programming. SIAM, 2001. 11. K. E. Brenan, S. L. Campbell, and L. R. Petzold. The Numerical Solution of Initial Value Problems in Diﬀerential-Algebraic Equations. SIAM, 1996. 12. D. Brown, A. Herbison, J. Robinson, R. Marrs, and G. Leng. Modelling the luteinizing hormone-releasing hormone pulse generator. Neuroscience, 63(3):869–879, 1994. 13. P. N. Brown and A. C. Hindmarsh. Reduced storage matrix methods in stiﬀ ODE systems. J. Appl. Math. Computation, 31:40–91, 1989. 14. P. N. Brown, A. C. Hindmarsh, and L. R. Petzold. Using Krylov methods in the solution of large-scale diﬀerential-algebraic systems. SIAM J. Sci. Comp., 15:1467–1488, 1994. 15. P. N. Brown, A. C. Hindmarsh, and L. R. Petzold. Consistent initial condition calculation for diﬀerential-algebraic systems. SIAM Journal on Scientiﬁc Computing, 1998. 16. C. Bunks, J. Chancelier, F. Delebecque, C. Gomez, M. Goursat, R. Nikoukhah, and S. Steer. Engineering and Scientiﬁc Computing with Scilab. Birkhäuser, 1999. 17. M. Caracotsios and W. E. Stewart. Sensitivity analysis of initial value problems with mixed ODEs and algebraic equations. Comp. Chem. Eng., 9(4):359–365, 1985. 18. J. Chancelier, F. Delebecque, C. Gomez, M. Goursat, R. Nikoukhah, and S. Steer. Introduction à Scilab. Springer, 2001. 19. W. R. Cowell, editor. Sources and Development of Mathematical Software. Prentice-Hall Series in Computational Mathematics, Cleve Moler, Advisor. Prentice-Hall, Upper Saddle River, NJ 07458, USA, 1984. 308 References 20. C. Deboor and R. Weiss. Solveblok: a package for solving almost block diagonal linear systems. ACM Transactions Mathematical Software, 6:80–87, 1980. 21. F. Delebecque and R. Nikoukhah. A mixed symbolic-numeric software environment and its application to control system engineering, pages 221–244. Elsevier Science Publishers, 1992. 22. P. Fritzson. Principles of Object-Oriented Modeling and Simulation with Modelica 2.1. WileyIEEE Press, 2004. 23. M. Goossens, F. Mittelbach, and A. Samarin. The LATEXCompanion. Addison Wesley, Reading, Massachusets, 1994. 24. A. Griewank, D. Juedes, H. Mitev, J. Utke, O. Vogel, and A. Walther. ADOL-C: A package for the automatic diﬀerentiation of algorithms written in C/C++: Algor. 755. ACM TOMS, 22(2):131–167, 1996. 25. S. Guerre-Delabriere and M. Postel. Mèthodes d’approximation - Équations diﬀérentielles Applications Scilab. Niveau L3. Ellipses, Paris, 2004. 26. E. Hairer, S. P. Norsett, and G. Wanner. Solving Ordinary Diﬀerential Equations I. Nonstiﬀ Problems, volume 8 of Springer Series in Comput. Mathematics. Springer-Verlag, 1993. 27. E. Hairer and G. Wanner. Solving Ordinary Diﬀerential Equations II. Stiﬀ and DiﬀerentialAlgebraic Problems, volume 14 of Springer Series in Comput. Mathematics. Springer-Verlag, 1991. 28. A. C. Hindmarsh. ODEPACK, A Systematized Collection of ODE Solvers, in Scientiﬁc Computing. vol. 1 of IMACS Transactions on Scientiﬁc Computation. North-Holland, Amsterdam, 1983. 29. C. V. Hollot and Y. Chait. Nonlinear stability analysis for a class of tcp/aqm networks. In Proc. of the 40th IEEE Conference on Decision and Control, pages 2309–2314, Orlando, Florida, December 2001. 30. F. L. Lewis. Optimal Control. Wiley, 1986. 31. T. Maly and L. Petzold. Numerical methods and software for sensitivity analysis of diﬀerential-algebraic systems. Applied Numerical Mathematics, 20:57–79, 1996. 32. W. Michiels and S.-I. Niculescu. Stability analysis of a ﬂuid ﬂow model for tcp like behavior. International Journal of Bifurcation and Chaos, page to appear, July 2005. 33. P. S. M. Pires and D. A. Rogers. Free/open source software: An alternative for engineering students. In Proc. 32nd ASEE/IEEE Frontiers in Education Conference, pages T3G–7 TO 11, Boston, MA, 2002. 34. K. Radhakrishnan and A. C. Hindmarsh. Description and use of LSODE, the livermore solver for ordinary diﬀerential equations. Technical Report report UCRL-ID-113855, Lawrence Livermore National Laboratory, December 1993. 35. O. E. Rössler. An equation for continuous chaos. Phys. Lett., 35A:397–398, 1976. Index $ 15 % 11 %ODEOPTIONS 80 %cpr 175, 230, 281 %e 11 %f 19 %inf 30 %io 37 %io(1) 38 %io2 38 %pi 11 %t 19 & 16, 19 ** 16 , 12 . 16 .* 16 .*. 16 ./ 16 .cos 175 .sce 27 .scilab 27 : 11 ; 12 < 19 <= 16, 19 <> 16 == 19 > 19 >= 19 ~ 16, 19 3D Rot. 49 abcd 86 abort 36 activation signal 179 activation timing 179 adda 86 addinter 63, 70 addmenu 43 AddNewblock 207 aleft 91 algebraic variable 77 Align 164 always activation 192 and 19 animation 61 animation speed 132 application n-link pendulum 125 pendulum 126 applications real-time 253 standalone 253 argn 33 aright 91 ascii 18 automatic diﬀerentiation 141 axes 53 current 55 style 54 backward diﬀerentiation formulas 79, 98 Basile 3 batch mode execution 27 batch reactor 148 BDF 79, 98 block Threshold 210 ABS 202 activation index 189 always active 183 basic 194 CBlock2 207 constant 184 Debug 270 delay 180 Demux 174 freq_div 186 Gain 201 hierarchy 173 HYSTERESIS 210 If-then-else 176, 185 integrator 166 interactive GUI 221 internal state 180 internally implicit 275 Jump 220 label 230 memory 253 number 230 parameters 166 read 234 S/H 186 sample and hold 186, 261 scale 224 Sigma 168 square wave generator 177 Stop 192 Super 173, 174, 176, 181, 193 times 168 write 234 blocks internally implicit 275 Pal editor 176 read from 192 bool2s 19 Boolean variable 19 bouncing ball 204, 293 boundary condition 74 boundary value problems 74 break 29 bvode 90 310 Index BVP 74 C2F 66 call 63 cd 42 chdir 42 CheckLhs 68 CheckRhs 68 clear 9, 27 clf 48 clock 164 code generation 253 ﬁle names 256 procedure 256 code2str 232 COLNEW 90 colormap 56, 58 COLSYS 90 COMEDI 265 command history 5 comparison operators 19 computational function 195, 197, 294, 296 type 5 205 concatenation column 12 row 12 conditional subsampling 186 conjugate gradient 111 constraint bound 107 box 107 holonomic 126 nonholonomic 126 constraint drift 127 context 171 control design 249 Control ﬂow else 28 elseif 28 if 28 then 28 control panel 225 create_palette 208 CreateVar 68 csim 87 impuls 87 csslti 195 curblock 206, 225 DAE 76, 128, 147, 275 index zero 85 dasrt 100 DASSL 98 dassl 128 datafit 117, 147, 241 Debug log ﬁle 271 decimation operation 184 deff 78 delay diﬀerential equations 221 delmenu 43 Demos 5 dep_t 183 dep_u 181 derivative 120, 122 dgetrf 66 diag 13 diﬀerence equation 75 diﬀerential algebraic equation 76, 147, 275 diﬀerential equation implicit 128 diﬀerential variable 77 diﬀerentiation numerical 120 stabilized 127 dir 42 discrete 95 disease spread 167 disp 28, 37 dom 86 down-sampling 213 download 7 drawlater 55 drawnow 55 driver 60 dscr 171 dsimul 97 Edit 164 eigenvalue assignment 171 elseif 28 emacs 5 entity 54 %eps 10 equations implicit 275 nonlinear 108 equilibrium 248 errcatch 19 error 33 eval 173 eval3dp 51 event 77 event clock 165 event generator 164 event mechanism 216 Example_events 256 exec 27, 36 execstr 18 existence and uniqueness exists 9 exp 14 expm 14 extensions 27 eye 13 factorial 31 failure detection 213 false 19 fast Fourier transform fchamp 50 feedback 212 FFT 213 Figure 54 figure 46 figure_style 54 file 37 File operations 7 fileinfo 42 find 20, 30 ﬁnk 3, 4 ﬁrst-order 73, 76 fit_dat 117 fixpnt 92 flts 97 fopen 38 for 29 format format 38 big endian 41 little endian 41 native 41 fplot3d 49 fsolve 112, 149 FSQP 109 full 21 fully implicit 77, 86 functions function 31 deﬁnition 26, 31 gca() 55 gcf() 55 gda() 55 gdf() 55 GENERIC 195 get 53 getcwd 42 getf 27 getfield 22 GetRhsVar 68 74 213 Index getscicosvars 270 global 34 gradient 108, 110 approximation 109 computation 147 numerical 120 grand 13 graph editing 49 graphic editing 54 export 49 functions 56 handle 54 mode setting 54 primitives 56 graphics mode new 53, 132 old 60 grep 18 %gtk 42 guess 93 builder.sce 69 examples 68 gateway 70 interface library 67 interfacing function 194, 195, 260, 293, 295 inverted pendulum 245, 294 ipar 91 isdef 9 isglobal 34 isoview 297 Hamiltonian 93 help apropos 6 browser window 6 help 6 Hessian 122 histplot 51, 58 HOME 27 hormone pulse generator 218 host 42, 232 hybrid 77, 135 hybrid system 100 hypermat 12, 22 Lagrangian 126 LaTeX psfrag 57 least squared 16 least squares 113 leastsq 113 length 17 length 18 Levenberg-Marquardt 114 LhsVar 68 lincos 248 line editor 5 line style 54 linear DAE 85 linear program 119 linear system 86, 171 linearization scicos 243 link color 165 creation 166 splitting 166 link 63 links activation 165 regular 165 linpro 119 linspace 13, 49 list 11, 22 listfiles 42 load 40 Load as Palette 176 ilib_build 69 ilib_for_link 63, 131, 204 impl 85 implicit diﬀerential equation 85 implicit semiexplicit DAE 77 index of a DAE 76 initial condition 74 consistent 76, 148 input ports 179 INRIA 3 int 13 int16 11 int32 11 int8 11 interface 66 jac 86, 99 Jacobian 80 numerical 120 job 195 jroot 198 Kirchoﬀ’s law 273 .*. 13 Kronecker product 13 loader.sce 204 logical or 16 logspace 13 ls 42 lsodar 79 lsode 79 lsqrsolve 113, 155 ltitr 97 ltol 92 MacOSX 4 macros 26 malloc 10 Maple 126, 130 matrix 12 matrix 13 concatenation 12 exponential 14 how stored 12 multiplication 16 sparse 21 term by term 16 Vandermonde 17 mclose 39 mexﬁles 69 mget 41 MINPACK 112 mllst 22 mode pixmap 297 mode 190 mode parameter 191 model ﬁsheries 209 predator prey 209 Modelica 274 modeling component-level 273 error 269 Monte Carlo 227 mopen 38 mput 41 MSDOS 42 msprintf 18 msscanf 18 multiple shooting 90 ncomp 91 NDcost 109 nevprt 189 node 54 norm 17 not 19 numdiff 120 311 312 Index obscont 249 observer 171, 212 ode 73 adams 79 atol 79 default value 80 discrete 79 external 80 fix 79 ﬁxed step 257 jac 80 lsoda 79 real-time 257 rk 79 rkf 79 root 79 rtol 79 default value 80 stiff 79 type 79 ode 78, 131, 135 ode options h0 80 hmax 80 hmin 80 itask 80 ixpr 81 jactype 81 maxordn 81 maxords 81 mi 81 mu 81 mxstep 81 tcrit 80 ode_discrete 95 ode_root 87 odedc 100 ODEPACK 78, 79 old_style 54 ones 13 optim 108, 147 optimal control 93 or 19 ordinary diﬀerential equation 73 Sinks 164, 192 Sources 164, 192 Threshold 192 param3d1 50 parameters symbolic 193 18 pathconvert 42 pause 35 PD controller 225 pendulum 125, 245, 294 phase_simulation 206 PI controller 237 pixmap 61 play 141 plot editing 49 parametric 49 surface 49 plot2d 48 plot2d2 48 plot2d3 48 plot2d4 48 plot3d 49 pmodulo 29 points 91 Poisson process 216 polarplot 51 pole placement 171 polyline menu 54 poly 20 pop 36 postscript 49 ppol 171, 212, 249 primitives 26 print 37 prompt 4 push 36 %pvm 42 palette electrical 192 thermo-hydraulics 192 palettes 159, 192 Branching 176, 192 copying blocks 163 Linear 166 Others 174 rand 13 read 38 realtime 132, 299 realtimeinit 132, 299 Rec 60 Region-to-Super-block 173 res 85, 98 quadratic program 120 quapro 120 quasi-Newton 111 queuing system 216 quit 27, 36 residual 85 resume 35, 36 RK4 79 RKF45 79 root ﬁnding 77 RTAI 265 RTAI-Lab 265 rtitr 97 sampled data 101 sampled data system 102 sampled system 86 save 40 scale graphic 61 SCI 4 Scicos .cosf 175 command names 228 context 169 diagram names 176 editor 163 error messages 267 graphics style 232 help 163 keyboard shortcuts 163 load 175 loading 227 Misc 163 Rename 176 Run 165 save 175 Save-as-Interf-Func 194 Setup 165 Simulate 165 scicos 163, 281 scicos_simulate 232 scicos_time 206 scicos_ver 175 scicosim 228 Scierror 68 Scifunc 193, 194 scilab.ini 27 scilab.star 27 ScilabEval 44 scope 164 scripts 27 scs 175 scs_m 281 second derivative approximation 122 select 28 case 28 Index else 28 semidef 120 semiexplicit DAE 77 set 53 set_blockerror 206 setbpt 36 seteventhandler 44 setfield 22 setmenu 43 setpoint 244 shooting methods 90 sign 18 signal processing 184 simulation phase 190, 198 simulation start time default 229 SIR model 167 SISO 87 size 206 sliding mode 268 sparse 21 sprand 21 stabilization Baumgarte 127 stacksize 10 state-space form 86 states continuous-time 190 discrete-time 190 hybrid 138 steadycos 247, 248 step 87 stiﬀ 79 stop button 166 str2code 232 strcat 18 stream input 38 output 38 strindex 18 strings 17 string 18 concatenation 17 stripblanks 18 strsubst 18 submatrix deletion 14 extraction 14 subplot 48 swap 41 Switch 176 synchronism 176 synchronization 176 SynDEx 265 syslin 86 syslin list 86 Taylor approximation 122 Taylor coeﬃcients 122 Tck/Tk menus 56 Tcl/Tk 44, 223 TCL_EvalFile 44 TCL_EvalStr 44 TCL_ExistVar 44 TCL_GetVar 44 TCL_SetVar 44 %tk 42 TCP/AQM Network 221 tic 83 timer 21, 29 tlist 86 tlist 11 toc 83 tokens 18 tol 92 ’ 13 true 19 type string 17 typeof 9 web 7 where 35 whereami 35 while 29, 30 who 10 whos 10 widget 223 deﬁnition 222 Windows 27 windows dialog 43 write 38 uicontrol 46 uimenu 46 Undefined 24 unix unix_g 42 unix_s 42 unix_w 42 unix_x 42 unix 42 unsetmenu 43 x_choices 43 x_choose 43 x_dialog 43 x_mdialog 43 x_message 43 xbasc 48, 60 xbasr 60 xclick 44 xdel 61 xget 61 xgetmouse 44 xinit 60 xload 41 xor 142 xpause 272 xpoly 61 xpolys 53 xrects 61 xrects 52 xs2latex 59 xs2ps 60 xsave 41 xset 61 clipgrf 52 pixmap 61 wshow 61 thickness 53 window 48 wwpc 61 xtape 60 varargin 34 vector ﬁeld 50 visible 55 Visual C++ 161 zero crossing zeros 13 zeta 91 zoom 49 138, 191 313

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertisement