Scilab/C2/Getting-Started/English

From Script | Spoken-Tutorial
Revision as of 09:57, 13 June 2014 by PoojaMoolya (Talk | contribs)

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

Title of script: Getting Started with Scilab

Author: Shalini , Anuradha A.

Keywords: Simple calculations, arithmetic, logarithm, exponential, trigonometric functions. diary, file storing operations, working directory.

Visual Cue Narration
Slide Welcome to the spoken tutorial on Getting Started with Scilab.
Slide In this tutorial we are going to learn:

Use Scilab as a calculator.

How to Store values in a variable.

How to Perform various mathematical operations using these variables.

How to Create a file to store commands executed during the session in the current working directory.

Define complex numbers.

How to Perform exponential, logarithmic and trigonometric operations on numbers.

Slide The prerequisite for this tutorial are Scilab should be installed on your system.

I am using Scilab 5.2.0 and Mac OS(X) for demonstration

Slide Here is the Flow chart for the tutorial.
Launch Scilab Click on the Scilab shortcut icon on your Desktop and Start Scilab.

This is the Scilab console window. Notice that the cursor is on the command prompt.

I suggest that you practice this tutorial in Scilab simultaneously while pausing the video at regular intervals of time.

Scilab Console

--> 42+4*4-64/4

Scilab can be used as a calculator. Let us see some of the basic operations that it can do.

Type 42 plus 4 multiplied by 4 minus 64 divided 4 and press enter.

The output is 42, as expected.

---> ans Note that the answer 42 is stored in the default variable "a n s".
-->a=12, b=21, c=33

We can also create named variables:

Type,

a equals 12,b=21 and c=33 and press enter.

This stores the values 12,21 and 33 in the variables a,b and c respectively. I will clear the scilab console using the clc command here

-->a+b+c

ans = 66.

Now we will perform some mathematical operations using these variables.

For example,

a+b+c gives the result 66 also

-->a*(b+c)

ans = 648.

also

a times into bracket (b+c) gives the result 648

-->d = (a+b)*c

d = 1089.

We can also assign the answer to another variable say 'd' by typing d = bracket (a+b) close the bracket multiplied by c gives the answer

d = 1089.

-->a,b,c,d

a = 12.

b = 21.

c = 33.

d = 1089.

We can check the values in the variables by typing the names of the variables separated by commas on the command line as


a,b,c,d and press enter


a = 12.

b = 21.

c = 33. and

d = 1089. I will clear the console here using the clc command

--> 7^2 To take the power, use the “raised to” symbol: for example, 7 square can be found by 7 raised to 2.
--> sqrt(17) For finding the square root of a number, say, 17, we use : sqrt(17).
--> 17^(0.5) This is same as 17 raised to the power of zero point five.
--> 34^(2/5) By convention, only the positive value comes as output. More generally, to find 34 to the power of (2 by 5), type:

Negative powers can also be used, and

So far, we have seen how to do some simple calculations and how to create variables in Scilab.

Now let us start with a new command which helps us remember previously issued commands and their resulting outputs.

--> pwd First type the command pwd

:ans =

/home/cdeep

This is the current working directory (on this computer).

Of course, when you type this on your computer, it could be different.

Use icon to change directory to desktop The current working directory can be changed by using the icon as shown.
--> diary('myrecord.txt') Now issue the diary command by typing:

diary('myrecord.txt') press enter

Slide This command will create a file with the name "myrecord.txt" in the current working directory.

A transcript of the Scilab session from now onwards will be saved in this file.

Its usefulness will be demonstrated at a later stage in this tutorial

Please pause the tutorial now and attempt exercise number one given with the video.
-->%i Now, let us see how Scilab handles complex numbers.

The imaginary unit i is defined in Scilab as percent i:

