Difference between revisions of "Scilab/C2/Scripts-and-Functions/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
(7 intermediate revisions by 4 users not shown) | |||
Line 4: | Line 4: | ||
|'''Narration''' | |'''Narration''' | ||
− | |||
|- | |- | ||
− | | 00 | + | | 00:01 |
− | |Welcome to the spoken tutorial on Scripts and Functions with Scilab. | + | |Welcome to the spoken tutorial on '''Scripts and Functions''' with Scilab. |
|- | |- | ||
− | | 00 | + | | 00:06 |
− | |Let us start with a brief introduction to the file formats in Scilab. | + | |Let us start with a brief introduction to the '''file formats''' in Scilab. |
|- | |- | ||
− | | 00 | + | | 00:12 |
|When several commands are to be executed, it may be more convenient to write these statements into a file with the Scilab editor. | |When several commands are to be executed, it may be more convenient to write these statements into a file with the Scilab editor. | ||
|- | |- | ||
− | | 00 | + | | 00:21 |
|These are called as SCRIPT files. | |These are called as SCRIPT files. | ||
|- | |- | ||
− | | 00 | + | | 00:24 |
− | |To execute the commands written in such a script file, the exec function can be used | + | |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 | + | | 00:34 |
− | |These | + | |These files generally have the extension '''.sce''' or '''.sci''', depending on its content. |
|- | |- | ||
− | | 00 | + | | 00:42 |
− | | Files having the .sci extension | + | | Files having the '''.sci''' extension contain Scilab function and or user defined functions. |
|- | |- | ||
− | | 00 | + | | 00:51 |
− | |Executing these files loads the functions into Scilab environment(but does not execute them), whereas | + | |Executing these files loads the functions into Scilab environment (but does not execute them), whereas |
|- | |- | ||
− | | 01 | + | | 01:00 |
− | | | + | | files having the '''.sce''' extension contain Scilab function and User defined functions. |
|- | |- | ||
− | | 01 | + | | 01:08 |
− | |Please remember that the convention of naming the extension as .sce and .sci are not | + | |Please remember that the convention of naming the extension as .sce and .sci are not rules but a convention followed by the scilab community. |
|- | |- | ||
− | |01 | + | |01:21 |
− | |Let us open Scilab | + | |Let us open Scilab console window on the computer. |
|- | |- | ||
− | |01 | + | |01:27 |
− | |Check on the present working directory by typing the command pwd on the command prompt | + | |Check on the present working directory by typing the command '''pwd''' on the command prompt. |
|- | |- | ||
− | | 01 | + | | 01:35 |
− | |Go to the Task bar of scilab console window and click on | + | |Go to the Task bar of scilab console window and click on Editor option to open the scilab editor. |
|- | |- | ||
− | | 01 | + | | 01:49 |
|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. | |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. | ||
|- | |- | ||
− | | 02 | + | | 02:03 |
− | |Select helloworld.sce file and click on Open | + | |Select helloworld.sce file and click on Open. |
|- | |- | ||
− | | 02 | + | | 02:10 |
− | |You may type the commands in the new file and | + | |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 | + | | 02:20 |
|Go to Execute button on the scilab editors menu bar and select Load into Scilab option. | |Go to Execute button on the scilab editors menu bar and select Load into Scilab option. | ||
|- | |- | ||
− | |02 | + | |02:29 |
|This will load the file into scilab console. | |This will load the file into scilab console. | ||
|- | |- | ||
− | |02 | + | |02:34 |
− | |After loading the file on the console the script produces the output as you see | + | |After loading the file on the console the script produces the output as you see. |
|- | |- | ||
− | |02 | + | |02:43 |
|It contains both the commands and the resulting output for the respective commands. | |It contains both the commands and the resulting output for the respective commands. | ||
|- | |- | ||
− | |02 | + | |02:49 |
|Now change the value of a to 1. | |Now change the value of a to 1. | ||
|- | |- | ||
− | |02 | + | |02:55 |
− | |In the editor,go to the File menu | + | |In the editor, go to the File menu and click on Save. |
|- | |- | ||
− | | 03 | + | | 03:02 |
− | |We can also execute the script directly from the scilab interpreter using the exec command and giving the path to the script file: | + | |We can also execute the script directly from the scilab interpreter using the '''exec''' command and giving the path to the script file as: |
|- | |- | ||
− | | 03 | + | | 03:12 |
− | | | + | | exec into brackets into double quotes helloworld.sce, that is the file name, and press Enter. |
|- | |- | ||
− | | 03 | + | | 03:31 |
− | | The script file produces a similar output with the use of exec function. | + | | The script file produces a similar output with the use of '''exec''' function. |
|- | |- | ||
− | | 03 | + | | 03:37 |
− | | Let us now talk about | + | | Let us now talk about '''functions'''. |
|- | |- | ||
− | | 03 | + | | 03:39 |
− | |A function definition starts with the keyword function and ends with the keyword endfunction. | + | |A function definition starts with the keyword '''function''' and ends with the keyword '''endfunction'''. |
|- | |- | ||
− | | 03 | + | | 03:46 |
− | | I have already saved a function file in function.sci using the scilab editor. | + | | I have already saved a function file in function.sci, using the scilab editor. |
|- | |- | ||
− | | 03 | + | | 03:57 |
− | |I will open that file | + | |I will open that file. |
|- | |- | ||
− | | 04 | + | | 04:03 |
− | |As you see the function is defined here. | + | |As you see, the function is defined here. |
|- | |- | ||
− | | 04 | + | | 04:08 |
− | |In these degrees is the output parameter and radians is the input parameter | + | |In these, '''degrees''' is the output parameter and '''radians''' is the input parameter. |
|- | |- | ||
− | | 04 | + | | 04:21 |
− | |To the function name radians2degrees. | + | |To the function name '''radians2degrees'''. |
|- | |- | ||
− | | 04 | + | | 04:26 |
− | |I will load this function in Scilab using the Execute menu option. | + | |I will load this function in Scilab, using the Execute menu option. |
|- | |- | ||
− | | 04 | + | | 04:40 |
| The function is now loaded in the scilab console. | | The function is now loaded in the scilab console. | ||
|- | |- | ||
− | | 04 | + | | 04:44 |
− | |It can also be loaded using the exec command | + | |It can also be loaded using the '''exec''' command. |
|- | |- | ||
− | | 04 | + | | 04:47 |
|Once a function is loaded, it can be called like any other Scilab function by passing specific arguments to that function. | |Once a function is loaded, it can be called like any other Scilab function by passing specific arguments to that function. | ||
|- | |- | ||
− | | 04 | + | | 04:56 |
− | |Make a mental note of | + | |Make a mental note of the percent sign (%) and recall the reason of its use. |
|- | |- | ||
− | | 05 | + | | 05:02 |
|Now let us find values for radians2degrees of %pi/2 and radians2degrees of (%pi/4). | |Now let us find values for radians2degrees of %pi/2 and radians2degrees of (%pi/4). | ||
|- | |- | ||
− | | 05 | + | | 05:17 |
− | |percent pi/2 and radians2degrees percent pi by 4 (%pi/4) | + | |percent pi/2 and radians2degrees percent pi by 4 (%pi/4). |
|- | |- | ||
− | | 05 | + | | 05:28 |
|Now we will see a function with more than one input and output arguments. | |Now we will see a function with more than one input and output arguments. | ||
|- | |- | ||
− | | 05 | + | | 05:33 |
− | |This function will | + | |This function will take '''polar coordinates''' as input argument and returns '''rectangular coordinates''' as output arguments. |
|- | |- | ||
− | | 05 | + | | 05:44 |
− | |I will open the file which I have already typed | + | |I will open the file which I have already typed. |
|- | |- | ||
− | | 05 | + | | 05:51 |
− | |Here you can see x and y are the output parameters and r and theta are the input | + | |Here you can see, '''x''' and '''y''' are the output parameters and '''r''' and '''theta''' are the input parameters to the function '''polar2rect'''. |
|- | |- | ||
− | | 06 | + | | 06:06 |
− | |I will load this function in scilab using the exec option | + | |I will load this function in scilab using the '''exec''' option. |
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:21 |
|Once the function is loaded, we need to call the function. This function requires two input arguments and two output arguments. | |Once the function is loaded, we need to call the function. This function requires two input arguments and two output arguments. | ||
|- | |- | ||
− | | 06 | + | | 06:31 |
− | |Therefore r = 2 | + | |Therefore r = 2, |
− | + | ||
|- | |- | ||
− | | | + | | 06:37 |
− | | | + | |theta = 45; |
|- | |- | ||
− | | | + | | 06:44 |
− | | | + | |and now we will call it x1 comma y1, output parameters, is equal to function name '''polar2rect into bracket r comma theta''' and press Enter. |
|- | |- | ||
− | | 07 | + | | 07:25 |
− | | | + | |You will see the value of x1 and y1. |
|- | |- | ||
− | | 07 | + | | 07:29 |
− | | | + | |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, the scope of these variables used in a particular function ends with the '''endfunction''' keyword of the function definition. |
|- | |- | ||
− | | | + | | 07:55 |
− | | | + | |Advantage of this feature is that, we can use same variable names in different function. |
|- | |- | ||
− | | 08 | + | | 08:05 |
− | | | + | |These variables won't get mixed up unless we use the global option. |
|- | |- | ||
− | | 08 | + | | 08:10 |
− | | | + | |To know more about the global variables, type '''help global'''. |
|- | |- | ||
− | | 08 | + | | 08:18 |
− | | | + | |Please note that if any variable is to be "watched" or monitored inside a function then '''disp''' is required. |
|- | |- | ||
− | | 08 | + | | 08:26 |
− | | | + | |Inside a function file, you can check for yourself the effect of putting a semicolon ( ; ) at the end of a statement. |
|- | |- | ||
− | | 08 | + | | 08:34 |
− | | | + | |Also check this for '''disp''' statements. |
|- | |- | ||
− | | 08 | + | | 08:38 |
− | | | + | |'''Inline Functions''': Functions are segments of code that have well defined input and output as well as local variables. |
|- | |- | ||
− | | 08 | + | | 08:46 |
− | | | + | |The simplest way to define a function is by using the command `'''deff'''. |
|- | |- | ||
− | | | + | | 08:53 |
+ | |Scilab allows the creation of in-line functions and are especially useful when the body of the function is short. | ||
+ | |||
+ | |- | ||
+ | | 09:02 | ||
|This can be done with the help of the function deff(). | |This can be done with the help of the function deff(). | ||
|- | |- | ||
− | | 09 | + | | 09:07 |
|It takes two string parameters. | |It takes two string parameters. | ||
|- | |- | ||
− | | 09 | + | | 09:10 |
|The first string defines the interface to the function and the second string defines the statements of the function. | |The first string defines the interface to the function and the second string defines the statements of the function. | ||
|- | |- | ||
− | | 09 | + | | 09:19 |
− | |The deff command defines the function in the Scilab and also loads it. | + | |The '''deff''' command defines the function in the Scilab and also loads it. |
|- | |- | ||
− | | 09 | + | | 09:26 |
− | |There is no need to load the function defined by using deff command explicitly through execute menu option . | + | |There is no need to load the function defined by using '''deff''' command explicitly through execute menu option . |
|- | |- | ||
− | | 09 | + | | 09:34 |
|Let us see an example to illustrate this concept: | |Let us see an example to illustrate this concept: | ||
|- | |- | ||
− | | 09 | + | | 09:41 |
− | |I will open a file inline.sci where I have | + | |I will open a file '''inline.sci''' where I have written the inline function. |
|- | |- | ||
− | | 09 | + | | 09:51 |
|I will resize the editor window. | |I will resize the editor window. | ||
|- | |- | ||
− | | 09 | + | | 09:57 |
− | |As mentioned earlier the first string defines the function declaration and the second string defines the statements of the function. | + | |As mentioned earlier, the first string defines the function declaration and the second string defines the statements of the function. |
|- | |- | ||
− | | 10 | + | | 10:13 |
− | |We will load this function | + | |We will load this function in Scilab editor and use it to find the values of degrees2radians of 90 and degrees2radians of 45. |
|- | |- | ||
− | | 10 | + | | 10:54 |
− | |A function should call, not just other functions within itself, but also | + | |A function should call, not just other functions within itself, but also '''itself'''. |
|- | |- | ||
− | | 11 | + | | 11:00 |
− | |This is | + | |This is '''recursive''' calling of a function. |
− | + | ||
|- | |- | ||
− | | 11 | + | | 11:03 |
|This is required, for example, when writing a function to calculate the factorial of an integer. | |This is required, for example, when writing a function to calculate the factorial of an integer. | ||
|- | |- | ||
− | | 11 | + | | 11:10 |
|Let us extend the discussion on file formats in Scilab: | |Let us extend the discussion on file formats in Scilab: | ||
|- | |- | ||
− | | 11 | + | | 11:14 |
− | |As mentioned earlier | + | |As mentioned earlier Scilab uses two types of file formats, namely the '''.sce''' file format and the '''.sci''' file formats. |
|- | |- | ||
− | | 11 | + | | 11:23 |
− | |The files with the .sce file extension are the script files | + | |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. |
|- | |- | ||
− | | 11 | + | | 11:35 |
− | |They can comprise comment lines utilized in documenting the function and they can also use the command | + | |They can comprise comment lines utilized in documenting the function and they can also use the command '''exec''' to execute the script. |
|- | |- | ||
− | | 11 | + | | 11:52 |
− | |The files with the .sci file extension are the function files that start with the function statement. | + | |The files with the '''.sci''' file extension are the function files that start with the function statement. |
|- | |- | ||
− | | 12 | + | | 12:00 |
− | |A single .sci file can have multiple function definitions which themselves contain any number of | + | |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. |
|- | |- | ||
− | | 12 | + | | 12:20 |
− | |This brings us to the end of this spoken tutorial on Scripts and Functions in Scilab. | + | |This brings us to the end of this spoken tutorial on '''Scripts and Functions''' in Scilab. |
|- | |- | ||
− | | 12 | + | | 12:25 |
|There are many other functions in Scilab which will be covered in other spoken tutorials. | |There are many other functions in Scilab which will be covered in other spoken tutorials. | ||
|- | |- | ||
− | | 12 | + | | 12:31 |
|Keep watching the Scilab links. | |Keep watching the Scilab links. | ||
− | |||
|- | |- | ||
− | | 12 | + | | 12:33 |
− | |This spoken tutorial has been created by the Free and Open Source Software in Science and Engineering Education (FOSSEE). | + | |This spoken tutorial: has been created by the Free and Open Source Software in Science and Engineering Education (FOSSEE). |
|- | |- | ||
− | | 12 | + | | 12:40 |
|More information on the FOSSEE project could be obtained from http://fossee.in or http://scilab.in | |More information on the FOSSEE project could be obtained from http://fossee.in or http://scilab.in | ||
|- | |- | ||
− | | 12 | + | | 12:50 |
|Supported by the National Mission on Eduction through ICT, MHRD, Government of India. | |Supported by the National Mission on Eduction through ICT, MHRD, Government of India. | ||
− | |||
|- | |- | ||
− | | 12 | + | | 12:56 |
− | |For more information, visit: http://spoken-tutorial.org/NMEICT-Intro | + | |For more information, visit: http://spoken-tutorial.org/NMEICT-Intro. |
|- | |- | ||
− | | 13 | + | | 13:06 |
− | |This is Anuradha Amrutkar from IIT Bombay signing off. | + | |This is Anuradha Amrutkar from IIT Bombay. signing off. |
|- | |- | ||
− | | 13 | + | | 13:10 |
− | |Thanks for joining us. Goodbye | + | |Thanks for joining us. Goodbye. |
Latest revision as of 11:31, 7 November 2017
Time | Narration |
00:01 | Welcome to the spoken tutorial on Scripts and Functions with Scilab. |
00:06 | 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 into a file with the Scilab editor. |
00:21 | These are called as SCRIPT files. |
00:24 | 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:34 | These files generally have the extension .sce or .sci, depending on its content. |
00:42 | Files having the .sci extension contain Scilab function and or user defined functions. |
00:51 | Executing these files loads the functions into Scilab environment (but does not execute them), whereas |
01:00 | files having the .sce extension contain Scilab function and User defined functions. |
01:08 | Please remember that the convention of naming the extension as .sce and .sci are not rules but a convention followed by the scilab community. |
01:21 | Let us open Scilab console window on the computer. |
01:27 | Check on the present working directory by typing the command pwd on the command prompt. |
01:35 | Go to the Task bar of scilab console window and click on Editor option to open the scilab editor. |
01:49 | 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. |
02:03 | Select helloworld.sce file and click on Open. |
02:10 | 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 Execute button on the scilab editors menu bar and select Load into Scilab option. |
02:29 | This will load the file into scilab console. |
02:34 | 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 commands. |
02:49 | Now change the value of a to 1. |
02:55 | In the editor, go to the File menu and click on Save. |
03:02 | We can also execute the script directly from the scilab interpreter using the exec command and giving the path to the script file as: |
03:12 | exec into brackets into double quotes helloworld.sce, that is the file name, and press Enter. |
03:31 | The script file produces a similar output with the use of exec function. |
03:37 | Let us now talk about functions. |
03:39 | A function definition starts with the keyword function and ends with the keyword endfunction. |
03:46 | I have already saved a function file in function.sci, using the scilab editor. |
03:57 | I will open that file. |
04:03 | As you see, the function is defined here. |
04:08 | In these, degrees is the output parameter and radians is the input parameter. |
04:21 | To the function name 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:44 | It can also be loaded using the exec command. |
04:47 | 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 percent sign (%) and recall the reason of its use. |
05:02 | Now let us find values for radians2degrees of %pi/2 and radians2degrees of (%pi/4). |
05:17 | percent pi/2 and radians2degrees percent pi by 4 (%pi/4). |
05:28 | Now we will see a function with more than one input and output arguments. |
05:33 | This function will take polar coordinates as input argument and returns rectangular coordinates as output arguments. |
05:44 | I will open the file which I have already typed. |
05:51 | Here you can see, x and y are the output parameters and r and theta are the input parameters to the function polar2rect. |
06:06 | I will load this function in scilab using the exec option. |
06:21 | Once the function is loaded, we need to call the function. This function requires two input arguments and two output arguments. |
06:31 | Therefore r = 2, |
06:37 | theta = 45; |
06:44 | and now we will call it x1 comma y1, output parameters, is equal to function name polar2rect into bracket r comma theta and press Enter. |
07:25 | You will see the value of x1 and y1. |
07:29 | 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, the scope of these variables used in a particular function ends with the endfunction keyword of the function definition. |
07:55 | Advantage of this feature is that, we can use same variable names in different function. |
08:05 | These variables won't get mixed up unless we use the global option. |
08:10 | To know more about the global variables, type help global. |
08:18 | Please note that if any variable is to be "watched" or monitored inside a function then disp is required. |
08:26 | Inside a function file, you can check for yourself the effect of putting a semicolon ( ; ) at the end of a statement. |
08:34 | Also check this for disp statements. |
08:38 | Inline Functions: Functions are segments of code that have well defined input and output as well as local variables. |
08:46 | The simplest way to define a function is by using the command `deff. |
08:53 | Scilab allows the creation of in-line functions and are especially useful when the body of the function is short. |
09:02 | 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 and the second string defines the statements of the function. |
09:19 | The deff command defines the function in the Scilab and also loads it. |
09:26 | There is no need to load the function defined by using deff command explicitly through execute menu option . |
09:34 | Let us see an example to illustrate this concept: |
09:41 | I will open a file inline.sci where I have written the inline function. |
09:51 | 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:13 | We will load this function in Scilab editor and use it to find the values of degrees2radians of 90 and degrees2radians of 45. |
10:54 | A function should call, not just other functions within itself, but also itself. |
11:00 | 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:10 | 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 formats. |
11:23 | 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. |
11:35 | They can comprise comment lines utilized in documenting the function and they can also use the command exec to execute the script. |
11:52 | The files with the .sci file extension are the function files that start with the function statement. |
12:00 | 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. |
12:20 | This brings us to the end of this spoken tutorial on Scripts and Functions in Scilab. |
12:25 | There are many other functions in Scilab which will be covered in other spoken tutorials. |
12:31 | Keep watching the Scilab links. |
12:33 | This spoken tutorial: has been created by the Free and Open Source Software in Science and Engineering Education (FOSSEE). |
12:40 | More information on the FOSSEE project could be obtained from http://fossee.in or http://scilab.in |
12:50 | Supported by the National Mission on Eduction through ICT, MHRD, Government of India. |
12:56 | For more information, visit: http://spoken-tutorial.org/NMEICT-Intro. |
13:06 | This is Anuradha Amrutkar from IIT Bombay. signing off. |
13:10 | Thanks for joining us. Goodbye. |