Anda di halaman 1dari 48

Basic of Scilab

Part II

Scilab Basic
Strings
A string is an array of characters and can include
letters, digits, other symbols and spaces
Created using single quote delimiter () or a
double quote delimiter ()
String data is stored as a string in SCILAB (not as
a matrix)

Scilab Programming
Boolean Arrays
Two types of Boolean constants
%t or %T is used when the statement is true
%f or %F is used when the statement is false
Used frequently in programming control
flow statements
Consist of RELATIONAL and LOGICAL
operators.

Scilab Programming
Relational Operator
A relational operator compares two numbers by
determining whether a comparison statement
(e.g.5<8) is true or false
Relation
Description
al
Operator
<
Less than
>
Greater than
<=
Less than or equal to
=>
Greater than or equal to
==
Equal to
<> or ~= Not equal to

Scilab Programming
Relational Operators Example

Scilab Programming
Logical Operators
Logical Operators are:
not (~),
and (&)
or (|)
A logical operator
examines true / false
statements and
produces a result which
is true(T) or false (F)
according to the
specific operator.
General rule of thumb :
zero is false, everything
else is true.

Scilab Programming
Input/Output Function in Scilab
Input and output devices are called logical units
Used to provide information to and to retrieve
information from a program.
An important component of SCILAB scripts and
functions.

disp function to print the output onto the console


input function to allow user to key an input into
console

Scilab Programming
List of Functions for Input/Output
SCILAB

Descriptions

input

Read from keyboard to display in


Scilab window

disp

Write input argument to Scilab


window

write / read

Write / Read matrix of


strings/numbers from ASCII file
(under Fortran type format)

save / load

Save current Scilab variables in


binary file
Load variables previously saved
with save

loadwave /
savewave

Read / Write a.wav sound file

loadmatfile /

Load / Save variables from / to file

Scilab Programming
Files in SCILAB Environment
Files are usually a collection of data in a
computer that can be placed in a storage
unit.
In SCILAB, there are two types of files :
Text files. Files that are text-based and can be
opened with NotePad, WordPad, SciPad or PFE
(Programmers File Editor). Examples of text files
include uncompiled libraries, uncompiled functions
and script files.
Binary files. Files that are created by specialized
software and can only be opened by its machine
language. Generally used to save storage space for
computation. Cannot be opened by text editor

Scilab Programming
Saving and Loading Variables
Variables in SCILAB can be saved and loaded in
binary format when necessary
The commands are save and load

Scilab Programming
Importing and Exporting
Data to SCILAB
For data transfer between
ASCII files and SCILAB, there
are C or Fortran based
commands that are available
in SCILAB
The Fortan commands are
write and read :

Scilab Programming
The Scinotes Editor
--> scinotes

Scilab Programming
Scilab Script File (*.sce)
ALL SCILAB script files are saved with a .sce
extension
The // marker is used as comment that provide
an explanation of your code to yourself and others.
Remember,
ALL SCILAB SCRIPT FILES NEEDS TO BE CALLED BY
SCILAB for it to work.

The command used to run SCILAB script files in


SCILAB is called exec.
-- >exec(myscript.sce)

Scilab Programming
Scilab Script File (*.sce)
Click on File and <save as> follow by <*.sce>

Scilab Programming
Output of the filesce1.sce

Practice Exercises
Create a script file that will allow the user to input
score for five quizzes and output the calculated the
average of the five quizzes.
Sample Output:

Scilab Programming
Scilab Function File (*.sci)
Click on File and <save as> follow by
<*.sci>
Key points of a function file :
A function always begins with function and
ends with endfunction
The function will now be available to call either
from other scripts or the command line (using
exec)
When executing files you need the complete
path, Scilab doesnt store user defined paths.
All variables used in executing the function file
is not

Scilab Programming
Structure of a Function File

body

Comments

Keyword: function

Output Argument(s)

Function Name (*.sci)

function x = fact(k)
// FACT Factorial of Numbers
// where x is the output
argument
// and k is the input argument
// Created by A.
k=int(k);
x=1;
for j=1:k,x=x*j;end
endfunction

-- > Output_value = fact(input_value)

Input Argument(s)

Command line syntax

Scilab Programming
Example of a function file

Practice Exercise
The area (A) of a triangle with sides of length a, b
and c is given by :
A = s (s - a)(s b)(s c)
where s = (a + b + c)/2. Write a SCILAB
function that will accept the values a, b and c as
inputs and return the value of A as output.
Filename should be myarea.sci
Find area (A) of triangle by testing the following
values:
a=3, b=4, c=5
a=6, b=6, c=6
a=6, b=8, c=10

Scilab Programming
Programming Construct
Conditional Program Construct
if then end,
if then else end,
if then elseif then else end
case select
Iterative Program Construct
while end
for end

