You are on page 1of 18

A Hands on Introduction

Scilab

by

Satish Annigeri Ph.D.


Professor of Civil Engineering B.V. Bhoomaraddi College of Engineering & Technology, Hubli
satish@bvb.edu

Department of Civil Engineering B.V. Bhoomaraddi College of Engineering & echnolog!" Hubli
#$ & #% April" &''(

Table of Contents
Preface.............................................................................................................................................ii Introduction..................................................................................................................................... 1 Tutorial 1 Tutorial # Tutorial ( Tutorial , Tutorial . Tutorial / Tutorial 1 Tutorial 3 Tutorial 5 !cilab En"ironment..................................................................................................... # The $or%&'ace and $or%ing Directory......................................................................( )atri* +'eration&....................................................................................................... , !ub-matrice&................................................................................................................. !tati&tic&...................................................................................................................... / Plotting 0ra'h&............................................................................................................1 !cilab Programming 2anguage................................................................................... 3 4unction& in !cilab...................................................................................................... 5 )i&cellaneou& Command&.........................................................................................16

7''endi*....................................................................................................................................... 11

Scilab Tutorial

Preface
!cilab i& a &oft8are for numerical mathematic& and &cientific "i&uali9ation. It i& ca'able of interacti"e calculation& a& 8ell a& automation of com'utation& through 'rogramming. It 'ro"ide& all ba&ic o'eration& on matrice& through built-in function& &o that the trouble of de"elo'ing and te&ting code for ba&ic o'eration& are com'letely a"oided. It& ability to 'lot #D and (D gra'h& hel'& in "i&uali9ing the data 8e 8or% 8ith. 7ll the&e ma%e !cilab an e*cellent tool for teaching, e&'ecially tho&e &ub:ect& that in"ol"e matri* o'eration&. 4urther, the numerou& toolbo*e& that are a"ailable for "ariou& &'eciali9ed a''lication& ma%e it an im'ortant tool for re&earch. Being com'atible 8ith )atlab;, all a"ailable )atlab )-file& can be directly u&ed in !cilab. !cico&, a hybrid dynamic &y&tem& modeler and &imulator for !cilab, &im'lifie& &imulation&. The greate&t feature& of !cilab are that it i& multi-'latform and i& free. It i& a"ailable for many o'erating &y&tem& including $indo8&, 2inu* and )ac+! <. )ore information about the feature& of !cilab are gi"en in the Introduction. !cilab can hel' a &tudent under&tand all intermediate &te'& in &ol"ing e"en com'licated 'roblem&, a& ea&ily a& u&ing a calculator. In fact, it i& a calculator that i& ca'able of matri* algebra com'utation&. +nce the &tudent i& &ure of ha"ing ma&tered the &te'&, they can be con"erted into function& and 8hole 'roblem& can be &ol"ed by &im'ly calling a fe8 function&. !cilab i& an in"aluable tool a& &ol"ed 'roblem& need not be re&tricted to &im'le e*am'le& to &uit hand calculation&. !cilab i& the outcome of year& of de"elo'ment and continue& to be im'ro"ed and de"elo'ed. Ha"ing a rich &et of feature& and being in 8ide u&e, it& de"elo'er& could "ery 8ell ha"e cho&en to commerciali9e it. But they ha"e cho&en to ma%e it a =free= &oft8are. 4ree, a& in =free of co&t= a& 8ell a& in =freedom=, becau&e the &ource code i& al&o a"ailable for tho&e 8ho 8i&h to modify and im'ro"e it. >ou can "i&it the follo8ing 8eb&ite& to &ee &ome definition& of &oft8are freedom and licen&ing i&&ue&? http://www.fsf.org/licenses/licenses.html and +'en !ource Initiati"e @http://www.opensource.org/licenses >ou can al&o read the !cilab &oft8are licen&e at the follo8ing 8eb&ite?
http://scilabsoft.inria.fr/license.txt

The !cilab licen&e i& included in the 7''endi* at the end of thi& document. $hen it& de"elo'er& ha"e been &o generou&, 8e a& u&er& mu&t contribute to thi& mo"ement by learning to u&e it and a''lying it to &ol"e 'roblem&. Thi& tutorial i& an attem't to introduce &tudent& to the ba&ic& of !cilab. The ne*t 'art of the tutorial i& aimed at teaching &tudent& of Ci"il Engineering to the ba&ic& of !cilab by a''lying it to the 'roblem of matri* analy&i& of 'lane frame&. I ho'e thi& moti"ate& &tudent& to learn and a''ly !cilab to &ol"e a 8ider range of 'roblem&. Thi& i& the fir&t "er&ion of thi& document and 8ill certainly contain error&, ty'ogra'hical a& 8ell a& factual. >ou can hel' im'ro"e thi& document by re'orting all error& you find and by &ugge&ting modification& and addition&. >our "ie8& are al8ay& 8elcome. I can be reached at the email addre&& gi"en on the co"er 'age. Acknowledgments It goe& 8ithout &aying that my fir&t indebtedne&& i& to the de"elo'er& of !cilab and the con&ortium that continue& to de"elo' it. I mu&t al&o than% Dr. 7.B. Aa:u, E&EE De'artment, BVBCET 8ho fir&t introduced me to !cilab and fore"er freed me from u&ing )atlab. 7'ril #66, Satish Annigeri

