Master Scicos!


Finn Haugen

8. July 2008


1 What is Scicos?
2 About this document  
3 The Scicos environment
4 An example: Simulator of a liquid tank
4.1 Developing the mathematical model of the system to be simulated
4.2 Downloading and running the simulator
4.3 Studying the simulator
4.4 Constructing the block diagram

1 What is Scicos?

Scicos is a block-diagram based simulation tool, which means that the mathematical model to be simulated is represented with function blocks. Scicos is quite similar to Simulink and LabVIEW Simulation Module. Scicos can simulate linear and nonlinear continuous-time, and discrete-time, dynamic systems. You can make the simulation run as fast as the computer allows, or you can make it run with a real or scaled time axis, thus simulating real-time behaviour.

Scicos is automtically installed when you install the mathematical tool Scilab. You can not install Scicos independently of Scilab. Here is information about installing Scilab.

The homepage of Scicos is at

[Table of contents]

2 About this document

This tutorial guides you through the basic steps towards mastering Scicos. I have written this document because I did not find a proper, updated tutorial on the Scicos homepage (there are however books about Scicos).

I assume that you do all the activities in the blue boxes, as here:

Activities are shown in blue boxes as this one.

Please send comments or suggestions to improve this tutorial via e-mail to

[Table of contents]

3 The Scicos environment


Launch Scilab.

To launch Scicos:

Either enter scicos at the Scilab command line (or Select Applications / Scicos in the Scilab menu).

The figure below shows the Scicos window where you will construct the (block) diagram.

The Scicos window

The blocks that is used to build the mathematical model to be simulated are organized in palettes. (When you construct a diagram you simply drag blocks from the palettes to the block diagram.) To display the palettes in a tree structure:

Select Palettes / Pal tree in the Scicos menu.

The figure below shows the palettes.

The block palettes

To see the individual blocks on a specific palette, click the plus sign in front of the palette.

As an example, display the blocks on the Sources palette.

The figure below shows some of the blocks on the Sources palette.

Some of the blocks on the Sources palette

An alternative way to open a specific palette is selecting the palette via the Palette / Palettes menu:

As an example, select the Sources palette via the Palette / Palettes menu.

The figure below shows (again) the Sources palette (this time all the blocks are shown).

The Sources palette

You can even open the palettes in this way:

As an example, open the Sources palette via Right-click on the block diagram / Select Palettes (in the menu that is opened).

It is useful to see the contents of the various palettes. Hence, in the following, the palettes are shown, and comments about selected (assumably) most useful blocks on the palettes are given.

Here are comments to selected blocks on the Sources palette shown above:

  • Activation clock (red clock) is used to activate or invoke other blocks with a specified period, e.g.  Scope blocks and Display blocks.
  • Clock generates a signal which value is the simulation time.
  • Constant (square block with "1" inside) generates a constant, or a parameter having a constant value.
  • Step generated a step from an initial value to a final value at a specific point of time.

[Table of contents]

The Sinks palette:

The Sinks palette

Comments to selected blocks in the Sinks palette:

  • Scope is used to plot signals as the simulation runs. Using a Mux block (on the Branching palette) you can merge a number of scalar signals into to be plotted in one Scope.
  • Display is used to display the numerical value of any signal.
  • To workspace is used to save a signal as a variable in the workspace of Scilab. The resulting variable is of mlist data type (you can learn more about mlist data type via Help in Scilab). The mlist variable will consist of the time-stamps and the signal values.
    Below is some example code (to be executed in Scilab, and remember to close Scicos before you attempt to execute code in Scilab) that illustrates how to get the time and the signal as individual arrays or vectors from the variable of mlist data type. In the example, these arrays are used as arguments in the plot command. Assume that you have given the name V to the workspace variable to be gernerated by the To workspace block. In the following t is chosed as the name of the time, and x is chosen as the name of the signal.

[Table of contents]

The Linear palette:

The Linear palette