-->5.2*%i (Five point two into percent eye gives result 5.2i
-->(10+5*%i)*(2*%i) also 10 plus 5 into percent i whole multiply with 2 into percent i gives us 10. + 20.i
-->%pi Let us see some other predefined numerical constants available in Scilab.

As with i, their names also start with the percent sign:

For example, percent pi

The value of pi is as expected.

-> sin(%pi/2) Now, we will demonstrate the use of pi using a few built-in trigonometric functions as follows.

For the functions sin of percent pi divided by 2 gives result 1.

-> cos(%pi/2) and cos percent pi divided by 2 gives result 6.123D-17.

Notice first that angles are measured in radians.

Notice that the second answer is zero for all practical purposes.

This is related to a number known as "machine epsilon",

It is the minimum digit resolution that Scilab can give.

--> %eps Type %eps in console to find its value on your computer.

In my computer it gives 2.220D-16

This shows the floating point precision that Scilab uses : 2.220D-16.

This number is a notation for 2.22 times 10^(-16).

-->0.000456

-->4.56d-4

-->4.56e-4

If one wants to write 0.000456, one can write 4.56d-4 or 4.56e-4.

While scilab variables and functions are case-sensitive, here we can use small d or capital D, or small e, or capital E.

-->%e The base of the natural logarithm is another important predefined numerical constant:

percent e gives us result 2.7182818 as expected.

-->exp(1) We can achieve the same result with the "e x p" function:

For instance: exp of 1:

ans =

2.7182818

You can see both the answers are same

-->%e^2 similarly,

%e square gives the following answer

ans =

7.3890561

-->exp(2) which can also be achieved by typing exp of 2 as shown.
-->log10(1e-23) The command log means the natural logarithm of a number, that is, to the base e.

Use log10 for taking the logarithm with respect to base 10.

For example, log10(1e-23) gives the expected answer : ans = -23.

We get complex numbers when taking logarithm of negative or complex numbers: you can check for yourself: log(-1) and log(%i)


-->diary(0) Now recall that we invoked a recording of all the typed commands into the file myrecord.txt through the diary command, now, let's see how to close that file and view it.

For closing the file type,

diary of zero

This command will close and save the file myrecord.txt.

Also recall that this file was created in current working directory, which in my case is my desktop.

Open file on the computer Let us open this file: do this using the Open-a-file icon on the top.
Slide Note that all transactions, both commands and the corresponding answers given by Scilab, have been saved into this file.

We know that, while a program is being developed, one experiments a lot before arriving at suitable code.

If the diary command is not used, it is difficult to keep a track of all the commands that did or didn't work.

Thus,the diary is one way to address this issue.

Now, no further transactions will be saved because we had closed the file using the command diary(0).

If we need to save the session once again, we need to issue the diary command again.

Remember, that the diary command will overwrite the file.

So, if the file contains some useful information, then one should use some other file name in the diary command.

Pause the video here and solve the second exercise given with the video.
You may have noticed that the solution for the problem was not exactly zero. For more information on how to deal with this, type “help clean”.
Slide In general, if you need help, then the 'help' command can be used.

For example, “help chdir” gives detailed information on how to change the current working directory.

Of course, we used the icon on the top.

Let me close the help browser.

The up - down arrow keys can been used to see the previously executed commands.

While using the up - down arrows, you can stop at any command,and press the Enter key to execute it.

You can edit the commands, if necessary. In fact, if you are looking for a previous command you typed, which started with the letter 'e', then type e, and then use up arrow key.

Use the tab key to auto-complete the commad. It gives us all the available options to choose.

Slide Summary


In this tutorial we have learnt to :

1.Use Scilab as a calculator with an example.

2.Store the result in the default variable ans.

3.Assign values to the variable using the equality sign.

4.Check values in variables by typing the name of the variable separated by commas on the console.

5.Check the current working directory using pwd command.

6. Use diary command to save all commands typed on the console into a file.

7.Define complex numbers, natural exponents and π using %i, %e and %pi respectively.

8.Use help command for detailed information about any command.

Slide This brings us to the end of this spoken tutorial on Getting Started with Scilab.

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

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

This is Anuradha Amrutkar from IIT Bombay signing off.

Thank you for joining. Goodbye.

Contributors and Content Editors

Chandrika, PoojaMoolya