You are on page 1of 22

1 The Scilab environment

To start Scilab:

Double-click the Scilab icon on the PC desktop, or
Go to Start / All Programs / Scilab / scilab (do not select scilab console).
Start Scilab.

Starting Scilab opens the Scilab command window, see the figure below.

The Scilab command window
Scilab commands are executed at the command line by entering the command, and
then clicking the Enter button on the keyboard.
Execute 1+1 (type 1+1 at the command line, and finish with Enter-button).

The result is shown in the command window (see the figure above).

2 Scilab Help
Open Scilab Help by clicking the Help button in the toolbar (the button with the question mark).

The Help window is shown below.

Scilab Help window
As you see from the Help window, the commands and functions are organized in a
number of categories.

Click the abs function (in the Elementary Functions category). The functions are as shown in the figure above. To get detailed help text about a specific function. The detailed help text for the abs function is shown in the figure below. click the Elementary Functions category to see the functions in that category. The detailed help text for the abs function . click that function.As an example.

You can also search for a function by first clicking the Search button in the Help window (the magnifying glass button). The result of the search is a list of relevant functions. To create the variable a and assigning to it the result of 1+1: a=1+1 (Enter) Hereafter. . Search for sine. (Enter) will not be shown. see the figure below. The result of the search for sine 3 Basic Scilab operations Typically you use variables in your calculations. but it is assumed that you click the Enter button.

d and D are two different variables: d. which opens the Browser Variables window. but because of the semicolon the response was not shown. D As you see from the response (not shown here).on one line: c=2+3. If you omit the variable name.separated by semicolon or comma . try (remember to type the semicolon): b=1+2.The response is shown in the command window (but shown here). (The userdefined variables are shown among many other variables. To verify that the variable b actually exists: b As you will see. You can execute one or more commands . while D does not exist (since we have not created D). Now. the result is assigned to the inbuilt variable ans: 2+2 The response shows that the variable ans has got the value 4. d exists. Execute the command who. which just lists the variables in the command window. d=2+4 Scilab is case-sensitive. The response should be similar to what is shown in the figure below. Menu Applications / Browser Variables. it exists.) . Scilab variables exists in the workspace. There are two ways to see the contents of a workspace:   Executing the command who at the command line. The response is not shown in the command window. The command was actually executed. For example.

see the figure below. This opens the Browser Variables window. Browser Variables window .The response of the command who Select the menu Applications / Browser Variables.

