Sei sulla pagina 1di 16

Introduction to Scientific 

Computing Tool ‐ Scilab

Chao‐Chieh Lan and Jian‐Hao Liou
Department of Mechanical Engineering
National Cheng Kung University
National Cheng Kung University

2009/09/22 1
Computing Tools for Mechanism Analyses
p g y
Low level Medium level High level
g
C++、Fortran Mathematica、Matlab Working Model、ADAMS

L
Low level:
l l
•General‐purpose •Fast computation  
•Matrix manipulation is cumbersome •No direct graphics support
•Difficult to debug
Medium level:
•Matrix manipulation is easy •Direct graphics support •Easy to debug
•Less general •Slower computation
High level:
g
•Graphical user interface •You don’t know if the software cheat you! 2
Scilab – A Free Alternative to Matlab
A Free Alternative to Matlab
• Scilab is a scientific software package for numerical 
computations. It was initially developed by INRIA and ENPC in 
France. 
• Scilab is an open source software. 
Since 1994 it has been distributed 
freely along with the source code
freely along with the source code 
via the Internet. Now, the latest 
edition is 5.1.1.
edition is 5.1.1. 
• Scilab syntax is largely based on the 
MATLAB language
MATLAB language.

• Other free alternatives to Matlab include Octave and RlabPlus.

3
Scilab Layout 
y
Command window:
*.sce is a script file that you can store 
you program. 
*.sci is a function file that you can store 
you frequently used subroutines. 

Scipad:
You can edit *.sce file 
and *.sci files here

4
Basic Operations
p
• The simplest way to execute scilab code is to type it 
in at the prompt, >> , in the graphical command 
window. 
• Scilab doesn’t need to declare a variable before 
using it. No pointers are required either.
• Scilab is a case‐sensitive software. For example:
A=[1 2;3 4];   but ‘a’ is not equal to ‘A’
• Use “;” after the command to suppress output to screen.
• π = %pi,   imaginary number =%i, %e = 2.71828 
• Lines after “//” are comments. Use “…” to split a long line into two lines.

• atan(p/q): 2‐quadrant inverse tangent;  atan(p,q): 4‐quadrant inverse tangent

• Cross product:No such command for Scilab. You have to write it by yourself.
Cross product:No such command for Scilab You have to write it by yourself
• Scilab is 1‐indexing, different from C++ which is 0‐indexing.  5
Matrix Manipulations
(1) The row elements of matrix are separated by space or comma (,) ; the column 
elements of matrix are separated by semicolon (;) , for example:
A=[1 2 3;4 5 6]                     A=[                  ]
(2) The multiplication and product of matrices: A=[                 ]     B=[                 ]
The multiplication of A and B is A*B’
h l i li i f d i * ’ or A’*B
’*
the product of  A ∙ B is  A .* B

(3) Set A is a matrix A’ is the transposition matrix of A A [


(3) Set A is a matrix, A’ is the transposition matrix of A.   A=[                 ] A’ [
]   A’=[            ] 
]

(4)   Denotes the solution to the matrix equation AX = B.
if a1*x1+a2*x2=b1 a3*x1+a4*x2=b2
if a1*x1+a2*x2=b1, a3*x1+a4*x2=b2
Set A=[ ] , B=[     ] , x=[   ]                 X=A\B

(5)   Colon is a very useful tool in Scilab. 

6
Programming: Loop and Conditional  Control

(1)  ‘for’ :for index = start : increment : end


statements
end

(2) ‘while’ : (3) ‘if’: (4)  ‘break’:it can 


while   expression if logical expression then interrupt loops.
statements statements
else then
else then
end
d
statements
end

7
Programming Tips and Useful 
Commands
• Put
Put the command 
the command “clear”
clear  and 
and “close”
close  in the very beginning of a 
in the very beginning of a “*.sce
sce” 
script file to delete previous results.
• Put
Put a command 
a command “pause”
pause  in between the lines to serve as a break point. 
in between the lines to serve as a break point.
Use “return” to continue executing.
• Use command “who_user” to list user’s defined variables.

• browsevar(): an embedded function which you can browse and edit existing 
variables.
a ab es
• fsolve: find a zero of a system of n nonlinear functions. Its calling sequence is ” 
[[x]=fsolve(x0 , ’fun’ , tol) “, ‘x’ is the optimizer solution, ‘x0’ is the initial value
] so e( 0 , u , to ) , s t e opt e so ut o , 0 s t e t a a ue
which you starting guess at the solution, ‘fun’ is the function that you want to 
solve, and ‘tol’ is the precision tolerance.

8
Programming of Function
of Function
(1) Edit the function on SciPad
(2) “cross”
(2)   cross  is the function name. You 
is the function name You
can name it by yourself.
(3) f is the output of the function. u1, 
u2, u3, v1,v2 and v3 are the input 
2 3 1 2 d 3 th i t
parameters of the function.
(4)   Save the file as “cross.sci”

(5) Load the function:exec(‘file path’) 
and the you can execute it
d h i
(6)   The variables defined inside the 
function are local. They do not 
affect the variables outside the 
function.

9
Graphics:Plot
p
(1)  xinit: Open a new graphic. If you don’t do this data will be plotted in the 
same graphic.
(2)  plot(x,y): Use this command to plot the curve about the relationship of ‘x’ 
and ‘y’, where x and y are both vectors.
(3) l t (i 1) Fi d t th l ti
(3)  locate(i,1): Find out the location of the points(p1,p2….pi)
f th i t ( 1 2 i)

(a)  the points which you choose. (b)  the coordinates of the points.

10
Graphics:Line Properties
p p
(1) Use
Use the commend ged()
the commend ged() to 
to
change the properties of figure.

((2) Choose the item that you want to   
) y
change its properties.
(3)  Change type and width of the curve.
(4)  Change the color of the curve.
11
Graphics:Axes
Graphics Axes Properties
Properties

(1) Key in the label of axes.
(2) Choose the item to adjust the properties of axes
(2)  Choose the item to adjust the properties of axes.
(3)  Change the font size of label.
(4)  The font size of the axes.

12
Graphics:Export
Graphics Export and Print
and Print
(1) Choose
“file”        “Export”

(2)  Choose the “Export   
type“ item, and decide 
the file type that you 
want to save.

File               Print            Postscript  

13
Animation of mechanisms

Slider crank mechanism demo 
(ex 4.3, p140 of Cleghorn)

14
Animation of cams (ex 8.7, p.397 of Waldron)

Oscillating Roller Follower


Oscillating  Roller Follower

15
Scilab Browse Help and Web Resources

• Scilab hompage
http://www.scilab.org/
• The instruction of Scilab (1)
The instruction of Scilab (1)
http://msdl.me.ncku.edu.tw/scilab/Site/Scilab_files/cintro.pdf
• The instruction of Scilab (2)
h //
http://mail.nkmu.edu.tw/~crlin/SciLab_Tutorial/SciLab_tut.htm
l k d / l / b l/ b h
16

Potrebbero piacerti anche