Você está na página 1de 55

STUDY EXPERIMENT 1

SCILAB WORKING ENVIRONMENT

SCILAB CONSOLE

Scilab Working Envionment

The first way is to use Scilab interactively, by typing commands in the console, analyzing
the results and continuing this process until the final result is computed. This document is
designed so that the Scilab examples which are printed here can be copied into the console. The
goal is that the reader can experiment by himself Scilab behavior. This is indeed a good way of
understanding the behavior of the program and, most of the time, it allows a quick and smooth
way of performing the desired computation.
In the following example, the function disp is used in the interactive mode to print out the
string Hello World!.
The completion in the console.

-->s="Hello World!" s =
Hello World! -->disp(s)
Hello World!
In the previous session, we did not type the characters -->which is the prompt, and which
is managed by Scilab. We only type the statement s="Hello World!" with our keyboard and then
hit the <Enter> key. Scilab answer is s = and Hello World!. Then we type disp(s) and Scilab
answer is Hello World!.
When we edit a command, we can use the keyboard, as with a regular editor. We can use the
left and right arrows in order to move the cursor on the line and use the <Backspace>and
<Suppr>keys in order to fix errors in the text.
In order to get access to previously executed commands, use the up arrow key. This allows
to browse the previous commands by using the up and down arrow keys.
The <Tab>key provides a very convenient completion feature. In the following session, we
type the statement disp in the console.
-->disp
Then we can type on the <Tab>key, which makes a list appear in the console, as presented
in figure 4. Scilab displays a listbox, where items correspond to all functions which begin with
the letters disp. We can then use the up and down arrow keys to select the function we want.
The auto-completion works with functions, variables, files and graphic handles and makes
the development of scripts easier and faster.

The editor
Scilab version 5.2 provides a new editor which allows to edit scripts easily. Figure 5 presents
the editor during the editing of the previous Hello World! example.
The editor.

The editor can be accessed from the menu of the console, under the Applications > Editor
menu, or from the console, as presented in the following session.

-->editor()
This editor allows to manage several files at the same time, as presented in figure 5, where
we edit five files at the same time.
There are many features which are worth to mention in this editor. The most commonly
used features are under the Execute menu.

Load into Scilaballows to execute the statements in the current file, as if we did a copy
and paste. This implies that the statements which do not end with the semicolon ;
character will produce an output in the console.

Evaluate Selection allows to execute the statements which are currently selected.

Execute File IntoScilaballows to execute the file, as if we used the exec function. The
results which are produced in the console are only those which are associated with
printing functions, such as disp for example.

We can also select a few lines in the script, right click (or Cmd+Click under Mac), and get the
context menu which is presented in figure 6.
The Edit menu provides a very interesting feature, commonly known as a pretty printer in
most languages. This is the Edit > Correct Indentation feature, which
Context menu in the editor.

automatically indents the current selection. This feature is extremelly convenient, as it allows to
format algorithms, so that the if, for and other structured blocks are easy to analyze.
The editor provides a fast access to the inline help. Indeed, assume that we have selected the
disp statement, as presented in figure 7. When we right-click in the editor, we get the context
menu, where the Help about disp entry allows to open the help page associated with the disp
function.
STUDY EXPERIMENT - 2

BASIC FUNCTIONS IN SCILAB

ans answer

backslash (\) left matrix division.

brackets ([,]) left and right brackets

colon (:) colon operator

comma (,) comma; instruction, argument separator

