Note to reader: This article was written in 2012. In Scilab later versions, an Xcos model file has extension .zcos. Read how to construct an Xcos feedback diagram on our new article Scilab Recipe 2: with Control Engineering Flavor.

For a software package that provides tools for a wide range of industrial applications, especially for feedback control systems, an engineer might mention MATLAB/Simulink from The MathWorks as his first choice. While I absolutely agree that most engineering schools and leading companies should have this professional software installed at their sites, the cost may be beyond reach for a student or small business in a developing country. As an alternative, I have tried to encourage the use of an open-source software known as Scilab/Scicos, which are quite similar to MATLAB/Simulink in many aspects.

From my own experience, reinforced by students’ complaints, previous versions of Scilab/Scicos had some bugs and were not quite user-friendly. Fortunately, newer versions of the software have been improved significantly and are stable enough for classroom and laboratory use. It seems to me one learning obstacle left is the lack of comprehensive “official” documentation, which is not unusual for a typical open-source software. The information or solution for your problem is scattered around useful online forums and websites out there. All you have to do is fish.

Nevertheless, I enjoy using the software and want to contribute to the community by writing articles of my own on the tips and tricks of using Scilab/Scicos for control engineering. I also use Scicos and Xcos (with Modelica extension) to simulate electric circuits. Most of them work well and yield expected results .

This first post in the sequel serves as a brief introduction to Scilab/Scicos, for those who have never used it before. I assume the reader has some familiarity with MATLAB/Simulink environment. Some documents about migrating from MATLAB to Scilab are available on the developer websites.

**Remark: ** Before we begin, I have to mention about the separation of Scilab/Scicos original developers into two groups, each with their own website. I have no information on the reason behind this separation and do not deem it as important to me as an end user. It only causes some confusion when discussing the overall software package with general audience. At the time of this writing, you can either download Scilab 5.3.3 from www.scilab.org with the used-to-be-Scicos simulation tool renamed to Xcos, or download Scicoslab4.4.1 from www.scicos.org (which somehow can be thought of as Scicos software with Scilab command window attached to it?). Nevertheless, the two packages have pretty much the same interface and could be roughly considered compatible. At this moment, I personally prefer the Scilab5.3.3 and will use it as reference in the materials.

### Scilab commands and the command window

Quite similar to Matlab, the first screen you see when launching Scilab is a command window, where you type a command after the -> prompt and get the answer or response instantly. This is the simplest way to interact with the software. Many Scilab commands are identical to MATLAB’s. Figure 1 shows how to solve a matrix equation in the command window.

### Writing a script file

A series of Scilab commands or an algorithm could be written as a script file. An editor is provided in the package for convenience (select Applications->SciNotes from the menu), though you could use any editor that doesn’t embed any special formatting in the file. By default, the script file is saved with .sce extension, for example, test.sce and could be executed with this command

–> exec( ‘test.sce’,-1)

the -1 argument is optional. It instructs Scilab not to display output from individual command in the file.

**Example 1: ** For the diode circuit in Figure 2, find *i* and *v* when VT = 12 V, RT = 22 Ohm

Since a diode is a nonlinear device, two conventional ways to solve this problem is 1) iteratively by writing a program with flowchart such as in Figure 3, or 2)using load-line analysis. In either case, it is straightforward to write a Scilab script file and run it. Listing 1 is a program to solve this problem using both methods. Figure 4 shows the plot from load-line analysis. Both methods yield the same answer *i* = 0.5 Amp, and *v* = 0.7 Volt

-->exec(‘ex1.sce',-1) Id1 = 0.5137095 Vd1 = 0.6983915 |

// ex1.sce Solve the diode problem in example 1 iteratively and compare with load-line method Vt = 12; Rt = 22; Isat = 1e-12; ktq = 0.0259; Vd1 = Vt/2; Vd2 = Vt; Finish = 0; idx = 0; while ~Finish, Id1 = (Vt - Vd1)/Rt; Id2 = Isat*(exp(Vd1/ktq) - 1); if Id1>Id2 then Vd1 = Vd1 + (Vd2 - Vd1)/2; elseif Id2> Id1 then Vd2 = Vd1; Vd1 = Vd1/2; end if abs(Id1-Id2)< 1e-14 then // stop when Id1 approximately equals Id2 Finish = 1; end idx = idx + 1; end print(%io(2),Id1) print(%io(2),Vd1) // load line analysis V=0:0.01:0.7; Ir = -V/Rt + Vt/Rt; Id =Isat*(exp(V/ktq) - 1); plot(V, Ir,'b',V, Id, 'r') xlabel('vD') ylabel('iD') |

### Plot Functions

Similar to MATLAB, Scilab provides various functions used for plotting waveforms, frequency responses, Bode. As an example, the following command set compares sine and cosine waveforms using different line styles and colors. The result is shown in Figure 5.

t=0:0.001:0.1; // time array from 0 to 0.1 sec, 1 ms sampling period f = 50; x = 2*sin(2*%pi*f*t); y = cos(2*%pi*f*t); plot(t,x,'r-.',t,y,'b-') // compare x(t) and y(t) legend(['x(t)';'y(t)']) xlabel('time') ylabel('volts') |

### Simulation with Xcos

As mentioned earlier, the simulation subsystem in Scilab package is called Xcos, which we will investigate more in subsequent articles. If you’re familiar with Simulink, the basic concept and functionality are pretty much the same. You start from dragging the required palettes from the library to the blank area of new Xcos file and connecting them together. From a simple PID control diagram in Figure 6, we see an obvious difference from Simulink is that Xcos has an explicit timing signal (indicated by the activation clock block connected by red line). If you already have Scilab 5.3.3 installed, download the file from the link below and run it. You will see the step response from scope output like shown in Figure 6. Try adjusting the PID gains to study how the step response changes with each parameter.

Download pid.xcos, a simple PID control simulation

Beyond standard palettes, Xcos provides simulation of engineering systems such as electrical and Thermo-Hydraulics, using the Modelica extension. You need to have a C compiler such as Microsoft Visual C++ installed in your computer for the model to run. Figure 7 shows palettes for electrical system and Figure 8 demonstrates a simulation of simple non-inverting opamp circuit.

Download opamp.xcos (A C-compiler must be installed)

### Summary

This first article in the Scilab series introduces the open-source Scilab package, which is a valuable tool for academic use as well as for practicing engineers. I truly admire the efforts from all contributors to make this software freely available. Scilab and Xcos have absolutely much more to study in detail. It is quite a challenge when you often feel like being on your own, compared to when using a well-documented proprietary software. But that pays off when you understand an issue better through hard work and dedication.

In the sequel we will emphasize on using Scilab for control engineering. Our ongoing research also involves building an embedded platform to be used with Scilab. You do the analysis and design in Scilab/Xcos, and then download the controller to the board for real-world experiments. That sounds like fun, doesn’t it?

Pingback: Scilab Recipe 2: with Control Engineering Flavor | Scilab Ninja