Comments to selected blocks in the Linear palette:

  • Sum (block) can be used to sum and/or subtract signals. The block is configured by double-clicking the block.
  • Gain represents a gain, i.e. multiplication by a constant. (The triangular block with 1 inside.)
  • Integrator is used to time-integrate signals that are time-derivatives of the state-variables of the system. The integrator output is then state-variables of the system. (Thus, the integrators implement the dynamics of the system.) The initial value and the maximum and minimum values of the integrator output can be set.
  • PID is a PID controller (Proportional + Integral + Derivative). The parameters are proportional gain Kp, integral gain Ki, and derivative gain Kd. Often the PID parameters are in stead given as Kp, integral time Ti, and derivative time Td. (The famous Ziegler-Nichols' PID controller tuning method gives values for Kp, Ti and Td.) The relation between the various parameters are as follows: Ki = Kp/Ti and Kd = Kp*Td.
  • Continuous fix delay (Time delay) represents a time delay, also denoted dead-time and transport delay. At the start of the simulation, the block outputs the Initial input parameter until the simulation time exceeds the Time delay parameter, when the block begins generating the delayed input.
  • Continuous-time (Laplace transform) transfer function (the num(s)/den(s) block). You configure the block by entering the numerator and denominator s-polynomials.
  • Discrete-time (z transform) transfer function (the num(z)/den(z) block). You configure the block by entering the numerator and denominator z-polynomials.
  • Unit delay block (with symbol 1/z inside) which implements delay of one time step.

[Table of contents]

The Nonlinear palette:

The Nonlinear palette

Comments to selected blocks in the Nonlinear palette:

  • Mathematical Expression is used to implement any Scilab function (you type the expression in the dialog box of the block).
  • Product implements multiplication or division. It takes any number of inputs.
  • Interp implements interpolation or table lookup between tabular data.

[Table of contents]

The Others palette:

The Others palette

Comments to selected block on the Others palette:

  • Text is used to add annotations (any text) to the block diagram.
  • End can be used to stop the simulation at a specific simulation time.

[Table of contents]

The Branching palette:

The Branching palette

Comments to a selected block on the Branching palette:

  • Mux is used to merge a number of scalar signals into a vectorized ("multivariable") signal. This block is useful e.g. to merge several signals to be plotted in one Scope.
  • Switch 1 can be used to select which of the inputs (among any number of inputs) to be propagated to the output.

[Table of contents]

4 An example: Simulator of a liquid tank

In this section we will study a premeade simulator of a liquid tank. Then, you will learn how to create a simulator by yourself. You are supposed to have basic knowledge about modeling of dynamic systems, as described in e.g. Dynamic Systems - modelling, analysis and simulation or in any other book about dynamic systems theory.

[Table of contents]

4.1 Developing the mathematical model of the system to be simulated

The system to be simulated is a liquid tank with pump inflow and valve outflow, see the figure below. The simulator will calculate and display the level h at any instant of time. The simulation will run in real time, thereby giving the feeling of a "real" system. Actually, since this tank is somewhat sluggish, we will speed up the simulation to have the simulation time running faster than real time, to avoid us waste our precious time. The user can adjust the inlet by adjusting the pump control signal, u.

Liquid tank

Any simulator is based on a mathematical model of the system to be simulated. Thus, we start by developing a mathematical model of the tank.

We assume the following (the parameters used in the expressions below are defined in the figure above):

  • The liquid density is the same in the inlet, in the outlet, and in the tank.
  • The tank has straight, vertical walls.
  • The liquid mass and level are related through

    m(t) = ρAh(t)

  • The inlet volumetric flow through the pump is proportional to the pump control signal:

    qin(t) = Kuu(t)

  • The outlet volumetric flow through the valve is proportional to the square root of the pressure drop over the valve. This pressure drop is assumed to be equal to the hydrostatic pressure at the bottom of the tank (sqrt means square root):

    qout(t) = Kvsqrt[ρgh(t)]

Mass balance (i.e., rate of change of the mass is equal to the inflow minus the outflow) yields the following differential equation:

dm(t)/dt = ρqin(t) - ρqout(t)]        (1)

or, using the above relations,

d[ρAh(t)]/dt = ρKuu(t) - ρKvsqrt[ρgh(t)]        (2)

We will now draw a mathematical block diagram of the model. This block diagram will then be implemented in the block diagram of the simulator VI. As a proper starting point of drawing the mathematical block diagram, we write the differential equation as a state-space model, that is, as a differential equation having the first order time derivative alone on the left side. This can be done by pulling ρ and A outside the differentiation, then dividing both sides by ρA. The resulting differential equation becomes

d[h(t)]/dt = (1/A)*{Kuu(t) - Kvsqrt[ρgh(t)]}        (3)

This is a differential equation for h(t). It tells how the time derivative dh(t)/dt can be calculated. h(t) is calculated (by the simulator) by integrating dh(t)/dt with respect to time, from time 0 to time t, with initial value h(0), which we here denote hinit. To draw a block diagram of the model (3), we may start by adding an integrator to the empty block diagram. The input to this integrator is dh/dt, and the output is h(t). Then we add mathematical function blocks to construct the expression for dh/dt, which is the right side of the differential equation (3). The resulting block diagram for the model (3) can be as shown in the figure below.

Mathematical block diagram of Differential Equation (3)

The numerical values of the parameters are as follows:

rho=1000; //[kg/m3]
g=9.81; //[m/s2]
Ku=5; //[m3/A]
A=1; //[m2]
h_init=0.5; //[m]
h_max=1; //[m]
h_min=0; //[m]

We will assume that there are level "alarm" limits to be plotted together with the level in the simulator. The limits are

h_AH = 0.9 m (Alarm High)

h_AL = 0.1 m (Alarm Low)

[Table of contents]

4.2 Downloading and running the simulator

  • Download the premade simulator tanksim.cos, and save it in the folder you prefer.
  • Open it in Scicos using the File / Open menu.
  • Run the simulator using the Simulate / Run menu. In the simulation the initial value of the level is 0.5 m. The pump control signal u is 0 up to simulation time 20 s, and at 20 s it is changed as a step from 0 to 0.01 A. The simulator runs five times faster than real time. The simulator from initial time of 0 s to final time of 50 s. If you want to stop the simulator before the final time, select Stop in the Scicos menu.