comments (//) comments

comparison comparison, relational operators

dollar ($) last index

dot (.) symbol

else keyword in if-then-else and select-case-then-else

elseif keyword in if-then-else

empty ([]) empty matrix

end end keyword

equal (=) assignment , comparison, equal sign

extraction matrix and list entry extraction

global Define global variable

hat (^) exponentiation

assignation partial variable assignation

great (>) greater than comparison

minus (-) subtraction operator, sign changes


not (~) logical not

parentheses ( ) left and right parenthesis

percent (%) special character

plus (+) addition operator

power (^,.^) power operation

quote (') transpose operator, string delimiter

semicolon (;) ending expression and row separator

slash (/) right division and feed back

star (*) multiplication operator

symbols scilab operator names

tilde (~) logical not

catch beginning of catch block in try-catch control instruction

Control flow

abort interrupt evaluation.

break keyword to interrupt loops

case keyword used in statement "select"

continue keyword to pass control to the next iteration of a loop

do language keyword for loops

for language keyword for loops

if keyword for conditional execution

pause pause mode, invoke keyboard

resume return or resume execution and copy some local variables

return return or resume execution and copy some local variables

select select keyword

then keyword in control flows 'if' and 'select'


while while keyword

Variables

Predefined variables

SCI variable containing the value of the root path of Scilab.

SCIHOME contains the path to preferences, history files of your Scilab session.

TMPDIR temporary directory path.

home Gives the user directory.

%e Euler number.

%eps epsilon (floating-point relative accuracy)

%f or %F Boolean variable for false.

%i imaginary unit

%inf infinity

%nan not-a-number

%pi ratio of circle's circumference to its diameter

%s A variable used to define polynomials.

%t or %T Boolean variable for true.

%z A variable used to define polynomials.

clear kills variables

clearglobal kills global variables

exists checks variable existence

getvariablesonstack get variable names on stack of scilab

isdef checks variable existence

isglobal check if a variable is global

names Scilab names syntax

predef variable protection


who listing of variables

who_user listing of user's variables

whos listing of variables in long form

exit Ends the current Scilab session

perl Call Perl script using appropriate operating system executable. This function is
obsolete.

quit Terminates Scilab or decreases the pause level

scilab Main script to start Scilab and miscellaneous tools (GNU/Linux, Unix and
Mac OS X)

startup startup files

Differential calculus, Integration

diff Difference and discrete derivative

feval multiple evaluation

impl differential algebraic equation

intc Cauchy integral

integrate integration of an expression by quadrature

intg definite integral

Elementary Functions

Bitwise operations

bitand bitwise AND

bitcmp bitwise complement

bitget bit at specified position

bitor bitwise OR

bitset set bit at specified position

bitxor bitwise XOR


isequalbitwise bitwise comparison of variables

Complex

complex Create a complex number.

conj Complex conjugate

imag imaginary part

imult multiplication by i the imaginary unitary

isreal check if a variable has real or complex entries

real real part

Discrete mathematics

factor factor function

factorial The factorial function

gcd Greatest Common Divisor

lcm least common multiple

perms all permutations of vector components

primes primes function

rat Floating point rational approximation

Elementary matrices

diag diagonal including or extracting

eye identity matrix

ones matrix made of ones

rand Random numbers

squarewave generates a square wave with period 2*%pi

sub2ind matrix subscript values to linear index

testmatrix generate special matrices, such as Hilbert, Franck


toeplitz Toeplitz matrix

zeros matrix made of zeros

Log - exp - power

exp element-wise exponential

expm square matrix exponential

log natural logarithm

log10 base 10 logarithm

log1p computes with accuracy the natural logarithm of its argument added by one

log2 base 2 logarithm

logm square matrix logarithm

polar polar form

sqrt square root

sqrtm matrix square root

Floating point

ceil round up

clean cleans matrices (round to zero small entries)

double conversion from integer to double precision representation

fix round towards zero

floor round down

format number printing and display format

frexp dissect floating-point numbers into base 2 exponent and mantissa

ieee set floating point exception mode

int round towards zero

isinf check for infinite entries

isnan check for "Not a Number" entries


nearfloat get previous or next floating-point number

nextpow2 next higher power of 2.

number_properties determine floating-point parameters

round round to nearest integer

Radix conversions

base2dec convert from base b to decimal

bin2dec convert from binary to decimal

dec2base Convert decimal to base N number in string

dec2bin convert from decimal to binary

dec2hex convert from decimal to hexadecimal

dec2oct convert from decimal to octal

hex2dec convert from hexadecimal to decimal

oct2dec convert from octal to decimal

Matrix manipulation

flip dim flip x block components along a given dimension

matrix reshape a vector or a matrix to a different size matrix

permute permute the dimensions of an array

pertrans Transposition with reference to the 2nd diagonal

repmat Replicate and tile an array

resize_matrix trim or/and extend (and cast) a matrix or hypermatrix

squeeze removes singleton dimensions of a hypermatrix

abs absolute value, magnitude

cross vector cross product

max maximum

min minimum
norm matrix norm

prod product of array elements

signm matrix signum function

sum sum of array elements

tril lower triangular part of matrix

triu upper triangle

Search and sort

dsearch distribute, locate and count elements of a matrix or hypermatrix in given


categories

find find indices of boolean vector or matrix true elements

gsort sorting by quick sort algorithm

lex_sort lexicographic matrix rows sorting

vectorfind finds in a matrix rows or columns matching a vector

Set operations

intersect returns the vector of common values of two vectors

members count (and locate) in an array each element or row or column of another
array

setdiff returns components of a vector which do not belong to another one

union extract union components of a vector

unique extract unique components of a vector or matrices

Trigonometry

acos element wise cosine inverse (radians)

acosd element wise cosine inverse, result in degree.

cos cosine function

sin sine function

sinc sinc function


sind sine function, argument in degree.

sinh hyperbolic sine

sinhm matrix hyperbolic sine

sinm matrix sine function

tan tangent

tand tangent, argument in degree.

tanh hyperbolic tangent

tanhm matrix hyperbolic tangent

tanm matrix tangent

o and logical AND of the elements of an array

o & logical AND operator

Linear Algebra

Eigenvalue and Singular Value

balanc matrix or pencil balancing

bdiag block diagonalization, generalized eigenvectors

gschur generalized Schur form. This function is obsolete.

gspec eigenvalues of matrix pencil. This function is obsolete.

hess Hessenberg form

pbig eigen-projection

projspec spectral operators

psmall spectral projection

schur [ordered] Schur decomposition of matrix and pencils

spec eigenvalues of matrices and pencils

sva singular value approximation


svd singular value decomposition

Plot and display

black Black-Nichols diagram of a linear dynamical system

bode Bode plot

bode_asymp Bode plot asymptote

evans Evans root locus

gainplot magnitude plot

hallchart Draws the Hall chart

m_circle plots the complex plane iso-gain contours of y/(1+y) (obsolete)

nicholschart Nichols chart

nyquist nyquist plot

phaseplot frequency phase plot

sgrid s-plane grid lines.

show_margins display gain and phase margin and associated crossover frequencies

svplot singular-value sigma-plot

zgrid zgrid plot

Polynomials

degree degree of polynomial matrix

denom denominator

derivat Rational matrix derivative

determ determinant of polynomial matrix

detr polynomial determinant

factors numeric real factorization

hermit Hermite form


inv_coeff build a polynomial matrix from its coefficients

invr inversion of (rational) matrix

pol2str polynomial to string conversion

polfact minimal factors

poly polynomial definition

residu residue

roots roots of polynomials


Ex No: 1 MATRIX COMMANDS

Date:

AIM:To write and perform basic commands related to matrices in Scilab.

ALGORITHM:
Step 1: Start the Scilab Software
Step 2: Get the input values for matrix
Step 3: Perform matrix operation using corresponding values
Step 4: Store the output values using some variables
Step 5: Stop

PROGRAM:

Matrices Commands:
Let matrix A be defined as follows:
A=[1 2; 0 4]
i.e., A=1 2
0 4
Commands:
1. det(A)
It returns the determination of a given square matrix A
OUTPUT:Ans=4

2. rank(A)
It returns the rank of a given rectangular matrix A
OUTPUT:Ans=2

3. trace(A)
It returns the sum of the diagonal elements of a rectangular matrix A
OUTPUT:Ans=5

4. inv(A)
It returns the inverse of a non-singular matrix A(i.e., A^-1)
OUTPUT:
Ans= 1.0000 -0.5000
0 0.2500

5. norm(A)
It returns the Euclidean norm of the given rectangular matrix A
OUTPUT:
Ans=4.4954

6. A(A transpose)
It returns the transpose of the specified rectangular matrix A
OUTPUT:
Ans=
1 0
2 4

7.x=A\b (Left division)


It returns the solution vector x of a linear system described by a set of linear algebraic
equation.
Ax=b
A=[1 2; 0 4];
b=[1; 2];
x=A\b
OUTPUT:
Ans =
x= 0
0.5000

8. find(A)
This function returns the row and column indices of the non-zero entries in the matrix A
OUTPUT:
Ans=
1
3
4
Matrix Manipulation Commands:
1.Reshaping of a matrix as a vector:
1 10 20
Let matrix A = 2 5 6 Reshape the matrix to a column vector.
7 8 9
Solution:
This command converts the matrix A to a column vector, B
A=[1 10 20; 2 5 6; 7 8 9]
B=A(:)

OUTPUT:
Ans=

B= 1
2
7
10
5
8
20
6
9

2. Expanding Matrix size


Expand the matrix size to A(2,3)=15 and matrix A= 6 7
8 9
Solution:
When single or few elements are entered in a matrix, MATLAB creates the matrix of proper
dimensions to accommodate the entered elements, the remaining unspecified elements are
assumed to be zero.
A=[6 7;8 9]

A(2,3)=15

OUTPUT:
Ans=
A=6 7 0
8 9 15

3. Appending a row/Column to a matrix


This command is used to append a column or a row to the given matrix.
Let A = 6 7 x=1 and y=3 4
8 9 2
i) Append the vector x as new column of A and store in matrix B
ii) Append the vector y as new row and store in matrix C

