Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

An Introduction to Scilab

Satish Annigeri
Ph.D.
Professor of Civil Engineering
B.V. Bhoomaraddi College of Engineering & Technology, Hubli
satish@bvb.edu
December 2009
Table of Contents
Preface........................................................................................................................................ ii
Introduction.............................................................................................................................. 1
Tutorial 1 !cilab En"ironment....................................................................................... #
E$erci%e 1 !cilab En"ironment...................................................................................... &
Tutorial ' The (or)%*ace and (or)ing Directory................................................+
E$erci%e ' The (or)%*ace and (or)ing Directory...............................................,
Tutorial # Creating -atrice% and !ome !im*le -atri$ .*eration%................./
E$erci%e # Creating -atrice% and !ome !im*le -atri$ .*eration%..............1'
Tutorial 0 !ub1matrice%................................................................................................. 1#
E$erci%e 0 !ub1matrice%................................................................................................. 1&
Tutorial & !tati%tic%.......................................................................................................... 1+
Tutorial + (or)ing 2ith Polynomial%...................................................................... 13
E$erci%e + (or)ing 2ith Polynomial%...................................................................... '4
Tutorial 3 Plotting 5ra*h%............................................................................................ '1
Tutorial , Plotting #D 5ra*h%...................................................................................... '0
Tutorial / !cilab Programming 6anguage.............................................................. '+
Tutorial 14 !cri*t 7ile% and 7unction 7ile%............................................................. #4
Tutorial 11 7unction% in !cilab................................................................................... #'
Tutorial 1' 7ile .*eration%.......................................................................................... #&
Tutorial 1# Polynomial Cur"e 7itting....................................................................... #3
Tutorial 10 8eading -icro%oft E$cel 7ile%.............................................................. 04
Tutorial 1& !ome -i%cellaneou% Command%......................................................... 0'
Tutorial 1+ Data !tructure%.......................................................................................... 0#
Tutorial 13 Directed 5ra*h%........................................................................................ 03
Tutorial 1, Princi*al !tre%%e%..................................................................................... &1
An Introduction to Scilab | i
Preface
!cilab i% an o*en %ource %oft2are for numerical mathematic% and
%cientific "i%uali9ation. It i% ca*able of interacti"e calculation% a% 2ell a%
automation of com*utation% through *rogramming. It *ro"ide% all ba%ic
o*eration% on matrice% through built1in 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 2e 2or) 2ith.
:ll the%e ma)e !cilab an e$cellent tool for teaching, e%*ecially tho%e %ub;ect%
that in"ol"e matri$ o*eration%. 7urther, 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 2ith -atlab
<
, all a"ailable -atlab -1file% can be
directly u%ed in !cilab 2ith the hel* of the -atlab to !cilab tran%lator.
!cico%, a hybrid dynamic %y%tem% modeler and %imulator for !cilab,
%im*lifie% %imulation%. The greate%t feature% of !cilab are that it i% multi1
*latform and i% free. It i% a"ailable for many o*erating %y%tem% including
(indo2%, 6inu$ and -ac.! =. -ore information about the feature% of !cilab
are gi"en in the Introduction.
!cilab can hel* a %tudent focu% on the *rocedure for %ol"ing a *roblem
in%tead of %*ending time and energy de"elo*ing a matri$ algebra library. 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 2hole *roblem% can be %ol"ed by %im*ly calling a fe2
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%.
It i% not the aim of thi% tutorial to be an e$hau%ti"e and in1de*th loo)
into !cilab. In%tead, it attem*t% to get a no"ice %tarted 2ith the lea%t fu%% and
i% aimed at anyone 2ho intend% to %tart learning to u%e !cilab entirely on
her o2n.
In thi% re"i%ion of the tutorial, e$erci%e% ha"e been added to *oint the
%tudent to tho%e a%*ect% that 2ere not e$*licitly co"ered by the tutorial and
lead the %tudent to2ard% %elf1di%co"ery and learning.
Acknowledgements
It goe% 2ithout %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.
:.B. 8a;u, E&EE De*artment, BVBCET, Hubli 2ho fir%t introduced me to
!cilab and fore"er freed me from u%ing -atlab.
7ebruary '414 Satish Annigeri
An Introduction to Scilab | ii
Introduction
!cilab i% a %cientific %oft2are *ac)age for numerical com*utation%
*ro"iding a *o2erful o*en com*uting en"ironment for engineering and
%cientific a**lication%. De"elo*ed %ince 1//4 by re%earcher% from I>8I:
?7rench >ational In%titute for 8e%earch in Com*uter !cience and Control@ and
E>PC ?>ational !chool of Bridge% and 8oad%@, it i% no2 maintained and
de"elo*ed by !cilab Con%ortium %ince it% creation in -ay '44# and integrated
into Digiteo 7oundation in Auly '44,. The current "er%ion i% &.'.1 ?7ebruary
'414@.
!ince 1//0 it i% di%tributed freely along 2ith %ource code through the
Internet and i% currently being u%ed in educational and indu%trial en"ironment%
around the 2orld. 7rom "er%ion & it i% relea%ed under the 5P6 com*atible
CeCI66 licen%e.
!cilab include% hundred% of mathematical function% 2ith the *o%%ibility to
add interacti"ely function% from "ariou% language% ?C, 7ortran...@. It ha%
%o*hi%ticated data %tructure% ?including li%t%, *olynomial%, rational function%,
linear %y%tem%...@, an inter*reter and a high le"el *rogramming language.
!cilab ha% been de%igned to be an o*en %y%tem 2here the u%er can define
ne2 data ty*e% and o*eration% on the%e data ty*e% by u%ing o"erloading.
: number of toolbo$e% are a"ailable 2ith the %y%temB
'1D and #1D gra*hic%, animation
6inear algebra, %*ar%e matrice%
Polynomial% and rational function%
!imulationB .DE %ol"er and D:E %ol"er
!cico% B a hybrid dynamic %y%tem% modeler and %imulator
Cla%%ic and robu%t control, 6-I o*timi9ation
Differentiable and non1differentiable o*timi9ation
!ignal *roce%%ing
-etanetB gra*h% and net2or)%
Parallel !cilab u%ing PV-
!tati%tic%
Interface 2ith Com*uter :lgebra ?-a*le, -uP:D@
Interface 2ith TclCT)
:nd a large number of contribution% for "ariou% domain%.
!cilab 2or)% on mo%t Dni$ %y%tem% including 5>DC6inu$ and on (indo2%
/=C>TC'444C=PCVi%taC3. It come% 2ith %ource code, on1line hel* and Engli%h
u%er manual%. Binary "er%ion% are a"ailable.
!ome of it% feature% are li%ted belo2B
Ba%ic data ty*e i% a matri$, and all matri$ o*eration% are a"ailable a% built1in
o*eration%.
Ha% a built1in inter*reted high1le"el *rogramming language.
5ra*hic% %uch a% 'D and #D gra*h% can be generated and e$*orted to
Introduction | 1
"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
5I7 format and
included in the
document for
*re%entation. !cilab
can e$*ort to
Po%t%cri*t and 5I7
format% a% 2ell a% to
=fig ?*o*ular free
%oft2are for dra2ing figure%@ and 6aTe= ?free %cientific document *re*aration
%y%tem@ file format%.
Introduction | 2
Tutorial 1 Scilab Environment
(hen you %tart u* !cilab, you %ee a 2indo2 li)e the one %ho2n in 7ig. 1
belo2. The u%er enter% !cilab command% at the *rom*t ?-->@. 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 EHel*F menu.
Clic)ing on the EHel*F menu o*en%
u* the Help Browser, %ho2ing a
li%t of to*ic% on 2hich hel* i%
a"ailable. Clic)ing on the rele"ant
to*ic ta)e% you to hy*erlin)ed
document% %imilar to 2eb *age%.
The Hel* Bro2%er ha% t2o tab%
Table of Contents and Search.
Table of Content% contain% an al*habetically arranged li%t of to*ic%. To %ee the
li%t of function% a"ailable in !cilab, %earch for Elementary Functions in the
content% *age of Hel* Bro2%er. .ther u%eful function% may be a"ailable under
different heading%, %uch a%, Linear Algebra, Signal rocessing, !enetic
Algorithms, "nterpolation, #etanet, $ptimi%ation and Simulation,
Statistics, Strings, Time and &ate etc. D%e the !earch tab to %earch the hel* for
%tring *attern%.
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 EinvF, %im*ly ty*e the follo2ing command at
the *rom*t. Thi% i% u%eful 2hen you already )no2 the name of the function and
2ant to )no2 it% in*ut and out*ut argument% and learn ho2 to u%e it.
-->help inv
!cilab hel* bro2%er
offer% t2o "ie2% of the
hel* content. In the
content% "ie2, to*ic% are
li%ted in a tree %tructure
2hich can be e$*anded
or colla*%ed to bro2%e
through the hel* content.
:lternately, the %earch
"ie2 allo2% the u%er to
ty*e in %earch *attern%
and to*ic% matching the
*attern are li%ted. The
content "ie2 gi"e% a
good o"er"ie2 2hile the
%earch "ie2 ta)e the u%er directly to the reGuired to*ic.
Tutorial 1 Scilab Environment | 3
Fig. 1.1 Scilab environment
Fig. 1.2 Scilab help browser
!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 follo2ing e$am*le%B
-->2+3
ans =
5.
-->2/3
ans =
.6666667
-->2^3
ans =
8.
-->a = 2
a =
2.
-->b = 3
b =
3.
-->c = a + b
c =
5.
-->pi = atan(1.0)*
pi =
3.115!27
-->sin(pi/)
ans =
0.7071068
-->e"p(0.1)
ans =
1.105170!
D%ually the an%2er of a calculation i% %tored in a "ariable %o that it could
be u%ed in the immediately %ub%eGuent e$*re%%ion%. If the u%er doe% not
e$*licitly %u**ly the name of the "ariable to %tore the an%2er returned by an
e$*re%%ion, !cilab u%e% a "ariable named ans to %tore %uch re%ult%. If the u%er
%*ecifie% a "ariable to %tore the re%ult% of an e$*re%%ion, re%ult% are %tored in
the u%er %*ecified "ariable.
Ending an e$*re%%ion 2ith a %emicolon ?;@ %u**re%%e% the echo of the
out*ut of an e$*re%%ion. The e$*re%%ion i% e"aluated and re%ult% a%%igned, but
the re%ult% are not echoed. Try the follo2ing commandB
-->a = 5#
-->$
and you 2ill notice that the *rom*t rea**ear% immediately, 2ithout echoing
a=5.
Hou could enter more than one command on the %ame line by %e*arating
the command% by %emicolon% ?;@ or a comma ?,@. The %emicolon %u**re%%e%
echoing of intermediate re%ult% but comma doe% not.
-->a=5# b=10# c=15#
-->$
-->%isp(&a b c')
5. 10. 15.
-->a=10(b=20(c=30
a =
10.
b =
20.
c =
30.
In fact, you could ha"e a mi$ of %emicolon% and comma%. E$*re%%ion%
follo2ed by %emicolon% 2ill not be echoed 2hile e$*re%%ion% follo2ed by
comma% 2ill be echoed.
-->a=100# b=200( c=300#
b =
200.
Tutorial 1 Scilab Environment | 4
Exercise 1 Scilab Environment
1. (hat are the other 2ay% of obtaining online hel* in !cilab, other than
ty*ing Ehel*F at the command *rom*tI ?Ans' 7rom the menu I !cilab
Hel* or *re%% F1 )ey@
'. (here are the demo *rogram% that demon%trate !cilabJ% ca*abilitie%I
?Ans' 5o to the menu I !cilab Demo%@
#. (here can you find u%er contributed document% on u%ing and a**lying
!cilabI (hat are the categorie% for the%e document%I (hich categorie%
intere%t youI ?Ans' 5o to the menu I (eblin)% Contribution%@
0. Ho2 can you %earch for a 2ord or *attern in the !cilab online hel*
bro2%erI
&. Can you u%e the built1in "ariable EansF in your calculation%I If %o, i% it a
good idea to do %o or i% it better to u%e your o2n named "ariable%I
+. (hen do you thin) it i% u%eful to u%e the %emicolon ?;@ to %u**re%% the
out*ut of a !cilab %tatementI
3. (hat are the rule% for choo%ing name% for "ariable% in !cilabI Can you
u%e a numeric character a% the fir%t characterI Can you u%e
under%core ? _ @ a% the fir%t characterI Can you u%e %*ecial character%,
%uch a% 1, K, C, I in a "ariable nameI
,. Can you change the font u%ed by !cilabI ?Ans' 5o to the menu
Preference% Choo%e 7ont@
/. (hat i% the command to clear the %creenI ?Ans' clc@
14. (hat i% the %hort cut )ey to clear the %creenI ?Ans' F2 )ey@
11. (hat i% command hi%toryI (hat are the %hortcut )ey% to u%e the
command hi%toryI ?Ans' 5o to the menu Edit Hi%tory. Hou can al%o
u%e the arro2 )ey%@
1'. 6i%t area% 2ithin your *ro*o%ed branch of %*eciali9ation 2here !cilab
can be a u%eful tool.
1#. I% there a command to record all command% that you ty*e and %a"e
them to a file %o that you can %ee them laterI
?Ans' Ty*e help diary@
10. Can you de%cribe %ome u%eful a**lication of the diary commandI
1&. Can you cu%tomi9e !cilab %tartu* to %uit your %*ecific need%I
?Ans' To cu%tomi9e !cilab at %tart u*, create a file ECBLDocument% and
!etting%LMD%erNL!cilabL%cilab1M"er%ionNL %cilab.iniF and *ut any "alid
!cilab command% in it that you 2i%h !cilab to e$ecute each time it
%tart% u*. MD%erN mu%t be re*laced 2ith your login name and !cilab
M"er%ionN currently i% &.'.4 %o that the folder name i% %cilab1&.'.4. The
abo"e location i% for -icro%oft (indo2% =P. 7or (indo2% Vi%ta, u%e
CBLD%er%LMD%erNL:**DataL8oamingL!cilabLM%cilab1
M"er%ionNL%cilab.ini@.
Exercise 1 Scilab Environment |
Tutorial 2 T!e "or#s$ace and "or#in% &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 2here all "ariable% and function% are
%tored, and i% called the (orkspace. Ouite often it i% nece%%ary to in%*ect the
2or)%*ace to chec) 2hether or not a "ariable or a function ha% been defined.
The follo2ing command% hel* the u%er in in%*ecting the memory %*aceB who,
whos and who_user(). D%e the online hel* to learn more about the%e command%.
The who command li%t% the name% of "ariable% in the !cilab 2or)%*ace.
>ote the "ariable name% *receded by the E%F %ymbol. The%e are %*ecial
"ariable% that are u%ed often and therefore *redefined by !cilab. It include% %pi
?

@, %e ?
e
@, %i ? 1 @, %inf ?

@, %nan ?>a>@ and other%.
The whos command li%t% the "ariable% along 2ith the amount of memory
they ta)e u* in the 2or)%*ace. The "ariable% to be li%ted can be %elected ba%ed
on either their ty*e or name. !ome e$am*le% areB
-->)h*s()
6i%t% entire content% of the 2or)%*ace, including
function%, librarie%, con%tant%
-->)h*s -t+pe c*nstants
6i%t% only "ariable% that can %tore real or
com*le$ con%tant%. .ther ty*e% are boolean,
%tring, function, library, *olynomial etc. 7or a
com*lete li%t u%e the command -->help ypeof.
-->)h*s -na,e na,
6i%t% all "ariable% 2ho%e name begin% 2ith the
letter% na!
To under%tand ho2 !cilab deal% 2ith number%, try out the follo2ing
command% and u%e the whos command a% follo2%B
-->a1=5#
Define% a real number "ariable 2ith name Ja1"
-->a2=s-.t(-)#
Define% a com*le$ number "ariable 2ith name
Ja2"
-->a3=&1( 2# 3( '#
Define% a '$' matri$ 2ith name Ja#J
-->)h*s -na,e a
6i%t% all "ariable% 2ith name %tarting 2ith the
letter JaJ
/a,e 0+pe 1i2e 3+tes
a3 c*nstant 2 b+ 2 8
a2 c*nstant 1 b+ 1 32
a1 c*nstant 1 b+ 1 2
>o2 try the follo2ing command%B
-->a1=s-.t(-!)
Con"ert% Ja1J to a com*le$ number
-->)h*s -na,e a
>ote that JaJ i% no2 a com*le$ number
-->a1=a3
Con"ert% Ja1J to a matri$
Tutorial 2 T!e "or#s$ace and "or#in% &irector' | (
-->a1=s-.t(-!)
Con"ert% Ja1J to a com*le$ number
-->)h*s -na,e a
>ote that Ja1J i% no2 a matri$
-->save(4e"01.%at4)
!a"e% all "ariable% in the 2or)%*ace to a di%) file
e$%1&da
-->l*a%(4e"01.%at4)
6oad% all "ariable% from a di%) file e$%1&da to
2or)%*ace
>ote the follo2ing *oint%B
!cilab treat% a %calar number a% a matri$ of %i9e 1$1 ?and not a% a
%im*le number@ 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.
Tutorial 2 T!e "or#s$ace and "or#in% &irector' | )
Exercise 2 T!e "or#s$ace and "or#in% &irector'
1. (hat are the a"ailable data ty*e% in !cilabI
?Ans' Ty*e the command help ype@.
'. (hat i% the command to li%t all "ariable% in the !cilab 2or)%*ace
2ho%e name begin% 2ith the letter% E%aFI
?Ans' whos -na!e sa@.
#. (hat i% the command to li%t all "ariable% in the !cilab 2or)%*ace of
ty*e booleanI ?Ans' whos -ype 'oolean@.
0. (hat i% the !cilab con%tant for the "alue of pI
?Ans' %pi@.
&. (hat i% the command to find the !cilab current 2or)ing directoryI
?Ans' pwd or (ecwd@.
+. (here can you find the !cilab current 2or)ing directoryI
?Ans' 7om the menu 7ile 5et Current Directory@.
3. Ho2 can you change the !cilab current 2or)ing directory to a different
locationI
?Ans' cd()direcory*) or go to the menu 7ile Change Directory and
choo%e the directory you 2ant to go to, in the dialog bo$@.
,. (hy i% it im*ortant to )no2 the !cilab current 2or)ing directoryI
?Ans' Becau%e it i% the default location 2here !cilab %a"e% all file%,
unle%% you e$*licitly %*ecify the full *ath@.
/. I% !cilab a %trongly ty*ed languageI
?Ans' !trongly ty*ed language% are tho%e in 2hich each "ariable and it%
ty*e mu%t fir%t be defined before it can be u%ed. 7urther, once defined,
it% ty*e u%ually cannot be changed. !ee (i)i*edia for a definition of
E%trongly ty*edF. I% it the %ame a% 2hat I ha"e 2ritten hereI (hat i%
%tatic ty*ing and ty*e %afetyI@.
Exercise 2 T!e "or#s$ace and "or#in% &irector' | *
Tutorial 3 Creatin% +atrices and Some Sim$le +atrix ,$erations
-atri$ o*eration% built1in into !cilab include addition, %ubtraction,
multi*lication, tran%*o%e, in"er%ion, determinant, trigonometric, logarithmic,
e$*onential function% and many other%. 6et u% fir%t learn creating matrice%B
-->a=&1 2 3'
Create a ro2 "ector
-->a=&1# 2# 3'
Create a column "ector
-->a=&1 2 3'4
Create a column "ector, %ame a% abo"e
-->"=&1 2 3'# +=& 5 6 7'#
-->a=&" +'
Create a matri$ a 2hich i% a
concatenation of $ and y, *ro"ided they
are com*atible ?%ame number of ro2%@
-->a=&1 2 3# 5 6# 7 8 !'#
Define a #$# matri$. !emicolon%
indicate end of a ro2
(e can no2 try out a fe2 %im*le matri$ o*eration%B
-->b = a4#
Tran%*o%e a and %tore it in '.
:*o%tro*he ?"@ i% the tran%*o%e
o*erator.
-->c = a + b
ans =
2. 6. 10.
6. 10. 1.
10. 1. 18.
:dd a to ' and %tore the re%ult in c. a
and ' mu%t be of the %ame %i9e.
.ther2i%e, !cilab 2ill re*ort an error.
-->% = a 5 b#
!ubtract ' from a and %tore the re%ult
in d.
-->e=a*b
ans =
1. 32. 50.
32. 77. 122.
50. 122. 1!.
-ulti*ly a 2ith ' and %tore the re%ult in
e. a and ' mu%t be com*atible for
matri$ multi*lication.
-->6=&3 1 2# 1 5 3# 2 3 6'#
Define a #$# matri$ 2ith name f.
-->7 = inv(6)
7 =
0.28571 0. -0.128571
0. 0.285713 -0.128571
-0.128571 -0.128571 0.285713
In"ert matri$ f and %tore the re%ult in
(. f mu%t be %Guare and *o%iti"e
definite. !cilab 2ill di%*lay a 2arning if
it i% ill conditioned.
-->6*7
The an%2er mu%t be an identity matri$
-->%et(6)
ans =
!.
Determinant of f.
-->l*7(a)#
-atri$ of log of each element of a.
Tutorial 3 Creatin% +atrices and Some Sim$le +atrix ,$erations | -
!ome matri$ o*eration% %uch a% multi*lication, e$*onentiation can be
*erformed on an element12i%e ba%i%.
-->a .* b
ans =
1. 8. 21.
8. 25. 8.
21. 8. 81.
Element by element multi*lication.
-->a^2
ans =
30. 36. 2.
66. 81. !6.
102. 126. 150.
!ame a% a + a
-->a .^2
ans =
1. . !.
16. 25. 36.
!. 6. 81.
Element by element %Guare.
-atri$ addition and %ubtraction already being element12i%e o*eration%,
element12i%e addition and %ubtraction re%ult in an errorB
-->a .+ b
-->a .- b
Error
There are %ome handy utility function% to generate commonly u%ed
matrice%, %uch a% 9ero matrice%, identity matrice%, diagonal matrice%, matri$
containing randomly generated number% etc.
-->a=2e.*s(5(8)
Create% a &$, matri$ 2ith all element% 9ero.
-->b=*nes((6)
Create% a 0$+ matri$ 2ith all element% 1
-->c=e+e(3(3)
Create% a #$# identity matri$
-->%=e+e(3(3)*10
Create% a #$# diagonal matri$, 2ith diagonal
element% eGual to 14.
-->"=.an%(3( 5)
Create a matri$ 2ith # ro2% and & column%,
the element% being random number%
bet2een 4 and 1.
-->+=.an%(3( 5) * 10
Create a matri$ 2ith # ro2% and & column%,
the element% being random real number%
bet2een 4 and 14.
-->+=int(.an%(3( 5)*100)#
Create a matri$ 2ith # ro2% and & column%,
the element% being random integer number%
bet2een 4 and 144.
It i% *o%%ible to determine the %i9e, length and ty*e of !cilab "ariable%.
-->+=&185# 6810# 11815'#
!i9e return% the number of ro2% and column%
Tutorial 3 Creatin% +atrices and Some Sim$le +atrix ,$erations | 1.
-->si2e(+)
ans =
3. 5.
in y
-->len7th(+)
ans =
15.
>umber of element% in y.
-->+(1)
ans =
1.
Element y(1) i% the %ame a% y(1, 1)
-->+(2)
ans =
6.
Element y(2) i% the %ame a% y(2, 1).
Elements are counted column)wise
-->+(6)
ans =
12.
Element y(,) i% the %ame a% y(#, 2).
Element% are counted column12i%e
It i% *o%%ible to generate a range of number% to form a "ector. !tudy the
follo2ing command%B
-->185
5enerate% a ro2 of number% from 1 to & at an
increment of 1
-->(185)4
5enerate% a column of number% from 1 to &
at an increment of 1
-->a=&185'
Create% a ro2 "ector 2ith & element%. !ame
a% -1, 2, #, ., 5/
-->b=&185'4
Create% a column "ector 2ith & element%.
!ame a% -1; 2; #; .; 5/
-->c=&080.585'
Create% a "ector 2ith 11 element% a% follo2%
-%, %&5, 1&%, 1&5, &&& .&5, 5&%/
: range reGuire% a %tart "alue, an increment and an end "alue, %e*arated
by colon% ?0@. If only t2o "alue% are gi"en ?%e*arated by only one colon@, they
are ta)en to be the %tart and end "alue% and the increment i% a%%umed to be 1
?a0' i% a %hort form for a010', 2here a and ' are %calar%@. The increment mu%t
be negati"e 2hen the %tart "alue i% greater than the end "alue.
8ange% *lay an im*ortant role in %ub1matri$ o*eration%, 2hich 2e 2ill %ee
in the ne$t tutorial.
Hou can al%o create an em*ty matri$ 2ith the commandB
-->a=-/
-->si1e(a)
ans =
%& %&
-->whos -na!e a
2a!e 3ype 4i1e 5yes
a consan % 'y % 1,
Tutorial 3 Creatin% +atrices and Some Sim$le +atrix ,$erations | 11
Exercise 3 Creatin% +atrices and Some Sim$le +atrix ,$erations
1. Can you u%e the &6 o*erator li)e you can u%e &+I ?Ans' >o. In fact, there
i% no need to@.
'. (hat i% the %i9e of an em*ty matri$ a = -/I
?Ans' !i9e 4 $ 4@
#. (hile generating a range, can you %*ecify a negati"e incrementI
?Ans' He%, if the %tart "ale i% greater than the end "alue@
0. (hat i% the command to generate "alue% from 4 to 'p at an increment
of pC1+I ?Ans' %0%pi71,02+%pi@.
&. (hat i% the command to e$tract the diagonal element% of a %Guare
matri$ into a "ectorI ?Ans' a = dia(($) create% a "ector a containing
the diagonal element% of matri$ $@.
+. 5i"en a %Guare matri$ a, ho2 can you create a matri$ ' 2ho%e diagonal
element% are the %ame a% tho%e of a but the other element% are all 9eroI
?Ans' ' = eye(a) &+ a@.
3. E$tract the off1diagonal term% ?at an off%et of 1@ of a %Guare matri$ into
a "ector.
?Ans' ' = dia(($, 1) to e$tract the term% abo"e the diagonal and
' = dia(($, -1) to e$tract the term% belo2 the diagonal. ' = dia(($)
i% a %hortcut for ' = dia(($, %). The off%et default% to 9ero@.
,. Create a matri$ of %i9e &$& ha"ing the reGuired element% on the
diagonal, abo"e the diagonal and belo2 the diagonal.
?Ans' ' = dia((-105/) create% a &$& matri$ 2ho%e diagonal element%
are the element% of the "ector -1 2 # . 5/@.
/. Create a matri$ of %i9e &$& ha"ing the reGuired element% on the
diagonal abo"e the main diagonal.
?Ans' ' = dia((-10./, 1) create% a &$& matri$ of 9ero% and *ut% the
element% of the "ector -1 2 # ./ on the diagonal abo"e the main
diagonal. To *lace the "ector on the diagonal belo2 the main diagonal
u%e ' = dia((-10./, -1)@.
14. Create a tri1diagonal matri$ of %i9e &$& 2ith the %*ecified element% on
the main diagonal, abo"e and belo2 the main diagonal.
?Ans' ' = dia((-105/) 6 dia((-,08/, 1) 6 dia((-1%01#/, -1) 2ill
*ut the "ector -1 2 # . 5/ on the main diagonal, -, 9 : 8/ on the
diagonal abo"e the main diagonal and -1% 11 12 1#/ on the diagonal
belo2 the main diagonal@.
Exercise 3 Creatin% +atrices and Some Sim$le +atrix ,$erations | 12
Tutorial 4 Sub/matrices
It i% *o%%ible to e$tract or re*lace an element of a matri$ by referring to it%
ro2 and column indice%.
-->a = &1 2 3# 5 6'#
Create a matri$ 2ith ' ro2% and # column%.
-->b = a(2(2)
b =
5.
E$tract the element of a at ro2 ' and column
' and a%%ign it to '
-->a(2( 2) = 100
a =
1. 2. 3.
. 100. 6.
8e*lace element of a at ro2 ' and column '
2ith the "alue 144
!imilar o*eration% can be *erformed on a %ub1matri$ of an e$i%ting matri$.
: %ub1matri$ can be identified by the ro2 and column number% at 2hich it
%tart% and end%. 6et u% fir%t create a matri$ of %i9e &$,.
-->a = int(.an%(5(8)*100)
5enerate% a &$, matri$ 2ho%e element% are
random integer number% bet2een 4 and 144.
!ince the element% are random number%, each *er%on 2ill get a different
matri$. 6et u% a%%ume 2e 2i%h to identify a '$0 %ub1matri$ of a demarcated by
ro2% # to 0 and column% ' to &. Thi% i% done 2ith a(#0., 205). The range of
ro2% and column% i% re*re%ented by the range command% #0. and 205
re%*ecti"ely. Thu% #0. define% the range #, 0 2hile 205 define% the range ', #, 0,
&. Ho2e"er, matri$ JaJ remain% unaffected.
-->b=a(38( 285)
Thi% command co*ie% the contiguou% %ub1
matri$ of %i9e '$0 %tarting from element at
?#, '@ u* to element ?0, &@ of a into '.
: %ub1matri$ can be o"er2ritten ;u%t a% ea%ily a% it can be co*ied. To ma)e
all element% of the %ub1matri$ bet2een the abo"e range eGual to 9ero, u%e the
follo2ing commandB
-->a(38( 285) = 2e.*s(2()
Thi% command create% a '$0 matri$ of 9ero%
and *ut% it into the %ub1matri$ of a bet2een
ro2% #B0 and column% 'B&.
>ote that the %ub1matri$ on the left hand %ide and the matri$ on the right
%ide ?a 9ero matri$ in the abo"e e$am*le@ mu%t be of the %ame %i9e. Ho2e"er, it
i% *o%%ible to lea"e out the number of ro2% and column% in the 1eros()
command and !cilab 2ill calculate the correct number of ro2% and column%
reGuired. Thu%, the follo2ing command i% the %ame a% the one abo"eB
-->a(38( 285) = 2e.*s()
(hile u%ing range to demarcate ro2% andCor column%, it i% *ermitted to
lea"e out both the %tart and end "alue% in the range, in 2hich ca%e they are
a%%umed to be 1 and the number of the la%t ro2 ?or column@, re%*ecti"ely. To
Tutorial 4 Sub/matrices | 13
indicate all ro2% ?or column%@ it i% enough to u%e only the colon ?0@. Thu%, the
%ub1matri$ con%i%ting of all the ro2% and column% ' and # of a i% a(0, 20#).
>aturally a(0, 0) re*re%ent% the 2hole matri$, 2hich of cour%e could be
re*re%ented %im*ly a% a.
!cilab u%e% a %*ecial %ymbol to refer to the number of the la%t ro2 ?or
column@ by the %ymbol ;. Thi% can be u%ed 2ithin range %*ecification% to
re*re%ent the number of the la%t ro2 ?or column@.
It i% not *o%%ible to %*ecify only the %tart "alue of the range and lea"e out
the end "alue or "ice "er%a. Either both mu%t be %*ecified or both mu%t be left
out. The %ub1matri$ con%i%ting of all ro2% of a and column% # to , i%
re*re%ented a%B
-->a(8( 389)
In the belo2, 2e are e$tracting all ro2% and column% from column # to la%t
but one, that i% 3. >ote that ; i% the la%t column and ;-1 i% the la%t but one
column.
-->a(8( 389-1)
>ote that the %ub1matri$ need not nece%%arily con%i%t of contiguou% ro2%
andCor column%. 7or e$am*le, to e$tract the odd ro2% and column from matri$
a, you could u%e the follo2ing commandB
-->c = a(18289( 18289)
Thi% command co*ie% the %ub1matri$ of a 2ith
ro2% 1, #, & and column% 1, #, &, 3 into '. The ro2%
are re*re%ented by the range 1020; 2hich im*lie%
%tart from 1, increment by ' each time and u* to ;,
2hich in thi% ca%e i% &.
-->c = a(8( 98-181)
Hou can e"en re"er%e the order of column% ?or
ro2% or both@. The range ;0-101 re"er%e% the
order of the column% ?ro2% are unaltered@, and
the modified matri$ are %tored in c.
Tutorial 4 Sub/matrices | 14
Exercise 4 Sub/matrices
1. E$tract the la%t column of a matri$ a and %tore it in matri$ '.
?Ans' ' = a(0, ;)@.
'. E$tract the la%t but one column of a matri$ a and %tore it in matri$ '.
?Ans' ' = a(0, ;-1)@.
#. 8e*lace the %ub1matri$ bet2een ro2% # to & and column% ' to the la%t
but one column, in matri$ a of %i9e &$& 2ith 9ero%.
?Ans' a(#05, 20;-1) = 1eros(#, .). In%tead of 1eros(#,.) you can
u%e 1eros(0,0) and !cilab 2ill calculate the reGuired number of ro2%
and column% it%elf@.
0. 8e*lace the e"en numbered column% of matri$ a ha"ing %i9e #$& 2ith
one%. ?Ans' a(0,2020;)=ones(0, 0)@.
&. (hat i% the %ub1matri$ of a e$tracted by the follo2ing command
a(10#,;-20;)I
?Ans' It e$tract% the fir%t # ro2% and la%t # column% of matri$ a@.
+. :%%uming a to be a &$, matri$, are the follo2ing "alid command%I If %o,
2hat do they doI If not, 2hat i% the correct commandI
1. <(10, 5) Incorrect. !hould indicate end ro2 number.
'. <(0, 5) Correct
#. a(10#, ;-10;) Correct
0. a(0;, #0,) Incorrect. !hould indicate %tart ro2 number.
Exercise 4 Sub/matrices | 1
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 ro2% ?or
column%@ a% the ob%er"ation% and the column% ?or ro2%@ a% the *arameter%. To
choo%e ro2% a% the ob%er"ation%, the indicator i% r or 1. To choo%e column% a%
the ob%er"ation%, the indicator i% JcJ or 2. 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 su!(), !ean(), sdev(), s_deviaion(), !edian().
6et u% fir%t generate a matri$ of & ob%er"ation% on # *arameter%. 7or the
*ur*o%e of thi% demon%tration, let the element% be random number%. Thi% i%
done u%ing the follo2ing commandB
-->a=.an%(5(3)
Create% a &$# matri$ of random number% .
:%%uming ro2% to be ob%er"ation% and column% to be *arameter%, the
%um, mean and %tandard de"iation are calculated a% follo2%B
-->s=s:,(a( 4.4)
!um of column% of a.
-->,=,ean(a( 1)
-ean "alue of each column of a.
-->s%=st%ev(a( 1)
!tandard de"iation of a. Po*ulation %i9e
%tandard de"iation.
-->s%2=st$%eviati*n(a( 4.4)
!tandard de"iation of a. !am*le %i9e
%tandard de"iation.
-->,%n=,e%ian(a(4.4)
-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 2ith c or 2.
(hen neither r ?or 1@ nor c ?or 2@ 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, ro2 or matri$ can be
obtained 2ith the !a$() and !in() function% re%*ecti"ely in the %ame 2ay a%
the %tati%tical function% abo"e, e$ce*t that you mu%t u%e r or c but not 1 or 2.
Tutorial Statistics | 1(
Tutorial ( "or#in% 0it! Pol'nomials
!cilab ha% %u**ort for o*eration% on *olynomial%. Hou can create
*olynomial%, find their root% and *erform o*eration% on them %uch a% addition,
%ubtraction, multi*lication, di"i%ion, %im*lification etc.
: *olynomial can be created in t2o 2ay%. .ne 2ay i% to define the
*olynomial in term% of it% root% and the other 2ay i% to define it in term% of it%
coefficient%. (hile creating a *olynomial, 2e mu%t choo%e a name for the
%ymbolic "ariable and indicate 2hether the *olynomial i% to be created in term%
of root% or in term% of coefficient%. The %ymbolic "ariable name can be of any
length, but only the fir%t four character% are %ignificant, the other character% are
ignored.
-->p1 = p*l+(&3 2'( 4"4)
or
-->p1 = p*l+(&3 2'( 4"4( 4.4)
Create a *olynomial p1 ha"ing root% #
and ', ta)ing the name of the %ymbolic
"ariable a% $. The *olynomial i%
p1 = 65 xx
2
.
-->p2 = p*l+(&6 -5 1'( 4"4( 4c4)
Create a *olynomial p2 ha"ing
coefficient% 65 xx
2
, a%%uming the
name of the %ymbolic "ariable a% $. The
*olynomial i% p2 = 65 xx
2
.
The third *arameter, 2hen %u**lied, may be "r" or "roos" in 2hich ca%e
the fir%t *arameter i% a "ector containing the root% of the *olynomial or it may
be "c" or "coeff" in 2hich ca%e the fir%t *arameter i% a "ector containing the
coefficient% of the *olynomial, %tarting from the con%tant a% the fir%t element
and *o2er of the %ymbolic "ariable increa%ing by one for each element in the
"ector.
Thu%, the *olynomial 2ith t2o root% i% a *olynomial of order t2o.
!imilarly, a *olynomial 2ith three coefficient% i% al%o a *olynomial of order t2o.
(hen the third *arameter i% not %u**lied, it default% to "r" or "roos".
It i% *o%%ible to *erform a number of o*eration% on *olynomial%, %uch a%,
find it% root%, add, %ubtract, multi*ly, di"ide and %im*lify.
-->p1 = p*l+(&6 -5 1'( 4"4)
p1 =
2
6 5 5" + "
-->.**ts(p1)
ans =
2.
3.
Create a *olynomial p1 ha"ing
coefficient% +, 1& and 1, ta)ing the name
of the %ymbolic "ariable a% $. The
*olynomial i%
p1 = 65 xx
2
. 7ind the root% u%ing
the function roos(p1).
-->p3 = p1 + p2
p3 =
2
12 5 10" + 2"
:dd the t2o *olynomial% p1 and p2 and
%tore the re%ult in the *olynomial p#.
!ubtraction can be *erformed in a
%imilar 2ay.
Tutorial ( "or#in% 0it! Pol'nomials | 1)
-->p = p1 * p2
p =
2 3
36 5 60" 37" 5 10" + "
Product of t2o *olynomial% i% al%o a
*olynomial, and i% calculated u%ing the
multi*lication o*erator ?+@.
-->p5 = p1 / p2
p5 =
1
-
1
-->t+pe*6(p5)
ans =
.ati*nal
Di"iding a *olynomial 2ith another
re%ult% not in a *olynomial, but a
rational.
-->p1 == p2
ans =
0
(e can *erform boolean o*eration% on
*olynomial%.
-->c*e66(p1)
ans =
6. -5. -1.
7ind the coefficient% of the *olynomial
p1.
-->%e.ivat(p1)
ans =
-5 + 2"
7ind the deri"ati"e of the *olynomial
p1.
-->c = c*,pani*n(p1)
c =
5 -6
1 0
Com*anion matri$ of the *olynomial i%
the matri$ 2ho%e characteri%tic
eGuation i% the gi"en *olynomial.
-->.**ts(p1)4
ans =
2. 3.
-->spec(c)
ans =
3.
2.
The root% of the characteri%tic eGuation
are the eigen"alue% of the com*anion
matri$.
There are other 2ay% of defining *olynomial%.
-->" = p*l+(0( 4"4)
" =
"
Create a "ariable named a% $, 2hich i% a
*olynomial of degree 9ero. The "ariable
$ can be u%ed a% a seed to create other
*olynomial%.
-->p6 = 6 5 5 * " + "^2
-->.**ts(p6)
ans =
2.
3.
Create the *olynomial p1 the %ame 2ay
you 2ould 2rite it on *a*er. Thi% i% the
%ame a% the *re"iou% *olynomial% p1
and p2 that 2e ha"e created.
-->c=&5 -6# 1 0'#
-->p7 = p*l+(c( 4"4)
Create a *olynomial from it%
com*anion matri$.
Tutorial ( "or#in% 0it! Pol'nomials | 1*
p7 =
6 5 5" + "^2
6et u% no2 ta)e a loo) at rational *olynomial% and e$*lore the function%
related to them.
-->" = p*l+(0( 4"4)
-->p=(1+2*"+3*"^2)/(+5*"+6*"^2)
p =
2
1 + 2" + 3"
-----------
2
+ 5" + 6"
Create the "ariable $ a% a *olynomial
of degree 4, thu% creating a seed for
a *olynomial in $.
Create the *olynomial p 2ith
numerator a% 12 x3 x
2
and
denominator 45 x6 x
2
.
-->n:,e.(p)
ans =
2
1 + 2" + 3"
-->%en*,(p)
ans=
2
+ 5" + 6"
E$tract the numerator and
denominator of the rational
*olynomial.
-->%e.ivat(p)
ans =
2
3 + 12" + "
-------------------------
2 3
16 + 0" + 73" +60" + 36
7ind the deri"ati"e of the rational
*olynomial.
-->&n( %'=si,p(("+1)*("+2)(
("+1)*("-2))
% =
-2 + "
n =
2 + "
!im*lify a rational *olynomial gi"en
the numerator and denominator of
the rational *olynomial. 8eturn% the
numerator and denominator after
%im*lification.
Tutorial ( "or#in% 0it! Pol'nomials | 1-
Exercise ( "or#in% 0it! Pol'nomials
1. Create a *olynomial 2ith $ a% the %ymbolic "ariable %uch that it% root%
are ' and #.
?Ans' p = poly(-2 #/, "$")@.
'. Create a *olynomial to re*re%ent + &$ K $
'
I
?Ans' p = poly(-, -5 1/, "$", "coeff")@
#. 7ind the root% of thi% *olynomial. ?Ans' ' and #@.
0. (hat o*eration% can you *erform on *olynomial%I
?Ans' Hou can *erform addition, %ubtraction, multi*lication and
di"i%ion o*eration% on *olynomial%. The%e o*eration% are *ermitted
*ro"ided the *olynomial% ha"e the %ame %ymbolic "ariable. But
o*eration% %uch a% trigonometric, logarithmic are not *ermitted@.
Exercise ( "or#in% 0it! Pol'nomials | 2.
Tutorial ) Plottin% 1ra$!s
6et u% learn to *lot %im*le gra*h%. (e 2ill ha"e to generate the data to be
u%ed for the gra*h. 6et u% a%%ume 2e 2ant to dra2 the gra*h of cos x and
sin x for one full cycle ? 2 radian%@. 6et u% generate the "alue% for the $1
a$i%, di"iding the cycle into 1+ eGual inter"al%, 2ith the follo2ing commandB
-->"=&08;pi/1682*;pi'4#
In the abo"e command, note that %pi i% a *redefined con%tant re*re%enting
the "alue of . The command to create a range of "alue%, %0%pi71,02+%pi,
reGuire% a %tart "alue, an increment and an end "alue. In the abo"e e$am*le,
they are 4, /16 and 2 re%*ecti"ely. Thu%, $ i% a "ector containing ##
element%.
>e$t, let u% create the "alue% for the y1a$i%, fir%t column re*re%enting
co%ine and the %econd %ine. They are created by the follo2ing command%B
-->+=&c*s(") sin(")'
>ote that cos($) and sin($) are the t2o column% of a ne2 matri$ 2hich i%
fir%t created and then %tored in y. (e can no2 *lot the gra*h 2ith the
commandB
-->pl*t2%("(+)
The gra*h generated by thi% command i% %ho2n belo2. The gra*h can be
enhanced and annotated. Hou can add grid line%, label% for $1 and y1a$e%, legend
for the different line% etc. Hou can learn more about the plo2d and other
related function% from the online hel*.
Fig. 6.1 Graph of sin(! an" cos(! #sing f#nction plot2d()
In the abo"e command, the number of ro2% in $ and y mu%t be the %ame
and column% of y are *lotted "er%u% $. !ince there i% only one column in $ and
there are ' column% in y, the t2o line% are *lotted on the gra*h, ta)ing $ to be
common for both line%.
Tutorial ) Plottin% 1ra$!s | 21
Fig. 6.2 C#stomi$ation of graphs interactivel%
Hou can cu%tomi9e the gra*h interacti"ely through a of dialog bo$. Hou can
%et a$i% label% ?label, font, font %i9e, colour etc.@, grid ?line ty*e, colour etc.@,
legend for the gra*h%. To do %o %elect 7igure Pro*ertie% from the Edit menu in
the gra*hic 2indo2 ?7ig. +.'@.
To dra2 the grid line% *arallel to the $ andCor y a$i%, choo%e the colour for
the grid a% 4 or more ?by default grid colour i% %et to 11, in 2hich ca%e grid line%
are not dra2n@. Hou can ty*e in a label for the a$e% and %et the font, font %i9e
and colour. Hou could al%o cu%tomi9e the location of the a$e% ?to*, middle or
bottom for $1a$i% and left, middle or right for y1a$i%@. Hou can al%o %*ecify the
a$i% %caling to be either linear ?the default@ or logarithmic. Hou can redefine the
minimum and ma$imum "alue% for the a$e% and re"er%e the direction of the
a$i% ?right to left for $1a$i% for $1a$i% in%tead of left to right or to* to bottom for
y1a$i% in%tead of from bottom to to*@.
Fig. 6.& C#stomi$ation of aes interactivel%
Tutorial ) Plottin% 1ra$!s | 22
Hou can al%o cu%tomi9e e"ery other com*onent of the gra*h, by clic)ing on
the a**ro*riate element in the ob;ect bro2%er on the left.
Hou can e$*ort the gra*h to one of the %u**orted format% ?5I7, Po%t%cri*t,
B-P, E-7@ by clic)ing on 7ile E$*ort in the main menu of the gra*hic
2indo2.
5ra*h% can be annotated through command line% al%o. 5rid%, title%, a$i%
label%, legend% etc. can be added 2ith the follo2ing command%B
-->"=&08;pi/32#2*;pi'4# +(8(1)=c*s(")# +(8(2)=sin(")# +(83)=c*s(")
+sin(")#
-->pl*t("( +)# "7.i%(1)#
-->"title(40<=>=/?@A0<=B CD/B0=?/14( 4"4( 46(")4)#
-->le7en%(4c*s(")4( 4sin(")( 4c*s(") + sin(")4( 1( ;C)#
Fig. 6.' Annotation of plot thro#gh comman"s
D%e the online hel* %y%tem to learn more detail% about the command%
$(rid, $ile, le(end. 7or e$am*le, the legend command abo"e *lace% the
legend in the u**er right ?1@ corner and doe% not dra2 a bo$ around the legend
?%F@. The alternati"e% for *o%ition of legend are 2 for to* left, # for bottom left
and . for bottom right. To dra2 a bo$ around the legend, either u%e %3 or do not
gi"e any "alue and it default% to %3. There are other command%, %uch a% $la'el
and yla'el, that you may find u%eful.
Tutorial ) Plottin% 1ra$!s | 23
Tutorial * Plottin% 3& 1ra$!s
Three dimen%ional *lot% of %urface% can be *lotted 2ith the plo#d()
family of function%. The *lot#d?@ function in it% %im*le%t form reGuire% a grid of
?$, y@ "alue% and corre%*onding "alue% of 1. The grid of ?$, y@ "alue% i% %*ecifie%
2ith the hel* of t2o "ector%, one for $1a$i% and the other for y1a$i%.
-->"=&08;pi/1682*;pi'4# si2e(")
ans =
33. 1.
-->2=sin(") * *nes(")4#
-->pl*t3%("( "( 2)#
The fir%t command on line 1 generate% a column "ector for one full cycle ?4
to 'p@ at an inter"al of pC1+ and ha% %i9e ##$1. 6et u% a%%ume the grid %*acing
to be the %ame along $ and y direction%. Therefore another "ector to re*re%ent
the %*acing of *oint% along y direction i% not reGuired. The %econd command
generate% the 9 "alue% for each *oint on the grid a% the %ine of the $ "alue. Thi%
generate% a # dimen%ional %ine 2a"e %urface.
It i% *o%%ible to ha"e different %*acing of *oint% along and y direction%.
-->"=&08;pi/1682*;pi'4# %isp(si2e("))
33. 1.
-->+=&080.585'# %isp(si2e(+))
1. 10.
-->2=sin(")**nes(+)# %isp(si2e(2))
33. 10.
-->pl*t3%("( +( 2)
Thi% al%o generate% a %ine 2a"e %urface but the %*acing of the grid along
the y direction i% different a% com*ared to the %*acing of the grid along $
direction.
The function plo#d1() i% %imilar to plo#d() e$ce*t that it *roduce% a
colour le"el *lot of a %urface u%ing %ame colour% for *oint% 2ith eGual height.
-->pl*t3%1("( +( 2)
The function plo#d2($, y, 1) generate% a %urface u%ing facet% rather
than grid%. Here $, y and 9 are each t2o dimen%ional matrice% 2hich de%cribe a
%urface. The %urface i% com*o%ed of four %ided *olygon%. $?i, ;@, $?iK1, ;@, $?i, ;K1@
and $?iK1, ;K1@ re*re%ent the $ coordinate% of one facet. H and 9 re*re%ent the y
and 9 coordinate% in a %imilar 2ay.
-->:=linspace(-;pi/2( ;pi/2( 0)#
-->v=linspace(0( 2*;pi( 20)#
-->"=c*s(:)4 * c*s(v)#
-->+=c*s(:)4 * sin(v)#
-->2=sin(:)4 * *nes(v)#
-->pl*t3%2("( +( 2)
The function plo#d#() i% %imilar to plo#d2() e$ce*t that it generate% a
me%h of the %urface in%tead of a %haded %urface 2ith hidden line% remo"ed.
Tutorial * Plottin% 3& 1ra$!s | 24
It i% *o%%ible to *lot multi*le gra*h% on one *age 2ith the hel* of the
su'plo() function. It can %ubdi"ide the *age into a rectangular array of ro2%
and column% and *o%ition a gra*h in a cho%en cell. 7or e$am*le %u'plo(2#5)
di"ide% the *age into ' ro2% and # column% re%ulting in + cell%. Cell% are
counted in %eGuence %tarting from left to*. The abo"e command %*ecifie% that
the out*ut of the ne$t *lot command mu%t be *ut in cell number &, that i% on
ro2 ' and column '. Try out the follo2ingB
-->cl6()#s:bpl*t(121)#pl*t3%3("(+(2)#s:bpl*t(122)#pl*t3%2("(+(2)
Thi% %ho2% the %ame %urface fir%t a% a me%h and the %econd a% a %urface
2ith hidden line% remo"ed, 2ith the gra*h% *laced %ide by %ide. The function
clf() clear% the current gra*hic figure.
Tutorial * Plottin% 3& 1ra$!s | 2
Tutorial - Scilab Pro%rammin% 2an%ua%e
!cilab ha% a built1in inter*reted *rogramming language %o that a %erie% of
command% can be automated. The *rogramming language offer% many feature%
of a high le"el language, %uch a% loo*ing ?for, while@, conditional e$ecution
?if-hen-else, selec@ 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 14 and di%*lay it% "alue each
time, you can try the follo2ing command% at the *rom*tB
-->6*. i=1810
-->%isp(i)
-->en%
The for loo* i% clo%ed by the corre%*onding end %tatement. .nce the loo*
i% clo%ed, the bloc) of %tatement% enclo%ed 2ithin the loo* 2ill be e$ecuted. In
the abo"e e$am*le, the disp(i) command di%*lay% the "alue of i.
Conditional e$ecution i% *erformed u%ing the if-hen-elseif-else
con%truct. Try the follo2ing %tatement% on the command lineB
-->"=10#
-->i6 "E0 then %isp(4/e7ative4)
-->elsei6 "==0 then %isp(4Fe.*4)
-->else %isp(4G*sitive4)
G*sitive
-->en%
Thi% 2ill di%*lay the 2ord =osiive. Hou may al%o notice that the
%tatement disp("=osiive") i% e$ecuted e"en before the )ey2ord end i% ty*ed.
: li%t of all the !cilab *rogramming language *rimiti"e% and command%
can be di%*layed by the command wha(). The command *roduce% the
follo2ing li%tB
if else for while end selec
case >ui e$i reurn help wha
who pause clear resu!e hen do
apropos a'or 'rea? elseif
Hou can learn about each command u%ing the hel* command. Thu% help
while 2ill gi"e com*lete information about while along 2ith e$am*le% and a
li%t of other command% that are related to it. The greate%t ad"antage i% that you
can te%t out e"ery language feature interacti"ely 2ithin the !cilab en"ironment
before *utting them into a %e*arate file a% a function.
7ollo2ing o*erator% are a"ailable in !cilabB
Symbol $peration Symbol $peration
- / -atri$ definition ; !tatement %e*arator
( ) E$traction eg. m P a?)@ ( ) In%ertion eg. a?)@ P m
" Tran%*o%e 6 :ddition
@ !ubtraction + -ulti*lication
Tutorial - Scilab Pro%rammin% 2an%ua%e | 2(
A 6eft di"i%ion 7 8ight di"i%ion
B E$*onent &+ Element 2i%e multi*lication
&A Element 2i%e left di"i%ion &7 Element 2i%e right di"i%ion
&B Element 2i%e e$*onent
7ollo2ing are the boolean o*erator% a"ailable in !cilabB
Symbol $peration Symbol $peration
== EGual to C= >ot eGual to ?al%o D>@
D 6e%% than D= 6e%% than or eGual to
> 5reater than >= 5reater than or eGual to
C >egation
E Element 2i%e :>D F Element 2i%e .8
The boolean con%tant% % and %3 re*re%ent T*+E and %f and %F re*re%ent
FALSE. The follo2ing are e$am*le% for ty*ical boolean o*eration%B
-->a =
int(.an%(3()*100)
5enerate matri$ a 2ith # ro2% and 0 column% and
containing integer random number% bet2een 4 and
144.
-->a == 0
8eturn% a matri$ a 2ith %ame %i9e a% matri$ a, 2ith
element% either T ?if the element i% eGual to 9ero@ or 7
?if the element i% not 9ero@.
-->a E 20
Create% matri$ a 2ith %ame %i9e a% matri$ a, 2ith
element% either 3 ?if the element i% le%% than '4@ or F
?if the element i% greater than or eGual to '4@.
-->b**l2s(a E 20)
7ir%t create% a matri$ of boolean "alue% 3 or F
de*ending on 2hether the element in matri$ a i% le%%
than '4 or greater than or eGual to '4. Then create% a
matri$ 2ith numerical "ale% 1 or 4 corre%*onding to 3
or F, re%*ecti"ely
-->6in%(a E 20)
Create% a "ector containing the indice% of element% of
matri$ a 2hich are le%% than '4
6et u% generate a matri$ a of %i9e #$0 containing random integer "alue%
bet2een 4 and 144 2ith the command a = in(rand(#,.)+1%%). If matri$ a
contain% the element% a% %ho2n belo2B
[a]=
[
21 33 84 6
75 66 68 56
0 62 87 66
]
, then the re%ult% of the "ariou% boolean
o*eration% are gi"en belo2B
To te%t if element% of a are 9eroB
-->a == 0
ans =
C C C C
C C C C
Tutorial - Scilab Pro%rammin% 2an%ua%e | 2)
0 C C C
To te%t if "alue of element% of a are le%% than '&B
-->a E 25
ans =
0 C C 0
C C C C
0 C C C
To te%t if element% of a are greater than or eGual to +'B
-->a >= 62
ans =
C C 0 C
0 0 0 C
C 0 0 0
To te%t if element% of a are not eGual to ++B
a H= 66
ans =
0 0 0 0
0 C 0 0
0 0 0 C
To te%t if element% of a are not eGual to ++, and out*ut 1 in%tead of T and 4
in%tead of 7B
-->b**l2s(a H= 66)
ans =
1. 1. 1. 1.
1. 0. 1. 1.
1. 1. 1. 0.
To find indice% of element% of a 2hich are greater than 34B
-->b=6in%(a > 70)
b =
2. 7. !.
The indice% are counted a% if a 2ere a one dimen%ional "ector and indice%
counted column 2i%e %tarting from the to* of the left mo%t column. To *rint the
"alue% of a corre%*onding to the%e indice% u%e the follo2ing commandB
-->a(b)
ans =
75.
8.
87.
Com*ound boolean o*eration% can be *erformed u%ing the :>D and .8
o*erator%. To te%t if element% of a are greater than '4 and le%% than 34B
-->(a > 20) I (a E 70)
ans =
0 0 C C
C 0 0 0
C 0 C 0
Tutorial - Scilab Pro%rammin% 2an%ua%e | 2*
To te%t if element% of a are either le%% than '& or greater than 3&B
-->(a E 25) J a > 75)
ans =
0 C 0 0
C C C C
0 C 0 C
It i% *o%%ible to te%t if all element% of matri$ a meet a logical te%t u%ing the
and() functionB
-->an%(a > 20)
ans =
C
-->an%(a E 100)
ans =
0
It i% *o%%ible to te%t if at lea%t one element of matri$ a meet% a logical te%t
u%ing the or() functionB
-->*.(a > 20)
ans =
0
-->*.(a E 200)
ans =
0
Tutorial - Scilab Pro%rammin% 2an%ua%e | 2-
Tutorial 1. Scri$t 3iles and 3unction 3iles
!cri*t file% contain any "alid !cilab %tatement% and function%. !cri*t file%
can be 2ritten in the built1in !cilab code editor called Sciad. !ci*ad i% in"o)ed
from the !cilab main menu through Application Editor. (hen a %cri*t file
can be loaded into !cilab 2or)%*ace from !ciPad main menu E,ecute Load
into Scilab ?)eyboard %hort cut Ctrl - L@. Code from Sciad can be loaded into
!cilab 2or)%*ace 2ith the command e$ec("filena!e&sci"), *ro"ided the file
filena!e&sci i% located in the current 2or)ing directory. To load function file%
in a directory other than the current 2or)ing directory, %*ecify the full *ath to
the file.
Fig. 1(.1 Script file in SciPa" being loa"e" into Scilab wor)space
Before being loaded into !cilab 2or)%*ace, the file i% chec)ed for %ynta$. If
code contain% %ynta$ error%, it i% not loaded into !cilab 2or)%*ace, in%tead,
error me%%age% are di%*layed in the !cilab 2indo2. If there are no error%, the
code i% com*iled and loaded into !cilab 2or)%*ace, and become% a"ailable for
u%e. If change% are made to the code in !ciPad, the *roce%% of E,ecute Load
into Scilab mu%t be re*eated. (hen modified code re*lace% *re"iou%ly
com*iled code, !cilab i%%ue% a 2arning indicating that a function ha% been
modified.
The difference bet2een %cri*t file% and function file% i% that %cri*t file% do
not contain any function definition% and contain only !cilab %tatement% mainly
for data in*ut, calculation% and out*ut. The re%ult% of e$ecution of a %cri*t file
are loaded into !cilab 2or)%*ace and hence ha"e global %co*e. That i%, all
"ariable% loaded from a %cri*t file are global "ariable% and are acce%%ible from
the !cilab 2or)%*ace.
.n the other hand, a function file u%ually contain% only function definition%
and no directly e$ecutable !cilab %tatement%. (hen loaded into !cilab
2or)%*ace, only the function code i% loaded. 7unction% o*erate in a modular
fa%hion in that they e$change "ariable 2ith !cilab 2or)%*ace or other function%
only through their in*ut and out*ut argument% and do not acce%% the !cilab
2or)%*ace directly. Variable% 2ithin the function% are local to the function
unle%% it i% an in*ut or out*ut "ariable.
Tutorial 1. Scri$t 3iles and 3unction 3iles | 3.
Dnli)e -atlab or .cta"e, one function file may contain more than one
function definition and there i% no %ti*ulated rule for naming file% e$ce*t that
they mu%t ha"e the e$ten%ion .sci or .sce. 7unction file% can be 2ritten u%ing
any te$t editor %uch a% >ote*ad, and not nece%%arily only 2ith Sciad. But
unli)e Sciad, other editor% cannot load the code into !cilab 2or)%*ace and
2ill ha"e to be loaded 2ith the e$ec() command.
-atlab and .cta"e %ti*ulatie that the name of the file mu%t be the %ame a%
the name of the function it contain%, 2ith a &! e$ten%ion. Con%eGuently, each
function file can contain only one function, but function% can be automatically
loaded into the 2or)%*ace a% and 2hen reGuired in%tead of reGuiring that they
be e$*licitly loaded into the 2or)%*ace by the u%er. Thi% al%o enable% them to
identify if the %ource code ha% been modified and recom*ile and load the
function into the 2or)%*ace on the fly 2hen needed. They organi9e function
file% in folder% and can be told in 2hich folder% to loo) 2hen %earching for a
reGuired function file.
!cri*t file% are an ea%y mean% of loading data into !cilab 2or)%*ace and
thu% a"oid file read o*eration%. Thi% i% becau%e %cri*t file% ha"e direct acce%% to
!cilab 2or)%*ace and can read data from and 2rite data to !cilab 2or)%*ace.
(hile thi% i% ea%y to under%tand and u%e, it can lead to error% in the ca%e of
large and com*le$ %et of data and code.
7unction file% do not ha"e direct acce%% to !cilab 2or)%*ace and therefore
e$change data through the in*ut and out*ut *arameter%. Thi% a**roach i% more
com*le$ but modular. E%%entially, 2e are gi"ing u* %im*licity in e$change for
modularity. 7unction file% ha"e the ad"antage of %e*arating the data in the
global 2or)%*ace from the data u%ed in%ide the body of a function. (hile thi%
hel*% a"oid error%, it add% a layer of com*le$ity for the *rogrammer. Due to
their re%tricted and 2ell defined mean% of data e$change, function% are modular
and ab%tract a com*le$ %et of o*eration% into a %ingle function call. 7unction%
are nece%%ary 2hen you 2i%h to build large and com*le$ *rogram%.
Tutorial 1. Scri$t 3iles and 3unction 3iles | 31
Tutorial 11 3unctions in Scilab
7unction% %er"e the %ame *ur*o%e in !cilab a% in other *rogramming
language%. They are inde*endent bloc)% of code, 2ith their o2n in*ut and
out*ut *arameter% 2hich can be a%%ociated 2ith "ariable% at the time of calling
the function. They modulari9e *rogram de"elo*ment and enca*%ulate a %erie%
of %tatement% and a%%ociate them 2ith the name of the function.
!cilab *ro"ide% a built in editor 2ithin !cilab, called Sciad, 2herein the
u%er can ty*e the code for function% and com*ile and load them into the
2or)%*ace. !ciPad can be in"o)ed by clic)ing on Applications Editor on the
main menu at the to* of the !cilab 2or) en"ironment.
6et u% 2rite a %im*le function to calculate the length of a line in the $1y
*lane, gi"en the coordinate% of it% t2o end% ?$1, y1@ and ?$', y'@. The length of
%uch a line i% gi"en by the e$*re%%ion l =

x
2
x
1

2
y
2
y
1

2
. 6et u% fir%t
try out the calculation in !cilab for the *oint% ?1,0@ and ?14,+@.
-->"1=1# +1=# "2=10# +2=6#
-->%"="2-"1# %+=+2-+1#
-->l=s-.t(%"^2 + %+^2)
l =
!.21!55
>ote that $1, y1, $2 and y2 are in*ut "alue% and the length JGJ i% the out*ut
"alue. To 2rite the function, *roceed a% de%cribed belo2B
1. .*en !ciPad by clic)ing on Application Editor on the main menu.
'. Ty*e the follo2ing line% of code into !ciPad and define the function
len()B
6:ncti*n &K' = len("1( +1( "2( +2)
%" = "2-"1# %+=+2-+1#
K = s-.t(%"^2 + %+^2)#
en%6:ncti*n
In the code abo"e, funcion and endfuncion are !cilab
)ey2ord% and mu%t be ty*ed e$actly a% %ho2n. They %ignify the %tart
and end of a function definition.
The "ariable enclo%ed bet2een %Guare brac)et% i% an out*ut
*arameter, and 2ill be returned to !cilab 2or)%*ace ?or to the *arent
function from 2here it i% in"o)ed@. 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%e@. The in*ut *arameter% $1, y1, $2 and y2 are
the "ariable% bringing in in*ut from the !cilab 2or)%*ace ?or the
*arent function from 2here it i% called@.
#. !a"e the content% of the file to a di%) file by clic)ing 7ile !a"e in
!ciPad and choo%e a folder and name for the file. >ote both the name
and folder for later u%e.
0. 6oad the function into !cilab by clic)ing on E,ecute Load into
Scilab on the !ciPad main menu.
Tutorial 11 3unctions in Scilab | 32
Thi% function can be in"o)ed from !cilab *rom*t a% follo2%B
-->ll = len($$1, yy1, $$2, yy2)
2here ll i% the "ariable to %tore the out*ut of the function and $$1, yy1, $$2 and
yy2 are the in*ut "ariable%. >ote that the name% of the in*ut and out*ut
"ariable% need not match the corre%*onding name% in the function definition.
(ith "er%ion & of !cilab, out*ut from %tatement% in a function are ne"er
echoed, irre%*ecti"e of 2hether the %tatement% end 2ith the %emicolon or not.
D%e the function disp() to *rint out intermediate re%ult% in order to debug a
function.
To u%e the function during %ub%eGuent %e%%ion%, load it into !cilab by
clic)ing on E,ecute Load into Scilab in the main menu. If there are no
%ynta$ error% in your function definition, the function 2ill be loaded into !cilab
2or)%*ace, other2i%e the line number containing the %ynta$ error i% di%*layed.
If it i% loaded %ucce%%fully into the !cilab 2or)%*ace, you can "erify it 2ith the
command whos -ype funcion and %earching for the name of the function,
namely, len.
(e can im*ro"e the abo"e function by re*re%enting a *oint a% an array of
%i9e 1$', 2here the element% re*re%ent the $ and y coordinate% of the *oint.
Thi% 2ay, the function can be re2ritten a% follo2%B
6:ncti*n &K' = len(p1( p2)
K = s-.t(s:,((p2 5 p1).^2))
en%6:ncti*n
The modified function can be u%ed a% follo2%B
-->p1 = &0( 0'# p2 = &( 3'#
-->ll = len(p1( p2)
ll =
5.
!cilab allo2% the creation of in1line function% and are e%*ecially u%eful
2hen the body of the function i% %hort. Thi% can be done 2ith the hel* of the
function deff(). It ta)e% t2o %tring *arameter%. The fir%t %tring define% the
interface to the function and the %econd %tring that define% the %tatement% of
the function. !e"eral %tatement% can be %e*arated by %emi colon% and 2ritten a%
a %ingle %tring.
The abo"e modified function to calculate the length could be defined in1
line a% follo2%B
-->%e66(4&K'=len(p1( p2)4( 4K=s-.t(s:,((p2-p1).^2))4)#
!cilab allo2% the u%er to call a function 2ith fe2er in*ut *arameter% than
the number of in*ut argument% in the function definition. It i% an error to
*ro"ide more in*ut *arameter% than the number of in*ut argument%. The %ame
i% the ca%e 2ith out*ut *arameter% and out*ut argument%.
It i% *o%%ible to inGuire and find the number of in*ut and out*ut
argument% from code 2ithin the function. The function ar(n() return% the
number of in*ut and out*ut argument%, 2hich in turn can be u%ed in%ide the
function body to ta)e deci%ion% %uch a% a%%igning default "alue% in ca%e the
Tutorial 11 3unctions in Scilab | 33
in*ut argument ha% not been %u**lied. D%ing "ariable% that ha"e not been
%u**lied a% argument% re%ult% in an error and the function being aborted.
6:ncti*n &a( b( c' = testa.7("( +( 2)
&*:t( inp' = a.7n(0)#
a = 0# b = 0# c = 0#
,p.int6(L?:tp:t8 ;%MnN( *:t)#
,p.int6(L =np:t8 ;%MnN( inp)#
en%6:ncti*n
(e can no2 load the abo"e function into !cilab 2or)%*ace and call it 2ith
different number of in*ut and out*ut *arameter% and %ee ho2 !cilab re%*ond%B
-->&a( b( c' = testa.7(1( 2( 3)#
?:tp:t8 3
=np:t8 3
-->&a( b( c' = testa.7(1( 2)#
?:tp:t8 3
=np:t8 2
-->&a( b' = testa.7(1)#
?:tp:t8 2
=np:t8 1
-->testa.7(1)#
?:tp:t8 1
=np:t8 1
-->testa.7(1( 2( 3( )#
O--e..*. 58
P.*n7 n:,be. *6 inp:t a.7:,ents
Q.7:,ents a.e 8
" + 2
-->&a(b(c(%'=testa.7(1(2)
O--e..*. 5!
P.*n7 n:,be. *6 *:tp:t a.7:,ents
Q.7:,ents a.e 8
a b c
!u**lying fe2er argument% than reGuired i% acce*table a% long a% that
"ariable i% not being u%ed 2ithin the function, but then %*ecifying an in*ut
"ariable and not u%ing it 2ould be *ointle%%. But, chec)ing the number of in*ut
argument% and a%%igning default "alue% to tho%e not %u**lied i% a good u%e of
the ar(n() function.
!u**lying more argument% than reGuired i% an error and function
e$ecution i% aborted.
!u**lying fe2er out*ut argument% than reGuired i% acce*table, but the
"alue% of "ariable% that are not caught are lo%t for %ub%eGuent u%e.
!u**lying more out*ut argument% than reGuired i% an error and function
e$ecution i% aborted.
Tutorial 11 3unctions in Scilab | 34
Tutorial 12 3ile ,$erations
!cilab can o*erate on file% on di%) and function% are a"ailable for o*ening,
clo%ing reading and 2riting di%) file%. The follo2ing line% of code illu%trate ho2
thi% can be accom*li%hedB
-->n=10# "=25.5# "+=&100 75#0 75# 200( 0'#
-->6%=,*pen(Le"01.%atN( L)N)# // ?pens a 6ile e"01.%at 6*. ).itin7
-->,6p.int6(6%( Ln=;%( "=;6MnN( n( ")#
-->,6p.int6(6%( L;12.6Mt;12.6MnN( "+)#
-->,cl*se(6%)#
Hou can no2 o*en the file e$%1&da in a te$t editor, %uch a% note*ad and
%ee it% content%. Hou 2ill notice that the command% are %imilar to the
corre%*onding command% in C, namely, fopen(), fprinf() and fclose(). >ote
that in the %econd command, the format %tring mu%t be %ufficient to *rint one
full ro2 of the matri$ $y. The %eGuence of o*eration% mu%t al2ay% be, o*en the
file and obtain a file de%cri*tor, 2rite to the file u%ing the file de%cri*tor and
clo%e the file u%ing the file de%cri*tor.
The different mode% in 2hich a file can be o*ened are
r 7or reading from the file. The file mu%t e$i%t. If the file doe% not
e$i%t mo*en return error code 1'.
2 7or 2riting to the file. If the file e$i%t%, it% content% 2ill be
era%ed and 2riting 2ill begin from the beginning of the file. If
the file doe% not e$i%t, a ne2 file 2ill be created.
a 7or a**ending to the file. If the file e$i%t%, it 2ill be o*ened and
2riting 2ill %tart from the end of the file. If the file doe% not
e$i%t, a ne2 one 2ill be created.
The function% !prinf() and !sprinf() are %imilar to !fprinf(), e$ce*t
that they %end the out*ut to the %tandard out*ut %tream and a de%ignated
%tring, re%*ecti"ely, in%tead of to a file. The %tatement% to *rint the abo"e data
to the %tandard out*ut ?!cilab te$t 2indo2@ are a% follo2%B
-->,p.int6(Ln=;%( "=;6MnN( n( ")#
-->,p.int6(L;12.6Mt;12.6MnN( "+)#
Being a %tandard file, it i% not nece%%ary to e$*licitly o*en and clo%e the
%tandard out*ut file. To out*ut the %ame data to the %tring% s1 and s2, u%e the
follo2ing command%B
-->s1 = ,sp.int6(Ln=;%( "=;6MnN( n( ")#
-->s2 = ,sp.int6(L;12.6Mt;12.6MnN( "+)#
8eading data from file% into "ariable% in the !cilab 2or)%*ace i% eGually
ea%y. The file 2ill ha"e to be o*ened in read mode and after "alue% ha"e been
read from file%, the file 2ill ha"e to be clo%ed. The function to read data i%
!fscanf(). The follo2ing code fragment read% an integer and a floating *oint
"alue% from the fir%t line in the data file and %tore them in the "ariable% n and $,
re%*ecti"ely. The %econd !fscanf() %tatement e$ecuted 2ithin the loo*, read%
the "alue% in the %ub%eGuent line% into the "ariable a. 7ollo2ing i% the codeB
Tutorial 12 3ile ,$erations | 3
&6%( e..' = ,*pen(4test.%at4( 4.4)#
&n( "' = ,6scan6(6%( L;6N)#
6*. i = 18
a(i(8) = ,6scan6(6%( L;6 ;6 ;6MnN)#
en%
The function !fscanf() return% n, the number of "alue% read and the "alue
$ read. In thi% e$am*le n mu%t be 1. If thi% "alue i% not 1, 2hich may ha**en if
the file doe% not contain enough data, it indicate% an error. Te%t the abo"e code
2ith the follo2ing data read from the fileB
-10.0
10. 2. 5.
1. 2. 3.
. 5. 6.
7. 8. !.
10. 11. 12.
:fter e$ecuting the abo"e %tatement%, "erify 2hether the data ha% been
read correctly into the "ariable% n, $ and a by di%*laying their "alue%.
Tutorial 12 3ile ,$erations | 3(
Tutorial 13 Pol'nomial Curve 3ittin%
: ty*ical e$*eriment collect% data related to one *arameter ?%ay $@ and the
corre%*onding "alue of a de*endent "ariable ?%ay y@. The%e ob%er"ation% can be
%tored in t2o "ector%, namely, $ and y. D%ing lea%t %Guare regre%%ion, it i%
*o%%ible to com*ute the coefficient% of a *olynomial function, of %ome %elected
degree, for y in term% of $. The eGuation for a *olynomial of degree n can be
e$*re%%ed a%B
y x=a
1
a
2
xa
3
x
2
a
n1
x
n
=

i=1
n1
a
i
x
i1
2here
a
i
, i=1to n1
are un)no2n coefficient% 2hich can be com*uted by
%ol"ing the follo2ing %et of linear %imultaneou% eGuation%B
[

x
0

x

x
2


x
n

x

x
2

x
3


x
n1

x
2

x
3

x
4


x
n2

x
n

x
n1

x
n2


x
2n
]{
a
1
a
2
a
3

a
n1
}
=
{

y

xy

x
2
y

x
n
y
}
or
[ X ] {a}={b}
(e can %ol"e for the un)no2n coefficient% a% follo2%B
a=[ X ]
1
b
.nce the coefficient% are determined, the *redicted "alue% of y for the
ob%er"ed "alue% of $ a% follo2%B
y x=

i=1
n1
a
i
x
i1
6et u% con%ider the %am*le data gi"en belo2B
$ 11 4 1 ' # & 3 /
y 11 # '.& & 0 ' & 0
6et u% fit a fourth order *olynomial eGuation to the abo"e data, of the form
y=a
1
a
2
xa
3
x
2
a
4
x
3
a
5
x
4
The reGuired %imultaneou% eGuation% that mu%t be %ol"ed are a% follo2%B
[
8

x

x
2

x
3

x
4

x

x
2

x
3

x
4

x
5

x
2

x
3

x
4

x
5

x
6

x
3

x
4

x
5

x
6

x
7

x
4

x
5

x
6

x
7

x
8
]
{
a
1
a
2
a
3
a
4
a
5
}
=
{

y

xy

x
2
y

x
3
y

x
4
y
}
The element% of the abo"e matri$ eGuation are li%ted in the table on the
ne$t *age.
Tutorial 13 Pol'nomial Curve 3ittin% | 3)

x
0

x
1

x
2

x
3

x
4

x
5

x
6

x
7

x
8
, '+ 134 1'#' /+,+ 3/'&+ ++&&14 &+,+/&' 0/'4,/++
!imilarly, the right hand "ector i%

y

xy

x
2
y

x
3
y

x
4
y
'0.& 14+.& +3+.& &4#'.& #//40.&
The %imultaneou% eGuation% are therefore a% follo2%B
[
8 26 170 1232 9686
26 170 1232 9686 79256
170 1232 9686 79256 665510
1232 9686 79256 665510 5686952
9686 79256 665510 5686952 49208966
]
{
a
1
a
2
a
3
a
4
a
5
}
=
{
24.5
106.5
676.5
5032.5
39902.5
}
!ol"ing the%e eGuation% gi"e%
a
1
=2.6855
,
a
2
=2.3015
,
a
3
=1.2326
,
a
4
=0.2169
and
a
5
=0.0118
The fourth order *olynomial eGuation i% thu%
y=2.68552.3015 x1.2326 x
2
0.2169 x
3
0.0118 x
4
In !cilab, it i% not nece%%ary to u%e loo*% to com*ute the element% of the
coefficient matri$ and right hand "ector. In%tead, they can be com*uted through
the follo2ing matri$ multi*licationB
-->"" = &".^0 " ".^2 ".^3 ".^'#
-->R = ""4 * ""#
-->b = ""4 * +#
-->a = inv(R) * b
a =
2.68558!5
2.3015!7
-1.2326305
0.2168!15
-0.01181!7
The *redicted "alue% of y ba%ed on thi% *olynomial eGuation can be
com*uted a% follo2%B
-->+(8(2) = "" * a#
Hou can *lot the gra*h of $ again%t ob%er"ed and *redicted "alue% 2ith the
follo2ingB
-->pl*t("( +)
>o2 that 2e )no2 ho2 thi% calculation i% to be done, let u% 2rite a
function to automate thi%. 6et the function interface be a% follo2%B
Interface: &a( +6' = p*l+6it("( +( n)
Input Parameters:
" = column vector of observed values of the independent variable
+ = column vector of observed values of dependent variable
Tutorial 13 Pol'nomial Curve 3ittin% | 3*
n = de!ree of the pol"nomial fit
Output parameters:
a = column vector of coefficients of the pol"nomial of order n that minimi#es the least
s$uares error of the fit a
i
, i=1 2... , n1 %here a
i
is the coefficient of the term
x
i1
. a
is a column vector.
+6 = a column vector of calculated values of y for a pol"nomial of order n.
The function polyfi() can be 2ritten in !ciPad and then loaded into
!cilab 2or)%*aceB
6:ncti*n &a( +6' = p*l+6it("( +( n)
"" = 2e.*s(len7th(")( n+1)#
6*. i = 18n+1
""(8(i) = ".^(i-1)#
en%
R = ""4 * ""#
b = ""4 * +#
a = inv(R) * b#
+6 = "" * a#
en%6:ncti*n
.nce the function i% %ucce%%fully loaded into the !cilab 2or)%*ace, to fit a
fourth order *olynomial, you can call it a% follo2%B
-->&a( +(8(2)' = p*l+6it("( +( )#
-->&a5( +(8(3)' = p*l+6it("( +( 5)#
-->pl*t("( +( "( +6)# "title(4G?KS/?@=QK BD<TA C=00=/>4( 4"4( 4+4)#
The fir%t line calculate% the coefficient% a of the *olynomial fit and the
calculated "alue% yf for a 0
th
order *olynomial fit and the %econd line doe% the
%ame for a &
th
order *olynomial fit. The calculated "alue% of y for each are
%tored in the '
nd
and #
rd
column% of y. The third line *lot% the gra*h of ra2 data
and fitted "alue% and add% a title for the gra*h and label% for the $ and y a$e%.
Fig. 1&.1 Pol%nomial c#rve fitting b% least s*#are metho"
Tutorial 13 Pol'nomial Curve 3ittin% | 3-
Tutorial 14 4eadin% +icrosoft Excel 3iles
!cilab *ro"ide% a %et of function% to read -icro%oft E$cel file%. !cilab
"er%ion &.1.1 can read E$cel file% u* to "er%ion '44# ?.$l%@, but not -icro%oft
E$cel '443 ?.$l%$@ file%. The %te*% to read an E$cel file areB ?i@ o*en the file 2ith
$ls_open() function, ?ii@ read the file content% 2ith the $ls_read() function
and ?iii@ clo%e the file 2ith the !close() function. Before o*ening the file, chec)
the current 2or)ing directory and change it if nece%%ary. :lternati"ely, note the
com*lete *ath to the file and u%e it to define the full *ath to the file location.
:%%uming the cell% A/'C0 in
Sheet/ contain data a% %ho2n in
the ad;oining figure, Halue i% a
#$0 matri$ containing, 3e$Ind i%
a #$0 matri$ and 443 i% a 1$#
matri$ a% %ho2n in the out*ut
abo"e.
7ollo2ing i% a %im*le e$am*le of ho2 you can o*en and read an E$cel file,
a%%uming that the file i% named es&$ls and i% located in the current 2or)ing
directoryB
-->&6%( sst( sheetna,es( sheetp*s' = "ls$*pen(4test."ls4)#
-->&val:e( te"t' = "ls$.ea%(6%( sheetp*s(1))#
-->,cl*se(6%)#
-->%isp(val:e)
1. 2. 3. /an
. 5. 6. /an
7. 8. !. /an
-->%isp(te"t)
0. 0. 0. 1.
0. 0. 0. 2.
0. 0. 0. 3.
-->%isp(sst)
O ?ne 0)* 0h.ee O
The function $ls_open() return% four "alue%, fd i% the file de%cri*tor that
i% reGuired during %ub%eGuent o*eration% on the file, 443 i% the "ector of all
%tring% in the file, 4heena!es i% the "ector of name% of all %heet% in the file and
4heepos i% a "ector of number% indicating the beginning of different %heet%
2ithin the file. The function $ls_read() reGuire% the file de%cri*tor fd and the
sheepos of the %heet to be read, *re"iou%ly obtained from $ls_open() function
call. To read the content% of the fir%t %heet, u%e sheepos(1). Thi% function
return% t2o "alue%, Halue i% a matri$ of containing all numerical "alue% in the
%heet 2ith 2an ?>ot a number@ in the cell *o%ition% containing non1numeric
data and e$ i% a matri$ containing 9ero% corre%*onding to cell% containing
numeric data and non19ero "alue% corre%*onding to %tring data. The non19ero
integer% in e$ *oint to the corre%*onding %tring "alue %tored in the "ariable
ss returned by the $ls_open() function call.
Tutorial 14 4eadin% +icrosoft Excel 3iles | 4.
There i% another function read$ls() that can read E$cel file%. It i% %im*ler
to u%e com*ared to the $ls_open(), $ls_read() and !close() combination.
-->sheets = .ea%"ls(4test."ls4)#
-->s1 = sheets(1)#
-->a = s1.val:e
a =
O 1. 2. 3. ?ne O
O . 5. 6. 0)* O
O 7. 8. !. 0h.ee O
-->s1.te"t
-->ans =
O ?ne O
O 0)* O
O 0h.ee O
The !cilab "ariable editor edivar can %ho2 the "alue% read by read$ls()
function and al%o allo2 editing and u*dating of "alue% to !cilab 2or)%*ace.
-->e%itva. s1
Tutorial 14 4eadin% +icrosoft Excel 3iles | 41
Tutorial 1 Some +iscellaneous Commands
!ome command% related to o*eration% on di%)% are im*ortant and they are
li%ted belo2. They are reGuired 2hen you 2ant to change the directory in
2hich your function file% are %tored or from 2here they are to be read.
p)%
Print% the name of the current 2or)ing directory
7etc)%()
!ame a% pwd.
ch%i.(4%i.4)
Change% the 2or)ing directory to a different di%)
location named dir.
In "er%ion &.1.1, changing directory ha% become ea%y and "i%ual. In the
main menu, go to File Change current directory... and visuall" bro%se to the
folder %hich "ou %ant to ma&e the current director" and clic& OK. 'he error prone
tas& of t"pin! len!th" folder paths is no lon!er necessar".
It i% *o%%ible to %a"e all the "ariable% in the !cilab (or)%*ace to a di%) file
%o that you can Guic)ly reload all the "ariable% and function% from a *re"iou%
%e%%ion and continue from 2here you left off. The command% u%ed for thi%
*ur*o%e areB
save(4p6.bin4)
!a"e% entire content% of the !cilab 2or)%*ace
?"ariable% and function%@ in the file pf&'in in the
current 2or)ing directory.
l*a%(4p6.bin4)
8e%tore% content% of the !cilab 2or)%*ace from the file
pf&'in in the current 2or)ing directory.
save(4p6.bin4( "+)
!a"e% only the "ariable $y of the !cilab 2or)%*ace in
the file pf&'in in the current 2or)ing directory.
It i% *o%%ible to determine the %i9e of "ariable% in the !cilab 2or)%*ace
2ith the follo2ing commandB
si2e(")
8eturn% a 1$' matri$ containing the number of ro2%
and column% in the matri$ $.
len7th(")
8eturn% the number of element% in matri$ $ ?number
of ro2% multi*lied by number of column%@.
clc
Clear the %creen. :lternately, you can *re%% the F2
function )ey
!cilab maintain% a hi%tory of the command% ty*ed at the command
*rom*t. Hou can %croll u* and do2n the command hi%tory 2ith the u*
arro2 ?@ and do2n arro2 ?@ )ey% on the )eyboard. Hou can mo"e to the left
and right along one of the *re"iou% command% from the command hi%tory 2ith
the left arro2 ?@, right arro2 ?@, Home and End )ey% on the )eyboard. Hou
can delete any of the character% 2ith the Delete or Bac)%*ace )ey%. In%ert ne2
character%. Hou can *re%% the Enter )ey to com*lete the command.
Tutorial 1 Some +iscellaneous Commands | 42
Tutorial 1( &ata Structures
!cilab ha% %u**ort for data %tructure%, %uch a% cell, hyper!arices, lis,
!lis, rlis, lis and sruc. Thi% tutorial 2ill touch u*on %ome of them,
namely, lis, cell and sruc.
List
The li%t data %tructure allo2% o*eration% %uch a% in%ertion, deletion,
concatenation.
-->lst=list(1( &1( 2# 3( '( &LaN( LbN( LcN')# // c.eate a list
-->lst(1) // e"t.act 6i.st ele,ent *6 lst
ans =
1.
-->lst(2)
ans =
1. 2.
2. .
-->lst(3)
ans =
Oa b cO
-->si2e(lst)
ans =
3.
-->lst(9) // e"t.act last ele,ent *6 lst
ans =
Oa b cO
-->lst(9+1) = LQppen% t* listN# // appen% t* lst
-->lst(9)
ans =
Qppen% t* list
-->lst(0) = L=nse.t be6*.e listN#
list(1)
ans =
=nse.t be6*.e list
Cell
The cell data %tructure allo2% the creation of matrice% in 2hich each
element can be of different ty*e. >ormally, all element% of a matri$ mu%t be of
the %ame data ty*e. Thu% a matri$ cannot ha"e one element a% a number 2hile
another element i% a %tring. The cell data %tructure doe% not ha"e thi% limitation.
In fact, each element in turn can be another data %tructure, thereby allo2ing
con%truction of com*le$ data %tructure%.
-->a = cell(3) // c.eate a cell )ith 3 .*)s an% 3 c*l:,ns
a =
OUV UV UV O
O O
Tutorial 1( &ata Structures | 43
OUV UV UV O
O O
OUV UV UV O
-->b = cell(3(1)# // c.eate a cell )ith 3 .*)s an% 1 c*l:,n
-->c = cell(2( 3( )# // c.eate a cell )ith 2 .*)s( 3 c*l:,ns an%
ca.%s
-->b(1).ent.ies = 183#
-->b(2).ent.ies = L1cilabN#
-->b(3).ent.ies = &1 2# 3 '#
-->b
b =
O&1 2 3' O
O O
ON1cilabN O
O O
OU2"2 c*nstantV O
-->b(1)
ans =
&1( 2( 3'
-->b(2)
ans =
L1cilabN
-->b(3)
ans =
ans =
U2"2 c*nstant)
-->b(3).ent.ies
ans =
1. 2.
3. .
-->b.%i,s
ans =
3 1
-->si2e(b)
ans =
3. 1.
Struct
The sruc data %tructure allo2% the creation of u%er defined data ty*e%
2ith u%er defined field name%. The sruc i% analogou% to the sruc in C
*rogramming language, but i% more fle$ible in that the ne2 field% can be
created e"en after the sruc ha% been created. Each field can %tore any ty*e of
!cilab data, including matrice%, cell%, li%t% or e"en other sruc%.
-->%t = st.:ct(4%ate4( 15( 4,*nth4( 4Q:74( 4+ea.4( 1!7)
%t =
%a+8 15
,*nth8 LQ:7N
+ea.8 1!7
Tutorial 1( &ata Structures | 44
The abo"e data %tructure contain% three field%, namely, day, month and
year. (hile day and year %tore numeric data, month %tore% %tring data. (e can
acce%% the indi"idual field% 2ith the . ?dot@ o*erator.
-->%t.%a+
ans =
15.
-->%t.,*nth
ans =
Q:7
-->%t.+ea.
ans =
1!7
(e can create ne2 field% by %im*ly a%%igning data to a ne2 filed name.
-->%t.%esc = L=n%epen%ence %a+N
%t =
%a+8 15
,*nth8 LQ:7N
+ea.8 1!7
%esc8 L=n%epen%ence %a+N
: %truct need not be e$*licitly created u%ing the sruc() function and a
field can al%o contain matrice%.
-->s.t+pe = L1paceN#
-->s.:nit.len7th = L,N#
-->s.:nit.6*.ce = LW/N#
-->s.n*%es = &1 0 0 0# 2 0 5 0# 3 8 5 0# 8 0 0'#
-->s
s =
t+pe8 L1pace
:nit8 &1"1 st.:ct'
n*%es8 &" c*nstant'
-->s.:nit
ans =
len7th8 L,N
6*.ce8 LW/N
-->s.n*%es
ans =
1. 0. 0. 0.
2. 0. 5. 0.
3. 8. 5. 0.
. 8. 0. 0.
Data %tructure% hel* organi9e data in a hierarchical manner and %im*lify
*a%%ing of data from one function to another. It no2 become% *o%%ible to *ac)
data into a %ingle ob;ect and un*ac) the ob;ect to e$tract indi"idual field%. Thu%
*a%%ing a %ingle ob;ect a% the argument to a function gi"e% acce%% to all the
indi"idual field% 2ithin the ob;ect in%ide it. Pre"iou%ly, 2e 2ould ha"e to *a%%
each field of the data %tructure% a% an indi"idual argument.
Tutorial 1( &ata Structures | 4
:% an illu%tration, let the different attribute% re*re%enting a %tructure be
?i@ ty*e of %tructure ?*o%%ible "alue% are %*ace, *lane, tru%%, grid etc.@, ?ii@ node
coordinate% ?a matri$ 2ith # column%@, ?iii@ member connecti"ity ?matri$ 2ith
# column%@ and ?i"@ material ?a matri$ 2ith # column%@. To *a%% the
information about a %tructure to a function, 2e 2ill ha"e to u%e four argument%.
In%tead, if 2e define a data %tructure 2ith the four attribute% a% it% field%, 2e
2ill need to *a%% only one argument to the function, and 2ithin the function, 2e
can acce%% the indi"idual field%.
Tutorial 1( &ata Structures | 4(
Tutorial 1) &irected 1ra$!s
roblem &efinition
It i% intended to re*re%ent a t2o dimen%ional tru%% in matri$ notation and
de"elo* function% to calculate the follo2ingB
1. function to calculate the length of a member gi"en it% Id
'. function to calculate the *ro;ection% along $1 and y1a$e% and angle
made by a member 2ith the $1a$i%
#. function to li%t the %tart and end node% of a %*ecified member
0. function to calculate the total length of all member%
&. function to li%t the member% meeting at a node
+. function to calculate the number of member% meeting at each ;oint
&ata &efinition
>ode% of a tru%% ha"e $ and y coordinate%. Interconnecti"ity of member% i%
e$*re%%ed in term% of a connecti"ity matri$. The connecti"ity matri$ re*re%ent%
the number% of the node% connected by a gi"en member.
1ode Coordinates
1ode , y
1 4.4 4.4
' #.4 4.4
# +.4 4.4
0 1.& 0.4
& 0.& 0.4
#ember Connecti2ity
#ember 1ode i 1ode 3
1 1 '
' ' #
# 0 &
0 1 0
& ' 0
+ ' &
Tutorial 1) &irected 1ra$!s | 4)
1
2
3
4 5
1 2
3
4
5
6 7
3 m 3 m
4

m
3 # &
Thi% data can be in*ut into t2o "ariable%, $y and conn, a% follo2%B
-->"+ = &0 0# 3 0# 6 0# 1.5 # .5 '
-->c*nn = &1 2# 2 3# 5# 1 # 2 # 2 5# 3 5'
Before attem*ting to 2rite the function%, let u% try out the command%
interacti"ely and "erify our under%tanding of the %te*%. To di%*lay the gra*h of
the tru%%, 2e mu%t fir%t define the gra*h data %tructure 2ith the hel* of the
!a?e_(raph() function and then u%e show_(raph() function.
-->tail = c*nn(8(1)4# hea% = c*nn(8(2)4#
-->&n*%es( %:,,+' = si2e("+)#
-->7 = ,aWe$7.aph(40.:ss4( 1( n*%es( tail( hea%)#
-->7.n*%es.7.aphics." = "+(8(1)4*100#
-->7.n*%es.7.aphics.+ = "+(8(2)4*100#
-->sh*)$7.aph(7)#
The abo"e command% *roduce the figure %ho2n abo"e. 6et u% no2 2rite
the function% to com*lete the "ariou% a%%igned ta%)%.
7unction rows($) return% the number of ro2% in a matri$ $.
6:ncti*n &.' = .*)s(")
&.(%:,,+' = si2e(")#
en%6:ncti*n
7unction (e_nodes(id, conn) return% the number% of the node% n1 and
n2 at the end% of member 2ith number id.
6:ncti*n &n1( n2' = 7et$n*%es(i%( c*nn)
n1 = c*nn(i%( 1)# // B*l:,n 1 *6 c*nnectivit+ ,at.i" st*.es sta.t
n*%e
n2 = c*nn(i%( 2)# // B*l:,n 2 *6 c*nnectivit+ ,at.i" st*.es en%
n*%e
en%6:ncti*n
7unction len(id, $y, conn) return% the length G of a member.
6:ncti*n &K' = len(i%( "+( c*nn)
&n1( n2' = 7et$n*%es(i%( c*nn)#
p1 = "+(n1(8)# p2 = "+(n2(8)# // B**.%inates *6 n*%es n1 an% n2
%" = p2 - p1# // Xi66e.ence *6 c**.%inates *6 n1 an% n2
K =s-.t(s:,(%" .^2))# // C*.,:la 6*. len7th 6.*, anal+tical
7e*,et.+
en%6:ncti*n
Tutorial 1) &irected 1ra$!s | 4*
7unction proJecions(id, $y, conn) return% the $ and y *ro;ection% d$
and dy of a member a% 2ell a% the angle hea made by the member 2ith the $1
a$i%.
6:ncti*n &%"( %+( theta' = p.*Yecti*ns(i%( "+( c*nn)
&n1( n2'=7et$n*%es(i%( c*nn)#
p1 = "+(n1(8)# p2 = "+(n2(8)#
%i66e.ence = p2 - p1#
%" = %i66e.ence(1)# // "-p.*Yecti*n is in c*l:,n 1 *6 %i66
%+ = %i66e.ence(2)# // +-p.*Yecti*n is in c*l:,n 2 *6 %i66
i6 %" == 0 then
theta = ;pi/2#
else
theta = atan(%+/%")#
en%
en%6:ncti*n
7unction oal_len(h($y, conn) return% the total length of all member%
in a tru%%.
6:ncti*n &t*t$K'=t*tal$len7th("+( c*nn)
,e,be.s = .*)s(c*nn)#
6*. i%=18,e,be.s
K(i%)=len(i%("+(c*nn)#
en%
// %isp(K)#
t*t$K = s:,(K)#
en%6:ncti*n
7unction !e!'ers_a_node(nodeid, conn) return% the number% of
member% meeting at a node.
6:ncti*n &,e,lst' = ,e,be.s$at$n*%e(n*%ei%( c*nn)
,e,lst = 6in%( (c*nn(8(1)==n*%ei%) J (c*nn(8(2)==n*%ei%) )#
en%6:ncti*n
7unction nu!_!e!'ers_a_node(nodeid, conn) return% the number of
member% meeting at node.
6:ncti*n &n' = n:,$,e,be.s$at$n*%e(n*%ei%( c*nn)
,e,lst = ,e,be.s$at$n*%e(n*%ei%( c*nn)#
n = len7th(,e,lst)#
en%6:ncti*n
7unction !a?e_russ(na!e, $y, conn) *re*are% the data %tructure to
re*re%ent a tru%%.
6:ncti*n &7' = ,aWe$t.:ss(na,e( "+( c*nn( scale)
t = c*nn(8(1)4# // tail n*%e n:,be.s
h = c*nn(8(2)4# // hea% n*%e n:,be.s
7 = ,aWe$7.aph(na,e( 1( .*)s("+)( t( h)#
7.n*%es.7.aphics." = "+(8(1)4 * scale#
7.n*%es.7.aphics.+ = "+(8(2)4 * scale#
en%6:ncti*n
Tutorial 1) &irected 1ra$!s | 4-
D%ing the data *re"iou%ly entered, 2e can te%t the abo"e function% a%
%ho2n belo2B
-->7 = ,aWe$t.:ss(40.:ss4( "+( c*nn( 100)#
-->sh*)$7.aph(7)
The other function% can al%o be te%ted a% follo2%B
-->t*t$len = t*tal$len7th("+( c*nn)# %isp(t*t$len)
26.088007
-->6*. i = 18.*)s("+)
--> n = n:,$,e,be.s$at$n*%e(i( c*nn)#
--> ,e,$lst = ,e,be.s$at$n*%e(i( c*nn)#
--> %isp(&n ,e,$list')
-->en%
2. 1. .
. 1. 2. 5. 6.
2. 2 7.
3. 3 . 5.
3. 6. 7.
(e can al%o te%t the length and *ro;ection function% in a %ingle for loo*B
-->6*. i = 18.*)s(c*nn)
--> K = len(i( "+( c*nn)#
--> &%"( %+( theta' = p.*Yecti*ns(i( "+( c*nn)#
--> %isp(&%" %+ theta K')
-->en%
3. 0. 0. 3.
3. 0. 0. 3.
3. 0. 0. 3.
1.5 . 1.2120257 .272001!
-1.5 . -1.2120257 .272001!
1.5 . 1.2120257 .272001!
-1.5 . -1.2120257 .272001!
The node number% and member number% can be di%*layed on the gra*h
u%ing the Vie2 .*tion% from the main menu of the gra*hic% 2indo2. 5ra*h
%etting% can be modified from the 5ra*h !etting% from the main menu.
5ra*h %etting% that can be changed are node diameter, edge 2idth, border node
2idth, edge colour inde$ etc.
Tutorial 1) &irected 1ra$!s | .
Tutorial 1* Princi$al Stresses
In the theory of ela%ticity, the %tre%% com*onent% at a *oint are
re*re%ented a% a matri$ con%i%ting of # normal %tre%%e% on the diagonal and
three %hear %tre%%e% off the diagonal. The condition of eGuilibrium reGuire% that
the matri$ be %ymmetric. : ty*ical %tre%% matri$ i% 2ritten a% follo2%B
[]=
[

x

xy

xz

xy

y

yz

xz

yz

z
]
The *rinci*al *lane% are defined a% tho%e *lane% on 2hich only normal
%tre%%e% act and are free of %hear %tre%%e%. They al%o re*re%ent the ma$imum
"alue% of the normal %tre%% at that *oint. The condition for a *lane to be a
*rinci*al *lane and the %tre%%e% on it to be the *rinci*al %tre%% i% e$*re%%ed in
the form of a homogeneou% eGuationB
[

x

xy

xz

xy

y

yz

xz

yz

z

]{
l
x
l
y
l
z
}
=
{
0
0
0
}
2here i% the *rinci*al %tre%% and
l
x
,
l
y
and
l
z
are the direction co%ine% of
the out2ard normal to the *rinci*al *lane. It i% reGuired to determine the
*rinci*al %tre%%e% and the direction co%ine% of the *rinci*al *lane%. The
characteri%tic eGuation of the abo"e %et of homogeneou% eGuation% i%

3
I
1

2
I
2
I
3
=0 . Being a cubic eGuation, it ha% three root% and thu% there
are three "alue% of *rinci*al %tre%%e%. Corre%*onding to each *rinci*al %tre%% i%
one *rinci*al *lane re*re%ented by the direction co%ine% of it% out2ard normal.
In mathematic%, thi% i% an eigen"alue *roblem, the eigen"alue% are the *rinci*al
%tre%%e% and eigen"ector% are the direction co%ine% of the out2ard normal to
the *rinci*al *lane%.
The *rinci*al %tre%%e% and *rinci*al *lane% can thu% be obtained 2ith the
spec function in !cilab. :% an e$am*le, let the %tre%% matri$ be a% follo2%B
[]=
[
3 2.5 4
2.5 1 0
4 0 2
]
Then the %olution can be obtained a% follo2%B
-->s = &3( 2.5( # 2.5( 1( 0# ( 0( 2'#
-->&"( p' = spec(s)
p =
-2.316605 0. 0.
0. 1.2!6876! 0.
0. 0. 7.137836
" =
0.652!665 -0.0!7!278 0.75102!3
-0.756!28 -0.826!! 0.3060537
-0.58!3651 0.5571020 0.5850523
Tutorial 1* Princi$al Stresses | 1
The eigen"alue% are on the diagonal of the matri$ p. The column% of
eigen"ector $ corre%*ond to the eigen"alue on the diagonal. Thu% the fir%t
column of $ corre%*ond% to the *rinci*al %tre%% "alue -2&.#1,,%5. The
eigen"alue% are arranged in increa%ing order 2herea% the *rinci*al %tre%%e%
are, by con"ention, arranged in decrea%ing order. Thu% the fir%t *rinci*al %tre%%
i%

1
=7.1347836
, %econd *rinci*al %tre%% i%

2
=1.2968769
and the third
*rinci*al %tre%% i%

1
=2.4316605
. The fir%t *rinci*al *lane i% defined by the
direction co%ine%
l
x
=0.7510293
,
l
y
=0.3060537
and
l
z
=5850523
.
The %olution can al%o be obtained by an alternati"e 2ay, u%ing
*olynomial%. In thi% a**roach the eigen"alue% can be be obtained ea%ily but
obtaining the eigen"ector% may in"ol"e a fe2 additional manual calculation%.
But it ha% the ad"antage that 2e can obtain the characteri%tic eGuation, 2hich i%
not a"ailable in the fir%t %olution. Here, let u% fir%t define a %eed for a *olynomial
a% p = poly(%, "p") and u%e it to re*re%ent the homogeneou% eGuation% a%
follo2%B
-->p = p*l+(0( 4p4)#
-->ss = s 5 e+e(s) * p#
-->ch = %et(ss)
ch =
2 3
-22.5 + 11.25p + p - p
-->.**ts(ch)
ans =
1.2!6876!
-2.316605
7.137836
To obtain the eigen"ector corre%*onding to a cho%en eigen"alue, 2e mu%t
%ub%titute the "alue of the eigen"alue into the homogeneou% eGuation%, and
choo%e any t2o of the three eGuation% and %ol"e them for t2o of the direction
co%ine% in term% of the third.
[

i

yz

yz

z

i
]{
l
y
l
z
}
=l
x
{

xy

xz
}
The abo"e eGuation% can be %ol"ed to obtain
l
y
and
l
z
in term% of
l
x
.
6et
l
y
=k
y
l
x
and
l
z
=k
z
l
x
. Then u%ing the fact that the %um of the %Guare of
the direction co%ine% i% unity, all the three direction co%ine% can be obtained.
l
x
2
l
y
2
l
z
2
=1 or 1k
y
2
k
z
2
l
z
2
=1
8earranging and %ol"ing 2e getB
l
x
=
1

1k
y
2
k
z
2
,
l
y
=k
y
1

1k
y
2
k
z
2
and
l
z
=k
z
1

1k
y
2
k
z
2
(e could 2rite our o2n function% to do the%e calculation%, ta)ing care to
%ort the eigen"alue% before *roceeding 2ith the eigen"ector calculation%.
Tutorial 1* Princi$al Stresses | 2

You might also like