Will we be using Xcos?
Session 2: Scilab as an interactive calculator
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 are not that difficult to master.
In this session you will learn the following:
- Using Scilab interactively to perform simple arithmentic operations
- Writing expressions and statements
- Creating and using variables
- Using Scilab built-in constants
- 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:
- At the command prompt, you entered the expression 2 + 3 and pressed the Enter key
- Scilab responded with the answer 5.
- 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
- 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 are no numeric types such as integers, float, double etc as in other programming languages. All numeric types are double precision real numbers (Scilab does allow integers int8, int16, int32, uint8, uint16, uint32, but they are rarely used)
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.
Scilab language is weakly typed. That is:
- 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.
- 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:
- We used the following operators: +, -, *, /, ^
- 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.
- A semicolon at the end of an expression suppresses the display of the result of that statement.
- 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.
- 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.
- All numbers are double precision real numbers or imaginary numbers.
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:
- Pi - %pi
- e - %e
- sqrt(-1) - %i
- Infinity - %inf
- Not a number - %nan
- True - %T or %t
- 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.
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:
- 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)
- TMPDIR - Directory where Scilab stores temporary files
- SCI - Directory where Scilab is installed. On Windows, it is in DOS filename convention of 8.3 characters.
- WSCI - Directory where Scilab is installed, in Windows filename convention.
- 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:
- sin(), cos(), tan() - angle must be input in radians.
- sind(), cosd(), tand() - angle must be input in degrees.
- asin(), acos(), atan() - Inverse trigonometric functions. Return angles in radians.
- asind(), acos(), atand() - Inverse trigonometric functions. Return angles in degrees.
- log(), log10() - Natural logarithm, Logarithm to base 10
- exp() - Anti natural logarithm
- sinh(), cosh(), tanh()
- 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:
- Name of a variable must consist of alphanumeric characters
- First character must be an alphabet
- 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:
- Choose names to be both short as well as self explanatory. How you achieve these contradictory goals is a matter of practice.
- 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:
- Created three variables named a, b and c and initialized them with values 2, 3 and -4 respectively.
- Evaluated the expression b^2 - 4ac and assigned the result to a new variable disc.
- 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.
Watch the following two screencasts, but before that, a note - I have used comments in the screencasts to explain what is being done because I could not record voice. When you try out the commands, you need not type the comments. In Scilab, comments begin with two forward slashes and end with the end of the line.
- Part 1: Interactive session in Scilab
- Part 2: Interactive session in Scilab showing determination of roots of a quadratic equation
- Discover other functions and what they do using Scilab Help browser.
- Discover other operators, especially boolean operators, using Scilab Help browser. Prepare a list of Scilab operators, especially boolean operators, which are usually neglected.
- Discover other constants using Variable Browser. Prepare a list of Scilab constants.
- Prepare a list of Scilab environment variables.