Solution:
I) A=[6 7; 8 9]
X=[1 ; 2]
B=[A X]
II) A=[6 7;8 9]
Y=[3 4]
C=[A ;Y]

OUTPUT:
Ans=
B=6 7 1
8 9 2

C=6 7
8 9
3 4

4. Deleting a row or column of a matrix:


This command is used to delete a column or a row of a given matrix.
Given the matrices F= 6 7 and G= 6 7 1 . Delete the first row from matrix F and first
two
8 9 8 9 2
columns of matrix G.
Solution:
F=[ 6 7 ; 8 9]
G=[ 6 7 1; 8 9 2]
F=(1,:)=[]
G(: , 1:2)=[]
OUTPUT:
Ans=
F=
8 9
G=1
2

5. Concatenation of matrices:
This command is used to join or combine small matrices together to make larger matrices or
bigger matrices.
Let matrix A be given by A= 1 2 . Compute matrix B where B= A1 A2 where the sub-
3 4 A3 A4
matrices, A1=A, A2=A+1, A3=A+24, A4=A+10

Solution:
A=[1 2; 3 4]
B=[ A A+1 ; A+24 A+10]

OUTPUT:
Ans=

B=
1 2 2 3
3 4 4 5
25 26 11 12
27 28 13 14

RESULT: Thus the matrix and manipulation commands are executed and output is verified
successfully.
Ex No: 2 POLYNOMIAL COMMANDS

Date:

AIM: To write and perform different operations on polynomial in Scilab.

ALGORITHM:
Step 1: Start the Scilab Software
Step 2: Get the input values for polynomial
Step 3: Perform operation using corresponding values
Step 4: Store the output values using some variables
Step 5: Stop

PROGRAM:
Commands on Polynomials:

1. ROOTS OF POLYNOMIALS
The roots of polynomial can be found by using the following command.
Find the roots of polynomial s^2+3s+2=0
Solution:
p=[1 3 2];
val=roots(p)
OUTPUT:
val =
-2
-1

2. POLYNOMIAL ADDITION AND SUBTRACTION


