Difference between revisions of "Scilab/C2/Scripts-and-Functions/English"
(Created page with ''''Title of script''': Scripts and Functions_new '''Author: Anuradha A.''' '''Keywords: Scripts, functions, exec''' {| style="border-spacing:0;" ! <center>Visual Clue</cente…') |
PoojaMoolya (Talk | contribs) |
||
Line 8: | Line 8: | ||
{| style="border-spacing:0;" | {| style="border-spacing:0;" | ||
− | ! <center>Visual | + | ! <center>Visual Cue</center> |
! <center>Narration</center> | ! <center>Narration</center> | ||
Latest revision as of 09:59, 13 June 2014
Title of script: Scripts and Functions_new
Author: Anuradha A.
Keywords: Scripts, functions, exec
|
|
---|---|
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.
|
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),
|
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 =
|
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.
| |
Scilab Console | After loading the file in the scilab console the script produces the output as you can see:
-->disp("Hello 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.
-->a=5; b=2; c=a+b; -->d=a+b+c; -->disp(d)
|
Let us now talk about Functions:
| |
I have already saved this function in a file function.sci using the scilab editor.
| |
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.
| |
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 =
ans =
|
Now we will write a function with more than one input and 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.
|
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.
|
Slide | Inline Functions:
|
Slide | Scilab allows the creation of in-line functions and are especially useful when the body of the function is short.
|
Slide | The deff command defines the function in Scilab and also loads it.
|
Scilab Editor | Let us see an example to illustrate this concept:
|
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 =
ans =
|
A function could call, not just other functions within itself, but also ITSELF.
| |
Slide | Let us extend the discussion on file formats in Scilab:
The files with the .sci file extension are the function files that start with the function statement.
|
Slide | This brings us to the end of this spoken tutorial on Scripts and Functions in Scilab.
|
Slide | This spoken tutorial has been created by the Free and Open Source Software in Science and Engineering Education (FOSSEE).
http://spoken-tutorial.org/NMEICT-Intro
Good bye |