powered by

SCILAB AS A CALCULATOR
The purpose of this tutorial is to get started using Scilab as a basic
calculator by discovering some predefined data types andfunctions.
Level

This work is licensed under a Creative Commons Attribution-NonComercial-NoDerivs 3.0 Unported License.

www.openeering.com

Step 1: The purpose of this tutorial
In the tutorial “First steps with Scilab” we have introduced to the user theScilab environment and its features and here the aim is to make him/her
comfortable with Scilab basic operations.

Step 2: Roadmap
In this tutorial, after looking over Scilab basic predefined data types andfunctions available in the environment, we will see the usage of variables,
how to define a new variable and some operations on numbers.
We will apply the acquired competencies for the resolution of a quadratic
equation of which we know the solution.

Scilab as a Calculator

www.openeering.com

Descriptions
Basic commands and operations
Predefined variables
Arithmetic and formats
Variables
Functions
ExampleConclusions and remarks

Steps
3-8
9
10-12
13-16
1718
19-20

page 2/12

Step 3: Scilab as a basic calculator
Scilab can be directly used to evaluate mathematical expressions.

0.4 + 4/2
ans =
2.4

ans is the default variable that stores the result of the last mathematicalexpression (operation). ans can be used as a normal variable.

Step 4: Comments
A sequence of two consecutive slashes // out of a string definition marks
the beginning of a comment. The slashes as well as all the following
characters up to the end of the lines are not interpreted.

// This is a comment// Let's divide the previous value by two
0.4 + 4/2
ans/2
ans =
2.4
ans =
1.2Step 5: Basic mathematical operators
Basic mathematical operators:-addition and subtraction: +, -(0.4 + 4)/(3-4^0.5) // A comment after the command-multiplication and division: *, /-power: ^ans =4.4-parentheses: ()Scilab as a Calculator

www.openeering.com

page 3/12

Step 6: The Scilab operator “,”
The Scilab operator , can be used to separate expressions in the samerow.

Step 7: The Scilab operator “...”The Scilab operator ... can be used to split an expression in more thanone row.

// Two expressions1*2 , 1.1 + 1.3
ans =
2.
ans =
2.4

// The expression is toooooooo long
1 + 1/2 + 1/3 + ...
1/4 + 1/5 + ...
1/6
ans =
2.45

Step 8: The Scilab operator “;”
The Scilab operator ; is used to suppress the output, which will not be
displayed in the Console.

The command ; can also be used to separate expressions (in generalstatements, i.e. Scilab commands) in the same row.

Scilab as a Calculator// An expression
1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6;// The result is stored in the ans variableans
ans =
2.45

www.openeering.com

page 4/12

Step 9: Predefined variables- 1/5

In Scilab, several constants and functions already exist and their names
begin with a percent character %.
For example, three of the main variables with a mathematical meaning are-%e, which is the Euler’s constant e-%pi, which is the mathematical constant-%i, which is the imaginary number i%pi // pi = 3.1415....
sin(%pi)ans =3.1415927
ans =
1.225D-16

In the example on the right we have displayed the value of and its sinus
through the use of the Scilab sinus function sin. We should obtain, but we get a really close to zero value because of the machinerounding error.

Step 10: Complex arithmetic%i // imaginary unitsqrt(-1)
exp(%i*%pi)+1 // The famous Euler relation

Also complex arithmetic is available. %i, is the imaginary unit i

On the right we get the imaginary unit also computing the square root of -1
and the Euler relation returns a really close to zero value because of themachine rounding error.

Scilab as a Calculator

ans =ians =ians =1.225D-16i

www.openeering.com

page 5/12

Step 11: Extended arithmetic
In Scilab, the “not a number” value Nan comes from a mathematically
undefined operation such as 0/0 and the corresponding variable is %nan,
while Inf stands for “infinity” and the corresponding variable is %inf.

The command ieee() returns the current floating point exception mode.
0  floating point exception produces an error
1  floating point exception produces a warning
2  floating point exception produces Inf or Nan

The command ieee(mod) sets the current floating point exception mode.
The initial mode value is 0.

Scilab as a Calculator

ieee(2) // set floating point exceptions for Inf andNan
1/00/0, %inf*%inf, %inf*%nanieee(0) // unset floating point exceptions for Inf and
Nan
1/0
0/0
ans =
Inf
ans =
Nan
ans =Nanans =Nan!--error 27
Division by zero...
!--error 27
Division by zero...

www.openeering.com

page 6/12

Step 12: Change the visualization format
All computations are done in double precision arithmetic, although thevisualization format may be limited.

Using the command format the option 'e' sets the e-format, while 'v'
sets the variable one. We can also choose the number of digits tovisualize.