Scilab Tutorial

ii

Introduction
!cilab i& a &cientific &oft8are 'ac%age for numerical com'utation& 'ro"iding a 'o8erful o'en com'uting en"ironment for engineering and &cientific a''lication&. De"elo'ed &ince 1556 by re&earcher& from IBAI7 @4rench Bational In&titute for Ae&earch in Com'uter !cience and Control, http://www.inria.fr/index.en.htmlC and EBPC @Bational !chool of Bridge& and Aoad&, http://www.enpc.fr/english/int_index.htmC, it i& no8 maintained and de"elo'ed by !cilab Con&ortium @http://scilabsoft.inria.fr/consortium/consortium.htmlC &ince it& creation in )ay #66(. Di&tributed freely and o'en &ource through the Internet &ince 155,, !cilab i& currently being u&ed in educational and indu&trial en"ironment& around the 8orld. !cilab include& hundred& of mathematical function& 8ith the 'o&&ibility to add interacti"ely 'rogram& from "ariou& language& @C, 4ortran...C. It ha& &o'hi&ticated data &tructure& @including li&t&, 'olynomial&, rational function&, linear &y&tem&...C, an inter'reter and a high le"el 'rogramming language. !cilab ha& been de&igned to be an o'en &y&tem 8here the u&er can define ne8 data ty'e& and o'eration& on the&e data ty'e& by u&ing o"erloading. 7 number of toolbo*e& are a"ailable 8ith the &y&tem? #-D and (-D gra'hic&, animation 2inear algebra, &'ar&e matrice& Polynomial& and rational function& !imulation? +DE &ol"er and D7E &ol"er !cico&? a hybrid dynamic &y&tem& modeler and &imulator Cla&&ic and robu&t control, 2)I o'timi9ation Differentiable and non-differentiable o'timi9ation !ignal 'roce&&ing )etanet? gra'h& and net8or%& Parallel !cilab u&ing PV) !tati&tic& Interface 8ith Com'uter 7lgebra @)a'le, )uP7DC Interface 8ith TclDT% 7nd a large number of contribution& for "ariou& domain&. !cilab 8or%& on mo&t Eni* &y&tem& including 0BED2inu* and on $indo8& 5<DBTD#666D<P. It come& 8ith &ource code, on-line hel' and Engli&h u&er manual&. Binary "er&ion& are a"ailable. !ome of it& feature& are li&ted belo8?

Ba&ic data ty'e i& a matri*, and all matri* o'eration& are a"ailable a& built-in o'eration&. Ha& a built-in inter'reted high-le"el 'rogramming language. 0ra'hic& &uch a& #D and (D gra'h& can be generated and e*'orted to "ariou& format& &o that they can be included into document&. To the left i& a (D gra'h generated in !cilab and e*'orted to 0I4 format and included in the document for 're&entation. !cilab can e*'ort to Po&t&cri't and 0I4 format& a& 8ell a& to <fig @'o'ular free &oft8are for dra8ing figure&C and 2aTe< @free &cientific document 're'aration &y&temC file format&.

Scilab Tutorial

Introduction

Tutorial 1 Scilab Environment

Fig. 1 Scilab environment

$hen you &tartu' !cilab, you &ee a 8indo8 a& &ho8n in 4ig. 1 abo"e. The u&er enter& !cilab command& at the 'rom't @-->C. But many of the command& are al&o a"ailable through the menu at the to'. The mo&t im'ortant menu for a beginner i& the GHel'H menu. Clic%ing on the GHel'H menu o'en& a hel' 8indo8 8ith a li&t of to'ic& on 8hich hel' i& a"ailable. Clic%ing on the rele"ant to'ic ta%e& you into a hy'erte*t lin%ed document& &imilar to 8eb 'age&. Hel' on &'ecific command& can al&o be acce&&ed directly from the command line in&tead of ha"ing to na"igate through a &erie& of lin%&. Thu&, to get hel' on the !cilab command Gin"H, &im'ly ty'e
-->help inv