Scilab Programming
Programming Examples (Conditional
Construct)
1.) Create a Scilab function named oddeven.sci
that will prompt the user to input an integer
number and determine if the number entered is
odd or even.
2.) The most popular grading system in theUnited
Statesuses discrete evaluation in the form of letter
grades (A-F).Create a Scilab function named
lgrade.sci that will prompt the user to input letter
grade (A-F and output the corresponding grade
percentage.
A = 90 100
B = 80 89

Scilab Programming
Solution to Programming Example 1:

Scilab Programming
Solution to Programming Example 2:

Scilab Programming
Programming Example (Iterative Construct)
Create a Scilab function named myfactorial.sci
that will prompt the user to input an integer
number and output its factorial.
Solution 1

Scilab Programming
Solution 2

Solution 3

Practice Exercises
1.) In number theory, the Euclidean algorithm is
used for finding the greatest common divisor for
two integers a and b (not zero) is based on the
following fact:
If r is the remainder when a is divided by b,
then gcd (a,b) = gcd (b,r).
See the following Pseudocode:
Euclid(a,b)
begin
while (b != 0)
begin
temp = a
a=b
b = temp
b=b%a
end
return(a)

Practice Exercises
2.) A mathematician named Ulam proposed
generating a sequence of numbers from any
positive integer n (n > 0) as follows:
If n is 1, stop.
If n is even, the next number is n/2.
If n is odd, the next number is 3*n + 1.
Continue with this process until reaching 1.
Ex: an input of 3 gives ->10 ->5 -> 16 ->8 ->
4 -> 2 ->1
3.) Create a function that will determine if the
number entered is PRIME or NOT PRIME.

Plotting and
Visualization
Graphics commands are executed on windows
different from the main one (the Scilex window)
There are many type of plots available in Scilab.

Histogram
2D plots
Projections
3D plots
Polygons
Vector fields
Matrix plots
Contour plots
Arcs
Bar / error bar
Bitmaps
Gray scale (images)
Latex, MathML, support
Surface
parameterized and many, many more

Plotting and
Visualization

Basic Plot
scf() - create figure
clf()
- to clear figure
xdel() - to close figure
plot(x, y)
x and y can be numbers, vectors or matrices
If x is omitted, a series of integers will be
assumed
If x and y are numbers then a single point is
drawn
If x and y are vectors (same length) then a line
is drawn
If x and y are matrices then a line is drawn for
every

Basic Plot

Plotting and
Visualization

Basic Plot

Plotting and
Visualization

LineSpec - an optional argument that can be


used inside a plot command to customize each new
line aspect

Basic Plot

Plotting and
Visualization

Plotting and
Visualization

Basic Plot
xgrid adds a grid on a 2D plot.

Plotting and
Visualization

Basic Plot
Adding Graph Annotation

Practice Exercise
Multiplot on One Axis.
Using the plot command, obtain the plots below.
Remember to label your graph on X and Y axis,
together with the Title and Legend.

Plotting and
Visualization

subplot
subplot(m,n,p) or subplot(mnp) breaks the
graphics window into an m-by-n matrix of subwindows and selects the p-th sub-window for
drawing the current plot.

Plotting and
Visualization

Output of subplot

Plotting and
Visualization

plot2d
plot2d plots a set of 2D curves.

Plotting and
Visualization

Variations of plot2d
plot2d2 2D plot step function
plot2d3 2D plot vertical bars
plot2d4 2D plot arrows style

plot2d2()

plot2d3()

plot2d4()

Plotting and
Visualization

fplot2d
fplot2d plots a curve defined by the external
function f.
Example :
cos(3x)

Plot the function

y = sin x 2.5

Plotting and
Visualization

histplot
plot a histogram

Practice Exercise
Plot the function

1- x2
y
where
x2 is from 0 to 10
1 x

Plotting and
Visualization

xrect draws a rectangle

Plotting and
Visualization

xarc
draws a part of an ellipse
xarc(x,y,w,h, a1, a2)
where:
x, y, w, and h are four real values defining a
rectangle.
a1,a2 are real values defining a sector.

Plotting and
Visualization

xpoly
draws a poly line or a polygon
xpoly(xv,yv [,dtype [,close]])
where :
xv,yv are matrices of the same size (points of the
polyline).
dtype are string (drawing style). default value is
"lines".
close integer. If 1, the polyline is closed; default
value is 0.

Conclusion
Scilab is an excellent teaching tool and will make
a huge impact in engineering and mathematics
based curriculum for students;
Benefits of SCILAB for different users :
interactive mode is great for novice
programmable mode is great for experts

It has the facility to integrate code written in C or


Fortran and extend its features which makes it a
great tool for research;
It is an ideal pedagogical (instruction methods)
tool where matrix computations and graph
visualization are involved;
Finally, it is free to distribute and use.

References
Documentation and Support Weblink:
http://www.scilab.org/support/documentation

SCILAB Manual:
http://www.scilab.org/support/documentation/ma
nuals
SCILAB File Exchange:
http://fileexchange.scilab.org/

Anda mungkin juga menyukai