of 16
IntroductiontoScientificComputingTool Scilab
ChaoChiehLanandJianHao LiouDepartmentofMechanicalEngineering
National Cheng Kung UniversityNationalChengKungUniversity
2009/09/22 1
ComputingToolsforMechanismAnalysesp g y
Lowlevel Mediumlevel HighlevelC++Fortran MathematicaMatlab
gWorkingModelADAMS
L l lLowlevel:
FastcomputationGeneralpurpose
NodirectgraphicssupportMatrixmanipulationiscumbersome
Difficulttodebug
Mediumlevel:
Directgraphicssupport EasytodebugMatrixmanipulationiseasy
Highlevel:
SlowercomputationLessgeneral
g
Graphicaluserinterface Youdontknowifthesoftwarecheatyou!2
Scilab A Free Alternative to MatlabScilab AFreeAlternativetoMatlab
Scilabisascientificsoftwarepackagefornumericalcomputations.ItwasinitiallydevelopedbyINRIAandENPCinFrance.
Scilabisanopensourcesoftware.Since1994ithasbeendistributedfreely along with the source codefreelyalongwiththesourcecodeviatheInternet.Now,thelatestedition is 5.1.1.editionis5.1.1.
ScilabsyntaxislargelybasedontheMATLAB language
OtherfreealternativestoMatlabincludeOctaveand RlabPlus.
MATLABlanguage.
3
Scilab LayoutyCommandwindow:
*.sceisascriptfilethatyoucanstoreyouprogram.
*.sciisafunctionfilethatyoucanstoreyoufrequentlyusedsubroutines.
Scipad:
Youcanedit*.scefileand*.scifileshere
4
BasicOperationsp Thesimplestwaytoexecutescilab codeistotypeitinattheprompt,>>,inthegraphicalcommandwindow.
Scilabdoesntneedtodeclareavariablebeforeusingit.Nopointersarerequiredeither.
Scilabisacasesensitivesoftware.ForexampleA=[12;34];butaisnotequaltoA
Use;afterthecommandtosuppressoutputtoscreen.
=%pi,imaginarynumber=%i,%e=2.71828
Linesafter// arecomments.Usetosplitalonglineintotwolines.
atan(p/q):2quadrantinversetangent;atan(p,q):4quadrantinversetangent
Cross productNo such command for Scilab You have to write it by yourself CrossproductNosuchcommandforScilab.Youhavetowriteitbyyourself.5 Scilabis1indexing,differentfromC++whichis0indexing.
MatrixManipulations(1) Therowelementsofmatrixareseparatedbyspace orcomma(,) ;thecolumn
elementsofmatrixareseparatedbysemicolon(;),forexample:
(2) Themultiplicationandproductofmatrices A=[]B=[]h l i li i f d i * *
A=[123;456]A=[]
ThemultiplicationofAandBisA*B orA*B
theproductofA B isA.*B
(3) Set A is a matrix A is the transposition matrix of A A [ ] A [ ]
(4)DenotesthesolutiontothematrixequationAX=B.
if a1*x1+a2*x2=b1 a3*x1+a4*x2=b2
(3)SetAisamatrix,AisthetranspositionmatrixofA.A=[ ]A=[]
ifa1*x1+a2*x2=b1,a3*x1+a4*x2=b2
SetA=[ ],B=[ ],x=[ ]X=A\B
(5)ColonisaveryusefultoolinScilab.
6
Programming:LoopandConditionalControl
(1)forforindex=start : increment : endstatements
end
(2) while (3) if (4)breakitcanwhileexpression
statements
d
iflogicalexpressionthen
statements
else then
interruptloops.
end elsethenstatements
end
7
ProgrammingTipsandUsefulCommands
Put the command clear and close in the very beginning of a * sce Putthecommand clear and close intheverybeginningofa .sce scriptfiletodeletepreviousresults.
Put a command pause in between the lines to serve as a break point.Putacommand pause inbetweenthelinestoserveasabreakpoint.Usereturntocontinueexecuting.
Usecommandwho_usertolistusersdefinedvariables.
browsevar():anembeddedfunctionwhichyoucanbrowseandeditexistingvariables.
fsolve:findazeroofasystemofnnonlinearfunctions.Itscallingsequenceis[x]=fsolve(x0,fun,tol),xistheoptimizersolution,x0istheinitialvalue
a ab es
[ ] so e( 0 , u , to ) , s t e opt e so ut o , 0 s t e t a a ue
whichyoustartingguessatthesolution,funisthefunctionthatyouwantto
solve,andtolistheprecisiontolerance.
8
Programming of FunctionProgramming ofFunction(1) EditthefunctiononSciPad(2) cross is the function name You(2) cross isthefunctionname.You
cannameitbyyourself.(3) fistheoutputofthefunction.u1,
2 3 1 2 d 3 th i tu2,u3,v1,v2andv3aretheinputparametersofthefunction.
(4)Savethefileascross.sci
(5) Loadthefunctionexec(filepath)d h iandtheyoucanexecuteit
(6)Thevariablesdefinedinsidethefunctionarelocal.Theydonotaffectthevariablesoutsidethefunction.
9
GraphicsPlotp(1)xinit: Openanewgraphic.Ifyoudontdothisdatawillbeplottedinthe
samegraphic.
(2)plot(x,y): Usethiscommandtoplotthecurveabouttherelationshipofxandy,wherexandyarebothvectors.
(3) l t (i 1) Fi d t th l ti f th i t ( 1 2 i)(3)locate(i,1):Findoutthelocationofthepoints(p1,p2.pi)
(a)thepointswhichyouchoose. (b)thecoordinatesofthepoints.
10
GraphicsLinePropertiesp p(1) Use the commend ged() to(1) Usethecommendged() to
changethepropertiesoffigure.
(2)Choosetheitemthatyouwantto( ) ychangeitsproperties.
(3)Changetypeandwidthofthecurve.
(4)Changethecolorofthecurve.11
GraphicsAxes PropertiesGraphics AxesProperties
(1)Keyinthelabelofaxes.(2) Choose the item to adjust the properties of axes(2)Choosetheitemtoadjustthepropertiesofaxes.
(3)Changethefontsizeoflabel.
(4)Thefontsizeoftheaxes.
12
GraphicsExport and PrintGraphics ExportandPrint(1) Choose
fileExport
(2)ChoosetheExporttypeitem,anddecidethefiletypethatyouwanttosave.
FilePrintPostscript
13
Animationofmechanisms
Slidercrankmechanismdemo(ex4.3,p140ofCleghorn)
14
Animationofcams (ex8.7,p.397ofWaldron)
Oscillating Roller FollowerOscillatingRollerFollower
15
Scilab BrowseHelpandWebResources
Scilabhompagehttp://www.scilab.org/
The instruction of Scilab (1) TheinstructionofScilab(1)http://msdl.me.ncku.edu.tw/scilab/Site/Scilab_files/cintro.pdf
TheinstructionofScilab(2)h // l k d / l / b l/ b hhttp://mail.nkmu.edu.tw/~crlin/SciLab_Tutorial/SciLab_tut.htm
16