Basic Simulation Lab Manual
Basic Simulation Lab Manual
LAB MANUAL
Basic Simulation Lab
Course Code: ECE 204
1
Lab/ Practicals details
(B). Compute the Running Sum (Check with sum), where Running Sum
for element j = the sum of the elements from 1 to j, inclusive.
4 Evaluating a given expression and rounding it to the nearest integer value PRACTICAL
using Round, Floor, Ceil and Fix functions; Also, generating and Plots of
(A) Trigonometric Functions - sin(t),cos(t), tan(t), sec(t), cosec(t) and
cot(t) for a given duration, ‘t’. (B) Logarithmic and other Functions –
log(A), log10(A), Square root of A, Real nth root of A.
5 Creating a vector X with elements, Xn = (-1) n+1/(2n-1) and Adding up 100 PRACTICAL
elements of the vector, X; And, plotting the functions, x, x 3, ex, exp(x2)
over the interval 0 < x < 4 (by choosing appropriate mesh values for x to
obtain smooth curves), on A Rectangular Plot
2
6 Generating a Sinusoidal Signal of a given frequency (say, 100Hz) and PRACTICAL
Plotting with Graphical Enhancements - Titling, Labeling, Adding Text,
Adding Legends, Adding New Plots to Existing Plot, Printing Text in
Greek Letters, Plotting as Multiple and Subplot.
8 Writing brief Scripts starting each Script with a request for input (using PRACTICAL
input) to Evaluate the function h(T) using if-else statement, where
9 Generating a Square Wave from sum of Sine Waves of certain Amplitude PRACTICAL
and Frequencies.
10 Basic 2D and 3D plots: parametric space curve, polygons with vertices, PRACTICAL
3D contour lines and pie and bar charts.
Assessment/Examination Scheme:
(40%) (60%)
Weightage (%) 15 10 10 5 30 30
3
EXPERIMENT NO 1
THEORY:
4
Window. You can run a script file named finish.m each time
MATLAB quits.
PROCEDURE:
5
To plot the graph of a function, you need to take the following
steps:
SUGGESTED PROGRAM:
1)
A=[1 2 3 4];
display(A);
B=[5 6 7 8];
display(B);
ADD=A+B;
display(ADD);
SUBTRACT=A-B;
display(SUBTRACT);
MULTIPLY=2*A;
display(MULTIPLY);
DIVISION=B/2;
display(DIVISION);
EXP=exp(A);
display(EXP);
OUTPUT
A=
1 2 3 4
B=
6
5 6 7 8
ADD =
6 8 10 12
SUBTRACT =
-4 -4 -4 -4
MULTIPLY =
2 4 6 8
DIVISION =
EXP =
b)
X=[1 20 3; 5 6 7; 8 9 10];
display(X);
RANK=rank(X);
display(RANK);
INVERSE=inv(X);
display(INVERSE);
TRANSPOSE=X';
display(TRANSPOSE);
OUTPUT:
X=
1 20 3
7
5 6 7
8 9 10
RANK =
INVERSE =
TRANSPOSE =
1 5 8
20 6 9
3 7 10
8
EXPERIMENT NO 2
THEORY:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operations
Set Operations
Arithmetic Operators
PROCEDURE:
Operat
Description
or
Addition or unary plus. A+B adds A and B. A and B
+ must have the same size, unless one is a scalar. A
scalar can be added to a matrix of any size.
Subtraction or unary minus. A-B subtracts B from A.
A and B must have the same size, unless one is a
-
scalar. A scalar can be subtracted from a matrix of
any size.
Matrix multiplication. C = A*B is the linear algebraic
product of the matrices A and B. More precisely,
*
For nonscalar A and B, the number of columns of A
must equal the number of rows of B. A scalar can
multiply a matrix of any size.
10
matrix, A\B is roughly the same as inv(A)*B, except
it is computed in a different way. If A is an n-by-n
matrix and B is a column vector with n components,
or a matrix with several such columns, then X = A\B
is the solution to the equation AX = B. A warning
message is displayed if A is badly scaled or nearly
singular.
Array left division. A.\B is the matrix with elements
.\ B(i,j)/A(i,j). A and B must have the same size, unless
one of them is a scalar.
Matrix power. X^p is X to the power p, if p is a
scalar. If p is an integer, the power is computed by
repeated squaring. If the integer is negative, X is
^
inverted first. For other values of p, the calculation
involves eigenvalues and eigenvectors, such that if
[V,D] = eig(X), then X^p = V*D.^p/V.
Array power. A.^B is the matrix with elements A(i,j)
.^ to the B(i,j) power. A and B must have the same
size, unless one of them is a scalar.
Matrix transpose. A' is the linear algebraic
' transpose of A. For complex matrices, this is the
complex conjugate transpose.
Array transpose. A.' is the array transpose of A. For
.' complex matrices, this does not involve
conjugation.
Relational Operators
Relational operators can also work on both scalar and non-scalar
data. Relational operators for arrays perform element-by-element
comparisons between two arrays and return a logical array of the
same size, with elements set to logical 1 (true) where the relation
is true and elements set to logical 0 (false) where it is not.
Operat
Description
or
11
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to
Logical Operators
MATLAB offers two types of logical operators and functions:
Bitwise Operations
Bitwise operator works on bits and performs bit-by-bit operation. The truth tables for &, |, and ^ are as
follows:
12
Assume if A = 60; and B = 13; Now in binary format they will be
as follows:
A = 0011 1100
B = 0000 1101
-----------------
~A = 1100 0011
Function Purpose
bitand(a, b) Bit-wise AND of integers a and b
bitcmp(a) Bit-wise complement of a
bitget(a,pos Get bit at specified position pos, in the integer
) array a
bitor(a, b) Bit-wise OR of integers a and b
bitset(a,
Set bit at specific location pos of a
pos)
Returns a shifted to the left by k bits, equivalent
to multiplying by 2k. Negative values of k
bitshift(a, k) correspond to shifting bits right or dividing by 2 |
k|
and rounding to the nearest integer towards
negative infinite. Any overflow bits are truncated.
bitxor(a, b) Bit-wise XOR of integers a and b
swapbytes Swap byte ordering
13
Set Operations
MATLAB provides various functions for set operations, like union,
intersection and testing for set membership, etc.
Function Description
Set intersection of two arrays; returns the
intersect(A,B) values common to both A and B. The values
returned are in sorted order.
Treats each row of A and each row of B as
intersect(A,B,'ro single entities and returns the rows common to
ws') both A and B. The rows of the returned matrix
are in sorted order.
Returns an array the same size as A, containing
ismember(A,B) 1 (true) where the elements of A are found in B.
Elsewhere, it returns 0 (false).
Treats each row of A and each row of B as
ismember(A,B,'r single entities and returns a vector containing 1
ows') (true) where the rows of matrix A are also rows
of B. Elsewhere, it returns 0 (false).
Returns logical 1 (true) if the elements of A are
in sorted order and logical 0 (false) otherwise.
issorted(A) Input A can be a vector or an N-by-1 or 1-by-N
cell array of strings. A is considered to be
sorted if A and the output of sort(A) are equal.
Returns logical 1 (true) if the rows of two-
dimensional matrix A are in sorted order, and
issorted(A,
logical 0 (false) otherwise. Matrix A is
'rows')
considered to be sorted if A and the output of
sortrows(A) are equal.
Set difference of two arrays; returns the values
setdiff(A,B) in A that are not in B. The values in the
returned array are in sorted order.
setdiff(A,B,'rows' Treats each row of A and each row of B as
) single entities and returns the rows from A that
14
are not in B. The rows of the returned matrix
are in sorted order.
The 'rows' option does not support cell arrays.
SUGGESTED PROGRAM:
A=ones(2,3)*6;
display(A)
B=ones(3,3);
display(B)
C=[A;B];
display(C);
OUTPUT
A=
6 6 6
6 6 6
B=
1 1 1
1 1 1
1 1 1
C=
6 6 6
6 6 6
15
1 1 1
1 1 1
1 1 1
A=magic(3);
display(A);
A1=rand(3);
display(A1);
A2=pascal(3);
display(A2);
B=sort(A);
display(B);
C=sort(A,'descend');
display(C);
D=sort(A,'ascend');
display(D);
E=sortrows(A,1);
display(E);
OUTPUT`
A=
8 1 6
3 5 7
4 9 2
A1 =
A2 =
1 1 1
1 2 3
16
1 3 6
B=
3 1 2
4 5 6
8 9 7
C=
8 9 7
4 5 6
3 1 2
D=
3 1 2
4 5 6
8 9 7
E=
3 5 7
4 9 2
8 1 6
Reshaping/Resizing
X=[1 2 3 4 ; 2 3 4 5 ; 5 6 7 8 ];
display(X);
Y=reshape(X,2,6);
display(Y);
Z=reshape(X,6,2);
display(Z);
17
OUTPUT:
X=
1 2 3 4
2 3 4 5
5 6 7 8
Y=
1 5 3 3 7 5
2 2 6 4 4 8
Z=
1 3
2 4
5 7
2 4
3 5
6 8
Rotation
A=[1 2 3 4 ; 2 3 4 5 ; 5 6 7 8 ];
display(A);
C=rot90(X);
display(C);
OUTPUT :
A=
1 2 3 4
18
2 3 4 5
5 6 7 8
C=
4 5 8
3 4 7
2 3 6
1 2 5
Flipping
A=[1 2 3 4];
display(A);
C=fliplr(A);
display(C);
OUTPUT:
A=
1 2 3 4
C=
4 3 2 1
A=[1 2 3 4; 2 3 4 5 ];
display(A);
C=fliplr(A);
display(C);
19
OUTPUT:
A=
1 2 3 4
2 3 4 5
C=
4 3 2 1
5 4 3 2
Logical Operations
A=[1 2 3 4 5];
display(A);
B=[1 0 0 1 0];
display(B);
C=xor(A,B);
display(C);
OUTPUT :
A=
1 2 3 4 5
B=
1 0 0 1 0
C=
0 1 1 0 1
A=[1 0 1 0 1];
display(A);
B=[1 0 0 1 0];
20
display(B);
C=or(A,B);
display(C);
C=and(A,B);
display(C);
C=not(A);
display(C);
OUTPUT:
A=
1 0 1 0 1
B=
1 0 0 1 0
C=
1 0 1 1 1
C=
1 0 0 0 0
C=
0 1 0 1 0
Relational Operators
A=[1 2 3; 4 5 6; 7 8 9];
B=[7 8 9; 4 5 6; 1 2 3];
21
display(A);
display(B);
display(A>B);
display(A<B);
display(A==B);
display(A~=B);
OUTPUT:
A=
1 2 3
4 5 6
7 8 9
B=
7 8 9
4 5 6
1 2 3
ans =
0 0 0
0 0 0
1 1 1
ans =
1 1 1
0 0 0
0 0 0
ans =
0 0 0
1 1 1
0 0 0
22
ans =
1 1 1
0 0 0
1 1 1
23
EXPERIMENT 3
AIM-a) To generate Random Sequence and plot them
b) To calculate sum matrix, cumulative sum matrix and plot the
sum matrix.
THEORY:
When you create random numbers using software, the results are
not random in a strict, mathematical sense. However, software
applications, such as MATLAB®, use algorithms that make your
results appear to be random and independent. The results also
pass various statistical tests of randomness and independence.
These apparently random and independent numbers are often
described as pseudorandom and pseudoindependent. You can use
these numbers as if they are truly random and independent. One
benefit of using pseudorandom, pseudoindependent numbers is
that you can repeat a random number calculation at any time.
This can be useful in testing or diagnostic situations.
Although repeatability can be useful, it is possible to accidentally
repeat your results when you really want different results. There
are several ways to avoid this problem. The documentation
contains several examples that show how to ensure that your
results are different when that is your intention.
All the random number functions, rand, randn, randi,
and randperm, draw values from a shared random number
generator. Every time you start MATLAB®, the generator resets
itself to the same state. Therefore, a command such
as rand(2,2) returns the same result any time you execute it
immediately following startup. Also, any script or function that
calls the random number functions returns the same result
whenever you restart.
PROCEDURE:
OUTPUT
2)
Z=randn(4,4);
disp(z);
plot(z)
title('random function');
xlabel('random variable');
ylabel('f(x)');
26
OUTPUT
b)
A=magic(4,4);
X=cumsum(A,1);
Y=cumsum(A,2);
Z=sum(A);
V=sum(A,1);
U=sum(A,2);
27
EXPERIMENT 4
AIM-a) Evaluating a given expression and rounding it to the
nearest integer value using Round, Floor, Ceil and Fix functions;
Also, generating and Plots of (A) Trigonometric Functions -
sin(t),cos(t), tan(t), sec(t), cosec(t) and cot(t) for a given duration,
‘t’. (B) Logarithmic and other Functions – log(A), log10(A), Square
root of A, Real nth root of A.
THEORY:
a=
Columns 1 through 4
-1.9000 -0.2000 3.4000 5.6000
Columns 5 through 6
7.0000 2.4000 + 3.6000i
round(a)
ans =
Columns 1 through 4
-2.0000 0 3.0000 6.0000
Columns 5 through 6
7.0000
28
floor(A) rounds the elements of A to the nearest integers less
than or equal to A. For complex A, the imaginary and real parts
are rounded independently.
Examples
a=
Columns 1 through 4
-1.9000 -0.2000 3.4000 5.6000
Columns 5 through 6
7.0000 2.4000 + 3.6000i
floor(a)
ans =
Columns 1 through 4
-2.0000 -1.0000 3.0000 5.0000
Columns 5 through 6
7.0000 2.0000 + 3.0000i
a=
Columns 1 through 4
-1.9000 -0.2000 3.4000 5.6000
Columns 5 through 6
7.0000 2.4000 + 3.6000i
29
fix(a)
ans =
Columns 1 through 4
-1.0000 0 3.0000 5.0000
Columns 5 through 6
7.0000 2.0000 + 3.0000i
PROCEDURE:
SUGGESTED PROGRAM:
a)
t=0:0.1:10*pi;
A=1; F=100;
x=A.*sin(2*pi*F*t);
subplot(511);
plot(t,x);
xlabel('time in seconds');
ylabel('sin fuction');
title('plotting the trignometric functions');
y=A.*cos(2*pi*F*t);
subplot(512);
30
plot(t,y);
xlabel('time in seconds');
ylabel('cos fuction');
title('plotting the trignometric functions');
r=A.*tan(2*pi*F*t);
subplot(513);
plot(t,r);
xlabel('time in seconds');
ylabel('tan fuction');
title('plotting the trignometric functions');
z=A.*cosec(2*pi*F*t);
subplot(514);
plot(t,z);
xlabel('time in seconds');
ylabel('cosec fuction');
title('plotting the trignometric functions');
q=A.*cot(2*pi*F*t);
subplot(515);
plot(t,q);
xlabel('time in seconds');
ylabel('cot fuction');
title('plotting the trignometric functions');
e=A.*sec(2*pi*F*t);
subplot(516);
plot(t,e);
xlabel('time in seconds');
ylabel('sec fuction');
title('plotting the trignometric functions');
OUTPUT
31
b)
x=0:0.1:10;
y=log(x);
title('log and exponential functions');
subplot(511);
plot(x,y);
xlabel('time in sec');
ylabel('log fuction');
z=log10(x);
subplot(512);
plot(x,z);
xlabel('time in sec');
ylabel('log fuctn');
u=exp(x);
subplot(513);
plot(x,u);
xlabel('time in sec');
ylabel('expfuction');
v=exp(-x);
subplot(514);
plot(x,v);
xlabel('time in sec');
ylabel('expfuctn');
OUTPUT
32
c)
A=0:0.1:45;
z=sqrt(A);
disp(A);
disp(z);
OUTPUT
33
EXPERIMENT 5
AIM-Creating a vector X with elements, Xn = (-1)n+1/(2n-1) and
Adding up 100 elements of the vector, X; And, plotting the
functions, x, x3, ex, exp(x2) over the interval 0 < x < 4 (by
choosing appropriate mesh values for x to obtain smooth curves),
on A Rectangular Plot
THEORY:
PROCEDURE
SUGGESTED PROGRAM:
a)
n=0:100;
34
Xn=[(-1).^(n+1)]./(2*n-1);
y=Xn;
sum(y);
disp(y);
plot(y);
OUTPUT
b)
x=0:1:4;
subplot(411);
plot(x);
title('plot of x');
xlabel('x');
ylabel('y');
y=x.^3;
35
subplot(412);
plot(y);
title('plot of x cube');
xlabel('x');
ylabel('y');
z=exp(x.^2);
subplot(413);
plot(z);
title('plot of x square');
xlabel('x');
ylabel('y');
v=exp(x);
subplot(414);
plot(v);
title('plot of e^x');
xlabel('x');
ylabel('y');
OUTPUT
36
EXPERIMENT 6
THEORY:
MATLAB allows you to add title, labels along the x-axis and y-axis,
grid lines and also to adjust the axes to spruce up the graph.
37
Color Code
White w
Black k
Blue b
Red r
Cyan c
Green g
Magenta m
Yellow y
subplot(m, n, p)
where, m and n are the number of rows and columns of the plot
array and p specifies where to put a particular plot.
PROCEDURE
38
2. Define the function, y = f(x).
SUGGESTED PROGRAM:
a)
t=0:0.0001:.5;
a=1;
f=100;
x=a.*sin(2*pi*f*t);
plot(x);
xlabel('time');
ylabel('sine function');
title('plot of sinosoidal signal');
OUTPUT
39
EXPERIMENT 7
THEORY:
You can also specify initial and boundary conditions for the
problem, as comma-delimited list following the equation as:
dsolve('eqn','cond1', 'cond2',…)
For the purpose of using dsolve command, derivatives are
indicated with a D. For example, an equation like f'(t) = -2*f +
cost(t) is entered as:
'Df = -2*f + cos(t)'
40
Higher derivatives are indicated by following D by the order of the
derivative.
s = dsolve('Dy = 5*y')
s=
C2*exp(5*t)
PROCEDURE:
SUGGESTED PROGRAM:
dsolve(‘Dy=y*x);
OUTPUT
C1*exp(x*t)
41
EXPERIMENT 8
AIM- Writing brief Scripts starting each Script with a request for
input (using input) to Evaluate the function h(T) using if-else
statement, where
THEORY:
if expression1
statements1
elseif expression2
statements2
else
statements3
end
PROCEDURE:
An if can have zero or one else's and it must come after any
else if's.
An if can have zero to many else if's and they must come
before the else.
43
Once an else if succeeds, none of the remaining else if's or
else's will be tested.
SUGGESTED PROGRAM:
clc
T=input(‘enter the value of T for the function h(T)’);
If(0<T&&T<100)
display(‘value of h’);
h=T-10;
elseif(T>100) display(‘value of h’)
h=0.45*T;
h=h+900;
elseif(T<0)
T=0;
else
display(‘error’)
end
OUTPUT
EXPERIMENT 9
THEORY:
The Gibbs phenomenon involves both the fact that Fourier sums
overshoot at a jump discontinuity, and that this overshoot does
not die out as the frequency increases.
The three pictures on the right demonstrate the phenomenon for
a square wave (of height ) whose Fourier expansion is
45
More precisely, this is the function f which equals
between and and between an
d for every integer n; thus this square wave has a jump
discontinuity of height at every integer multiple of .
As can be seen, as the number of terms rises, the error of the
approximation is reduced in width and energy, but converges to a
fixed height. A calculation for the square wave (see Zygmund,
chap. 8.5., or the computations at the end of this article) gives an
explicit formula for the limit of the height of the error. It turns out
that the Fourier series exceeds the height of the square wave
by
PROCEDURE:
SUGGESTED PROGRAM:
t=0:0.1:10;
x=sin(t);
plot(t,x);
title('Sine funtion');
xlabel('t');
ylabel('x');
OUTPUT
47
b)
f=5;
w=2*pi*f;
t=0:0.0001:1;
y=0;
for n=1:2:99;
y=y+(1/n).*sin(n*w*t);
end
plot(t,y);
title('Harmonic funtion');
xlabel('x');
ylabel('y');
OUTPUT
48
c)
f=5;
w=2*pi*f;
t=0:0.0001:1;
y=0;
for n=1:2:99;
y=y+(1/n).*sin(n*w*t);
end
plot(t,y);
OUTPUT
49
50
EXPERIMENT 10
THEORY:
drawPolygon(POLY);
drawPolygon(PX, PY);
pie(X) draws a pie chart using the data in X. Each slice of the pie
chart represents an element in X.
52
If X is of data type categorical, the slices correspond to
categories. The area of each slice is the number of elements in
the category divided by the number of elements in X.
bar(y) creates a bar graph with one bar for each element in y. If y
is an m-by-n matrix, then bar creates m groups of n bars.
CODE:
53
Output
54
55
56
CONCLUSION:
57
List of contributors
Dr Rinki Gupta
58