pi The response is shown in the figure below. 1e-1. then you should consider writing these expressions in a Scilab script instead. Here are some illustrative examples (I assume that you see the principles from these examples): 0. Scilab functions are vectorized. We will return to vector. exp(1). the variables you created in the workspace are deleted. consult Scilab Help. (A vector is simply a one-dimensional matrix.1. but the internal representation of the number in Scilab isindependent if the display format. We will not look at details. if you really need to save variables that are a result of some Scilab expressions. You can save variables in a file using the save function. However. i. If you need to change the display format. functions can be called with vectorial arguments. More about scripts soon.and matrix . Note that if you exit from Scilab. Various ways to enter numbers You can determine how numbers are displayed in the command window with the format function.The Browser Variables window contains at the bottom a number of utility buttons (not described in detail here).e. 2^3. There are various ways to enter numbers (the pi is an inbuilt constant).

operations in a later section. then this vector is used as an argument in the sine function (thesine function assumes the argument is an angle in radians). t=[0:10]'. The result of the vectorized function call sin(0. first a vector of name t is created. sin(0.1*t) The response is shown in the figure below.) In the following example.1*t) where t is a vector .

Scilab responds with an error! See below. The result is shown in the command window.sce at the command line.sce script:   With the Execute / Load into Scilab menu in Scipad By executing the command exec script1.sce at the command line Let us try the Execute menu first: Select the Execute / Load into Scilab menu in Scipad. There are two ways to run the script1. Scilab responds with an error when after executing the command exec script1.sce .sce (of course some other name can be used) in the directory (folder) C:\temp or in any other directory you prefer. Now let us try the exec command: Execute the command exec script1.Note that you can open several scripts in the same Scipad window with the File / New menu. Save the script with name script1.

2. (Matrices and vectors are very similar to arrays as used in programming languages as C. What is then the present Current directory? Excute the menu File / Get Current Directory. 5 Matrix operations In Scilab numbers are in general stored in matrices. The response (in the command window) may be different on different PCs. A=[1. . and then execute the script: Excute the menu File / Change Directory.The error comes because script1. On my PC the response is C:\Documents and Settings\User\SciLab We saved script1.4] The figure below shows A as displayed in the command window.sce at the command line.sce is not in the Current directory of Scilab. Visual Basic etc. comma (or space) is used to separate numers on a row (or line). This opens a window in which you select to become the Current directory.3. Matrices with only one column or only one row are denoted vectors. Retrieving data from matrices Let us create a 2x2 matrix of name A. In general. which can be regarded as a table. and semicolon is used to separate rows. Execute the command exec script1.sce in the C:\temp directory which is different from the present Current Directory.) Creating matrices. The Current directory is the directory where Scilab looks for your script when you try to execute it with the exec command. Now the script should run without errors. Let us change the Current Directory to C:\temp.

2:1] Create a column vector.y] Get the size (dimensions) of matrix B: size(B) Get the first element of vector r. y=[4. last element 1 and increment (step size) 0. Note that 1 (not 0) is the first index! r(1) Now try r(0) You get an error because index number 0 can not be used. note the apostrophe to transpose: c=[0:0.5.2.6]'.Creating and displaying the matrix A Create a row vector with first element 0.2:1]' Create matrix B from given column vectors x and y: x=[1.2: r=[0:0. . B=[x.3]'.

2].4].2) Create a matrix of dimension 3x2 consisting of ones: D=ones(3.2) Some special matrices Create an identity matrix of dimension 2x2: C=eye(2. and assign the values to vector v: v=B(:.Get the second column in matrix B. As an example. to perform elemenwise multiplication of two vectors: v1=[1.2) Matrix calculations Matrices can be used in matrix calculations. v2=[3. Element z(1) is v1(1)*v2(1).2) Create a matrix of dimension 3x2 consisting of zeros: E=zeros(3. Add two matrices (assuming A and C have been created as explained above): F=A+C Multiply two matrices: G=A*C Take the inverse of a matrix: H=inv(A) Elementwise calculations Elementwise calculations are made with the dot operator. z=v1. and element z(2) is v1(2)*v2(2). .*v2 The result is shown in the figure below.

//y is a column vector Very basic plotting: scf(1). In the following are a number of examples demonstrating various plotting options.Elementwise multiplication of two vectors 6 Plotting The basic command for plotting data in a figure is plot. If you type the commands. you may omit the comments which is the text following the // sign to save time. //Opens (new) figure with ID 1. let us generate some data that will be used in the following examples. (It is of course up to you if you type the code from scratch on the command line or in Scipad.) First. (scf = set current fig) plot(y) //Plots y against y-indexes (integers along x-axis) .1:10]'. //t is a column vector u=-1+0. //u is a column vector y=sin(2*t). or if you just copy the code. t=[0:.2*t.

Below is shown the Scilab figure with the plot. Before we continue with more plotting commands. Along the x-axis are the indexes of the y vector. let us take a look at some buttons and menus in the Graphics window. see the figure below. Click the GED button in the figure window. . This opens the Clicking the GED button opens the Graphics Editor. The indexes are integers from 1 to 101.

add grid. You can produce various graphics files from the plot: Select the menu File / Export in the figure window. The various options will not be described here because it is rather easy to investigate the possibilities by yourself. .The Graphics Editor With the graphics editor you can change line colours. This will be shown in subsequent examples. Many of the options in the Graphics Editor can alternatively be set with options to the plot command. etc. line style. add labels to the axis.

However.g. //clears the figure plot(t.y) where it is of course assumed that vectors t and y have the same length (same number of elements). the new plot adds to the previous plot. as MS Word or Scientific Workplace. Assume that we will plot y against t in Figure 1 which is the same figure as we used above. you should select Enhanced Meta File (EMF). This is done with the command plot(t. To clear the previous plot. and plots in the same figure: scf(1). If you just use the plot command. //Clears a plot.This opens the Export dialog window shown below.y) //Plots in figure 1 The result is shown in the figure below. In this case you should select the GIF format (this format does not give you vectorized graphics). showing two (or more curves). this is not what you want. //Sets figure 1 to become current figure clf. The Export dialog in the figure window If you want to create a graphis file to put into a document processor. Typically. whch is a vectorized graphics format which means that the picture can be enlarged and still look sharp. EMF files can not be used in native web documents. we use the clf (clear figure) command before we use the plot command. We continue with looking at more options to the plot command. Observe that the x-axis now contains the t values. e. . in HTMLfiles to be displayed in a web browser.

) .ax1. To plot with with grid and plot title and labels: scf(1). title('Experiment 1') xlabel('t [s]') ylabel('y [V]') The resulting plot is shown in the figure below.0] is black. (gca=get current axes) //[0. [0.Suppose you want to show the plot in a new Figure 2 in stead of the previously opened Figure 1: scf(2).ax1.//Adds grid.0]. //Sets figure 2 to become current figure plot(t.0]. //Opens (new) figure with ID 1. you can in stead use the Graphics Editor (by clicking the GED button in the figure window). is too cumbersome for generating a grid.grid=[0.grid=[0. resp.y) //Plots in figure 1 To clear Figure 2: Just close the Figure 2 window by clicking the close button in the upper right corner of the window (or with the menu File / Close in the window). (scf = set current fig) plot(t.y) //Plots u against t ax1=gca().0] is colour code for x and y grid. (If you think the code ax1=gca().

u) //Plots y against t. . and u against t The result is shown in the figure below. //Opens figure 1 and clears its present content plot(t.t.To plot two curves in one plot (note the ordering of the vectors in the plot command): scf(1).clf.y.

'r--'.) The result is shown in the figure below.t.y.To plot two curves with options for the line style and colours: scf(1). //Opens and clears figure 1 plot(t. clf.u. (Many more options can be found from Help linespec.'b') //'r--' is red and dashes. . 'b' is blue.

y) The result is shown in the figure below. //Opens and clears figure 1 subplot(211) //Plot number 1 in a 2x1 "table" of plots plot(t. clf. .u) subplot(212) //Plot number 2 in the 2x1 "table" of plots plot(t.To plot several subplots in one figure: scf(1).

Subplots .