format('v',20); %pi // Change visualization
format('e',20); %pi // Change visualization
format("v",10); %pi // Restore original visualization
ans =3.14159265358979312
ans =3.1415926535898D+00
ans =3.1415927

Step 13: Defining new variables// Define variables a and b
a = 4/3;
b = 3/4;// Define variable c as expression of a and bc = a*b;// Display the resultdisp(c)

Syntax:name of the variable = expression
where expression can involve other variables.

Some constraints:
1.

-

Variable names can be up to 24 characters long-

Variable names are case sensitive (variable A is different from a)-

The first letter must be an alphabetic character (a-A, z-Z ) or theunderscore character ( _ )-Names must not contain blanks and special characters

The disp command is used to display data to the console.

Scilab as a Calculator

www.openeering.com

page 7/12

Step 14: String variables// Two strings
a = 'Hello';b = 'World';// String concatenationc = a + " " + b + "!" ;disp(c);// Concatenation of a string with a numberd = "Length of " + a + " is " + string(length(a))

String variables are delimited by quotes characters of type " or '.

The command string converts a number into a string.

Hello World!
d =Length of Hello is 5

Step 15: Boolean variables
Boolean variables are used to store true (%t or %T) or false data (%f or %F)
typically obtained from logical expressions.

The comparison operators are:-< : Less than-<= : Less than or equal to-== : Equal to->= : Greater than or equal to-> : Greater thanScilab as a Calculator// Example of a true expression
res = 1>0// Example of a false expression
res = 1<0
res =T
res =F

www.openeering.com

page 8/12

Step 16: Main advantages using Scilab// a contains a number
a = 1;
disp(a)// a is now a stringa = 'Hello!';disp(a)When working with variables in Scilab we have two advantages:-Scilab does not require any kind of declaration or sizing-

The assignment operation coincides with the definition

1.
In the example on the right we have not declared the type and the size ofa: we just assigned the value 1 to the new variable.

Hello!

Moreover, we have overwritten the value 1 of type double contained in awith the string Hello! by simply assigning the string to the variable.

In the Variable Browser we can see that the type of a changes outright:

Scilab as a Calculator

www.openeering.com

page 9/12

Step 17: Scilab functions
Many built-in functions are already available, as you can see in the tableon the right. Type in the Console the command help followed by thename of a function to get the description, the syntax and some examples
of usage of that function.

Field
Trigonometry
Log - exp – power
Floating point
ComplexIn the examples on the right you can see different ways to set input andoutput arguments.

Commands
sin, cos, tan, asin, acos, atan,sinh, cosh, ...
exp, log, log10, sqrt, ...
floor, ceil, round, format,
ieee, ...
real, imag, isreal, ...

// Examples of input argumentsrandsin(%pi)max(1,2)
max(1,2,5,4,2)// Examples of output argumentsa = randv = max(1,2,5,4,2)[v,k] = max(1,2,5,4,2)

Scilab as a Calculator

www.openeering.com

page 10/12

Step 18: Example (quadratic equation)// Define input dataa = 3; b = -2; c = -1/3;// Compute delta
Delta = b^2-4*a*c;// Compute solutions
x1 = (-b+sqrt(Delta))/(2*a);x2 = (-b-sqrt(Delta))/(2*a);// Display the solutions
disp(x1); disp(x2);

The well-known solutions of a quadratic equationare√
where. Ifsolutions are imaginary.
0.8047379- 0.1380712

We assess the implementation on the following input data:
Coefficient
abc// Exact solutions
x1e = (1+sqrt(2))/3x2e = (1-sqrt(2))/3// Compute differences between solutions
diff_x1 = abs(x1-x1e)
diff_x2 = abs(x2-x2e)

Value+3.0-2.0-1.0/3.0

where the solutions are√x1e =0.8047379
On the right you can find the implementation and the validation of thenumerical solutions with respect to the exact solutions.

x2e =- 0.1380712
diff_x1 =0.
diff_x2 =0.

Scilab as a Calculator

www.openeering.com

page 11/12

Step 19: Concluding remarks and References

1. Scilab Web Page: www.scilab.org.

In this tutorial we have introduced to the user Scilab as a basic calculator,
in order to make him/her comfortable with Scilab basic operations.

2. Openeering: www.openeering.com.

Step 20: Software content
To report a bug or suggest some improvement please contact Openeering
team at the web site www.openeering.com.
Thank you for your attention,---------------------SCILAB AS A CALCULATOR----------------------------------Main directory-------------license.txt
example_calculator.sce: the license file
: examples in this tutorial

Anna Bassi, Manolo Venturin

Scilab as a Calculator

www.openeering.com

page 12/12