This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Getting Started with Scilab

Session 2: Scilab as an interactive calculator

Satish Annigeri's picture
Fri, 2011-05-20 12:44

Objectives

Having explored the Scilab environment and user interface, you are now ready to get down to business - doing some calculations. If you have used any programming language before, you will be familiar with expressions, statements, variables and constants. If not, nothing to worry, they ar enot that difficult to master.

In this session you will learn the following:

  1. Using Scilab interactively to perform simple arithmentic operations
  2. Writing expressions and statements
  3. Creating and using variables
  4. Using Scilab built-in constants
  5. Using Scilab environment variables

Interactive Calculations using Scilab

If you have used a calculator before, you will be at home in the Scilab console environment. Just type the mathematical expressions you wish to calculate. All arithmmetic operations such as addition, subtraction, multiplication, division, exponentiation etc. are readily available. Operator precedence, use of parentheses to correctly evaluate expressions are all as you would expect on a calculator. So, try out the following:


-->2 + 3
ans  =
    5.

Let me point out a few things to clarify what happened:
  1. At the command prompt, you entered the expression 2 + 3 and pressed the Enter key
  2. Scilab responded with the answer 5.
  3. Scilab used a built-in variable named ans to store the result of the expression you typed, because you did not supply a variable to store the result
  4. The result is a real number (note the decimal after 5) even though the numbers added were integers. In Scilab, all numbers are by default real numbers, unless they happen to be imaginary numbers. There is no numeric types such as integers, float, double etc as in other programming languages. All numeric types are double precision real numbers.

Let us repeat the same expression with a small change.


-->x = 2 + 3
 x  =
    5.

This time, the result of the expression was stored in the variable x, as specified by you and the result was echoed on the console. Let us now try another simple calculation.
-->y = 2 * 3;
-->y
 y  =
    6.

This time, you are attempting a multiplication and have specified that the result be stored in a variable named y. Note the semicolon (;) at the end of the expression. Putting a semicolon at the end of an expression suppresses the display of the result, but the statement is evaluated and result is stored in y. You can verify that by typing the next statement at the command prompt where you type the name of the variable whose value you want to display. Scilab responds by displaying the value of y.

Important Note

Scilab language is weakly typed. That is:

  1. It is not necessary to declare a variable before being able to use it. In a strongly typed programming language such as C, a declaration makes known to subsequent parts of the program, the name and type of a variable.
  2. It is possible to alter the type of a variable at anytime, as long as you, the programmer, know what type it is at the instant when it is used. In a strongly typed programming language, type of a variable cannot be altered once it is declared.

Multiple Statements on One Line

You can type multiple statements on one physical line by separating each statement from the next with a comma or a semicolon. Results of statements followed by a comma are displayed while those of statements followed by semicolon are suppressed. Try out the following:


-->a = 10 - 5, b = 3 / 4; c = 2^3
 a  =
    5.
 c  =
    8.

One Statement on Multiple Lines

Values of a and c are displayed while that of b is not. By the way, operator for exponentiation is ^. Just as you can put multiple multiple statements on a single physical line, you can also split a long statement (or even a short one, if you so wish) on multiple physical lines. Try out the following:


-->a = 2 + ...
-->5 * 2
 a  =
    12.

Let us summarize:
  1. We used the following operators: +, -, *, /, ^
  2. If the user does not specify a variable to store the results of an expression, Scilab uses its built-in variable ans. Corollary: Do not use the name ans as the name of one your own variables, it is reserved for use by Scilab.
  3. A semicolon at the end of an expression suppresses the display of the result of that statement.
  4. Multiple statements on the same physical line can be separated by either a comma or a semicolon. Results of statements followed by a semicolon are not displayed.
  5. A statement can be split on to multiple lines by ending the line with three consecutive dots (...). Then the subsequent line is treated as a continuation of the statement on the previous line.
  6. All numbers are double precision real numbers or imaginary numbers.

Built-in Constants

Scilab predefines commonly used constants so that they are readily available to the user. Names of all constants can be easily recognized as they start with the precent (%) symbol. Available constants are:

  1. Pi - %pi
  2. e - %e
  3. sqrt(-1) - %i
  4. Infinity - %inf
  5. Not a number - %nan
  6. True - %T or %t
  7. False - %F or %f

