Scilab
Scilab is a Logiciel of numerical Calcul providing an environment of calculation for scientific applications. Developed since 1990 by researchers of INRIA and ENPC, it is, since the creation of Scilab Consortium in May 2003, developed and maintained by the INRIA.
The Syntaxe and the possibilities offered by Scilab are similar to those of Matlab, but the two software is not compatible even if a Matlab translator towards Scilab exists.
Distributed free and with its Source code via Internet since 1994, it available is precompiled for a great number of architectures. Nevertheless (contrary to what is alleged on its site) it is neither about a software Open source according to the Open Source Initiative nor of a Free software. Indeed, the license of Scilab does not authorize the commercial distribution of a modified version. According to the classification of FSF, it would be thus rather about a semi-free Logiciel.
Brief presentation of the use
Scilab can carry out instructions in line of order, as well as command files ( scripts ) containing instructions (format text). One can also carry out programs FORTRAN or C starting from Scilab. Scilab is supplemented by a graphic environment Scicos comparable with the graphic Environnement Simulink provided with Matlab.
Elementary syntax in line of order
Invites line of order ( prompt ) consists of a “ flèche ” : two indents and a higher sign --> . The instruction is typed then validated with the key of Carriage return ( Enter , Return ). The result is posted after, except if the line ends in a Point-virgule in which case the result is hidden. For example :
-->a = 1;
-->A = 2;
-->a + has
years =
3.
The first order associates the value “ 1 ” with the variable “ a ” (hidden result), the second order associates the value “ 2 ” with the variable “ A ” (syntax is sensitive to the breakage), the third order calculates the sum of both ( years is the abbreviation of answer which means “ réponse ” in English).
One can write several instructions on the same line by separating them from a Virgule, or a semicolon if one does not want to post the result of it. One can also put an instruction on several lines (for example for reasons of presentation) by putting three points “… ” before pressing the key of carriage return.
Scilab uses the traditional functions and operators (+, -, *, /, ^ or ** , sqrt () for the square Racine, cos () for the Cosinus, int () for the whole Partie, round () for the Arrondi with nearest, abs () for the absolute Value…) and some others (for example rand to have a random random number between 0 and 1). The function who posts the declared variables. The variable ans contains the last result.
The decimal Séparateur is the point. To enter the imaginary I , it is necessary to use %i ; it simply appears in the form “ i ” in the results. To enter infinite the ∞, it is necessary to use %inf ; it simply appears in the form “ inf ” in the results. The value of π is obtained by %pi, and the constant of Neper E by %e.
Boolean
When one assigns a Boolean value , one uses %t for “ vrai ” ( true ) and %f for “ faux ” ( false ). The result of the operation posted is respectively T or F.
The operator Or is noted “ | ” (tube), And are noted “ & ” (Esperluette), Non is noted “ ~ ” (Tilde). Example --> (%t & %f) | %f years = F.
Boolean can be also obtained by comparing two values, with the relations of equality == and difference < > or ~=, and the relations of order < , < =, > and > =.
Polynomials and rational fractions
It is here not of functions polynomials which would allow only their numerical evaluation, but about Polynôme S formal.
The declaration of a polynomial is done with two functions. The first function, x = poly (0, “X”) which defines “ x ” like the unspecified one of a polynomial, then the statement of the polynomial using this unspecified. Example
-->x = poly (0, “X”); p = X ^ 2 + 2 * X + 1
p =
2
1 + 2x + X
The function roots (p) gives the roots of the polynomial p , the function coeff (p) gives a matrix whose coefficients are the coefficients of the polynomial p . The function varn (p) returns the name of unspecified of p (here, X ).
In fact, the order x = poly (0, “X”) defines that X is the simplest polynomial whose unspecified one is the character “ x ” and the root is 0, i.e. the students' rag procession X . The order x = poly (2, “X”) defines in the same way the simplest polynomial having for root 2, i.e. X - 2.
This function makes it possible to define a polynomial having several roots, by using a matrix line instead of a simple number. For example, x=poly (2, “X”) defines the simplest polynomial having for roots 1 and 2, is the polynomial X
2
- 3 X + 2.
One can also use this function to generate a polynomial whose coefficients are contained in a matrix line, by adding the character ' c' at the end of the arguments. For example, x = poly ('' a1 '',…, '' year '', “X”, “it) defines the polynomial a0 + a1 · X +… + year · X
N
.
Scilab manages same manner the rational fractions. It is enough to declare a students' rag procession with poly () and to define the rational fraction then. Example
-->x = poly (0, “X”); F = (1 + X)/(1 - X)
F =
1 + X
-----
1 - X
-->1/f
years =
1 - X
-----
1 + X
To have the numerator of F , it is enough to type f.num ; the denominator of F is obtained with f.den.
The function derivat ( p ) gives the formal derivation of the polynomial or a rational fraction p . The function simp ( Q ) makes the simplification of the rational fraction Q .
The use of the polynomial or a rational fraction as a function is done by the function horner : horner (p, 2) calculates p (2).
Character
The isolated characters or the character strings are noted between simple apostrophes, or between guillemets : ' abc' or " abc" .
The apostrophe indicates the end of a chain; to put an apostrophe in a chain, it is necessary to write two of them coupled .
The + addition on character strings produces the Concaténation. A character string can be evaluated, i.e. transformed into Scilab order, by the function evstr () .
Matrices
Scilab was conceived for the matrix Algebra. All the functions apply to matrix s ; for example if M is a matrix, then log (M) will be the matrix whose coefficients are the logarithms of the coefficients of M . The elements of the matrices can be of any type ( Real number, Complex number, Boolean, Polynôme, rational Fraction, Character string…).
To define a matrix starting from his coefficients, one places them between two hooks . The elements of a line are separated from a space or from a comma, the lines are separated from a semicolon. With posting, the matrix is represented like a table framed by points of exclamation. For example :
-->
years =
! 1. 0. 0. !
! 0. 1. 0. !
! 0. 0. 1. !
The empty matrix is noted by . The expression M (I, J) indicates the element ( I , J ) of the matrix M . The character : (Colon) means “ all indices ”, for example M (1, :) is the first line of the matrix (it is a Vecteur line). The character $ (Dollar) indicates the last index (line or column) of a matrix.
The expression N1 : N2 makes it possible to generate a matrix-line whose first coefficient is N1 , the last is lower or equal to N2 , and the step between the coefficients is 1. The expression N1 : not : N2 makes it possible to generate a matrix-line by choosing the step. For example
-->1.1: 5.2
years =
! 1.1.2.1.3.1 4.1.5.1!
--> 1:2: 5
years =
! 1 3 5!
--> ' a' : “of
years =
abcd
The function zeros ( m , N ) creates a matrix m × N filled of 0 ; the function ones ( m , N ) creates a matrix m × N filled of 1. The function eye ( N , N ) creates a Matrice unit N × N . One can also pass a matrix M in parameter of these fonctions ; they create a of the same matrix then dimension than the matrix M . For example, M=zeros (M) puts all the coefficients of M at zero.
If F is an external function (i.e. by defined by deff or function, to see hereafter), and that X and is vectors there, then the function feval makes it possible to build a matrix Z = F ( X , there )
-
z = feval (X, there, F): there is Z ( I , J ) = F ( X ( I ), there ( J ))
The function size (M) returns the size of the matrix in the shape of a matrix 2×1 containing the number of lines then the number of columns.
Operations specific to the traditional matrices sont :
- the transposition : it is enough to put an apostrophe
'after the matrice ; - the product of matrices :
*; - produced and element report/ratio by élément :
.*and./; - the Produces tensorial
.*.; - the determinant of a square matrix M :
det ( M )(determ ()for a matrix of polynomials anddetr ()for a matrix of rational fraction) ; - the trace of a square matrix M :
trace ( M ); - the reverse of a invertible Matrix M :
inv ( M ).
A Vecteur is a matrix line or column (matrix 1 × N or N × 1) ; the expression V (I) indicates the component I vector V . If V1 and V2 is vectors columns, then the scalar Produit is V1' * V2 ; if they are vectors lines, the scalar product is V1 * V2'.
Numerical calculation
Scilab can carry out numerical calculations.
One can make of it a commonplace use as Calculatrice, while simply entering the operations to carry out.
The function deff makes it possible to define new functions (called “ functions externes ”) being able to express itself with the already preset operators (“ functions primitives ”). One passes two character strings like paramètre ; the first indicates the name of the function and the variables used in entry and exit, and the second indicates the formula. For example, the function
- F ( X ) = 2 · X
-
deff (“= F (X)”, “there = 2 * X”)
; Integration
-
integrate (calculates the integral of the function described by expression (it is an interpretable character string by Scilab, such as for example' expression' , “X”, x0, x1)' sin (X) '), X being the variable of integration, between the values x0 and x1 .
; Solution of equations
- Scilab has several primitives allowing the solution of equations (see Solveur of equations ), enter others:
-
linsolve: resolution of a System of linear equations,
syntax:=linsolve (has, b)
where has is the real matrix of the coefficients of the system of equations, and B a vector of constants; the solutions of has × X + B = 0 are form X 0 + W × kerA , W being an arbitrary reality; -
solve: symbolic system resolution of a linear system,
syntaxe:w = solve (has, b)
where has is a higher triangular matrix of characters (coefficients of the system of equation), B is a vector of characters (the part on the right of the signs equal), and W is the matrix result of has × W = B ; -
ode: resolution of one of differential equation ordinary ( ordinary differential equation ); if the differential equation is Dy / dt = ƒ ( T ), then ƒ having been defined (external function), syntax to determine ( T ) isy there = ode (y0, t0, T, F)
where there 0 and T 0 are the initial values of the system, T is a vector of values for which one calculates the solutions, and is the vector of solutions there (plot (T, there)makes it possible to plot the graph of the solutions);
the primitiveodeadmits arguments making it possible to solve specific situations:' roots'to add an equation G ( T , there ) = 0,' discrete'to calculate in a recursive way there ( K +1) =ƒ ( K , there ( K )) starting from an initial state there ( K 0).
Layout of function
Layout 2D
The layout of a function is made in two stages
- define the extent of the variable X-coordinate and the step, in the shape of a vector column, for example
x = '; - to trace the function with the order
plot (X, F (X))if ƒ is the function.
plot (X, '' f2 '' (X) '' f3 '' (X)).
One notes in fact that ƒ ( X ) is itself a vector. One can in a general way define a vector of the values of X and a vector of the values of there , and the function plot (X, there) will trace the group of dots.
If ƒ is an external function (for example definite with deff, to see above, or function, to see hereafter), then one can trace directly the function with fplot2d (X, F) . One can also define there by y = feval (X, F) , then to trace with plot (X, there) .
The function xgrid makes it possible to post a grid corresponding to the graduations.
The functions plot2d I , used in the place of plot, make it possible to vary the general appearance of the tracé :
-
plot2d: feature “ normal ” ; identical toplot, but the use of marqueurs  allows; ; -
plot2d2: feature in escalier ; -
plot2d3: layout in barres ; -
plot2d4: layout in “ flèches ” (for a field of vectors) ;
These functions plot2d I accept arguments modifying the layout, in the form plot2d I (X, there, arguments ) . The arguments are form keyword = value , and are separated by commas.
To place markers, one uses the argument style = N or N is a positive or negative entirety (for example plot2d (X, there, style = 1) ) ; a negative number replaces the points by markers (a star for -10, from the rounds for -9,…, from the small points for 0, the list is obtained by typing the order getsymbol) and a positive number indicates a feature full but with given color (according to the chart of defined color, to see hereafter).
If the layout comprises several curves (thus put in a matrix line Y ), the argument style is a matrix, the value of each element indicating the style of each curve, for example
-
Y = sin (X); plot2d (X, Y, style = -1)
leg = (' texte1 @ texte2 ' ) , the arobase being used to separate the legends, for example -
plot2d (X, Y, style = -1, leg = “cosinus@sinus”)
For a logarithmic scale, one uses the argument logflag = standard where standard is a chain of two characters, “ n ” (for normal) or “ l ” (for logarithmic curve), first character corresponding to the axis of the X and the second with the axis of the there . For example
-
plot2d (X, there, logflag = “nl”)for an axis of the X linear and an axis of the there logarithmique ; -
plot2d (X, there, logflag = “it)to have two logarithmic scales.
One can choose the type of axis with the argument axesflag= N where N is an entirety positif : 0 not to have from axis, 1 for axes “ classiques ” recutting itself in bottom on the right, 4 for axes “ classiques ” cutting itself in the center, 5 for axes cutting itself in (0, 0)…
One can define the number of graduations and under-graduations of the axes with the argument nax = '' Nx '', '' ny '', '' Ny '' where Nx is the number of graduations of the axis X , nx the number of under-graduations…
The argument rect = '' ymin '', '' xmax '', '' ymax '' makes it possible to limit the layout to the zone included/understood in the rectangle defined by the values in the matrix.
The function polarplot carries out a layout in polar Coordonnées. The function R ( theta ) is traced by the order:
-
polarplot (R, theta)
Chart of a field
If X is a vector column of m elements, there a vector column of N elements and Z a matrix m × N , then the function
-
grayplot (X, there, Z)
The levels of color are indicated by the function
-
xset (“colormap”, cmap )
colormap : -
graycolormap ( N ), where N is an entirety, generates N levels of gray between the black and the blanc ;jetcolormap ( N ), where N is an entirety, generates N levels of color between blue and the rouge ;hotcolormap ( N ), where N is an entirety, generates N levels of color between the red and the yellow.
xset (“colormap”, graycolormap (32)) to have 32 levels of gray. One can carry out a range of the black towards the red with -
cmap = graycolormap (32); cmap (: , 2:3) = 0or withr =/31:32 '/32; cmap = zeros (32, 2)
-
cmap = graycolormap (32); cmap (: , 2:3) = 1or withr =/31:32 '/32; cmap = ones (32, 2)
get (sdf (), “color_map”) .
One can plot a level line with the function
-
contour2d (X, there, Z, N )
-
contour2d (X, there, Z,)
One can also trace a field of vectors. For that, one needs a matrix vx having the components according to X field of vector, a matrix vy having the components according to there of this field, and to use the function
-
champ (X, there, vx, vy)
champ1, the traced vectors have all the same length, the standard of the field is indicated by the color of the vector, according to the principle exposed for grayplot.
Layout 3D
Scilab also allows the layout of surfaces three dimensions ( X , there , Z ). If X is a matrix column of m elements, there a matrix column of N elements, and Z a matrix m × N , then the function
-
plot3d (X, there, Z)
- Z ( I , J ) = F ( X ( I ), there ( J )).
deff or function), one can define Z with the function feval (see higher), or use fplot3d (X, there, F) .
The function param3d (fx (T), fy (T), fz (T)) trace the curve paramétrique ; T is a vector containing the successive values of the parameter.
The functions plot3d1, fplot3d1 and param3d1 make it possible to use levels of colors to indicate the value of Z . The use is identical to that of plot3d, but the levels of color are indicated by the function
-
xset (“colormap”, cmap )
The point of view of surface 3D is defined by two angles in degrees, θ, rotation around the axis of Z , and α, rotation around the axis of the Y (see also Angles of Euler ) : plot3d (X, there, Z, theta , alpha ) , param3d (X, there, Z, theta , alpha ) … ;
-
plot3d (X, there, Z, 0,0)gives a sight of top, similar to the charts 2D, projection of surface on the plan XY according to the axis Z ; -
plot3d (X, there, Z, 90,0)gives a front view, projection of surface on the plan XZ according to the axis Y ; -
plot3d (X, there, Z, 0,90)gives a side view, projection of surface on the plan YZ according to the axis X ; -
plot3d (X, there, Z, 45,45)is the sight by defect, of isometric type Perspective.
ebox: -
plot3d (X, there, Z, ebox= xmax, ymin, ymax, zmin, zmax) -
plot3d (X, there, Z, theta, alpha, ebox= xmax, ymin, ymax, zmin, zmax)
Representation of statistical data
If X is a vector, the function histplot ( N , X ) , N being an entirety, will cut out the interval of values taken by the coefficients of X in N sections of equal width, and will trace the histogram of distribution of the values according to these sections. If N is a vector whose coefficients are strictly increasing, the values of the coefficients of N are used to determine the sections.
If X is a matrix, hist3d ( X ) trace a histogram 3D such as the parallelepiped located in ( I , J ) has as a height X ( I , J ). As for all the functions of layout in three dimensions, one can define the visual angle with θ and α.
Drawing
It is possible to directly draw geometrical figures on the graph.
The function xpoly (X, there, “lines”, 1) makes it possible to trace a Polygone closed, x being the list of the X-coordinates of the tops (in the shape of a vector or a matrix) and y being the list of the ordinates of the tops (it is a of the same matrix dimension than x). The function xfpoly (X, there, “lines”, 1) makes it possible to trace a polygon filled.
The function xrect (X, there, L, H) trace a rectangle whose point in top on the left is ( X , there ), of width L and height H . The function xfrect (X, there, L, H) trace a rectangle filled.
The function xstring ( X , there , writes the character string chain on the graph, the point in bottom on the left of the text being at the point ( X , there ).
The function xarc (X, there, L, H, a1, a2) trace an arc of ellipse included/understood in the rectangle whose point in top on the left is ( X , there ), of width L and height H . Each degree of angle is divided into 64 sectors, the arc leaves the trigonometrical Angle a1 ×64 ° and goes until the angle ( a1 + a2 ) ×64 °. The function xfarc (X, there, L, H) trace an arc of ellipse filled.
To change the properties of the features of the drawings, the order set should be used (. The principal properties of an object are:
-
line_style: type of feature; it is an entirety, 0 for a full feature, 2 for long dotted lines, three for short dotted lines, 4 for a feature of axis (alternation feature-point)… -
thickness: thickness of the feature, in the form of a positive entirety; -
mark_size: in the case of a layout by markers, size of the markers.
= get (“hdl”) hdl meaning handle (literally “handle”, intermediary of action). To define the property of this element, one makes For example xarc (0,1,0.5, 0.5, 0,360*64) // ellipse set (“line_style”, 2) // dotted lines or xarc (0,1,0.5, 0.5, 0,360*64) // ellipse a=get (“hdl”) a.line_style=2 // dotted lines
Working, axes and fenestration
The function xset makes it possible to define the apparence :
- police force of the graduations and titers :
xset (“make”, standard , size ), where - size is a number indicating the size of the characters, in unit arbitraire ;
- standard is a number indicating the type of police : 1 for the Greek letters (Symbol police force), 2-5 for a police force with footing S standard Times (2 for letter Romance, 3 in italic, 4 in fat, 5 in Italic fat) ; 6-9 for a police force without standard footing Arial (6 for Roman letters, 7 in italic, 8 in fat, 9 in Italic fat) ;
- thickness of the features of axis and the tracé :
xset (“thickness”, E )where E is a number indicating the thickness in unit arbitraire ; - size of the markers (if necessary, to see hereafter) with
xset (“mark size”, size ).
VAr =get (“current_axes”) . The principal properties are: -
x_locationandy_location: take the values' top',' middle'or' bottom'according to whether it is wanted that the axis either in top, passes by the origin, or or in bottom; -
isoview:' on'is worth if that the scales are identical on the axes,' off'is wanted if the scale is free; -
sub_ticks: stampwhere nx and ny is the nombr ede under-graduation (it is necessary 4 under-graduations so that the principal graduation is divided into 5);
One can place several layouts side by side with the instruction subplot :
-
subplot ( m , N , I )place the layout which follows the instruction in the I e box (in the European reading order) of a table m × N ; the layout can comprise several instructionsplot,xset,xtitle.
The function xtitle ( puts a title at the graph and the axis. The function titlepage ( puts a title in the middle of the graph. The function clf erases the chart window.
Example
Here the graph which one obtains with the instructions below.
clear; clf; deff (“= F (X, there)”, “Z = sin (X) * cos (there)”) xset (“colormap”, jetcolormap (32)) X = %pi * '; there = X; Z = feval (X, there, F); subplot (2, 2,1) grayplot (X, there, Z) subplot (2, 2,2) contour2d (X, there, Z, 5) subplot (2, 2,3) plot3d1 (X, there, Z, 0,90) subplot (2, 2,4) plot3d1 (X, there, Z, 70,70)
Programming
Scilab accepts a certain number of instructions :
- conditional execution
if condition then,…, else,…, end - buckles iterative
for variable = beginning : fine ,…, endorfor variable = beginning : not : fine ,…, end - buckles iterative antéconditionnée :
while condition C,…, endorwhile condition C,…, else,…, end
It is possible to define functions with passage of parameters. The function is a subroutine with its own variables, and which can contain loops, conditional connections… For example, to define a function F (X) : -->function = F (X) -->… --> endfunction The parameter X passed to the function F ; the value turned over by F will be that which is allotted to there in the function.
One can record the environment, i.e. the whole of the variables, of which in particular functions, with the order save (. This creates a binary file (Scilab does not add extension). This environment can be reloaded with the function load (.
One can also write the instructions in a textual file, then to make copy/to stick since the text editor towards the line of ordering of Scilab. The source code can contain comments introduced by two fraction lines //.
Environment
In Scilab, the environment is the whole of the variables (including the functions) which were définies ; the environment contains a certain number of preset variables. The whole of the variables is given by the order who.
When a function is used, the execution of the function is done in a new environment, copies initial environment. The variables which are modified there it are not in the initial environment (one works only in local variables), only the result of the function is returned.
One can open an environment intentionally, with the function pause ; invites becomes -1-> then, indicating that one is in the first level of under-environment. One is also in such a configuration when the execution of a function is stopped (for example with the combination of keys CTRL+C). To return to the preceding environment, the order return should be used.
If one uses return without argument, the variables are not transmitted to the environment father. It is possible to transmit the variables a1 , a2 ,…, year in the environment father, with syntax ( x1 , x2 ,…, xn ) =return ( a1 , a2 ,…, year ) , where x1 , x2 ,…, xn is the names of the variables in the environment father.
The order clear allows “ nettoyer ” an environment, i.e. to remove all the variables.
See too
Similar software
- PsiLAB, homonym (in English pronociation) and also intended for mathematical calculations, data processing and graphic posting
- AMESim
- MatLab
- GNU Octave
- Sysquake
External bonds
-
Official site of SciLab
- Man Scilab (manual on line)
- Gallery on Commons
- Forum comp.soft-sys.math.scilab (to put questions in English about Scilab)
| Random links: | Rene Genis | Obliged instrument | Sobekhotep Ier | Soiron | Formulate 3 Euroseries 2005 | L'eau_pleurante,_Nébraska |