Scilabforverybeginners - Scilab ESI Group

Transcription

ScilabforverybeginnersScilabEnterprisesS.A.S- ‐143bisrueYvesLeCoz- ‐78000Versailles(France)- ‐www.scilab- ‐enterprises.com

Thisdocumenthasbeenco- ol)inAntony,Hauts- ‐de- ‐Seine(France). erybeginners- ‐2/33

workspacecustomization11Chapter2- lities33Scilabforverybeginners- ‐3/33

le:registrantscancommunicatewitheachotherbye- ties/user zone/mailing ilabforverybeginners- ‐4/33

aceinScilabconsistsofseveralwindows: Afterdouble- ge11):Intheconsoleaftertheprompt“-- orrespondingresult.-- 57/4ans 14.25-- (2 9) 5ans keyboard'sarrowkeys s- ‐5/33

th“ ultiplication,“/“fordivision,“ “forexponents.Forexample:-- 2 3.4ans sthesquareroot):-- sqrt(9)answhile:-- SQRT(9) !--error 43.Undefined variable: eandπ:-- %e%e-- %pi %pi2.7182818 splayediinoutput:-- 2 3*%ians 2. theresultisnotdisplayed.-- (1 sqrt(5))/2;-- (1 sqrt(5))/2ans 1.618034Scilabforverybeginners- ‐6/33

2).Forexample:-- exp(10)/factorial(10)ans intheembeddedhelpbyclickinginthemenubaron? ScilabHelp.Thetabkey ftertypingintheconsolethecommand:-- iredfunctionorselectitwiththemouseorwiththekeys owareparticularlyuseful.Applications ontrolTointerruptarunningprogram,youcan: TypepauseintheprogramorclickonControl nallcases,theprompt“-- “willturninto“-1- “,theninto“-2- “ ckonControl ityofreturn,typeabortintheconsoleorclickonControl Abortinthemenubar.Scilabforverybeginners- ‐7/33

sticoninthetoolbar swithadefaultfilenamed“Untitled inOptions Auto- enabledbydefault: (,[, if,function, mple,wecalculate10termsofthesequence(𝑢! )definedby:𝑢! 1𝑢!!! 2𝑢! 3Mention- ccountinthecalculations.- ‐Tochangethefont,clickonOptions Preferences.- isisnotthecase,clickonFormat - ‐8/33

SavingAnyfilecanbesavedbyclickingonFile sareavailable: Execute“ tory).Execute“ .Execute“ leofcurve,typeintheconsole:-- plotMention- .- entfigure“).Ifseveralgraphicwindowsareopen,youc cilabforverybeginners- ‐9/33

efulin3- ications,clickonEdit inehelpToaccesstheonlinehelp,clickon? ScilabHelpinthemenubar,ortypeintheconsole:-- Forexample:-- help eginners- ‐10/33

ight. beginners- ‐11/33

