SciLab Tutorial
SciLab Tutorial
SciLab Tutorial
This tutorial is intended for use by students and/or faculty using mathematical operations as part of their program or course study material.
Workshop Description: This workshop is intended to familiarize the user with the most superficial functions and of SciLab, and explain the basic structure. For continued use the help menu will come in handy.
SciLab 3.1.1 Tutorial Contents: Introduction to Scilab Comparison to Mathematica Getting Started Opening the Program Talking Between Scilab and the Editor Basic Matrix Operations and Commands Introduction to Scilab: Scilab has been developed for system control and signal processing applications and is easily interfaced with Fortran or C subprograms. Scilab is made of three distinct parts: an interpreter, libraries of functions (Scilab procedures) and libraries of Fortran and C routines. A key feature of the Scilab syntax is its ability to handle matrices: basic matrix manipulations such as concatenation, extraction or transpose are immediately performed, as well as basic operations such as addition or multiplication. Scilab also aims at handling more complex objects than numerical matrices. For instance, control people may want to manipulate rational or polynomial transfer matrices. This is done in Scilab by manipulating lists and typed lists which allow a natural symbolic representation of complicated mathematical objects such as transfer functions, linear systems or graphs. Scilab provides a variety of powerful primitives for the analysis of non-linear systems. Integration of explicit and implicit dynamic systems can be accomplished numerically. There exist numerical optimization facilities for non linear optimization (including non differentiable optimization), quadratic optimization and linear optimization. (www.scilab.org) Comparison to Mathematica: Other computational software on the market are usually fairly expensive and often boast for being more user friendly, for instance, Mathematica. Mathematica seamlessly integrates a numeric and symbolic computational engine, graphics system, programming language, documentation system, and advanced connectivity to other applications. It is this range of capabilities--many world-leading in their own right--that makes Mathematica uniquely capable as a "one-stop shop" for you or your organization's technical work. Some of these uses require in-depth Mathematica knowledge, while others do not. Mathematica is unusual in being operable for less involved tasks as well as being the tool of choice for leading-edge research, performing many of the world's most complex computations. It is Mathematica's complete consistency in design at every stage that gives it this multilevel capability and helps advanced usage evolve naturally. (www.wolfram.com)
Mathematica costs about $200 for a temporary student license and almost $2,000 for professional editions. Mathematica is not necessarily any easier to use than SciLab, you will need to learn a new software program regardless of the one you choose, but the layout of Mathematica is a little bit simpler to use. In Mathematica, you can physically click and edit anywhere in your code, in SciLab you have to re-type or recall the command in order to edit it. Getting Started, Opening the Program: Scilab is found in a few places in the CAL: Mathematics Tools, Modeling Tools, and Statistical Tools, as well as on Masu/Public/Installers Free-Open source software. Once Scilab is running you are met with the command window. Startup the editor (from the menu bar) and you are ready to start playing with Scilab, either from the command line or from the editor. Here are some quick editing/reference commands that may be useful: Ctrl-p recall previous line Ctrl-n recall next line Ctrl-b move backward one character Ctrl-f move forward one character Ctrl-h delete previous character Ctrl-a move to beginning of line Ctrl-e move to end of line Ctrl-u cancel current line Ctrl-c interrupt Scilab and pause after carriage return. Demos for interactive run of some demos File Operations facility for loading functions or data into Scilab, or executing script files. Help invokes on-line help with a directory with the names of the corresponding items. The help command is the easiest way to find out more about Scilab commands. The command -->help name gives information about the Scilab command name. For example (where // indicates something that is commented out): -->help sin // Information about the sine function -->help + // Gives links to help on Scilab operator names -->help log // This is enough information about log // to show log means log to the base e (ie. Ln), not base 10 The help information may also provide an example of how the command is used and you can use cutting and pasting to run the example commands. Getting Started, Talking Between Scilab and the Editor: From the Scilab command window you can run Scilab commands, or you can use any text editor to create files to execute in Scilab, SciPad under Editor in the toolbar is built into Scilab and works fine for this purpose. The command pwd will tell you the directory you are presently working in. This is where Scilab will look for the files you
want to execute. You can change this using Change Directory item under the File menu or from the command line in Scilab using the command: -->chdir directory. Task #1: From SciPad, create this command: A = [ 1 2 3 ; 4 5 6 ; 7 8 9] and then save the file into the same directory in which Scilab is looking (as shown by the pwd command). Name it whatever youd like, but remember the file extension .sce Now we can run the commands from the newly created .sce file via this command at the Scilab prompt: --> exec name of your file.sce Note, as you make changes to your executable file, you will need to save those changes and then run the exec command again (you can save typing by using the up arrow to recall previous commands). Try changing your matrix A in SciPad and reexecute the file in Scilab again. Getting Started, Basic Matrix Operations and Commands: Now we are ready to play around a little. Make sure that the Scilab prompt is ready to go (it should look like this: --> ). For the next couple of tasks we are going to type commands directly into the Scilab prompt, not the editor. Task #2: Scilab provides many vector and matrix operations. For instance we can multiply matrices and vectors. In Scilab, enter this command: --> x = [ 0 ; 5 ; 35] 2 12 8 and given a matrix A = 6 1 7 type the command:
13 9 3
-->A = [ 2 -12 8; 6 1 7; 13 9 -3 ] (semi-colons may be replaced by returns) Try these simple operations: -->A*x -->A*A -->a*x Ha! You have now learned how to do some basic Linear Algebra using Scilab! You probably noticed that the last operation a*x didnt work, thats because Scilab is case sensitive so a and A will represent different variables. Also note a semi-colon at the end of a line will stop Scilab output for that line which is useful if you are working with large vectors or matrices. Try: -->y=A*x; Note that the answer is not printed out. To see y just type: -->y
Scilab 3.1.1 Jennifer Murphy Rev. 1.0 6-7-06
Let us now rewrite our matrix A by having Scilab create random numbers. Previously, A was a 3 x 3 matrix; enter this command at the Scilab prompt: -->A=rand(3,3) (in the help menu, you will see that the command rand(n,m) requires n to be the number of rows and m is the number of columns). By using A again, we have effectively written over the previous value for A. Task #3: First, lets clear the value for y and x that we calculated in the previous exercise. In Scilab enter this command: -->clear y; clear x Another simple feature Scilab has is producing vectors that increase at a certain increment. For instance, try this: -->x=0:0.5:50 This will produce a vector starting at 0 and increasing by 0.5 units until 50 is reached. Note that a semi-colon at the end of the line wouldve been helpful here. We can also compute functions using vectors. For example, lets do some trigonometry! Try this: -->y=sin(x) (note, the sine function is called in lowercase) Now we have a vector d and a vector f of the same length (these vectors are simply matrices that are 1 x 101, x being 0 thru 50 in increments of .5 and y being the sine of each of those values) Task #4: Now lets make a 2-d plot! Using the vectors found in the previous task, lets make a plot of vector x verses vector y. Enter this command after the Scilab prompt: -->plot2d(x,y) Beautiful! A plot of x versus y should be produced. You can save the graph to a file by using the file export menu item on the graphics window. There are a variety of types of files you can save your graph as, but it is important to type the file extension after the title when you are saving it, otherwise it will just be a generic file with no file type. Yeah! Now you know some very basic operations and commands available to you through Scilab. This program has virtually unlimited power and can do much more than is possible to cover at an introductory level. As long as users are willing to sift through the help menu, this program can be used by just about anyone for just about any reason. Mathematica definitely has some competition!