Two polynomials can be added by using the arithmetic operator plus + and subtracted by
minus -
Add the two polynomials, a=(s^2+2s+1) and b=(s^3+s+5)
Solution:
a=[1 2 3 4] ;
b=[2 3 4 5];
c=a+b
OUTPUT:
c=
3 5 7 9
The resultant polynomial is 3s^3+5s^2+7s+9
Subtract the two polynomials, a=(3s^3+2) and b=(s+7)
Solution:
a=[3 0 0 2] ;
b=[0 0 1 7]; c=a-b
OUTPUT:
c=
-3 0 1 5
The resultant polynomial is -3s^3+s+5

3. POLYNOMIAL MULTIPLICATION:
The product of two polynomials is obtained by the convolution operation of their coefficients
using the MATLAB function conv.
Evaluate the product of polynomials (s+2) and (s^2+4s+8)
Solution:
a=[1 2];
b=[1 4 8];
c=conv(a,b)

OUTPUT:
c=
1 6 16 16
The resultant polynomial is s^3+6s^2+6s+16

4. CHARACTERISTIC POLYNOMIAL OF A MATRIX


Characteristic Polynomial of a Matrix can be obtained by using command poly.
Determine the characteristic equation of the matrix A= 0 1
2 3

Solution:
a=[0 1; 2 3];
p=poly(a,x)

OUTPUT:
p =x2 -3x -2
The resultant polynomial is s^2-3s-2

RESULT:Thus the polynomial commands are executed and output is verified successfully.
Ex No: 3 PROGRAM ON RELATIONAL OPERATIONS

Date:

AIM:
To execute a Scilab program by using Relational operators.

ALGORITHM:
Step 1: Start the program.
Step 2: Assign the values for X matrix in program.
Step 3: Compare X with a matrix using relational operator
Step 4: Display True and false values according to the elements in matrix.
Step 5: End the program.

PROGRAM:
X=5*ones(3,3);
X >=[1 2 3;4 5 6;7 8 9];

OUTPUT:
111
110
000

RESULT:Thus the program using relational operator is executed and output is verified
successfully.
Ex No: 4 PROGRAM ON BITWISE OPERATIONS

Date:

AIM:
To execute a scilab program to perform bitwise operations using a function and to create a
xor gate using and and or gates.

ALGORITHM:
Step1: Start the program.
Step 2: Create a function named as Xor.
Step 3: Get the values of x and y from the user in runtime.
Step 4:Execute the following line of codes:
a=bitand(x,y);
b=bitcmp(a,1);
c=bitor(x,y);
d=bitand(b,c);
Step 5: Display the value of d using display function.
Step 6: End the function using end function.
Step 7: End the program.

PROGRAM:
[x,y]=([0 0 1 1],[0 1 0 1])
a=bitand(x,y);
b=bitcmp(a,1);
c=bitor(x,y);
d=bitand(b,c);
disp("after xor gate ")
disp(d);

OUTPUT:
[x,y]=xor([ 0 0 1 1 ], [ 0 1 0 1 ] )
after xor gate
0. 1. 1. 0.
y =
0. 1. 0. 1.
x =
0. 0. 1. 1.

RESULT:Thus the program using bitwise operatorsis executed and output is verified
successfully.
Ex No: 5 PROGRAM ON LOGICAL OPERATIONS

Date:

AIM:
To execute a Scilab program by using Logical operators.

