Scilab
Chapter 3. Scilab as a calculator
Chapter 3.1 Introduction
Scilab, like every programming language, has its own instructions, e.g. addition, subtraction, sin(x)… etc.
Scilab’s Console-basic window is ideal for testing them.
You simply enter e.g.:
2+2
and you immediately get the result 4.
With this approach, Scilab, or more precisely, its Console, acts as an ordinary calculator. On the one hand, it is a degradation for a serious programming language. On the other hand, it is an ideal method of learning new instructions. For example, mathematical ones, such as 4 bacic operations, sine, cosine logarithm… We will learn about the remaining instructions, i.e. control, input-output, in the next chapters.
Chapter 3.3 Basic Math Instruction
Choose the most convenient method for reading the course and testing it immediately.
In the previous chapter I chose the method from Chapter 2.5, that is
small course and Scilab windows at the same time,
you will mostly test the instructions without the animation, which I will leave for less typical cases.
We’ll start with the simplest action
Addition
2+2=4
The animation concerns the Polish version. However, you should have no problem interpreting the English version. The note also applies to the next animations. You already know how to do it from the previous chapter. Just in case, I’ll show you this in the animation in Fig.3-1.
Fig.3-1
2+2
1. Open Scilab by clicking its icon
2. Type into the Console window
2+2
3. Enter–>The result will appear 4
4. Type clear; clc//instructions to clean up the Variable Browser and Console.
Always do this when testing subsequent instructions, i.e. Subtraction, Multiplication…
You can check other instructions in the same way
Subtraction
5-3=2
Multiplication
5*3=15
Division
20/4=5
Absolute value
abs(-2)
–> abs(-2)
ans =2.
Complex operations, e.g. with brackets and division
(3+5*7)/(2+3*4)+4=6.7142857
Exponentiation
2^3=8
Root
sqrt(9)=3
sqrt(10)=3.1622777…
Nth degree root
nthroot(27,3)
ans =
3.
Correct. The third root of 27 is 3.
Let’s check again
nthroot(2,2)
ans =
1.4142136
and exponentiation
2^0.5
ans =
1.4142136
Sine
sin(%pi/2) (enter the data in radians)
ans =
1.
Cosine
cos(%pi/2)
ans =
6.123D-17
What’s up? It should be 0! And so it is. Note that 6.123D-17 is a very, very small number!
Scilab calculated it this way by decomposing cos(x) into a polynomial. It then treats this number as 0.
Check e.g. that
1+6.123D-17=1
Tangent
tan(%pi/4)
ans =
1.0000000
Cotangent
cotg(%pi/4)
ans =
1.0000000
If you want to treat trigonometric functions in a human way, i.e. in degrees, add the letter d.
So there will be sind(x), cosd(x), tand(x) and cotg(x)
Let’s check, for example, sin(30º)
sind(30)
ans =
0.5000000
Let’s also check the claim
sin²(x)+cos²(x)=1
for x=30º
–> [sind(x)]^2+[cosd(x)]^2
ans =
1.
4 important mathematical constants π, e
%pi=3.1415927…
%e=2.7182818…
%eps=2.220D-16=”almost zero”
Sometimes Scilab gives a very small number “almost zero” in its calculations instead of zero. It is known, for example, that cos(0) = 0 and in Scilab the output will be cos(0) = “almost zero”.
%i=0. + i –>imaginary number i
Complex number 2+3i
2+3*%i=2. + 3.i
Note:
In this chapter, you tested instructions as regular operations, e.g. 2+2. Here, the result of 2 was assigned to a specific ans variable. So ans=2+2=4. If you then entered e.g. 2+3 into the console, the result 5 would also be in the same variable ans=2+3=5. What if you used addition with assignment to a variable with a specific address in memory, e.g. x=2+2 and y=2+3? Then it could make our job easier by calculating z=x*y=4*5=20.
Defining a vector
After entering it into the console
x=[1,2,3]
and clicking Enter, the text will appear
x =
1. 2. 3.
In this way, the variable x was created as a vector with the following values 1, 2, 3.
Note:
The first element of this vector is x(1)=1, not x(0)=1.
Conclusion:
Vector indexing in Scilab. It starts from 1, not 0! I spent a lot of time looking for errors while testing the program. Remember that!
A vector, as well as matrices, do not have to consist only of numbers. They can also be made of strings (writings).
x=[‘Warsaw’, ‘Kraków’, ‘Suwałki’]
Check in the console
x=[‘Warsaw’, ‘Kraków’, ‘Suwałki’]
x =
“Warsaw” “Kraków” “Suwałki”
Transposing a vector
x=[1,2,3]
y=x’
y =
1.
2.
3.
A new vector y was created, which from a “horizontal” x vector became a “vertical” y vector
Dot product of 2 vectors
x=[1,2,3]
y=[4,5,6]
z=x*y’–>spatial product – note the ‘ sign
After pressing Enter, the scalar product of these vectors was calculated as
x=[1,2,3]
x =
1. 2. 3.
–> y=[4,5,6]
y =
4. 5. 6.
–> z=x*y’
z =
32.
Please note that the scalar product of 2 vectors is number = 32, otherwise it is a scalar and not a vector!
I will check manually.
1*4+2*5+3*6=32
Correct!
Matrix
A matrix is a generalization of a vector and its example is e.g.
A=[2,2,4;5,2,3;4,2,7]
This looks strange. Check it in the Scilab console
A=[2,2,4;5,2,3;4,2,7]
A =
2. 2. 4.
5. 2. 3.
4. 2. 7.
You can perform addition, subtraction, multiplication and division operations on matrices.
e.g.
–> B=[1,2,5;-3,2,-2;-1,2,3]
B =
1. 2. 5.
-3. 2. -2.
-1. 2. 3.
There is no problem with addition and subtraction
A+B
ans =
3. 4. 9.
2. 4. 1.
3. 4. 10.
But when it comes to multiplication, you need to remember the theory.
–> A*B
ans =
-8. 16. 18.
-4. 20. 30.
-9. 26. 37.
How to create a matrix whose every element is squared?
A=[2,2,4;5,2,3;4,2,7]
A =
2. 2. 4.
5. 2. 3.
4. 2. 7.
–> A.^2(note the dot next to A)
ans =
4. 4. 16.
25. 4. 9.
16. 4. 49.
Each element of matrix A multiplied by the corresponding element of matrix B
Note:
This is not a multiplication of matrices A and B
A=
A=[2,2,4;5,2,3;4,2,7]
A =
2. 2. 4.
5. 2. 3.
4. 2. 7.
B=[1,2,5;-3,2,-2;-1,2,3]
B =
1. 2. 5.
-3. 2. -2.
-1. 2. 3.
A.*B (note the dot next to A)
ans =
2. 4. 20.
-15. 4. -6.
-4. 4. 21.
Other mathematical operations instructions
I have provided only the most necessary Scilab instructions. You will find every math instruction in the help.
e.g. Prime factorization
factor(20)
ans =
2. 2. 5.
Indeed, 20=2*2*5