Scilab/C2/Scripts-and-Functions/English

From Script | Spoken-Tutorial
Revision as of 19:35, 27 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Scripts and Functions_new

Author: Anuradha A.

Keywords: Scripts, functions, exec


Visual Clue
Narration
Slide Welcome to the spoken tutorial on Scripts and Functions with Scilab.
Slide Let us start with a brief introduction to the file formats in Scilab.
Slide When several commands are to be executed, it may be more convenient to write these statements in a text file with the Scilab editor.


These are called SCRIPT files.

Slide These file generally have the extension .sce or .sci, depending on their content.
Slide Files having the .sci extension contain only function definitions and executing these files loads the functions into Scilab environment (but does not call them),


whereas


Files having the .sce extension can contain both Scilab function definitions as well as executable statements (including function calls).

Slide Please remember that the convention of naming the extension as .sce and .sci are not RULES, but a convention followed by the scilab community.
Scilab Console Let us open Scilab on the computer.

Check the present working directory by typing pwd on the command prompt

-->pwd

ans =


C:\Documents and Settings\cdeep_lap12

Go to the Task bar of scilab console window and click on editor option to Open the scilab editor
I have already typed the commands in a file and saved it as helloworld.sce, therefore I will open that file using Open a file shortcut icon.
Scilab Editor disp("Hello World")

a=1; b=2; c=a+b;

d=a+b+c;

disp(d)

disp("Goodbye World")

You may type the commands in the new file and Save this file to the current working directory as helloworld.sce through the File Menu.
Go to Execute button on the scilab editors menu bar and select Load into Scilab option.


This will load the file in scilab console.

Scilab Console After loading the file in the scilab console the script produces the output as you can see:

-->disp("Hello World")


Hello World


--> a=1; b=2; c=a+b;


--> d=a+b+c;


--> disp(d)


6.


--> disp("Goodbye World")


Goodbye World

It contains both the commands and the resulting output for the respective commands.
Scilab Editor Now change the value of a to 1 in the editor,go to File menu , again select save and close it.
Scilab Console We can also execute the script directly from the scilab interpreter using the exec command and giving the path to the script file:

-->exec("helloworld.sce")

Scilab Console The script file produces a similar output with the use exec function.


-->disp("Hello World")


Hello World

-->a=5; b=2; c=a+b;

-->d=a+b+c;

-->disp(d)


14.


-->disp("Goodbye World")


Goodbye World



Let us now talk about Functions:


A function definition starts with the keyword function and ends with the keyword endfunction.

I have already saved this function in a file function.sci using the scilab editor.


The function is defined as you see,

Scilab Editor function [degrees] = radians2degrees(radians)

degrees = radians*(180/%pi);

endfunction

Here degrees is the output parameter and radians is the input parameter to the function named radians2degrees.


I will now load this function in Scilab using the Execute menu option.

Scilab Console It can also be loaded using exec command

-->exec functions.sci

Scilab Editor The function is now loaded in the scilab console.

-->function [degrees] = radians2degrees(radians)

--> degrees = radians*(180/%pi);

-->endfunction

Once a function is loaded, it can be called like any other Scilab function by passing specific arguments to that function.
Make a mental note of the % sign and recall the reason for its use.
Scilab Console Now let us find values for radians2degrees(%pi/2) and radians2degrees(%pi/4).

-->radians2degrees(%pi/2)

ans =


90.


-->radians2degrees(%pi/4)

ans =


45.



Now we will write a function with more than one input and output arguments.


This function will takes polar coordinates as input argument and returns rectangular coordinates as output arguments.

Scilab Editor function [x, y] = polar2rect(r,theta)

x = r*cos(theta*%pi/180)

y = r*sin(theta*%pi/180)

endfunction

Scilab Console To load this function in scilab type

-->exec polar2rect.sci

Scilab Console Once the function is loaded, we need to call the function. This function requires two input arguments and two output arguments.

---> r = 2;

---> theta = 45;

-->[x1, y1] = polar2rect(2,45)

y1 =

1.4142136

x1 =

1.4142136

Slide An interesting features of Scilab is that you can define any number of functions in a single .sci file.
Slide While doing this please remember that by default all the variables defined inside the function are local and the scope of variables used in a particular function ends with the endfunction keyword of the function definition
Slide Advantage of this feature is that we can use same variable names in different function.


These variables won't get mixed up unless we use the global option.

Slide To know more about the global variables type help global and see a detailed help yourself.
Slide Please note that if any variable is to be "watched" or monitored inside a function, then disp is required.
Slide Inside a function file, you can check for yourself the effect of putting a semicolon ( ; ) at the end of a statement with or without a semicolon.


Also check this for disp("...") statements.

Slide Inline Functions:


Functions are segments of code that have well defined input and output as well as local variables.


The simplest way to define a function on the interpreter itself is by using the command `deff'.

Slide Scilab allows the creation of in-line functions and are especially useful when the body of the function is short.


This can be done with the help of the function deff().


It takes two string parameters.


The first string defines the function declaration and the second string defines the statements of the function.

Slide The deff command defines the function in Scilab and also loads it.


There is no need to load the function defined by using deff command explicitly through execute menu option since this is not being saved in a script file.

Scilab Editor Let us see an example to illustrate this concept:


deff("[radians] = degrees2radians(degrees)","radians = degrees*(%pi/180)")

As mentioned earlier the first string defines the function declaration and the second string that defines the statements of the function.
Scilab Console We can directly use it to find the values of degrees2radians(90) and degrees2radians(45).

-->degrees2radians(90)

ans =


1.5707963


-->degrees2radians(45)

ans =


0.7853982

A function could call, not just other functions within itself, but also ITSELF.


This is "recursive" calling of a function.


This is required, for example, when writing a function to calculate the factorial of an integer.

Slide Let us extend the discussion on file formats in Scilab:


As mentioned earlier SCILAB uses two types of file formats, namely the SCE file format and the SCI file format.


The files with the .sce file extension are the script files, which contain the SCILAB commands that you enter during an interactive kind of SCILAB session.


They can also have comment lines used for documenting the script and they can also use the command EXEC to execute the script.

The files with the .sci file extension are the function files that start with the function statement.


A single .sci file can have multiple function definitions which themselves contain any number of SCILAB statements that perform operations on the function arguments, or on the output variables after they have been evaluated.

Slide This brings us to the end of this spoken tutorial on Scripts and Functions in Scilab.


There are many other functions in Scilab which will be covered in other spoken tutorials.


Keep watching the Scilab links.

Slide This spoken tutorial has been created by the Free and Open Source Software in Science and Engineering Education (FOSSEE).


More information on the FOSSEE project could be obtained from http://fossee.in or http://scilab.in


Supported by the National Mission on Eduction through ICT, MHRD, Government of India.


For more information, visit:

http://spoken-tutorial.org/NMEICT-Intro


This is Anuradha Amrutkar from IIT Bombay signing off.

Good bye

Contributors and Content Editors

Chandrika, PoojaMoolya