The figure below shows the block diagram of tanksim.cos.

The block diagram of tanksim.cos

The figure below shows the simulated pump control signal u. The subsequent figure shows the response in the level h and the level alarm values h_AL and h_AH.

The simulated pump control signal u

The response in the level h and the level alarm values h_AL = 0.1 m and h_AH = 0.9 m

[Table of contents]

4.3 Studying the simulator

Setting up the simulator
Open the Simulate / Setup menu.

This opens the dialog window shown in the figure below.

The dialog window opened with the Simulate / Setup menu.

Most parameters can generally be left unchanged in this dialog window, except the following parameters:

  • The Final integration time defines the final (stop) time of the simulation. But, if an End block exists in the block diagram, the Final simulation time parameter in that block also defines the simulation stop time. It turns out that the applied stop time is the smallest of the two. In this example, I want the stop time to be 50 sec, and I want to use the End block with the Context variable t_stop to define the stop time. Therefore, I have set the Final integration time in the Setup dialog window to be larger than t_stop = 50, namely 10000.
  • The Realtime scaling parameter defines the relative speed of the real time compared to the simulated time. In this example Realtime scaling is 0.2 which means that the real time runs 0.2 times as fast as the simulated time. In other words, the simulator runs 1/0.2 = 5 times faster than real time. You can use this parameter to speed up or slow down the simulator. E.g. it may be convenient to speed up the simulation of a slow system, and to slow down the simulation of a fast system (so that you are able to follow the simulated response at it develops in the scopes).
  • The maximum step size can be set to a proper value, e.g. one tenth of the quickest time-constant (or apparent time-constant) of the system to be simulated. In our example we can set it to the timestep parameter in the Context (Scicos then automatically enters the numerical value of the Context parameter in the maximum step size field). 

The solver method (i.e. the numerical method that Scicos uses to solve the underlying algebraic and differential equations making up the model) can be selected via the solver parameter, but in most cases the default solver can be accepted.

Close the simulation setup dialog window.

How does Scicos know what is the time unit in the simulator? Hours? Minutes? Seconds? The answer is that you must define the time unit yourself, and you do it by determining the time unit of the time-dependent parameter values, for example whether a mass flow parameter is given in kg/s or kg/min, etc. A general advice is to use seconds. Remember to use the selected time unit consistently in the simulator!

Defining model (block) parameters in the Context

Model parameters and simulation parameters can be set in the Context of the simulator. The Context is simply a number of Scilab expressions defining the parameters (or variables) and assigning them values. These parameters are used in the blocks in the diagram.

Open the Context via the Diagram / Context menu.

The Context of tanksim.cos is as follows:

t_stop=50; //[s]
timestep=0.1; //[s]
rho=1000; //[kg/m3]
g=9.81; //[m/s2]
Ku=5; //[m3/A]
A=1; //[m2]
h_init=0.5; //[m]
h_max=1; //[m]
h_min=0; //[m]
h_AH=0.9; //[m]
h_AL=0.1; //[m]
t_u_step=20; //[s]
u0=0; //[A]
u1=0.01; //[A]

As an example, open the Step input block (by double-clicking) it, and observe that the Context variables t_u_step, u0, and u1 (see above) are used as parameters in the block.

It is actually not necessary to use Context variables in blocks. You can use numerical values directly. But let me give you a good advice: Use Context variables! Because then all the parameter values appears only one place, not scattered around and "hidden" in the block diagram.

The Activation clock block

The Activation clock block activates the Scope blocks and the Display block.

Open the Activation clock block.

The Activation clock block contains the Init time parameter which in our example is set to the Context variable t_start, and the Period parameter which is set to the Context variable timestep. The period defines the time interval (period) between each time the adjacent blocks are activated.

Close the Activation clock block.

[Table of contents]

4.4 Constructing the block diagram

You construct the block diagram by dragging blocks from the relevant palette to the block diagram. Then you connect the blocks (this is done in the natural way using the mouse on the computer). You configure a block by double-clicking it and entering numerical or (preferably) a Context variable (parameter) name.

Here are couple of tips regarding constructing block diagrams:

  • You draw a new branch from an existing wire by double-clicking the wire, and then drawing the new branch.
  • You can enter annotations at any place in the block diagram using the Text block on the Others palette.
  • There are many options available while constructing the block diagram via
    • Edit menu
    • Format menu
    • Right-click on the block diagram
  • You can temporarily (while working with Scicos) perform calculations in the Scilab window via the menu Tools / Activate Scilab window.
  • You can put a selected part of the block diagram into a super block via the menu Diagram / Region to Super Block.
  • Remember to save your simulator regularly (File / Save menu).
Play with tanksim.cos! Remove some of the blocks, then insert them from their respective palettes. Also try to edit the Context by adding (and possibly later removing) some commands, e.g. one or more new variables including comments about the variables.

Finally, make sure you are able to run your simulator.

[Table of contents]

[Finn's Scilab and Scicos Page] [TechTeach]