ALGORITHM:
Step 1: Start the program.
Step 2: Assign the values for a and b in program.
Step 3: Execute the following line of codes:
a&&b
a||b
(~(a&&b)
Step 4: Display the statements according to the logical operator using display function.
Step 5: End the program.

PROGRAM:
a=00;b=10;
if a&b
disp(Condition is true);
else
disp(Condition is false);
end
if a|b
disp(Condition is true);
end
if (~(a&b))
disp(Condition is true);
end

OUTPUT:
Condition is false
Condition is true
Condition is true

RESULT:Thus the program using logical operators is executed and verified successfully.
PROGRAM TO FIND LARGEST OF FIVE NUMBERS USING IF CONDITION

Ex.No:6

Date:

AIM:
To write a Scilab program to find the largest of five numbers using if condition.
ALGORITHM:
Step 1: Start
Step 2: Accept three numbers from the user
Step 3: Initialize a variable s with 0 which is taken as the smallest number for the time
being
Step 4: Run a loop of three iterations which will change the value of initialize variable to the
number greater than it each time if found.
Step 5: Display the largest number which is stored in the variable s
Step 6:Stop

PROGRAM:
s=0;
for i=1:5
a=input("input the numbers");
if(a>s)then
s=a;
end;
end;
disp(s);

OUTPUT:
Input the numbers: 5, 6, 2, 9, 1
Output:
9

RESULT:Thus the programto find largest of five numbers is executed and output isverified
successfully.
PROGRAM FOR FIBONACCI SERIES USING FOR LOOP

Ex.No:7

Date:

AIM:
To execute a program to display Fibonacci series by using for loop.

ALGORITHM:
Step 1: Start the program.
Step 2: Get the input from the user for limit of series.
Step 3: Initialize the value of a and b as 1.
Step 4: Repeat the following step until limit is reached:
s=a+b;
Step 5: Display the series.
Step 6: End the program

PROGRAM:
n=input("Enter the limit");
a=1;
b=1;
s=0;
disp(a);
disp(b);
fori=2:n
s=a+b;
disp(s);
a=b;
b=s;
s=0;
end;

OUTPUT:
Enter the Number 5
1
1
2
3
5

RESULT:Thus the program for Fibonacci series using for loop is executed and output is
verified successfully.
PROGRAM FOR MULTIPLICATION TABLE USING FOR LOOP

Ex.No:8

Date:

AIM:
To execute a program to display Multiplication Table using for loop.
ALGORITHM:
Step1: Start the program.
Step2: Get the input from the user to generateMultiplication table.
Step3: Get the limit from the user.
Step4: Repeat the following step until limit is reached:
f=f*i
Step5: Print the factorial of the number.
Step6: End the program.

PROGRAM:
n=input("Enter a number");
k=input("Enter Limit");
s=0;
fori=1:k
s=n*i;
disp(s);
i=i+1;
end;
OUTPUT:
Enter a number: 5
Enter Limit: 4
5
10
15
20
RESULT:Thusthe above multiplication program using for loop is executed and output is
verified successfully.
PROGRAM FOR FACTORIAL USING WHILE LOOP

Ex No: 9

Date:

AIM:
To execute a program to display factorial of a number using while loop.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the input from the user to find factorial for that number.
Step 3: Initiate value of f as 1.
Step 4: Repeat the following step until limit is reached:
f=f*i
Step 5: Print the factorial of the number.
Step 6: End the program.

PROGRAM:
n=input("input a number");
f=1;
while n>0
f=f*i;
end;
disp(f);

OUTPUT:
Factorial of which number: 5
120.

RESULT:Thus the factorial program using while loop is executed and output is verified
successfully.
PROGRAM FOR PRIME NUMBER USING FOR LOOP

Ex No: 10

Date:

AIM:
To execute a Scilab program to check whether a given number is prime number or not.

ALGORITHM:
Step 1: Start the program.
Step 2: Get the input from the user to check whether it is prime number or not.
Step 3: Initialize a variable k with 0
Step 4: Run a loop of three iterations which will change the value of initialize variable to the
number greater than it each time if found.
Step 5: Find modulo value using modulo operator
Step 6: Display statement according to the value of k
Step 7: End the program.

PROGRAM:
a=input("enter no");
k=0;
for i=1:a
if(modulo(a,i)==0)then
k=k+1;
end;
end;
if(k==2)then
disp('Prime number');
else
disp("not a Prime number");
end

OUTPUT:
Enter a number:
7
Prime number
9
Not a prime number

RESULT:
Thus the prime numberprogram using while loop is executed and verified successfully.
PROGRAM FOR PALINDROME OF A NUMBER USING WHILE LOOP

Ex.No:11

Date:

AIM:
To execute a program to check whether a given number is palindrome or not using while
loop.

ALGORITHM:
Step 1: Start the program.
Step 2: Get the input from the user to find palindrome or not.
Step 3: Initiate value of f as 0.
Step 4: Repeat the following step until condition satisfied
r=modulo(n,10);
n=n-r;
f=(f*10)+r;
n=n/10;
Step 5: Check whether k==f, if display the given number is palindrome.
Step 6: Else Display it is not a palindrome number.
Step 7: End the program.

PROGRAM:
n=input("enter a number");
k=n;
f=0;
while(n>0)
r=modulo(n,10);
n=n-r;
f=(f*10)+r;
n=n/10;
end;
if(k==f)then
disp("Palindrome");
else
disp("Not a palindrome");
end;

OUTPUT:
Enter a number
121
Palindrome
123
Not a palindrome

RESULT: Thus the Palindrome number program using while loop is executed and verified
successfully.

PROGRAM FOR STRING PALINDROME USING IF CONDITION


Ex.No : 12

Date:

AIM:
To execute a program to check whether a given String is palindrome or not using if
condition.

ALGORITHM:
Step 1: Start the program.
Step 2: Get the string input from the user and store it variable a.
Step 3: Reverse the string using strrev function and assign it into b
Step 4: Check a==b, if yes display the entered string is palindrome
Step 5: Else display it is not a string palindrome.
Step 6: End the program.

PROGRAM:
a=input(" enter a string","string");
b=strrev(a);
if(a==b)
disp("String is Palindrome");
else
disp("String is not a Palindrome");
end;

OUTPUT:
Enter a string
Madam
String is Palindrome
Enter a string
Chennai
String is not a palindrome

RESULT:Thus the String Palindrome program using if condition is executed and output is
verified successfully.
PROGRAM FOR SUM OF INTEGERS

Ex.No:13

Date:

AIM:
To find the number of integers that are greater than 50 and less than 100 and are divisible by
11, and to find the sum of these numbers.

ALGORITHM:
Step1: Start
Step2: Assign sum=0, c=0
Step3: for i=50, increasing i by 1 upto 100, repeats steps 5 to 7
Step4: if i is divisible by 11, goto step 6 and 7
Step5: Display i, and assign c=i
Step6: sum=sum+c
Step7: Display sum
Step8: Stop the program

PROGRAM:
sum=0;
c=0;
for i=50:1:100
if modulo(i,11)==0
disp(i);
sum=sum+i;
end;
end;
disp(sum);

OUTPUT:
55+66+77+88+99=385

RESULT: Thus the program for sum of integers which are greater than 50 and less than 100
and divisible by 11 is found and output verified successfully.
PROGRAM TO REVERSE A NUMBER

Ex.No: 14

Date:

AIM:

To write a Scilab program to reverse a given number.

ALGORITHM:
Step 1: Start
Step 2: Get input from the user in variable n
Step 3: while n>0
Step 4: d=mod(n,10)
Step 5: n=fix(n/10)
Step 6: r=r*10+d
Step 7: End while loop
Step 8: Display r
Step 9: Stop the program

PROGRAM:
r=0;
n=input(Enter the number);
while n>0
d=modulo(n,10);
n=fix(n/10);
r=r*10+d;
end;
disp(r);

OUTPUT:
Enter the number 23
32
Enter the number 456
654

RESULT:Thus the reverse of given number is obtained and output was verified successfully.
PROGRAM USING SELECT STATEMENT ON ARITHMETIC OPERATORS

Ex No 15:
Date:

AIM:
To write a Scilab program to perform arithmetic operations using select statement.
ALGORITHM:
Step 1: Start the program
Step 2: Get a input from user and assign it in a variable
Step 3:Get another input from user and assign it in b variable
Step 4:Get the choice from user and assign it in ch variable
Step 5: Perform the following operations using cases
1.Addition 2. Subtraction 3. Multiplication 4.Division
Step 6:Display the result of arithmetic operations according to the choice
Step 7:End the program

PROGRAM:
a=input('enter a number');
b=input('enter second number');
ch=input('Enter your choice');
select ch
case 1 then
c=a+b;
disp(c);
case 2 then
d=a-b;
disp(d);
case 3 then
e=a*b;
disp(e);
case 4 then
f=a/b;
disp(f);
else
disp('Invalid')
end;
OUTPUT:
Enter a number: 5
Enter second number: 6
Enter your choice 3
30

RESULT:
Thus the program to perform arithmetic operations using select statement is executed
and output is verified successfully.
PROGRAM USING SELECT STATEMENT ON DIFFERENT OPERATIONS

Ex.No.16

Date:

AIM: Write a program to operate different cases.


Case1- Check number is even or odd.
Case2-Check number is palindrome or not.
Case 3- Sum of digit.

ALGORITHM :
Step 1: Start the program.
Step 2: Enter the number to repeat the program
Step 3: Create while loop
Step 4: Enter the choice.
Step 5: Case1: Use if and modulo to check the number is even or odd. Display the result
Step 6: Case 2: Declare n=a and s=0
Take the remainder after dividing the number by 10 using modulo function and assign it
m
Sum=sum*10+m
Divide by 10
Run it till a>0
Check if n=s
Display the result
Step 7: Case 3:
Declare n=a s=0
Take the remainder dividing the number by 10 using modulo function and assign it m
Divide by 10; a=floor(a/10); s=s+m
display the sum; End the while loop ;End the program

PROGRAM:

k=input("How many times to be executed")


while (k>0)
disp("1. Even or Odd");
disp("2 Palindrome");
disp("3. Sum of digits");
ch=input("enter the choice");
a=input("enter the number");
select ch
case 1 then
if (modulo(a,2)==0) then
disp("given number is even");
else
disp("given number is odd")
end
case 2 then
n=a;
s=0;
while (a>0)
m=modulo(a,10);
s=(s*10)+m;
a=floor(a/10);
end
if (n==s) then
disp("the given number is palindrome")
else
disp("the given number is not palindrome")
end
case 3 then
n=a;
s=0;
while(a>0)
m=modulo(a,10);
a=floor(a/10);
s=s+m;
end
disp("sum of digits is :")
disp(s)
end
k=k-1;
end

OUTPUT :
How many times to be executed 3
1. Even or Odd
2 Palindrome
3. Sum of digits
Enter the choice 1
Enter the number 2
given number is even
1. Even or Odd
2 Palindrome
3. Sum of digits
enter the choice 2
enter the number 121
the given number is palindrome
1. Even or Odd
2 Palindrome
3. Sum of digits
enter the choice 3
enter the number 456
sum of digits is: 15.

RESULT:Thus the program to perform different operations using select statement is


executed and output is verified successfully.
CIVIL APPLICATION

Ex.No:17

Date:

AIM:To develop a program that finds out whether a tank is overflowing or not wrt the
shape of the tank, its dimensions and rate of flow.

ALGORITHM:
STEP 1: Assume tank of shape rectangular, cylindrical or any other shape. Assume its
dimensions also.
STEP 2: Calculate volume of the tank. For e.g. Assuming the tank is cylindrical, then *Vtank
=
h Where r radius of tank (m) Where h height of tank (m)
STEP 3: Calculate volume of liquid. V_liq = F x t Where F - rate of flow (m3 /min) Where
t
time taken (min)
STEP 4: Conditions If V_liq &gt; V_tank Tank is Overflow If V_liq &lt; V_tank Tank is not
Overflow

PROGRAM:

F=input('Enter the Value of Flow Rate:');

t=input('Enter the time to fill the Tank:');

r=input('Enter the Radius of the Tank:');

h=input('Enter the Height of the Tank:');

Vtank=%pi*r*r*h;

disp('Vtank:');

disp(Vtank);

Vliquid=F*t;

disp('Vliquid');

disp(Vliquid);

if Vliquid>Vtank then

disp('Tank is Overflow');

else

disp('Tank is not Overflow');


end;

OUTPUT:
Enter the Value of Flow Rate: 10
Enter the time to fill the Tank: 2
Enter the Radius of the Tank: 3
Enter the Height of the Tank: 4
Vtank:
113.09734
Vliquid
20.
Tank is not Overflow
PROGRAM USING FUNCTIONS

Ex.No:18

Date:

AIM :
To write a program using functions

PROGRAM:
To compute a given function : To computer the expression : x3+3x2y+xy

function[z]=calc(x,y)
z=x^3+3*x^2*y+x*y
endfunction

a=input(enter a);
b=input(enter b);
c=input(enter c);
d=input(enter d);
e=calc(a,b);
f=calc(c,d);
disp(e);
disp(f);

OUTPUT:
Entera:2
Enterb:3
Enterc:4
Enterd:5

50.

324.

PROGRAM: To find the factorial of a number


function[f]=fact(n)
f=1;
for i=2:n
f=f*I;
end;
endfunction;
disp(fact(4));
disp(fact(6));

OUTPUT:
24
720

RESULT: Thus the programs using functions was executed successfully.


PROGRAM FOR 2D GRAPHICS
Ex No 19:

Date:

AIM:
To plot a simple graph, printing labels and grid box in Scilab.

ALGORITHM:
Step 1: Start the Scilab Software
Step 2: Get the input values from user
Step 3: Perform operation using corresponding values
Step 4: Display the graph
Step 5: Stop the program

1.Plot the curve given by equation y=x^2 where x varies from 0 to 10


PROGRAM:
x=0:1:10;
y=x^2;
plot(x,y)

OUTPUT:
2.Plot the curve given by equation y=sin(x), as x varies from 0 to 2pi. Also label the x-
and y- axes and provide a suitable title to the plot.
PROGRAM:

x=[0:0.1:2*%pi]';
plot2d(sin(x));

OUTPUT:
3.

Plot the graph for the table with dashed dotted red lines, marker type diamond, marker edge
color green, marker size = 12 and label the graph along with grid and box. Also plot the bar
chart and pie diagram for the data.

Solutions:

Ans 1)