on the command line. !cilab can be u&ed a& a &im'le calculator to 'erform numerical calculation&. It al&o ha& the ability to define "ariable& and &tore "alue& in them &o that they can be u&ed later. Thi& i& demon&trated in the follo8ing e*am'le&?
-->2+3 ans !. -->2/3 ans .""""""# -->2$3 ans %. -->a 2 a 2. -->b 3. b 3. -->c a+b c !. -->pi atan&'.()*+ pi 3.'+'!,2# -->sin&pi/+) ans (.#(#'("% -->exp&(.') ans '.'(!'#(,

Bote that !cilab create& a "ariable named GansH to &tore re&ult& of calculation& 8hene"er the u&er doe& not &u''ly a "ariable for the 'ur'o&e. >ou could enter more than one command on the &ame line by &e'arating the command& by &emicolon&@IC. The &emicolon &u''re&&e& echoing of intermediate re&ult&. Try the command -->a=5; and you 8ill notice that the 'rom't rea''ear& immediately 8ithout echoing a=5.

Scilab Tutorial

Tutorial 1 Scilab Environment

Tutorial 2 The Workspace and Working

irector!

$hile the !cilab en"ironment i& the "i&ible face of !cilab, there i& another that i& not "i&ible. It i& the memory &'ace 8here all "ariable& and function& are &tored, and i& called the Workspace. )any a time& it i& nece&&ary to in&'ect the 8or%&'ace to chec% 8hether a "ariable or a function ha& been defined or not. The follo8ing command& hel' the u&er in in&'ecting the memory &'ace? who , whos and who_user&). E&e the online hel' to learn more about the&e command&. The who command li&t& the name& of "ariable& in the !cilab 8or%&'ace. Bote the "ariable name& 'receded by the GJH &ymbol. The&e are &'ecial "ariable& that are u&ed often and therefore 'redefined by !cilab. It include& J'i @ C, Je @ e C, Ji @ 1 C, Jinf @ C, Jnan @BaBC and other&. The whos command li&t& the "ariable& along 8ith the amount of memory they ta%e u' in the 8or%&'ace. The "ariable& to be li&ted can be &elected ba&ed on either their ty'e or name. !ome e*am'le& are?
-->whos&)

2i&t& entire content& of the 8or%&'ace, including function&, librarie&, con&tant& 2i&t& only "ariable& that can &tore real or com'le* con&tant&. +ther ty'e& are boolean, &tring, function, library, 'olynomial etc. 4or a com'lete li&t u&e the command -->help t-peof. 2i&t& all "ariable& 8ho&e name begin& 8ith the letter& nam

-->whos -t-pe constants -->whos -name nam

To under&tand ho8 !cilab deal& 8ith number&, try out the follo8ing command& and u&e the
whos command a& follo8&?
-->a' !. -->a2 s0rt&-+) -->a3 1'2 2. 32 +3 -->whos -name a 4ame a3 a2 a' 5-pe constant constant constant

Define& a real number "ariable 8ith name =a'/ Define& a com'le* number "ariable 8ith name =a2/ Define& a #*# matri* 8ith name =a3= 2i&t& all "ariable& 8ith name &tarting 8ith the letter =a=
6i7e 2 b- 2 ' b- ' ' b- ' 8-tes +% 32 2+

Bo8 try the follo8ing command&?


-->a' s0rt&-,) -->whos -name a -->a' a3 -->whos -name a -->save&/ex('.dat/) -->load&/ex('.dat/)

Con"ert& =a'= to a com'le* number Bote that =a= i& no8 a com'le* number Con"ert& =a'= to a matri* Bote that =a= i& no8 a matri* !a"e& all "ariable& in the 8or%&'ace to a di&% file ex('.dat 2oad& all "ariable& from a di&% file ex('.dat to 8or%&'ace

Bote the follo8ing 'oint&? !cilab treat& a &calar number a& a matri* of &i9e 1*1 @and not a& a &im'le numberC becau&e the ba&ic data ty'e in !cilab i& a matri*. !cilab automatically con"ert& the ty'e of the "ariable a& the &ituation demand&. There i& no need to &'ecifically define the ty'e for the "ariable.

Scilab Tutorial

Tutorial 2 The Workspace and Working Director

Tutorial " #atri$ %perations


)atri* o'eration& that are built-in into !cilab are addition, &ubtraction, multi'lication, tran&'o&e, in"er&ion, determinant, trigonometric, logarithmic, e*'onential function& and many other&. !tudy the follo8ing e*am'le&?
-->a 1' 2 3. + ! ". # % ,3. -->b a/. -->c a+b -->d a-b -->e a*b -->f 13 ' 2. ' ! 3. 2 3 "3. -->g inv&f) -->f*g -->det&f) -->log&a) -->a .* b -->a$2 -->a .$2

