Scilab/C2/Scripts-and-Functions/English-timed
From Script | Spoken-Tutorial
Time | Narration
|
00.02 | Welcome to the spoken tutorial on Scripts and Functions with Scilab. |
00.07 | Let us start with a brief introduction to the file formats in Scilab. |
00.12 | When several commands are to be executed, it may be more convenient to write these statements in to a file with Scilab editor. |
00.22 | These are called SCRIPT files. |
00.25 | To execute the commands written in such a script file,the exec function can be used, followed by the name of the script file |
00.35 | These file generally have the extension dot sce or dot sci, depending on its content. |
00.43 | Files having the dot sci extension contain scilab function and or user defined function. |
00.52 | Executing these files loads the functions into Scilab environment (but does not execute them), |
00.59 | whereas |
01.02 | Files having the dot sce extension contain Scilab function and user defined function. |
01.09 | Please remember that the convention of naming the extension as dot sce and dot sci are not RULES, but a convention followed by the scilab community. |
01.22 | Let us open the Scilab Console window on the computer. |
01.28 | On the present working directory by typing the command pwd on the command prompt |
01.36 | Go to the Task bar of scilab console window and click on the editor option to Open the scilab editor |
01.50 | I have already typed the commands in a file and saved it as helloworld.sce, |
01.56 | Therefore I will open that file using Open a file shortcut icon. |
02.04 | Select helloworld.sce file and click on open. |
02.11 | 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. |
02.20 | Go to the execute button on the scilab editors menu bar and select load into scilab option. |
02.30 | This will load the file into the scilab console. |
02.35 | After loading the file on the console the script produces the output as you see. |
02.43 | It contains both the commands and the resulting output for the respective command. |
02.49 | Now change the value of a to 1. |
02.55 | In the editor go to the file menu and click save. |
03.02 | We can also execute this script directly from the scilab interpretor using the exec command |
03.09 | And giving the path to the script file as exec into brackets into double quotes helloworld.sce that is the file name and press enter. |
03.32 | The script file produces a similar output with the use exec function. |
03.37 | Let us now talk about Functions: |
03.40 | A function definition starts with the keyword function and ends with the keyword endfunction. |
03.47 | I have already saved a function file in function.sci using the scilab editor. |
03.57 | I will open that file as you see |
04.05 | The function is defined here. |
04.09 | In this degrees is the output parameter and radians is the input parameter to the function named radians2degrees. |
04.26 | I will load this function in Scilab using the Execute menu option. |
04.40 | The function is now loaded in the scilab console. |
04.45 | It can also be loaded using exec command |
04.48 | Once a function is loaded, it can be called like any other Scilab function by passing specific arguments to that function. |
04.56 | Make a mental note of the% sign and recall the reason for its use. |
05.02 | Now let us find values for radians2degrees of %pi by 2 and radians2degrees of %pi by 4. |
05.17 | %pi by 2 and radians2degrees of %pi by 4 |
05.28 | Now we will see a function with more than one input and output arguments. |
05.34 | This function will takes polar coordinates as input argument and returns rectangular coordinates as output argument. |
05.45 | I will open the file which I have already typed. |
05.52 | Here you can see x and Y are the output parameters and r and theta are the input parameters to the function polar2rec. |
06.06 | I will load this function in scilab using the exec option. |
06.22 | Once the function is loaded, we need to call the function. |
06.25 | This function requires two input arguments and two output arguments. |
06.32 | Therefore r is equal to 2 , theta is equal to 45 and now we will call it x1,y1 output parameters is equal to function name polar to rect into bracket r comma theta and press enter. |
07.25 | You will see the values of x1 and y1 |
07.30 | One of the interesting features of Scilab is you can define any number of functions in a single .sci file. |
07.38 | While doing this please remember that by default all the variables defined in a function are local |
07.46 | The scope of variables used in a particular function ends with the endfunction keyword of the function definition |
07.56 | Advantage of this feature is that we can use same variable names in different function. |
08.06 | These variables won't get mixed up unless we use the global option. |
08.11 | To know more about the global variable type help global. |
08.19 | Please note that if any variable is to be "watched" or monitored inside a function, then disp is required. |
08.27 | Inside a function file, you can check for yourself the effect of putting a semicolon ( ; ) at the end of a statement. |
08..35 | Also check this for disp("...") statements. |
08.38 | Inline Functions: |
08.40 | Functions are segments of code that have well defined input and output as well as local variables. |
08.47 | The simplest way to define a function is by using the command `deff'. |
08.54 | Scilab allows the creation of in-line functions and are especially useful when the body of the function is short. |
09.03 | This can be done with the help of the function deff(). |
09.07 | It takes two string parameters. |
09.10 | The first string defines the interface to the function. |
09.14 | The second string defines the statements of the function. |
09.20 | The deff command defines the function in Scilab and also loads it. |
09.27 | There is no need to load the function defined by using deff command explicitly through execute menu option. |
09.35 | Let us see an example to illustrate this concept. |
09.41 | Iwill open a file inline.sci where I have written the inline function. |
09.52 | I will resize the editor window: |
09.57 | As mentioned earlier the first string defines the function declaration and the second string defines the statements of the function. |
10.14 | We will load this function in scilab editor and |
10.25 | Use it to find the values of degrees2radians of 90 and degrees2radians of 45. |
10.55 | A function should call, not just other functions within itself, but also ITSELF. |
11.01 | This is "recursive" calling of a function. |
11.03 | This is required, for example, when writing a function to calculate the factorial of an integer. |
11.11 | Let us extend the discussion on file formats in Scilab: |
11.14 | As mentioned earlier SCILAB uses two types of file formats, namely the SCE file format and the SCI file format. |
11.24 | The files with the dot sce file extension are the script files, |
11.28 | which contain the SCILAB commands that you enter during an interactive kind of SCILAB session. |
11.36 | They can comprise comment lines utilized in documenting the function and they can also use the command EXEC to execute the script. |
11.53 | The files with the dot sci file extension are the function files that start with the function statement. |
12.00 | A single dot sci file can have multiple function definitions which themselves contain any number of SCILAB statements that perform operations on the function argument, |
12.15 | or on the output variables after they have been evaluated. |
12.21 | This brings us to the end of this spoken tutorial on Scripts and Functions in Scilab. |
12.26 | There are many other functions in Scilab which will be covered in other spoken tutorials. |
12.31 | Keep watching the Scilab links. |
12.34 | This spoken tutorial has been created by the Free and Open Source Software in Science and Engineering Education (FOSSEE). |
12.42 | More information on the FOSSEE project could be obtained from http://fossee.in or http://scilab.in |
12.51 | Supported by the National Mission on Eduction through ICT, MHRD, Government of India. |
12.57 | For more information, visit:spoken hyphen tutorial dot o r g slash NMEICT hyphen intro |
13.07 | This is Anuradha Amrutkar from IIT Bombay signing off. |
13.11 | Thanks for joining us.'Good bye |