Chapter2- lineprecededby“-- ds(calculationresults,errormessages ).Donotwrite“-- esentedinatable(without“-- esofdimension1 norn 1andanumberisitselfamatrixofdimension1 r:-- a!--error 4Undefined variable : nerror:-- a %pi/4a 0.7853982-- aa finedbythesystem:-- Piby2 %pi/2Piby2 erybeginners- ‐12/33

eisautomaticallyassignedtothevariablecalledans:-- 3*(4-2)ans 6.-- ansans d.-- function d dollars(e,t); d e*t; endfunction-- dollars(200,1.4)ans dusingthefollowingcommands:-- function y f(x); y 36/(8 exp(-x)); endfunction-- function y g(x); y 4*x/9 4; latevalues:-- f(10)ans 4.4999745-- g(12.5)ans 9.5555556Scilabforverybeginners- ‐13/33

easilyusingthe“ isplay:-- v [3;-2;5]v 3.- 2.5.Todefinearowvectorandobtainarowdisplay:-- v [3,-2,5]vMentionThiscommandc analsobetypedundertheform:v [3 -2 5] 3.- 2.5.Todefineamatrixandobtainatabulardisplay:-- m [1 2 3;4 5 6;7 8 9]m 1.2.3.4.5.6.7.8.9.MentionThiscommandc analsobetypedundertheform:m [1,2,3;4,5,6;7,8,9]Scilabforverybeginners- ‐14/33

ththevectorvpreviouslydefined:-- v(2)ans - 2.-- disp(v(2))- uotes:-- disp("Bob won")Bob ga“ “betweenthedifferentparts:-- d 500;-- disp("Bob won " string(d) " dollars")Bob won 500 -- disp("It''s fair")It's icsequence.Wegive: beginningvalue:step:endingvalue sof1from3and10:-- 3:10ans o10:-- 1:2:10ans 1.3.5.7.9.Scilabforverybeginners- ‐15/33

orwhichdecreasesinstepsof4from20to2:-- u 20:-4:2u mberofiterationsiswrittenwithfor byrecurrenceby:𝑢! 4𝑢!!! 𝑢! 2𝑛 3AlgorithmScilabEditorPut4inu(1)u(1) 4;Fornfrom1to20for n 1:20u(n 1)takesthevalueu(n) 2n 3u(n 1) u(n) 2*n leTostopaloopwhenagivengoalisreached,while orPut1.2inh(h treeheight)h 1.2;Put2005iny(y year)y 2005;Whileh 7while h 7htakesthevalueh 0.3(thetreegrows)h h 0.3;ytakesthevaluey 1(oneyearpasses)y y 1;EndwhileendDisplayy(thefinalyear)disp("I will cut the.tree in " susing“.“( nners- ‐16/33

GreaterLessorequalGreaterorequal TrueFalseAndOrNo%T%F& onsareapproachedand“ hetests“ “and“ “willcomparetermbyterm.Forexample:-- X [1,2,5]; Y [5,3,5];-- X Yans F F TTotestiftwovectorsareequal,isequalisusedand isequalisusediftheyaredifferent:-- isequal(X,Y)ans F-- isequal(X,Y)ans TIf thenThebasicconditionalstatementsarethefollowing: if then else end,if then elseif then else end.if f then.Scilabforverybeginners- ‐17/33

Example:Alicethrowsthreedice. Ifshegetsthree6’sshewins wins 10,Ifshegetstwoidenticalnumbersshewins teAlice’swinningsusingthefunctions: grand(seepage22), eraltimesinD, DD grand(1,3,"uin",1,6);IfAlicegetsthree6,thenif D [6,6,6] thenAlicewins20dollarsW wins10dollarselseif length(unique(D)) 1 thenW 10;elseif length (unique(D)) 2 wins5dollarsW 5;elseOtherwiseW 0;AlicewinsnothingendEndifdisp("Alice wins " .DisplayAlice'swinningsstring(W) " dollars")2- ‐Dand3- nsofcolorandpointsstylewithinquotes: Colors"b" blue(bydefault),"k" black,"r" red,"g" green,"c" cyan,"m" magenta,"y" yellow,"w" white. PointstylesJoined(bydefault),or"."," ,"v"," ",and" ".Scilabforverybeginners- ‐18/33

[2,5])Plotsofplanecurvesdefinedbyfunctionsy f(x)Forafunction𝑥 𝑓(𝑥)thevaluesofxarespecifiedusingthelinspace commandbywriting:x iable𝑥 ,bthehighestvalueof s𝑓and𝑔definedovertheinterval[- ‐2;5]by:𝑓 𝑥 (𝑥 ! 2𝑥)𝑒 !! ,and𝑔 𝑥 sin!!Scilabforverybeginners- ‐19/33

bluebydefault:ScilabEditorGraphicsWindowfunction y f(x)y (x 2 2*x)*exp(-x)endfunctionx unction y g(x)y sin(x/2)endfunctionx stoplotthesequencesofpoints𝑀(𝑛, 𝑢 𝑛 )aftercalculatingthecoordinates𝑢 (𝑛)ofavector𝑢 nquotes:redandnon- andareconnected.ScilabEditorGraphicsWindowfor n 1:50u(n) (-0.8) n;endclf; plot(u,"*r")Scilabforverybeginners- ‐20/33

lot(X,Y," ")willplotascatterplotof𝑀 (𝑋! ; 𝑌! )withbluetriangles.ScilabEditorGraphicsWindowX [1,3,3,7,7,9,10];Y [8,7,5,5,4,2,2];clf; plot(X,Y," exampleswillillustrate3- atrixofdimension𝑛 pointwithX- ‐coordinate𝑥!andY- ‐coordinate𝑦! .Toplotthesurfacedefinedbyafunctionoftheform𝑧 𝑓(𝑥, 𝑦),itisnecessaryto: Definefunction𝑓Calculatez feval(x,y,f)'feval(x,y,f)returnsthe𝑚 𝑛  matrixwhose𝑖𝑗elementis𝑓 (𝑥! , 𝑦! ��'“Executesurf(x,y,z).Toplotthesurface𝑧 2𝑥 ! 𝑦 nction z f(x,y)z 2*x 2 y 2;endfunctionx linspace(-1,1,100);y linspace(-2,2,200);z - ‐21/33

amedimensionandcorrespondstothepoints(𝑥! , 𝑦! , 𝑧! )onthecurve.Toplotthehelixdefinedby(𝑥 cos 𝑡 , 𝑦 sin 𝑡 , 𝑧 𝑡):ScilabEditorGraphicsWindowt ndomsequences ger,𝑚and𝑛integersand𝑚 𝑛.-- t grand(1,4,"uin",1,6)t 3.1.3.6. ,𝑎and𝑏realand𝑎 𝑏.-- tr grand(1,2,"unf",-1,1)tr - 0.74602640.9377355Scilabforverybeginners- ‐22/33

tedonpage32.Keepparticularlyinmind: bEditorGraphicsWindowx [1:10];n [8,6,13,10,6,4,16,7,8,5];clf; bar(x,n) csWindowX [1,2,5];n1 [5,10,5];n2 witchfromonelinetoanother.-- m [1 2 3;4 5 6]m heform:m omodifythem.Scilabforverybeginners- ‐23/33

-- m(2,3)ans 6.-- m(2,3) 23m thematrixm,type:-- m(2,:)ans 4.5.23.Andthethirdcolumn:-- m(:,3)ans inglequotesymbol“'“:-- m'ans /“.-- A [1,2,3;4,5,6]A 1.2.3.4.5.6.-- B [1;1;2]Scilabforverybeginners- ‐24/33

B 1.1.2.-- A*Bans Matrixmultiplication9.21.-- A*ADimensionsarenotconsistent!--error 10Inconsistent multiplication.-- A.*Aans Elementwisemultiplication1.4.9.16.25.36.-- 2*(A 2)ans ntbecause2isanumber-- A/Aans 1.1.518D-163.795D-151.GivesthematrixXforwhichX*A tionaccuracy,page29).-- A./Aans aseofvectors:-- C 1:4C 1.2.3.4.-- C*CDimensionsarenotconsistent!--error 10Inconsistent multiplication.Scilabforverybeginners- ‐25/33

-- C.*Cans 1.4.9.16.ItisalsopossibletowriteC rationelementwise.Thisisnotthecasewithmatrices.-- 1/Cans vectorXforwhichC*X 10.06666670.10.1333333-- (1)./Cans 1.0.50.33333330.25ReverseelementwiseAspreviously,C er1.Itisalsopossibletowrite1 sTosolvethelinearsystem  AX Y,inwhichAisasquarematrix,usethebackslash“\“X rixZforwhichZA Y.Tosolvethesystem:142531 𝑋 61-- A [1 2 3;4 5 6];-- Y [1;1];-- X A\YX - 0.50.0.5-- A*Xans 1.1.Scilabforverybeginners- ‐26/33

-- v [2,6,9,6,-4,0,2]v 2.6.9.6.- 4.0.2.2.2.6.6.9.6.2.2.0.- 4.-- gsort(v,"g","i")ans - 4.0.-- gsort(v)ans ns(rows,columns)foramatrix.-- U [1:10]U 1.2.3.4.5.6.7.8.9.10.-- length(U)ans 10.-- m [1 2 3;4 5 6];-- size(U)ans 2.3.Scilabforverybeginners- ‐27/33

- U [1:10];-- sum(U)ans 55.-- prod(U)ans ndsortstheminascendingorder.-- v [2,6,9,6,-4,0,2]v 2.6.9.6.- 4.2.6.9.0.2.-- unique(v)ans - lerthan5:-- w [1,5,3,8,14,7,3,2,12,6]; find(w 5)ans ements𝑤! , 𝑤! , s- ‐28/33

Tofindalltheelementsofthevector𝑤equalto3:-- w [1,5,3,8,14,7,3,2,12,6]; find(w 3)ans ionNumbershaveanabsolutevaluebetweenabout2.2 10- ‐308and1.8 10 le1:Computationofsin(π)-- sin(%pi)ans 1.225D-16Thevalueof𝑠𝑖𝑛 thetrianglewithsides𝟑,1et2isaright- ‐angledtriangle:-- a sqrt(3)a 1.7320508-- b 1b 1.-- c 2c 2.-- a 2 b 2 c 2ansTheprogramrespondsfalsebecausethevalueofa 2 b 2isapproximate FScilabforverybeginners- ‐29/33

-- abs(a 2 b 2-c 2) sioniscalledfor F-- abs(a 2 b 2-c 2)/c 2 ioniscalledfor ,typeformat(20).Reconsidering𝑎 3:-- a or. 3.-- heerror-- a 2ans .𝑦 ! 𝑡 𝑓 𝑡, 𝑦 �(𝑡)arevectors,𝑦 𝑡! 𝑦!thenapplytheodefunction:y ode(y0,t0,t,f),with: ion yprim f(t,y)yprim(1) .yprim(2) .yprim(n) .endfunctionScilabforverybeginners- ‐30/33

𝑦! 1𝑦! 1Thesolutionyisamatrixofdimension n T : 𝑦! 1𝑦! 2𝑦! 2 𝑦! 2 𝑦! 𝑇𝑦! 𝑇 𝑦! 𝑇 𝒚" 𝟒𝒚Example:Solvingthedifferentialequation𝒚 𝟎 𝟑, 𝒚! 𝟎 ationsoforder1asdefinedby:𝑌 𝑚 1 2𝑦′ , 𝑌𝑝𝑟𝑖𝑚 ���𝑟𝑖𝑚 2 4 𝑌(1)CommentScilabEditorfunction yprim uesoftforplotting.yprim(1) y(2);yprim(2) -4*y(1) tfort0 0; tmax initialconditions.t t0:0.05:tmax;Weexecuteode.y0 3; yprim0 0;Weplottheintegralcurveofywithrespecttot.y ode([y0;yprim0],t0,t,f);clf; plot(t,y(1,:))Scilabforverybeginners- ‐31/33

Chapter3–UsefulScilabfunctionsAnalysis heinteger𝑛forwhich𝑛 𝑥 𝑛 𝑛 1 𝑥 𝑛.Probabilityandstatistics factorial(n)returnsthefactorialofnwithn positiveorzerointeger.grand(1,p,"uin",m,n) returnsavectorof prandomintegersequencestakenbetweenmandn with p positiveinteger,mandnintegersand𝑚 𝑛.grand(1,p,"unf",a,b) returnsavectorof prandomrealsequencestakenbetweenaandb with p positiveinteger,aandbrealand𝑎 thenumberofcoordinatesofvector𝑣 an(v)returnstheaverageofthevectorofnumbers𝑣 ector.bar(v,n,couleur)drawsthebargraphwith𝑣asX- ‐coordinate,𝑛asY- 2,3 asX- graphwith𝑣asX- ‐coordinate,n1asY- ‐coordinateinblueandn2asY- ‐coordinateingreen,with𝑣 and𝑝positiveintegers,returnsamatrix𝑛 𝑝isrealbetween0and1,floor(rand() p)willbe1with𝑝probabilityand0with1 𝑝probability.Scilabforverybeginners- ‐32/33

Displayandplot clf rlyspacedbetween𝑎and𝑏 thecurrentone.surfallows3- fied.plot(Y," leofthevaluesofA.disp("Sentence" �� aphicswindow.Utilities ofitsrepeatedco

Scilab!forvery!beginners!;"2/33! ycée!Descartes!