Define a (*( matri* Tran&'o&e a and &tore it in b. 7dd a to b and &tore the re&ult in c. a and b mu&t be of the &ame &i9e. !ubtract b from a and &tore the re&ult in d. )ulti'ly a 8ith b and &tore the re&ult in e. a and b mu&t be com'tible for matri* multi'lication. Define a (*( matri* 8ith name f. In"ert matri* f and &tore the re&ult in g. f mu&t be &Kuare and 'o&iti"e definite. 7 8arning 8ill be di&'layed if it i& ill conditioned. The an&8er mu&t be an identity matri* Determinant of f. )atri* of log of each element of a. Element by element multi'lication. !ame a& a*a. Element by element &Kuare.

There are &ome handy function& to generate commonly u&ed matrice&, &uch a& 9ero matrice&, identity matrice& etc.
-->a 7eros&!2%) -->b ones&+2") -->c e-e&323) -->d e-e&323)*'(

Create& a .*3 matri* 8ith all element& 9ero. Create& a ,*/ matri* 8ith all element& 1 Create& a (*( identity matri* Create& a (*( diagonal matri*

It i& 'o&&ible to generate a range of number& to form a "ector. !tudy the follo8ing command?
-->a 1':!3 -->b 1(:(.!:!3

Create& a "ector 8ith . element& a& follo8& L1, #, (, ,, .M Create& a "ector 8ith 11 element& a& follo8& L6, 6.., 1.6, 1.., ... ,.., ..6M

7 range reKuire& a &tart "alue, an increment and an ending "alue, &e'arated by colon& @?C. If only t8o "alue& are gi"en @&e'arated by only one colonC, they are ta%en to be the &tart and end "alue& and incremented i& a&&umed to be 1. >ou can create an em'ty matri* 8ith the command a 13.

Scilab Tutorial

Tutorial ! "atri# $perations

Tutorial & Sub'matrices


7 &ub-matri* can be identified by the ro8 and column number& at 8hich it &tart& and end&. 2et u& fir&t create a matri* of &i9e .*3.
-->a rand&!2%)*'((

0enerate& a .*3 matri* 8ho&e element& are generated a& random number&.

!ince the element& are random number&, each 'er&on 8ill get a different matri*. 2et u& a&&ume 8e 8i&h to identify a #*, &ub-matri* of =a= demarcated by ro8& ( to , and column& # to .. Thi& i& obtained a& a&3:+2 2:!). The range of ro8& and column& i& re're&ented by the range command& (?, and #?. re&'ecti"ely. Thu& (?, define& the range (, , 8hile #?. define& the range #, (, ,, .. Ho8e"er, matri* =a= remain& unaffected.
-->b a&3:+2 2:!)

Thi& command co'ie& the &ub-matri* of into =b=.

7 &ub-matri* can be o"er8ritten :u&t a& ea&ily a& it can be co'ied. To ma%e all element& of the &ub-matri* bet8een the abo"e range eKual to 9ero, u&e the follo8ing command?
-->a&3:+2 2:!) 7eros&22+)

Thi& command create& a #*, matri* of 9ero& and 'ut& it into the &ub-matri* of =a= bet8een ro8& (?, and column& #?..

Bote that the &ub-matri* on the left hand &ide and the matri* on the right &ide @a 9ero matri* in the abo"e e*am'leC mu&t be of the &ame &i9e. $hile u&ing range to demarcate ro8& andDor column&, it i& 'ermitted to lea"e out the &tart @or endC "alue in the range, in 8hich ca&e it i& a&&umed to be 1 @or the number of the la&t ro8 or columnC. To indicate all ro8& @or column&C it i& enough to u&e only the colon @?C. Thu&, the &ubmatri* con&i&ting of all the ro8& and column& # and ( of a, the command i& a&:2 2:3). Baturally a&:2 :) re're&ent& the 8hole matri*, 8hich of cour&e could be re're&ented &im'ly a& a.

Scilab Tutorial

Tutorial % Sub&matrices

'

Tutorial ( Statistics
!cilab can 'erform all ba&ic &tati&tical calculation&. The data i& a&&umed to be contained in a matri* and calculation& can be 'erformed treating ro8& @or column&C a& the ob&er"ation& and the column& @or ro8&C a& the 'arameter&. To choo&e ro8& a& the ob&er"ation&, the indicator i& =r= or 1. To choo&e column& a& the ob&er"ation&, the indicator i& =c= or #. If no indicator i& furni&hed, the o'eration i& a''lied to the entire matri* element by element. The a"ailable &tati&tical function& are sum&), mean&), stdev&), st_deviation&), median&). 2et u& fir&t generate a matri* of . ob&er"ation& on ( 'arameter&. 2et the element& be random number&. Thi& i& done u&ing the follo8ing command?
-->a rand&!23)

