Scilab Tutorial

Enhance your skills with real-world coding challenges and comprehensive word problem solutions for effective learning.

1. Introduction to Scilab

SciLab – Open Source Tool and an Alternative for Matlab & Octave

  • What is Scilab?
    A powerful open-source software for numerical computation, data analysis, and visualization.

  • Scilab is case sensitive. “A” is not equal to “a”.

  • In Scilab, everything is a matrix

  • Freely downloaded numerical computation software for Engineering and Scientific applications.

  • Similar to Matlab but requires less disk space than Matlab & Octave

  • It includes a Matlab-to-Scilab translator (.m files to .sci files) and to dynamically compile / link other languages such as C, Java , etc.,

  • Integrated object-oriented 2-D and 3-D graphics with animation

  • Available for OS : Windows, Linux and Mac OS X

  • Link : http://www.scilab.org/  &  https://scilab.in/DownloadScilab

  • Why Learn Scilab?
    Ideal for engineering, mathematics, and scientific computations. Free alternative to MATLAB.

2. Getting Started

  • Download from https://scilab.in/DownloadScilab

  • Familiarize yourself with the Scilab console, editor, and workspace.

3. Scilab workspace

  • The console for making calculations,

  • The editor for writing programs,

  • The graphics windows for displaying graphics àbar(10:12)

  • The embedded help.

4. Console as a calculator

  • If no variable name is given, Scilab uses the inbuilt variable ans

  • Expressions can be written on the same line by separating them with a comma (;)

5. Predefined Variables

6. General functions

7. Statistical functions

Sort:

gsort(x) -> Descending order

gsort(x,’g’,’i') -> Ascending order

8. Trigonometry functions

9. Basic Syntax and Operations

Arithmetic Operations

Sample:

a = 10; b = 5;

sum = a + b; // Addition

difference = a - b; // Subtraction

product = a * b; // Multiplication

quotient = a / b; // Division

Special Constants

disp(%pi); // Value of π

disp(%e); // Value of Euler's number

10. Variables and Data Types

  • Variables: Scilab is an interpreted language, which implies that there is no need to declare a variable before using it.

  • Variables are created at the moment where they are first set

  • Variables:  Dynamically typed.  Example:
            x = 42; // Integer
            y = 3.14; // Float
           name = "John"; // String
  • Type Checking:
          disp(typeof(x));// Check data type

11. Vectors and Matrices

Creating Vectors

v = [1, 2, 3, 4, 5]; // Row vector

v2 = [1; 2; 3; 4; 5]; // Column vector

Matrix Creation

A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; // 3x3 Matrix

Basic Operations

transpose_A = A'; // Transpose

det_A = det(A); // Determinant

inv_A = inv(A); // Inverse

Predefined Functions - Matrix

  • zeros(3,4) // Matrix 3x4, with all elements zeros

  • ones(4, 2) // Matrix 4x2, with all element one

  • eye(3, 3) // Identity matrix of size 3x3

  • diag([1 2 3]) // Diagonal matrix of size 3x3, diagonal

                                    elements given in the row matrix [1 2 3]

  • rand(3, 5) // Matrix of size 3x5, with random numbers as

                                       its elements

12. Scilab Editor

  • Command = editor()

  • Click Launch SciNotes icon in the Console,

  • When several commands are to be executed, it may be more convenient to write these statements into a file with Scilab editor.

  • Depending on its content has the extension

.sce

.sci

.sci vs .sce

  • Files having the .sci extension are containing Scilab functions and 40 executing them loads the functions into Scilab environment (but does not execute them)

  • Files having the .sce extension are containing both Scilab functions and executable statements.

13. Functions for programming

input ()
disp( )
mprintf()
  • mprintf
  • converts, formats, and writes data to the main scilab window
Example:
  • mprintf('At iteration %i, Result :\nalpha=%f',33,0.535)
disp
  • disp(x1,[x2,...xn])
  • Example
  • disp([1 2],3)
  • disp("a",1,"c")
  • deff('[]=%t_p(l)','disp(l(3),l(2))')
  • disp(tlist('t',1,2))
input
x = input(message [, "string"])
Arguments:
Message : character string
"string“ : the character string "string" (may be abbreviated to "s")

17. Functions

  • Functions can have an arbitrary number of input and output arguments so that the complete syntax for a function which has a fixed number of arguments is the following:
[o1 , ... , on] = myfunction ( i1 , ... , in )
Where o1, ,,on are Output arguments i1,.. in are Input Arguments
The input and output arguments are separated by commas ",". Notice that the input arguments are surrounded by opening and closing braces, while the output arguments are surrounded by opening and closing square braces .
  • To define a new function, use the function and endfunction Scilab keywords.
  • Defining a Function:

function y = square(x)
y = x^2;
endfunction
Usage:
result = square(4);
disp(result); // Output: 16

15. Practice Problem

clear;
clc;
result = 0;
  n = input('Number of Element');
  //for loop for summation of number
for i = 1:n
   tem = input('please input a number: ');
   result = result + tem;
end
disp(result);
Note: Press F5 –To Execute

16. Plotting and Visualization

Basic Plot
x = linspace(0, 2 * %pi, 100); // Generate 100 points
y = sin(x); // Sine function
plot(x, y);
xlabel("x-axis");
ylabel("y-axis");
title("Sine Wave");
Multiple Plots
y1 = sin(x);
y2 = cos(x);
plot(x, y1, '-r', x, y2, '-b'); // Red and blue lines
legend("sin(x)", "cos(x)");

14. Control Statements

If-Else
x = 10;
if x > 5 then
disp("x is greater than 5");
else
disp("x is less than or equal to 5");
end
Loops
  • For Loop:

for i = 1:5
disp(i);
end
  • While Loop:

i = 1;
while i <= 5 do
disp(i);
i = i + 1;
end

18. File Handling

Loading and Saving Data
  • Save Variables:
save("data.sci", x, y);
  • Load Variables:
load("data.sci");

19. Scilab Toolboxes

  • Commonly Used Toolboxes:
    • Control Systems Toolbox: For systems analysis.
    • Optimization Toolbox: For solving optimization problems.
  • Install via atomsInstall("toolbox_name").

21. System Commands

  • pwd = retrieve the name of the current directory
  • cd or chdir = change the current directory
  • Clock = retrieves the date as a vector with six parameters [year, month, day, hour, minute, second].
  • date = retrieves a date as a character string.
  • calendar = displays a monthly or annual calendar.

23. Conclusions

  • Scilab is a non-commercial open source platform for Engineering and Scientific computations.
  • Scilab is ideal for educational institutes, schools and industries.
  • Scilab/Scicos is a better alternative for Matlab/Simulink.
  • Can perform mathematical computations, algorithm development, simulation, prototyping, and data analysis using scilab.
  • A valuable tool for researchers at no cost.

20. Hands-On Practice

Simple Applications:
1. Calculate Compound Interest:
P = 1000; r = 5/100; n = 10;
A = P * (1 + r)^n;
disp("Amount after 10 years: " + string(A));
2. Simulate Projectile Motion:
u = 20; g = 9.81; theta = 45 * %pi / 180;
T = 2 u sin(theta) / g;
t = linspace(0, T, 100);
x = u cos(theta) t;
y = u sin(theta) t - 0.5 g t.^2;
plot(x, y);
xlabel("Distance (m)"); ylabel("Height (m)");

22. Scilab Application

  • Educational Institutes, Research centers and companies
  • Math and computation , Algorithm development
  • Modeling, simulation, and visualization 6
  • Scientific and engineering graphics, exported to various formats so that can be included into documents.
  • Application development, including GUI building