Code:

year = [ 2008,2009,2010,2011,2012 ]

population = [ 65,72,74,76,84 ]

plot (year,population,'rd-','markeredgecolor','g','markersize',12);

title('2D PLOTS and PRINTING LABELS');


4. Write a Scilab program to get 5 subjects for 5 members of student and calculate the
average and draw the bar chart and pie chart for individual students performance.

Solution:

Code:

S1 = [30 35 48 36 40]; S1 = [30 35 48 36 40];

S2 = [32 33 25 12 27]; S2 = [32 33 25 12 27];

S3 = [4 0 16 5 1];

S3 = [4 0 16 5 1]; S3 = [4 0 16 5 1];

S4 = [47 49 48 45 50]; S4 = [47 49 48 45 50];

S5=[27 18 39 30]; S5=[27 18 39 30];

a = sum(S1)/5; a = sum(S1)/5;

b=sum(S2) /5; b=sum(S2) /5;

c = sum(S3)/5; c = sum(S3)/5;

d = sum(S4)/5; d = sum(S4)/5;

e = sum(S5)/5; e = sum(S5)/5;

S = [a b c d e]; S = [a b c d e];
45
bar (S,'y'); pie (S);

46
5. Write a SciLab program using subplots
AIM
To write the SciLab program to use subplot
PROGRAM
f=input(enter freq);
a=input(enter amplitude);
t=0:0.01:2;
y=a*sin(2*%pi*f*t);
y1=a*cos(2*%pi*f*t);
y2=t;