Create& a .*( matri* of random number& .

7&&uming ro8& to be ob&er"ation& and column& to be 'arameter&, the &um, mean and &tandard de"iation are calculated a& follo8&?
-->s sum&a2 /r/) -->m mean&a2') -->sd stdev&a2 ') -->sd2 st_deviation&a2 /r/) -->mdn median&a2/r/)

!um of column& of a. )ean "alue of each column of a. !tandard de"iation of a. !tandard de"iation of a. !am'le &i9e &td. )edian of column& of a.

The &ame o'eration& can be 'erformed treating column& a& ob&er"ation& by re'lacing the =r= or 1 8ith =c= or #. $hen neither =r= @or 1C nor =c= @or #C i& &u''lied, the o'eration& are carried out treating the entire matri* a& a &et of ob&er"ation& on a &ingle 'arameter. The ma*imum and minimum "alue& in a column, ro8 or matri* can be obtained 8ith the max&) and min&) function& re&'ecti"ely in the &ame 8ay a& the abo"e &tati&tical function&, e*ce't that you mu&t u&e =r= or =c= but not 1 or #.

Scilab Tutorial

Tutorial ' Statistics

Tutorial ) Plotting *raphs


2et u& learn to 'lot &im'le gra'h&. $e fir&t ha"e to generate the data to be u&ed for the gra'h. 2et u& a&&ume 8e 8ant to dra8 the gra'h of co& x and &in x for one full cycle @ # radian&C. 2et u& fir&t generate the "alue& for the *-a*i& 8ith the follo8ing command?
-->x 1(:9pi/'":2*9pi3.

In the abo"e command, note that 9pi i& a 'redefined con&tant re're&enting the "alue of . The command to create a range of "alue&, (:9pi/'":2*9pi, reKuire& a &tarting "alue, an increment and an ending "alue. In the abo"e e*am'le, they are 6, / 1/ and # re&'ecti"ely. The increment i& o'tional and 8hen not gi"en, it i& ta%en to be 1. Thu&, =x= i& a "ector 8ith (( element&. Be*t, let u& create the "alue& for the y-a*i&, fir&t column re're&enting co&ine and the &econd &ine. They are created by the follo8ing command&?
-->- 1cos&x) sin&x)3

Bote that cos&x) and sin&x) are the t8o column& of a ne8 matri* 8hich i& fir&t created and then &tored in y. $e can no8 'lot the gra'h 8ith the command?
-->plot2d&x2-)

The gra'h generated by thi& command i& &ho8n belo8. The gra'h can be enhanced and annotated. >ou can add grid line&, label& for *- and y-a*e&, legend for the different line& etc.

Fig. 2 Graph of sin(x) an cos(x) !sing f!nction plot2d()

>ou can learn more about the plot2d and other related function& from the online hel'.

Scilab Tutorial

Tutorial ( )lotting *raphs

Tutorial + Scilab Programming ,anguage


!cilab ha& a built-in inter'reted 'rogramming language &o that a &erie& of command& can be automated. The 'rogramming language offer& mo&t of the feature& of any high le"el language, &uch a& loo'ing @for, 8hileC, conditional e*ecution @if-then-el&e, &electC and function&. The greate&t ad"antage i& that the &tatement& can be any "alid !cilab command&. To loo' o"er an inde* "ariable =i= from 1 to 16 and di&'lay it& "alue each time, the follo8ing loo' you can try the follo8ing command& at the 'rom't?
-->for i ':'( -->disp&i) -->end

The for loo' i& clo&ed by the corre&'onding end &tatement. +nce the loo' i& clo&ed, the bloc% of &tatement& enclo&ed 8ithin the loo' 8ill be e*ecuted. The disp&i) command di&'lay& the "alue of i. Conditional e*ecution i& 'erformed u&ing the if-then-elseif-else con&truct. Try the follo8ing &tatement& on the command line?
-->x '(. -->if x:( then disp&/4egative/) -->elseif x ( then disp&/;ero/) -->else disp&/<ositive/) -->end <ositive

Thi& 8ill di&'lay the 8ord <ositive. 7 li&t of all the !cilab 'rogramming language 'rimiti"e& and command& can be di&'layed by the command what&). The command 'roduce& the follo8ing li&t?
if exit then else return do for while help what apropos abort end who brea= select pause elseif case clear 0uit resume

>ou can learn about each command u&ing the hel' command. Thu& help while 8ill gi"e com'lete information about while along 8ith e*am'le& and other command& that are related to it.

Scilab Tutorial

Tutorial + Scilab )rogramming ,anguage

Tutorial - .unctions in Scilab


