SCI Lab Based OTDM Practical File
SCI Lab Based OTDM Practical File
ASSIGNMENT USING
SCILAB
SEMESTER- 4th
B.TECH ECE-AI-1
1
1.3 The Menu Bar
The following options in menu are particularly useful:
Applications
• The command history allows you to find all the commands from previous
sessions to the current session.
• The variables browser allows you to find all variables previously used during
the current session.
Editing
• Preferences (in Scilab menu under Mac OS X) allow you to set and customize
colors, fonts and font size in the console and in the editor, which is very useful
for screen projection.
• Clicking on Clear Console clears the entire content of the console. In this case,
the command history is still available and calculations made during the session
remain in memory. Commands that have been erased are still available through
the keyboards arrow keys.
Also apart from usual operators for summation, subtraction, multiplication and
division, comparison operators are as given:
2
1.5 Booleans
Boolean variables can store true or false values. In Scilab, true is written with %t
or %T and false is written with %f or %F.
1.7 Strings
Strings can be stored in variables, provided that they are delimited by double
quotes. The concatenation operation is available from the + operator. They are many
functions which allow to process strings, including regular expressions.
1.8 Matrices
There is a simple and efficient syntax to create a matrix with given values.
The following is the list of symbols used to define a matrix:
• square brackets ”[” and ”]” mark the beginning and the end of the matrix
3
Use of colon operator in matrices
Following table depicts use of colon operator in matrices:
4
disp (” Goodbye !” ) end
The previous script produces:
Goodbye !
In order to get a boolean, any comparison operator can be used, e.g. ==, >, etc or any
function which returns a boolean. In the following session, we use the == operator to
display the message ”Hello !”.
i = 2 if ( i
== 2
) then
disp (”
Hello ! ”) else disp (”
Goodbye !” ) end
The select statement
The select statement allows to combine several branches in a clear and simple way.
Depending on the value of a variable, it allows to perform the statement
corresponding to the case keyword. There can be as many branches as required. In the
following script, we want to display a string which corresponds to the given integer i.
i=2
select i case 1
disp (” One ”)
case 2 disp (”
Two ”) case 3
disp (” Three ”)
else
5
statement is in fact much more general, as it allows to loop through the values of a
matrix. In the following Scilab script, we display the value of i, from 1 to 3.
for i = 1 : 3
disp (i) end
The previous script produces the following output.
1.
2.
3.
In the previous example, the loop is performed over a matrix of floating point
numbers containing integer values. Indeed, we used the colon ”:” operator in order to
produce the vector of index values [1, 2, 3]. The following session shows that the
statement 1:3 produces all the required integer values into a row vector.
—— > i = 1 : 3 i
= 1.2.3.
The while statement
The while statement allows to perform a loop while a boolean expression is true. At
the beginning of the loop, if the expression is true, the statements in the body of the
loop are executed. When the expression becomes false (an event which must occur at
certain time), the loop is ended. In the following script, we compute the sum of the
numbers i from 1 to 10 with a while statement.
s=0i
=1
while ( i <= 10 )
s=s+ii
=i
+ 1 end
At the end of the algorithm, the values of the variables i and s are: s =
55 and i = 11.
The break and continue statement
The break statement allows to interrupt a loop. Usually, we use this statement in loops
where, if some condition is satisfied, the loops should not be continued further. In the
6
following example, we use the break statement in order to compute the sum of the
integers from 1 to 10. When the variable i is greater than 10, the loop is interrupted.
s=0i
=1
while ( %t ) if ( i
> 10 ) then break
end
s=s+ii
=i
+ 1 end
At the end of the algorithm, the values of the variables i and s are:
s = 55 and i = 11
The continue statement allows to go on to the next loop, so that the statements in the
body of the loop are not executed this time. When the continue statement is executed,
Scilab skips the other statements and goes directly to the while or for statement and
evaluates the next loop. In the following example, we compute the sum s = 1 + 3 + 5
+ 7 + 9 = 25. The modulo(i, 2) function returns 0 if the
number i is even. In this situation, the script increments the value of i and use the
continue statement to go on to the next loop.
s=0i
=1
while ( i <= 10) if ( modulo (i,
2) == 0 ) then
i=i+
1
continue
else
s=s+ii
=i
7
Chapter 2 Lab Objective and Requirement
C154.2: use conditional expressions and looping statement to solve problems associated
with conditions and repetitions.
8
Chapter 3 Matrix Operations
1. Start
6. Stop
Flow Chart for Matrix Addition
9
Scilab Code for Matrix Addition
clc
m=input(”enter number of rows in the Matrices: ”);
n=input(”enter number of columns in the Matrices: ”);
disp(’enter the first Matrix’) for i=1:m for j=1:n
A(i,j)=input(’\’); end end
disp(’enter the second Matrix’)
for i=1:m for j=1:n
B(i,j)=input(’\’); end end for i=1:m for
j=1:n C(i,j)=A(i,j)+B(i,j); end end
disp(’The first matrix is’) disp(A)
disp(’The Second matrix is’) disp(B)
disp(’The sum of the two matrices is’)
disp(C)
10
3.2 Matrix Multiplication
• The number of columns in the first matrix must be equal to the number of rows in
the second matrix
1. Start
4. Check the number of rows and column of first and second matrices
5. If number of rows of first matrix is equal to the number of columns of second matrix, go
to step 6. Otherwise, print ’Matrices are not conformable for multiplication’ and go to
step 3
8. Stop
Flow Chart for Matrix Multiplication
11
m=input(”Enter number of rows in the first Matrix: ”);
n=input(”Enter number of columns in the first Matrix: ”);
p=input(”Enter number of rows in the second Matrix: ”);
q=input(”Enter number of columns in the second Matrix: ”); if
n==p disp(’Matrices are conformable for multiplication’) else
disp(’Matrices are not conformable for multiplication’) break;
end disp(’enter the first Matrix’) for i=1:m for j=1:n
A(i,j)=input(’\’); end end
disp(’enter the second Matrix’)
for i=1:p for j=1:q
B(i,j)=input(’\’);
end end
C=zeros(m,q); for
i=1:m for j=1:q
for k=1:n
C(i,j)=C(i,j)+A(i,k)*B(k,j);
end end
end disp(’The first matrix is’) disp(A)
disp(’The Second matrix is’) disp(B)
disp(’The product of the two matrices is’)
disp(C)
12
3.3 Matrix Transpose
T
• The transpose of an m × n matrix A is n × m matrix A
1. Start
6. Stop
Flow Chart for Matrix Transpose
13
Scilab Code for Matrix Transpose
clc
m=input(”Enter number of rows in the Matrix: ”);
n=input(”Enter number of columns in the Matrix: ”);
disp(’Enter the Matrix’) for i=1:m for j=1:n
A(i,j)=input(’\’); end end
B=zeros(n,m); for i=1:n for
j=1:m B(i,j)=A(j,i) end end
disp(’Entered matrix is’)
disp(A) disp(’Transposed
matrix is’) disp(B)
Matrix Transpose using functions
// Save file as transpose.sce function
[ ]=transpose(m, n, A)
B=zeros(m,n);
B=A’
disp(’The matrix is’) disp (A)
disp(’Transposed matrix is’)
disp (B) endfunction
Algorithm:
1. Start
3. Check Det(A). If Det(A) = 0, Print ’Inverse does not exist’ else go to step 4.
14
5. Make element B(1, 1) = 1 and using this make B(2, 1) = 0 and B(3, 1) = 0 using
row/column transformations.
6. Make element B(2, 2) = 1 and using this make B(1, 2) = 0 and B(3, 2) = 0.
7. Make element B(3, 3) = 1 and using this make B(1, 3) = 0 and B(2, 3) = 0.
8. Thus final augmented matrix is in the form B[I, A−1]. Print A−1
9. Stop
Flow Chart for Matrix Inverse
Scilab Code for Matrix Inverse using Gauss Jordan Method clc
disp(’Enter a 3 by 3 matrix row-wise, make sure that diagonal elements are non
-zeros’)
for i=1:3
for j=1:3
A(i,j)=input(’\’); end end disp(’Entered Matrix
is’) disp(A) if det(A)==0 disp(’Matrix is singular,
Inverse does not exist’) break; end
//Taking the augmented matrix
B=[A eye(3,3)]
disp(’Augumented matrix is:’)
disp(B)
// Making B(1,1)=1
B(1,:) = B(1,:)/B(1,1);
//Making B(2,1) and
15
B(3,1)=0 B(2,:) = B(2,:) -
B(2,1)*B(1,:);
B(3,:) = B(3,:) - B(3,1)*B(1,:);
//Making B(2,2)=1 and B(1,2),
B(3,2)=0 B(2,:) = B(2,:)/B(2,2);
B(1,:) = B(1,:) - B(1,2)*B(2,:);
B(3,:) = B(3,:) - B(3,2)*B(2,:);
//Making B(3,3)=1 and B(1,3),
B(2,3)=0 B(3,:) = B(3,:)/B(3,3);
B(1,:) = B(1,:) - B(1,3)*B(3,:);
B(2,:) = B(2,:) - B(2,3)*B(3,:); disp(’Augumented
matrix after row operations is:’) disp(B) B(:,1:3)=[ ]
disp(’Inverse of the Matrix is’) disp(B)
function [ ]=inverse(m, A)
C=zeros(m,m);
B=det(A) if
B==0
disp(’Matrix is singular, Inverse does not exist’)
break; end C=inv(A) disp(’The matrix is’) disp
(A) disp(’Inverse of given matrix is:’) disp (C)
endfunction
3. Find Trace(A) and Det(A) as the 2 eigen values λ1, λ2 are roots of the char-
-acteristic equation λ2 — trace(A) + det(A) = 0
4. Find λ1 and λ2
0
X2 =
1
6. Stop
Flow Chart for Eigen Values and Vectors
17
disp(’Enter the 2 by 2 Matrix row-wise’)
for i=1:2 for j=1:2
A(i,j)=input(’\’); end end
b=A(1,1)+A(2,2);
c=A(1,1)*A(2,2)-A(1,2)*A(2,1);
// characteristic equation is λ2 — trace(A) + det(A) = 0, here λ1 ≡ e1, λ2 ≡ e2 e1
= (b + sqrt(b∧2 — 4 ∗ c))/2; e2 =
(b — sqrt(b∧2 — 4 ∗ c))/2; if
A(1, 2)=˜ 0
X1 = [A(1,2); e1-A(1,1)]; X2
= [A(1,2); e2-A(1,1)]; elseif
A(2, 1)=˜ 0
X1 = [e1-A(2,2); A(2,1)];
X2 = [e2-A(2,2); A(2,1)];
else
X1 = [1; 0];
X2 = [0; 1];
end disp(’First Eigen value is:’);
disp(e1) disp(’First Eigen vector
is:’); disp (X1) disp(’Second
Eigen value is:’); disp(e2)
disp(’Second Eigen vector is:’);
disp (X2)
1. Start
//Scilab Code to find mean, mode, median, moments, skewness and kurtosis of
linear data clc
19
function [ ]= moments(A) B=gsort(A); n
= length(B); meanA = sum(B)/n; if
pmodulo(n,2)==0 medianA
=((B(n/2)+B(n/2 +1)))/2; else medianA
= B((n+1)/2); end
C = diff(B)
//C= diff(B) calculates differences between adjacent elements of B along the first array
dimension whose size exceeds 1:
//If B is a vector of length n, then C = diff(B) returns a vector of length n-1.
The elements of C are the differences between adjacent elements of B.
//C = [B(2)-B(1) B(3)-B(2)........B(m)-B(m-1)]
D = find(C)
//D = find(C) finds the idices(positions), where value is non zero E =
diff(D)
[m k] = max(E) // maximum ’m’ at kth position modeA =
B(D(k)+1)
printf(’Mean of the given data is : %f \n \n’, meanA);
printf(’Median of the given data is : %f \n \n’, medianA);
printf(’Mode of the given data is : %f \n \n’, modeA);
printf(’First moment about the mean(M1)= %f \n \n’, 0); for
i=1:n X(i)=A(i)-meanA; end
M2 = sum(X.*X)/n;
M3 = sum(X.*X.*X)/n;
M4 = sum(X.*X.*X.*X)/n; printf(’Second moment about the
mean(M2)= %f \n \n’, M2); printf(’Third moment about the
mean(M3)= %f \n \n’, M3); printf(’Fourth moment about the
mean(M4)= %f \n \n’, M4); sd= sqrt (M2);
printf(’Standard deviation: %f \n \n’, sd); Csk=
(meanA - modeA)/sd;
printf(’Coefficient of skewness: %f \n \n’, Csk); Sk=
(M 3)∧2/(M 2)∧3;
printf(’Skewness: %f \n \n’, Sk);
Kur= M 4/(M 2)∧2;
printf(’Kurtosis: %f \n \n’, Kur); endfunction
20
4.2 Measures of Central Tendency (Grouped Data)
Algorithm to find mean, median, mode and moments of grouped data
1. Start
7. Find mean = Σ
fi
10. Evaluate S.
12. Stop
Flow Chart for Central Tendencies(Grouped Data)
21
Scilab code for Mean and Moments of Grouped Data clc
n=input(’Enter the no. of observations:’); disp(’Enter the values of
xi’); for i=1:n
x(i)=input(’\’); end; disp(’Enter the
corresponding frequencies fi:’) sum=0; for
i=1:n
f(i)=input(’\’); sum=sum+f(i); end; r=input(’How
many moments to be calculated:’); sum1=0 for
i=1:n sum1=sum1+f(i)*x(i); end
A=sum1/sum; //Calculate the average
printf(’Average=%f \n’,A); for j=1:r
sum2=0; for i=1:n y(i)=f (i) ∗ (x(i) —
A)∧ j; sum2=sum2+y(i); end
M(j)=(sum2/sum); //Calculate the moments
printf(’Moment about mean M(%d)=%f \n’,j,M(j));
end sd=sqrt(M(2)); //Calculate the standard deviation
printf(’Standard deviation=%f \n’,sd);
Chapter 5
22
//w White
//Specifier Marker Type
23
5.3 Unit Step function I
clc
x = [-1 -2 -3 -4 -5 0 1 2 3 4 5 ]; y =
[0 0 0 0 0 1 1 1 1 1 1 ];
plot(x,y, ’ro’) xlabel(’x’);
ylabel(’y’); title(’Unit Step
Function’);
24
Chapter 6
Then to find the next approximation to the root, one of the three conditions arises:
1. f (x1) = 0, then we have a root at x1.
2. f (x1) < 0,then since f (x1 )f (a) < 0,the root lies between x1 and a.
3. f (x1) > 0, then since f (x 1 )f (b) < 0,the root lies between x1 and b. We can further divide
this sub-interval into two halves to get a new sub-interval which contains the root. We can
continue the process till we get desired accuracy.
1. Start
3. Input a and b where a, b are end points of interval (a, b) in which the root lies.
5. If f (a) and f (b) have same signs, then display function must have different signs at a
and b, exit. Otherwise go to next step.
6. Input e and set iteration number counter to zero. Here e is the absolute error i.e. the
desired degree of accuracy.
25
7. root = (a + b)/2
26
b = root end
iter=iter+1 end printf(’\n \n The solution of given equation is %f after %i
Iterations’,root,iter —1)
f‘(xn)
4. Use Newtons iteration formula to get new better approximate of the root, say x2. Repeat
the process for x3, x4 .....till the actual root of the function is obtained, fulfilling the
tolerance of error.
clc
deff(’y = f (x)’,’y = x3 + x2 — 3 ∗ x — 3’)
deff(’y = df(x)’,’y = 3 ∗ x2 + 2 ∗ x — 3’)
x(1)=input(’Enter Initial Guess:’); e= input(”
answer correct upto : ”); for i = 1 : 100
27
x(i + 1) = x(i) — f (x(i))/df (x(i)); err(i)
= abs((x(i + 1) — x(i))/x(i)); if err(i) <
e
28
Chapter 7
29
// Scilab code for Trapezoidal Rule
clc
deff(’y = f (x)',' y = x/(x 2 + 5)’); a=input(”Enter
Lower Limit: ”) b=input(”Enter Upper Limit:
”) n=input(”Enter number of sum intervals: ”)
h = (b — a)/n
sum1 = 0 sum2
= 0 for i = 0 : n
x=a+i∗hy
= f (x)
disp([xy])
if (i == 0)|(i == n) sum1 =
sum1 + y
else
sum2 = sum2 + y
end
end
30
val = (h/2) ∗ (sum1 + 2 ∗ sum2)
disp(val,”Value of integral by Trapezoidal Rule is:”)
1. Start
3. Input a, b and n, where a, b are lower and upper limits of integral and n is number of
intervals in which area is to be divided. Note that nmust be even.
31
// Scilab Code for Simpsons 1/3rd Rule
clc
deff(’y = f (x)',' y = x/(x 2 + 5)’); a=input(”Enter
Lower Limit: ”) b=input(”Enter Upper Limit:
”) n=input(”Enter number of sum intervals: ”)
h = (b — a)/n x(1) = a;
sum = f (a); for i = 2 : n
x(i) = x(i — 1) + h;
end
for j = 2:2:n
sum = sum + 4 ∗ f (x(j)); end for k
= 3 : 2 : n sum = sum + 2 ∗ f
(x(k)); end sum = sum + f (b); val =
sum ∗ h/3;
disp(val,”Value of integral by Simpsons 1/3rd Rule is:”)
32
∫b f
a
f (xn)]
1. Start
2. Define and Declare function y = f(x) whose integral is to be computed.
3. Input a, b and n, where a, b are lower and upper limits of integral and n is number of
intervals in which area is to be divided. Note that nmust be a multiple of 3.
33
Scilab Code for Simpson’s 3/8th Rule
clc
deff(’y = f (x)',' y = x/(x 2 + 5)’); a=input(”Enter
Lower Limit: ”) b=input(”Enter Upper Limit:
”) n=input(”Enter number of sum intervals: ”)
h = (b — a)/n x(1) =
a; sum = f (a); for i =
2 : n x(i) = x(i — 1) +
h;
end
for j = 2 : 3 : n
sum = sum + 3 ∗ f (x(j)); end
for k = 3 : 3 : n sum = sum +
3 ∗ f (x(k)); end for l = 4 : 3 :
n sum = sum + 2 ∗ f (x(l));
end
sum = sum + f (b); val =
sum ∗ 3 ∗ h/8;
disp(val,”Value of integral by Simpson’s 3/8th Rule is:”)
34
Chapter 8
Method is to use the concept of local linearity to join multiple small line segments
so that they make up an approximation of the actual curve, as shown below.
The upper curve shows the actual graph of a function. The curve 0˜AA5 give
approximations using Euler’s method. Generally, the approximation gets less accurate
the further we go away from the initial value. Better accuracy is achieved when the
points in the approximation are chosen in small steps . Each next approximation is
estimated from previous by using the formula yn+1 = yn + hf (xn, yn)
1. Start
3. Input x0, y0 , h and xn for the given initial value condition y(x0) = y0. h is step size and xn is
final value of x.
35
5. Print values of x 0 to xn and y 0 to yn
7. Stop
Scilab Code for Solving Initial Value Problem using Euler’s Method // Solution of
Initial value problem dy = 2 — 2y — e−4x, y(0) = 1, h = .1, xn = 1
dx
// If f is a Scilab function, its calling sequence must be ydot = f (x, y) // ydot
is used for first order derivative dy
dx
// ode is scilab inbuilt function to evaluate the D.E. in the given format clc
function ydot = euler(x, y)
ydot= 2 — 2 ∗ y — %e∧(—4 ∗
x) endfunction x0=input(”Enter initial
value x0: ”) y0=input(”Enter initial
value y0: ”) h=input(”Enter step size h:
”) xn=input(”Enter final value xn: ”) x
= x0 : h : xn;
y=ode(y0, x0, x, euler)
disp(x,” x value:”)
disp(y,” y value:”) plot
(x, y)
36
Chapter 9
The problem of finding a function y of x when we know its derivative and its value y0 at
a particular point x0 is called an initial value problem.
1. Start
7. Stop
38