subplot (3,1,1); plot (t,y);


subplot (3,1,2); plot (t,y1);
subplot (3,1,3); plot (t,y2);
OUTPUT:
Enter freq=5
Enter amplitude=2

47
RESULT: Thus the graph is plotted with label, grid box using 2D Graphical commands is
executed and output is verified successfully.

48
3D GRAPHICS

Ex.No :20

Date:

AIM:
To write a Scilab program using 3D Graphical Commands .

ALGORITHM:
Step 1: Start the Scilab Software
Step 2: Get the input values from user
Step 3: Perform operation using corresponding values
Step 4: Display the graph
Step 5: Stop the program

PROGRAM:
t=[0:0.3:2*%pi]';
z=sin(t)*cos(t');
plot3d(t,t,z);

OUTPUT:

RESULT: Thus the 3D graphical commands are plotted output is verified successfully.

49
Ex: No: 21

Date:

PROGRAM ON GUI (CALENDAR)

Front End Design:

Codings:

// This GUI file is generated by guibuilder version 3.0


//////////
f=figure('figure_position',[400,50],'figure_size',[648,577],'auto_resize','on','background',
[33],'figure_name','Graphic window number %d');
//////////
delmenu(f.figure_id,gettext('File'))
delmenu(f.figure_id,gettext('?'))
delmenu(f.figure_id,gettext('Tools'))
toolbar(f.figure_id,'off')
handles.e1=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-
1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',
[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-
1],'HorizontalAlignment','left','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.1,0.7979167,0.29375,0.1041667],'Relief','default','SliderStep',[0.01,0.1],'String','enter
year','Style','text','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','e1','Callback','')
handles.e2=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-
1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',
[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-
1],'HorizontalAlignment','left','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.096875,0.6020833,0.2953125,0.0979167],'Relief','default','SliderStep',
[0.01,0.1],'String','enter month','Style','text','Value',
[0],'VerticalAlignment','middle','Visible','on','Tag','e2','Callback','')

