Copyright © Michael Richmond.
This work is licensed under a Creative Commons License.
Introduction to Scilab
- Scilab is interpreted, not compiled
- Variables
- no need to declare in advance -- in fact, can't declare
- no need to specify type
- can switch from one type to another
- assign value with '='
- if type name all by itself, with no semicolon, value printed to screen
(in fact, this is true for any expression which doesn't end in
semicolon)
- Workspace
- save it with save filename
- restore with load filename
- record all commands typed with diary filename
- stop recording with diary off
- Script files
- place commands into a file mycommands.sci
(the name doesn't have to end with .sci,
but it may make the file easy to recognize)
- type exec mycommands.sci, and everything is executed
- Arithmetic
- standard operators + - * /
- exponent operator ^
- use parantheses to specify order of operations
- there are no C-like ++ or +=
- Built-in Functions
- lots and lots and lots of 'em --
see
the section on elementary functions on the Scilab home page
or just type help elementary
for the basic math functions
- all trig functions take arguments in radians (not degrees)
(and no, pi is not pre-defined for you)
- i is pre-defined to mean "square root of negative 1";
square roots of negative numbers yield complex results
- type help foobar to get help on function "foobar"
- Printing values
- Method 1: type the variable or expression without semicolon
- Method 2: use the disp function
- Method 3: for maximum flexibility,
use mprintf (sends output to screen),
mfprintf (sends output to a file) or
msprintf (sends output to a string variable)
- Example: print to screen: mprintf('value of a is %7.1f \n', a)
- note that the format string (like all strings) is defined
by single-quote (apostrophe) characters ', rather than
by double-quote (quotation marks) characters "
- Example: to print to a file,
- open the file: fid = mopen(filename, 'w')
- write to it: mfprintf(fid, 'string %s and integer %d\n', str, int)
- close the file: mclose(fid)
- Expressions -- for scalar quantities (vectors and matrices have
their own set of functions), each of the following
expressions will return T or F
- equal to: a == b
- not equal to: a ~= b
this is not the same as the != comparison in C
- greater than: a > b
- less than: a < b
- greater than or equal to: a >= b
- less than or equal to: a <= b
- Control Flow
- If
if (a == b) then if (a == b) then if (a == b) then
do this do this do this
end else elseif (a > b)
do that do that
end else
do the other thing
end
- for
for i = 1 : 10 for i = 1 : 0.1 : 2
x = x + i; x = sin(i);
end end
- note that the limits are inclusive at both ends, unlike C
- note that if you include the increment, it falls in the middle
of the for statement, unlike C
- while
while (i < 100) while (i < 100)
do something do something
end if (result > i)
break
end
end
- note that there is no "continue", like in C
- the break command works in for loops, too
Copyright © Michael Richmond.
This work is licensed under a Creative Commons License.