4unction& &er"e the &ame 'ur'o&e in !cilab a& in other 'rogramming language&. They are inde'endent bloc%& of code, 8ith their o8n in'ut and out'ut 'arameter& that can be a&&ociated 8ith "ariable& at the time of calling the function. They modulari9e a 'rogram and enca'&ulate a &erie& of &tatement& and a&&ociate them 8ith the name of the function. !cilab 'ro"ide& a built in editor, called a& !ciPad 8ithin !cilab 8herein the u&er can ty'e the code for function& and com'ile and load it into the 8or%&'ace. !ciPad can be in"o%ed by clic%ing on =Editor= choice on the main menu at the to' of the !cilab 8or% en"ironment. 2et u& 8rite a &im'le function to calculate the length of a line in the *-y 'lane, gi"en the coordinate& of it& t8o end& @*1, y1C and @*#, y#C. The length of &uch a line i& gi"en by the e*'re&&ion l = x # x 1# " # " 1# . Before defining the function in !ciPad, let u& try it out in !cilab.
-->x' '. -' +. x2 '(. -2 ". -->dx x2-x'. d- -2--'. -->l s0rt&dx$2 + d-$2) l ,.2',!++!

!tudying the abo"e &tatement& to be 'ut into the function&, notice that x', -', x2 and -2 are in'ut "alue& and the length =l= i& the out'ut 'arameter. Define the code a& de&cribed belo8? 1. +'en !ciPad by clic%ing on Editor on the main menu. #. Ty'e the line& of code to define the function. (. !a"e the content& of the file to a di&% file by clic%ing 4ile N !a"e in !ciPad and choo&ing a name for the file. ,. 2oad the function into !cilab by clic%ing on =2oad into !cilab= on the !ciPad main menu. ty'e the follo8ing code into !ciPad?
function 1l3 len&x'2 -'2 x22 -2) dx x2-x'. d- -2--'. l s0rt&dx$2 + d-$2). endfunction

In the abo"e code, function and endfunction are !cilab %ey8ord& and mu&t be ty'ed e*actly a& &ho8n. They &ignify the &tart and end of a function definition. The "ariable enclo&ed bet8een &Kuare brac%et& i& an out'ut 'arameter, and 8ill be returned to !cilab 8or%&'ace @or to the 'arent function from 8here it 8a& in"o%edC. The name of the function ha& been cho&en a& =len= @&hort for length, a& the name length i& already u&ed by !cilab for another 'ur'o&eC. The in'ut 'arameter& x', -', x2 and -2 are the "ariable& bringing in in'ut from the !cilab 8or%&'ace @or the 'arent function from 8here it i& calledC. Thi& function 8ill be in"o%ed a& ll len&xx'2--'2xx22--2) 8here ll i& the out'ut "ariable and xx', --', xx2 and --2 are the in'ut "ariable&. Bote that their name& need not match the corre&'onding name& in the function definition. Bote the &emicolon& @IC u&ed at the end of the e*ecutable &tatement& 8hich &u''re&& the echo of intermediate re&ult&. >ou can remo"e them if you need to debug the function 8hen the re&ult& are not matching the e*'ected re&ult&. 7lternately, u&e the function disp&) to 'rint out intermediate re&ult& 8ithin a function to debug it and remo"e it once the bug i& eliminated. To try out the function, load it into !cilab by fir&t &a"ing the content& to a di&% file @4ile N !a"eC and then clic%ing on =2oad into !cilab=. If there are no &ynta* error& in your function definition, it 8ill be loaded into !cilab 8or%&'ace and thi& can be "erified 8ith the command whos -t-pe function and &earching for the name of your function, namely, len. To u&e the function enter the follo8ing command? l len&'2+2'(2"). Ho8e"er, if there are &ynta* error&, the line on 8hich the error occur& i& di&'layed and you 8ill ha"e to remo"e the &ynta* error& and re'eat the abo"e &te'&.
Scilab Tutorial Tutorial - .unctions in Scilab

Tutorial / #iscellaneous Commands


!ome command& related to o'eration& on di&%& are im'ortant and they are li&ted belo8. They are im'ortant 8hen you 8ant to change the directory in 8hich your function file& are &tored or from 8here they are they are to be read.
pwd getcwd&) chdir&/dir/)

Print& the name of the current 8or%ing directory !ame a& pwd. Change& the 8or%ing directory to a different di&% location named dir.

It i& 'o&&ible to &a"e all the "ariable& in the !cilab $or%&'ace to a di&% file &o that you can Kuic%ly reload all the "ariable& and function& from a 're"iou& &e&&ion and continue from 8here you left off. The command& u&ed for thi& 'ur'o&e are?
save&/pf.bin/) load&/pf.bin/) save&/pf.bin/2 x-)

