Scilab basic contains basics of Scilab language. you will familier with all basic terms of Scilab.
What is Scilab?
Scilab is free and open-source software for engineers & scientists. Scilab was developed with the Scilab team within the ESI Group.
It is a high-level numerical-oriented programming language.
Sci lab is used for Mathematics and Simulation, Optimization, Statistics Signal Processing, Algorithm and application development, 2D and 3D data visualization, Image processing and Computer vision, in the field of IoT.
Scilab is an interpreted language. IT has a variety of functions to solve complex problems easily.
Scilab is available under the GPL License. It means any one can
Use the software for any purpose,
Change the software to suit your needs,
Share the software with your friends and neighbors, and
Share the changes you make.
Scilab Basics
it contains following topics
- Keyword
- Variables
- Constants
- Comments
- Operators
- If else
- looping
- functions
- file creation in Scilab
Scilab keywords
Following are keywords in Scilab
- backslash — (\) left matrix division.
- brackets [,;] — Concatenation. Recipients of an assignment. Results of a function
- colon (:) — Ranging operator. Addresses all elements along an array dimension or of a list.
- comma — (,) comma; instruction, argument separator
- comments — (// or /*…*/) comments
- comparison — comparison, relational operators
- dollar — ($) last index
- dot — (.) symbol
- equal — (=) assignment , comparison, equal sign
- getscilabkeywords — returns a list with all scilab keywords.
- hat — (^) exponentiation
- greater — (>) greater than comparison
- minus — (-) subtraction operator. Sign change
- not — (~) logical not
- parentheses — ( ) left and right parenthesis
- percent — (%) special character
- plus (+) — Numerical addition. Text concatenation (gluing)
- quote — (‘) transpose operator, string delimiter
- semicolon — (;) ending expression and row separator
- slash — (/) right divisions. System’s feed back. Comments
- star — (*) multiplication operator
- symbols — scilab operator names
- tilde — (~) logical not
Scilab variables
In Scilab everything is the matrix. Scilab uses Boolean, string, integer, and complex numbers
Rules for Variable names
- Variable names can take as long as you want.
- Variable are case sensitive.
- It allows characters (A-Z,a-z) digits(0-9) and special symbols(%,_,#,!,$,?).
- First letter must be character or special character (%,_,#,!,$,?).
- Multilingual UTF-8 characters are accepted.
Assigning value to variable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | -> a=5 a = 5. --> name="Ram" name = "Ram" --> price=33.12 price = 33.12 --> result= %T result = T --> %age=25 %age = 25. --> #note="Note" #note = "Note" --> नाम ="Ram" नाम = "Ram" |
Scilab Constants
Sr No | Constant | Description |
---|---|---|
1 | SCI, WSCI | Variable containing the value of the root path of Scilab. |
2 | SCIHOME | Contains the path to preferences, history files of your Scilab session. |
3 | TMPDIR | Temporary directory path. |
4 | home | Gives the user directory. |
5 | %e | Euler number. |
6 | %eps | epsilon (floating-point relative accuracy) |
7 | %f or %F | Boolean variable for false. |
8 | %i | imaginary unit |
9 | %inf | infinity |
10 | %nan | not-a-number |
11 | %pi | ratio of circle’s circumference to its diameter |
12 | %s | A variable used to define polynomials. |
13 | %t or %T | Boolean variable for true. |
14 | %z | A variable used to define polynomials. |
Comments
To make single line comment // are used and to make multiline comment /* and */
are used.
Example
// This is a single line comment
/* this is
Multiline comment*/
scilab Basic Operators
Arithmetic Operators
Scilab has following arithmetic operators
Sr No | Operator | Example |
---|---|---|
1 | + (Addition) | 5+4=9 |
2 | – (Substraction) | 5-4=1 |
3 | * (Multiplication) | 5*4=9 |
4 | / (Division) | 4/2=2 |
5 | ^ (Power) | 2^3=8 |
relational operators in scilab
Scilab has following relational operators
Sr No | Operator | Example |
---|---|---|
1 | > | 5<6 (Less than) T |
2 | < (Greater than) | 6<7 F |
3 | <= (Less than equals to) | 5<=6 T |
4 | >= (Greater than equals to) | 6>=7 F |
5 | == (Equals to) | 6==6 T |
6 | ~= (Not Equals to) or <> | 7 ~=5 F |
scilab not equal
The not equals in sci lab is mentiond as ~= (Not Equals to) or <>
logical operators in scilab
Logical operator of scilab is as below
Sr No | Operator | Example |
---|---|---|
1 | & (AND) | %T & %F F |
2 | | (OR) | %T |%F T |
3 | ~ (NOT) | ~%T F |
if else in scilab
In programming, we have to choose any one choice either this or that. in this case to choose one among two options we use if-else statement.
Syntax of if-else is as below
1 2 3 | if (condition) then statements end |
Let’s see one example to check the greater among two numbers
1 2 3 | --> if 6>5 then disp("hello") end |
Output
1 | "hello" |
Using both if and else condition is Scilab
scilab if else example
scilab programs to check greater among two numbers
1 2 3 4 5 6 7 | A=5; B=7; if (a>b) then > disp ("A is greater") > else > disp ("B is greater") > end |
Output
1 | "B is greater" |
Select Statements
To select among multiple options scilab uses select statements
Syntax is select statement
1 2 3 4 5 6 7 | select expr, case expr1 then statements case expr2 then statements ... case exprn then statements else statements end |
Based on matching case expression statements are executed.
If no case expression is matched then else part is executed.
Here else is optional.
Example of case statement is as below
1 2 3 4 5 6 7 8 9 | n=2; select n case 1 then disp("case 1") case 2 then disp("case 2") else disp("No match") end |
Output
1 | "case 2" |
Looping Statements
Scilab supports for and while statements for looping
scilab for loop
Syntax of for statment
1 2 3 | for variable=n1:step:n2 statements end |
1 2 3 | --> for i=1:10 > disp "Hello" > end |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | "Hello" "Hello" "Hello" "Hello" "Hello" "Hello" "Hello" "Hello" "Hello" "Hello" |
1 2 3 | for i=1:2:20 disp(i) end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 1. 3. 5. 7. 9. 11. 13. 15. 17. 19. |
How to perform a reverse loop in Scilab?
1 2 3 | for i=10:-1:1 disp(i) end |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 10. 9. 8. 7. 6. 5. 4. 3. 2. 1. |
Use of break and continue in Scilab
The break is used to end the looping statements.
Continue transfer the control to the beginning of the loop statement.
Example of break in Scilab
1 2 3 4 5 6 | for i=1:5 disp(i) if(i==3) break end end |
When break keyword is used in above program it break the loop and print value 1,2 and 3.
Output
1 2 3 | 1. 2. 3. |
Example of continue in scilab
1 2 3 4 5 6 | for i=1:5 > if(i==3) then > continue > end > disp(i) > end |
When continue occurs in the program it transfers the control to the beginning of the loop
Output
1 2 3 4 | 1. 2. 4. 5. |
- While is a another looping keyword
Syntax is
while expr
statements
end
1 2 3 4 5 6 | i=0; n=10; while i<n disp(i); i=i+1; end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. |
Functions in Scilab
Function definition syntax
1 | function [y1, ..., yn]=foo(x1, ...,xm) |
1 | function [y1, ..., yn, varargout]=foo(x1, ...,xm,varargin) |
Scilab function to add two numbers.
1 2 3 | function [s]=sum(a,b) > s=a+b > endfunction |
Above is a function definition. To call sum function use sum(val1,val2)
as below.
1 2 3 | --> sum(1,2) ans = 3. |
Scilab function to find the max of two numbers
1 2 3 4 5 6 7 | function [result]=max(a,b) if (a>b) then result=a else result=b end endfunction |
1 2 3 | --> max(2,3) ans = 3. |
How to create a file is Scilab
open a new file
write a basic statement
disp("Display from file")
Save file.
Press F5.
this will execute the command on the command window.
As per Scilab.org On Windows, if your machine is based on an Intel processor, the Intel Math Kernel Library (MKL) enables Scilab to perform faster numerical computations.