1.
Basic Mathematical Operations in Scilab
Notes:
Scilab is a powerful software used for numerical computation. Before we begin program-
ming, it’s important to understand how to perform arithmetic operations. These are the
fundamental building blocks of any program.
• + (Addition): Adds two numbers. Example: 3 + 5 = 8
• - (Subtraction): Subtracts one number from another. Example: 9 - 2 = 7
• * (Multiplication): Multiplies two numbers. Example: 4 * 3 = 12
• / (Division): Divides one number by another. Example: 8 / 2 = 4
• ^ (Exponentiation): Raises a number to a power. Example: 2^3 = 8
Note: In Scilab, variables must be defined before use. For example, x = 5; assigns the
value 5 to the variable x.
Example:
// Basic operations
x = 10;
y = 5;
add = x + y; // Result: 15
sub = x - y; // Result: 5
mul = x * y; // Result: 50
div = x / y; // Result: 2
pow = x^2; // Result: 100
disp(add);
disp(sub);
disp(mul);
disp(div);
disp(pow);
Tips:
• Use disp() to print results.
• Every command in Scilab must end with a semicolon (;) to suppress output. If you
omit the semicolon, the result is shown immediately.
2. Polynomial Roots and Formation
Notes:
Polynomials are mathematical expressions involving a variable (usually x) raised to vari-
ous powers. In Scilab, we can define polynomials, find their roots, and create new polynomials
from given roots.
1
• To define a polynomial from coefficients : use poly(p,"x","coeff")) p = [1 -3 2];
gives output 1 − 3x + 2x2
• To find the roots (solutions) of the polynomial: roots(p)
• To form a polynomial from roots: use poly([r1, r2, ...], ’x’)
Example:
// Define polynomial x^2 - 3x + 2
p = [1 -3 2];
r = roots(p);
disp(r);
// Form polynomial from roots 2 and 1
roots_array = [2, 1];
p_new = poly(roots_array, ’x’);
disp(p_new);
Tips:
• The coefficients are written from the highest degree to the constant term.
• roots([1 2 3 4]) gives roots of x4 + 2x3 + 3x2 + 4.
• Roots may be real or complex.
3. Matrix and Vector Operations
Notes:
Scilab is well-suited for working with vectors (1D arrays) and matrices (2D arrays). These
are essential in scientific and engineering computations.
• A row vector: v = [1 2 3]
• A column vector: v = [1; 2; 3]
• A matrix: A = [1 2; 3 4] represents a 2x2 matrix
• Matrix addition: C = A + B (both matrices must be of the same size)
• Matrix multiplication: C = A * B (check inner dimensions)
• Element-wise multiplication: C = A .* B
• Transpose of matrix: A’
• Inverse of matrix: inv(A)
2
Example:
A = [1 2; 3 4];
B = [5 6; 7 8];
C = A + B; // Matrix addition
D = A * B; // Matrix multiplication
E = A .* B; // Element-wise multiplication
T = A’; // Transpose of A
Inv=inv(A) // Inverse of A
disp(C);
disp(D);
disp(E);
disp(T);
disp(Inv);
Tips:
• Use semicolons to separate rows in a matrix.
• Use dot (.) for element-wise operations.
Tips:
• The inverse exists only for square matrices with non-zero determinant.
• You can multiply A * inv(A) to verify it returns the identity matrix.
4. Quotient and Remainder
Notes:
When one integer is divided by another, we can calculate both the quotient (how many
times the divisor fits into the dividend) and the remainder (what is left over).
In Scilab:
• Use floor() for integer division (quotient).
• Use the modulo() function to find the remainder.
Syntax:
q = floor(a / b); // Quotient
r = modulo(a, b); // Remainder
Example:
3
a = 17;
b = 5;
q = floor(a / b);
r = modulo(a, b);
disp("Quotient:");
disp(q);
disp("Remainder:");
disp(r);
Tips:
• The modulo(a, b) function returns the remainder when a is divided by b.
• The floor() function returns the greatest integer less than or equal to the value.
5. Solution of system of linear equations
Consider the following system of equations:
(
2x + 3y = 8
4x + y = 7
This can be written in matrix form as:
AX = B
where
2 3 x 8
A= , X= , B=
4 1 y 7
Syntax:
// Define coefficient matrix A
A = [2 3; 4 1];
// Define constant matrix B
B = [8; 7];
// Solve the system AX = B using left matrix division
X = A \ B;
// Display the result
disp("Solution (x, y):");
disp(X);
4
Explanation:
• A \ B computes the solution to the linear system AX = B.
• The operator \ performs left matrix division (i.e., A−1 B if A is invertible).
• The result X is a column vector containing the values of x and y.
Notes:
• Make sure that matrix A is square and has a non-zero determinant (i.e., it is invertible).
• You can use det(A) to check the determinant of A.
6. If elseif else
Conditional statements are used to execute specific blocks of code based on whether a con-
dition is true or false. Scilab supports standard conditional constructs: if, elseif, and
else.
Syntax:
if condition1 then
// statements if condition1 is true
elseif condition2 then
// statements if condition2 is true
else
// statements if none of the above conditions are true
end
Example 1: Positive, Negative, or Zero
The following Scilab code checks whether a number is positive, negative, or zero.
n = input("Enter a number: ");
if n > 0 then
disp("Positive");
elseif n < 0 then
disp("Negative");
else
disp("Zero");
end
Explanation:
• The if statement checks the first condition (n > 0).
• If the first condition is false, the elseif condition is checked (n < 0).
5
• If none of the conditions are true, the else block executes.
• The conditional block is closed using end.
Notes:
• You can have multiple elseif branches.
• The else block is optional.
• Use disp() to display output.
• Use input() to read input from the user.
The input() Function
Syntax:
variable = input("Prompt message: ");
Optional syntax for string input:
string_variable = input("Enter a string: ", "string");
Description:
• input() prompts the user to enter a value from the keyboard.
• The input can be numeric, a matrix, or a string.
• When expecting text, use the second argument "string".
Example:
age = input("Enter your age: ");
name = input("Enter your name: ", "string");
disp("Name: " + name);
disp("Age: " + string(age));
7. For Loop
The for loop in Scilab is used to repeat a block of code a fixed number of times. It is useful
when the number of iterations is known in advance.
Syntax
for variable = start:step:end
// statements
end
• start is the initial value of the loop variable.
6
• step is the increment (default is 1 if omitted).
• end is the final value.
Simplified syntax (step = 1):
for i = 1:10
// statements
end
Example 1: Print Numbers from 1 to 5
for i = 1:5
disp(i);
end
Example 2: Sum of First 10 Natural Numbers
sum = 0;
for i = 1:10
sum = sum + i;
end
disp("Sum = " + string(sum));
Notes
• Use disp() to display values inside the loop.
• Loop variable can also be used in reverse, e.g., for i = 10:-1:1.
• Nested for loops are allowed.
8. While Loop
The while loop in Scilab is used to repeat a block of statements as long as a specified
condition remains true.
Syntax:
while condition
// statements
end
• The condition is evaluated before each iteration.
• If the condition is true, the loop body is executed.
• If the condition becomes false, the loop terminates.
7
Notes:
• Be sure to update variables inside the loop to avoid infinite loops.
• You can use break to exit a loop early.
• Use disp() to display intermediate results.
Example: Sum of Digits of a Positive Integer
Scilab code:
n = input("Enter a positive integer: ");
sum = 0;
while n > 0
digit = modulo(n, 10); // Get the last digit
sum = sum + digit; // Add it to sum
n = floor(n / 10); // Remove the last digit
end
disp("Sum of digits = " + string(sum));
Explanation:
• The loop continues as long as n > 0.
• modulo(n, 10) extracts the last digit.
• floor(n / 10) removes the last digit.
• Digits are added one by one to compute the total sum.
9. Reverse the digits of a positive number
Objective: To write a Scilab program that takes a positive integer as input and prints its
digits in reverse order.
Algorithm:
1. Accept a positive integer n from the user.
2. Initialise rev = 0 to store the reversed number.
3. Repeat the following steps while n > 0:
• Extract the last digit using modulo(n, 10).
• Append the digit to rev using rev = rev * 10 + digit.
• Remove the last digit from n using floor(n / 10).
8
4. Display the final reversed number.
Scilab Code:
clc;
clear;
// Input from user
n = input("Enter a positive integer: ");
rev = 0;
if n <= 0 then
disp("Please enter a positive integer.");
else
while n > 0
digit = modulo(n, 10);
rev = rev * 10 + digit;
n = floor(n / 10);
end
disp("Reversed number is:");
disp(rev);
end
10. GCD of two positive integers
Objective: To write a Scilab program that finds the Greatest Common Divisor (GCD) of
two integers using the Euclidean Algorithm.
Algorithm:
1. Accept two integers a and b from the user.
2. Convert both numbers to their absolute values using abs().
3. Repeat the following steps until b becomes 0:
• Compute the remainder r = modulo(a, b).
• Assign a = b and b = r.
4. When b = 0, a contains the GCD.
Scilab Code:
clc;
clear;
9
// Input from user
a = input("Enter first integer: ");
b = input("Enter second integer: ");
// Check for valid input
if a == 0 & b == 0 then
disp("GCD is not defined for both numbers being zero.");
else
a = abs(a);
b = abs(b);
// Euclidean Algorithm
while b <> 0
r = modulo(a, b);
a = b;
b = r;
end
disp("GCD is:");
disp(a);
end
10