!a"e& entire content& of the !cilab 8or%&'ace @"ariable& and function&C in the file pf.bin in the current 8or%ing directory. Ae&tore& content& of the !cilab 8or%&'ace from the file pf.bin in the current 8or%ing directory. !a"e& only the "ariable x- of the !cilab 8or%&'ace in the file pf.bin in the current 8or%ing directory.

It i& 'o&&ible to determine the &i9e of "ariable& in the !cilab 8or%&'ace 8ith the follo8ing command?
si7e&x-) length&x-)

Aeturn& a 1*# matri* containing the number of ro8& and column& in the "ariable x-. Aeturn& the number of element& in x- @ro8& multi'lied by column&C.

!cilab can o'en file& on di&% and a number of function& are a"ailable for o'ening, clo&ing reading and 8riting di&% file&. The follo8ing line& of code illu&trate ho8 thi& can be accom'li&hed?
-->n '(. x 2!.!. x- 1'(( #!.( #!. 2((2 (3. -->fd file&>ex('.dat?2 >w?). // @pens a file ex('.dat for writing -->mfprintf&fd2 >n 9d2 x 9fAn?2 n2 x). -->mfprintf&fd2 >9'2.+fAt9'22+fAn?2 x-). -->mclose&fd).

>ou can no8 o'en the file ex('.dat in a te*t editor, &uch a& note'ad and &ee it& content&. >ou 8ill notice that the command& are &imilar to the corre&'onding command& in C, namely, fopen&), fprintf&) and fclose&). Bote that in the &econd command, the format &tring mu&t be &ufficient to 'rint one full ro8 of the matri* x-. The &eKuence of o'eration& mu&t al8ay& be, o'en the file and obtain a file de&cri'tor, 8rite to the file u&ing the file de&cri'tor and clo&e the file u&ing the file de&cri'tor.

Scilab Tutorial

Tutorial / "iscellaneous 0ommands

11

0ppendi$
SCILAB License ************** 1- Preface ********** The aim of this license is to lay do n the conditions ena!lin" yo# to #se$ modify and circ#late the S%&T'A()* +o e,er$ I-(IA and )-PC remain the a#thors of the S%&T'A() and so retain .ro.erty ri"hts and the #se of all ancillary ri"hts* /- 0efinitions ************** The S%&T'A() is defined as all s#ccessi,e ,ersions of SCILAB soft are and their doc#mentation that ha,e !een de,elo.ed !y I-(IA and )-PC* SCILAB 0)(I1)0 S%&T'A() is defined as all or .art of the S%&T'A() that yo# ha,e modified and2or translated and2or ada.ted* SCILAB C%3P%SIT) S%&T'A() is defined as all or a .art of the S%&T'A() that yo# ha,e interfaced ith a soft are$ an a..lication .ac4a"e or a tool!o5 of hich yo# are o ner or entitled !eneficiary* 6- %!7ect and conditions of the S%&T'A() license ************************************************ a8 I-(IA and )-PC a#thori9e yo# free of char"e$ to re.rod#ce the S%&T'A() so#rce and2or o!7ect code on any .resent and f#t#re s#..ort$ itho#t restriction$ .ro,idin" the follo in" reference a..ears in all the co.ies: Scila! ;c8I-(IA-)-PC* !8 I-(IA and )-PC a#thori9e yo# free of char"e to correct any !#"s$ carry o#t any modifications re<#ired for the .ortin" of the S%&T'A() and to carry o#t any #s#al f#nctional modification or correction$ .ro,idin" yo# insert a .atch file or yo# indicate !y any other e<#i,alent means the nat#re and date of the modification or the correction$ on the corres.ondin" file;s8 of the S%&T'A()* c8 I-(IA and )-PC a#thori9e yo# free of char"e to #se the S%&T'A() so#rce and2or o!7ect code$ itho#t restriction$ .ro,idin" the follo in" reference a..ears in all the co.ies: Scila! ;c8I-(IA-)-PC* d8 I-(IA and )-PC a#thori9e yo# free of char"e to circ#late and distri!#te$ free of char"e or for a fee$ the S%&T'A() so#rce and2or o!7ect code$ incl#din" the S%&T'A() modified in accordance ith a!o,e-mentioned article 6 !8$ on any .resent and f#t#re s#..ort$ .ro,idin": * * * the follo in" reference a..ears in all the co.ies: Scila! ;c8I-(IA-)-PC* the S%&T'A() is circ#lated or distri!#ted #nder the .resent license* .atch files or files containin" e<#i,alent means indicatin" the nat#re and the date of the modification or the correction to the S%&T'A() file;s8 concerned are freely circ#lated*