50
handles.e3=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-
1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',
[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-
1],'HorizontalAlignment','left','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.1046875,0.4041667,0.2890625,0.0958333],'Relief','default','SliderStep',
[0.01,0.1],'String','enter date','Style','text','Value',
[0],'VerticalAlignment','middle','Visible','on','Tag','e3','Callback','')
handles.e4=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-
1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',
[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-
1],'HorizontalAlignment','left','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.1,0.2,0.2953125,0.0979167],'Relief','default','SliderStep',[0.01,0.1],'String','see the
day','Style','text','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','e4','Callback','')
handles.ed1=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-
1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',
[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-
1],'HorizontalAlignment','left','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.6015625,0.8020833,0.2921875,0.0958333],'Relief','default','SliderStep',
[0.01,0.1],'String','y','Style','edit','Value',
[0],'VerticalAlignment','middle','Visible','on','Tag','ed1','Callback','')
handles.ed3=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-
1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',
[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-
1],'HorizontalAlignment','left','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.6046875,0.4,0.2875,0.0958333],'Relief','default','SliderStep',
[0.01,0.1],'String','d','Style','edit','Value',
[0],'VerticalAlignment','middle','Visible','on','Tag','ed3','Callback','')
//////////
// Callbacks are defined as below. Please do not delete the comments as it will be used in
coming version
//////////

functioned4_callback(handles)
y=eval(get(handles.ed1,'string'));
m=eval(get(handles.ed2,'string'));
d=eval(get(handles.ed3,'string'));

num=datenum(y,m,d);
[n,s]=weekday(num);
disp('u were born'+s)
set(handles.ed4,'string',(s));

endfunction

51
OUTPUT:

Result:

Thus the Program demonstrating calendar is executed successfully in Scilab.

52
Ex.No: 20 Program on GUI (Simple Calulator)

Date:

Front End Design:

Codings:

// This GUI file is generated by guibuilder version 3.0


//////////
f=figure('figure_position',[400,50],'figure_size',[648,577],'auto_resize','on','background',
[33],'figure_name','Graphic window number %d');
//////////
delmenu(f.figure_id,gettext('File'))
delmenu(f.figure_id,gettext('?'))
delmenu(f.figure_id,gettext('Tools'))
toolbar(f.figure_id,'off')
handles.dummy=0;
handles.e1=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-
1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',
[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-
1],'HorizontalAlignment','left','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.1,0.8020833,0.2375,0.0979167],'Relief','default','SliderStep',[0.01,0.1],'String','enter x
value','Style','text','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','e1','Callback','')
handles.e2=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-
1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',
[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-
1],'HorizontalAlignment','left','ListboxTop',[],'Max',[1],'Min',[0],'Position',

53
[0.103125,0.6,0.2359375,0.1041667],'Relief','default','SliderStep',[0.01,0.1],'String','enter y
value','Style','text','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','e2','Callback','')
handles.e3=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-
1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',
[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-
1],'HorizontalAlignment','left','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.103125,0.4020833,0.2328125,0.0958333],'Relief','default','SliderStep',
[0.01,0.1],'String','result','Style','text','Value',
[0],'VerticalAlignment','middle','Visible','on','Tag','e3','Callback','')
handles.ed1=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-
1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',
[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-
1],'HorizontalAlignment','left','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.4984375,0.8041667,0.3,0.0979167],'Relief','default','SliderStep',
[0.01,0.1],'String','x','Style','edit','Value',
[0],'VerticalAlignment','middle','Visible','on','Tag','ed1','Callback','')
handles.ed2=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-
1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',
[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-
1],'HorizontalAlignment','left','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.5,0.5979167,0.2984375,0.1],'Relief','default','SliderStep',
[0.01,0.1],'String','y','Style','edit','Value',
[0],'VerticalAlignment','middle','Visible','on','Tag','ed2','Callback','')
handles.ed3=uicontrol(f,'unit','normalized','BackgroundColor',[-1,-1,-
1],'Enable','on','FontAngle','normal','FontName','Tahoma','FontSize',
[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[-1,-1,-
1],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.5015625,0.4,0.29375,0.1],'Relief','default','SliderStep',
[0.01,0.1],'String','z','Style','pushbutton','Value',
[0],'VerticalAlignment','middle','Visible','on','Tag','ed3','Callback','ed3_callback(handles)')
//////////
// Callbacks are defined as below. Please do not delete the comments as it will be used in
coming version
//////////
functioned3_callback(handles)
x=eval(get(handles.ed1,'string'));
y=eval(get(handles.ed2,'string'));
z=x+y;
disp(z);
set(handles.ed3,'string',string(z));

endfunction

54
OUTPUT:

Result :

Thus the program on simple calculator is written and executed successfully in scilab.

PROJECT WORK
55

Você também pode gostar