Use the Variable Browser environment to see a list of constants in the Scilab Workspace. Use the online help to learn more about some of the constants.

Environment Variables

Scilab uses a few environment variables for its internal use and their values are initialized when Scilab starts. Environment variables are by convention in all uppercase letters. If you know *nix operating systems, you will immediately notice the resemblance to the bash and other shells. To see the value assigned to an environment variable, just type its name at the command prompt. To know the names of environment variables, open the Variable Browser and search for names in all capitals.

Some useful Environment Variables you must know are:

  1. SCIHOME - Name of the directory where Scilab configuration files for the user are stored. On Windows, it will be somewhere within the users Home directory. For example, on a Windows 7 machine, SCIHOME may be C:\Users\<Username>\AppData\Roaming\Scilab\Scilab-5.3.1. On a Linux machine, it might be /home/<username>/.scilab (Note: In the place of <Username>, the actual username will appear)
  2. TMPDIR - Directory where Scilab stores temporary files
  3. SCI - Directory where Scilab is installed. On Windows, it is in DOS filename convention of 8.3 characters.
  4. WSCI - Directory where Scilab is installed, in Windows filename convention.
  5. MSDOS - Boolean constant set to True if Scilab is running in Windows operating system.

Built-in Mathematical Functions

We can now move on to using some built-in mathematical functions, such as, trigonometric, logarithmic, hyperbolic etc. When using trigonometric functions, remember that all angels are in radians. If you wish to use angles in degrees, take care to convert them to radians before using them. Or, Scilab has a set of functions that take angles in degrees.

Here are the functions you are likely to use often:

Trigonometric Functions

  1. sin(), cos(), tan() - angle must be input in radians.
  2. sind(), cosd(), tand() - angle must be input in degrees.
  3. asin(), acos(), atan() - Inverse trigonometric functions. Return angles in radians.
  4. asind(), acos(), atand() - Inverse trigonometric functions. Return angles in degrees.

Logarithmic Functions

  1. log(), log10() - Natural logarithm, Logarithm to base 10
  2. exp() - Anti natural logarithm

Hyperbolic Functions

  1. sinh(), cosh(), tanh()
  2. asinh(), acosh(), atanh()

Defining and Using Variables

Scilab is more like  programmable calculator rather than a simple one. It can define variables, assign them values and use the variables as part of expressions and statements. The rules for choosing names of variables are similar to those in other programming languages and are:

  1. Name of a variable must consist of alphanumeric characters
  2. First character must be an alphabet
  3. Underscore character is treated as an alphabet

In addition to the above rules, it is convention to adopt the following guideline even though not following them is syntactically accepted:

  1. Choose names to be both short as well as self explanatory. How you achieve these contradictory goals is a matter of practice.
  2. separate words in a multi-word name by underscore character or use CamelCase

Let us try this out:


-->a = 2; b = 3; c = -4;
-->disc = b^2 - (4 * a * c)
 disc  =
    41.
-->r1 = (-b + sqrt(disc)) / (2 * a), r2 =(-b + sqrt(disc)) / (2 * a)
r1 = 
     0.8507811
r2 =
    -2.3507811

This is what we did:

  1. Created three variables named a, b and c and initialized them with values 2, 3 and -4 respectively.
  2. Evaluated the expression b^2 - 4ac and assigned the result to a new variable disc.
  3. Evaluated the expressions (-b + sqrt(disc))/(2a) and (-b - sqrt(disc)) / (2a) and assigned the values to two new variables r1 and r2 respectively.

The workspace now contains all these variables and can be used in subsequent expressions or statements.

Screencasts

Watch the video on YouTube. Note: Comments in Scilab begin with two forward slashes (//) and end at the end of line. Comments in the screencast need not be typed. They are only to explain what is being done.

Exercises

  1. Discover other functions and what they do using Scilab Help browser.
  2. Discover other operators, especially boolean operators, using Scilab Help browser.
  3. Discover other constants using Variable Browser.