=- %!7ect and conditions of the 0)(I1)0 S%&T'A() license ******************************************************** a8 I-(IA and )-PC a#thori9e yo# free of char"e to re.rod#ce and modify and2or translate and2or ada.t all or .art of the so#rce and2or the o!7ect code of the S%&T'A()$ .ro,idin" a .atch file indicatin" the date and the nat#re of the modification and2or the translation and2or the ada.tation and the name of their a#thor in the S%&T'A() file;s8 concerned is inserted* The S%&T'A() th#s modified is defined as 0)(I1)0 S%&T'A()* The I-(IA a#thori9es yo# free of char"e to #se the so#rce and2or o!7ect code of the S%&T'A()$ itho#t restriction$ .ro,idin" the follo in" reference a..ears in all the co.ies: Scila! ;c8I-(IA-)-PC* !8 I-(IA and )-PC a#thori9e yo# free of char"e to #se the S%&T'A() so#rce and2or o!7ect code modified accordin" to article =-a8 a!o,e$ itho#t restriction$ .ro,idin" the follo in" reference a..ears in all the co.ies: >Scila! inside ;c8I-(IA-)-PC>* c8 The I-(IA and the )-PC a#thori9e yo# free of char"e to circ#late and distri!#te for no char"e$ for non-commercial .#r.oses the so#rce and2or o!7ect code of 0)(I1)0 S%&T'A() on any .resent and f#t#re s#..ort$ .ro,idin": * * * the reference > Scila! inside ;c8I-(IA-)-PC > is .rominently mentioned;

the 0)(I1)0 S%&T'A() is distri!#ted #nder the .resent license; the reci.ients of the distri!#tion can access the S%&T'A() code so#rce;

Scilab Tutorial

2ppendi#

11

* d8

the 0)(I1)0 S%&T'A() is distri!#ted #nder a name other than SCILAB* the 0)(I1)0 S%&T'A() shall ha,e !een

Any commercial #se or circ#lation of .re,io#sly a#thori9ed !y I-(IA and )-PC*

5- %!7ect and conditions of the license concernin" C%3P%SIT) S%&T'A() ********************************************************************* a8 I-(IA and )-PC a#thori9e yo# to re.rod#ce and interface all or .art of the S%&T'A() ith all or .art of other soft are$ a..lication .ac4a"es or tool!o5es of hich yo# are o ner or entitled !eneficiary in order to o!tain C%3P%SIT) S%&T'A()* !8 I-(IA and )-PC a#thori9e yo# free$ of char"e$ to #se the S%&T'A() so#rce and2or o!7ect code incl#ded in the C%3P%SIT) S%&T'A()$ itho#t restriction$ .ro,idin" the follo in" statement a..ears in all the co.ies: >com.osite soft are #sin" Scila! ;c8I-(IA-)-PC f#nctionality>* I-(IA and )-PC a#thori9e yo#$ free of char"e$ to circ#late and distri!#te for no char"e$ for .#r.oses other than commercial$ the so#rce and2or o!7ect code of C%3P%SIT) S%&T'A() on any .resent and f#t#re s#..ort$ .ro,idin": * * * * e8 the follo in" reference is .rominently mentioned: >com.osite soft are #sin" Scila! ;c8I-(IA-)-PC f#nctionality >; the S%&T'A() incl#ded in C%3P%SIT) S%&T'A() is distri!#ted #nder the .resent license ; reci.ients of the distri!#tion ha,e access to the S%&T'A() so#rce code; the C%3P%SIT) S%&T'A() is distri!#ted #nder a name other than SCILAB* C%3P%SIT) S%&T'A() shall ha,e !een

c8

Any commercial #se or distri!#tion of .re,io#sly a#thori9ed !y I-(IA and )-PC*

?- Limitation of the arranty ***************************** )5ce.t hen mentioned other ise in ritin"$ the S%&T'A() is s#..lied as is$ ith no e5.licit or im.licit arranty$ incl#din" arranties of commerciali9ation or ada.tation* @o# ass#me all ris4s concernin" the <#ality or the effects of the S%&T'A() and its #se* If the S%&T'A() is defecti,e$ yo# ill !ear the costs of all re<#ired ser,ices$ corrections or re.airs* A- Consent ********** 'hen yo# access and #se the S%&T'A()$ yo# are .res#med to !e a are of and to ha,e acce.ted all the ri"hts and o!li"ations of the .resent license* B- Bindin" effect ***************** This license has the !indin" ,al#e of a contract* @o# are not res.onsi!le for res.ect of the license !y a third .arty* C- A..lica!le la ***************** The .resent license and its effects are s#!7ect to &rench la co#rts*

and the com.etent &rench

Scilab Tutorial

2ppendi#

12

Bote&

Scilab Tutorial

Scilab Tutorial

You might also like