Practical File: Course Title: Basic Simulation Lab Course Code: ES-204 Credit Units:01
Practical File: Course Title: Basic Simulation Lab Course Code: ES-204 Credit Units:01
Practical File: Course Title: Basic Simulation Lab Course Code: ES-204 Credit Units:01
Code:
clc
clear all
close all
x=[12,41,73;25,54,84;32,61,29];
display(x);
y=[51,92,83;64,85,66;97,58,29];
display(y);
i=inv(x);
display(i);
a=inv(y);
display(a);
t=transpose(x);
display(t);
z=transpose(y);
display(z);
r=rank(x);
display(r);
f=rank(y);
display(f);
Output
x=
12 41 73
25 54 84
32 61 29
y=
51 92 83
64 85 66
97 58 29
i=
-0.1549 0.1421 -0.0217
0.0855 -0.0866 0.0356
-0.0088 0.0253 -0.0164
a=
0.0495 -0.0780 0.0357
-0.1652 0.2388 -0.0707
0.1647 -0.2168 0.0564
t=
12 25 32
41 54 61
73 84 29
z=
51 64 97
92 85 58
83 66 29
r=3
f=3
Experiment-2
1 2 3 7 8 9
4 5 6 6 5 4
7 8 9 1 2 3
ans =
8 9
s1 =
1 2 3
4 5 6
7 8 9
s2 =
1 2 3
4 5 6
7 8 9
s3 =
1 2 3
4 5 6
7 8 9
s4 =
1 2 3
6 5 4
7 8 9
r1 =
3 6 9
2 5 8
1 4 7
r2 =
1 6 7
2 5 8
3 4 9
r3 =
9 8 7
6 5 4
3 2 1
c1 =
7 8 9
1 2 3
4 5 6
c2 =
6 5 4
1 2 3
7 8 9
c3 =
2 3 1
5 6 4
8 9 7
c4 =
9 7 8
3 1 2
6 4 5
f1 =
7 8 9
4 5 6
1 2 3
f2 =
9 8 7
4 5 6
3 2 1
Logical Operation:
1.OR: a|b, produces a matrix in which element is 1 only when there is one in any of two
matrices or in both the matrices else gives zero
2.AND: a&b,produces a matrix in which element is one if both elements in a and b is one,
otherwise gives zero
3.NOT: ~a, it changes the 1 by 0 and 0 by 1 in the matrix a and generates a new matrix
4.XOR: xor(a,b), gives 1 as output when both elements in a and b are not nonzero or when
both elements are not zero I.e. when both are not same
Code:
clc
clear all
close all
x=[1,0,1;0,0,1;1,1,1];
y=[0,1,0;1,1,1;0,0,1];
I=xor(x,y);
display(x);
display(y);
display(I);
o=or(y,x);
display(o);
a=and(x,y);
display(a);
n1=not(x);
n2=not(y);
display(n1);
display(n2);
Output:
x=
1 0 1
0 0 1
1 1 1
y=
0 1 0
1 1 1
0 0 1
I=
1 1 1
1 1 0
1 1 0
o=
1 1 1
1 1 1
1 1 1
a=
0 0 0
0 0 1
0 0 1
n1 =
0 1 0
1 1 0
0 0 0
n2 =
1 0 1
0 0 0
1 1 0
Relational Operation:
Code:
clc
clear all
close all
x=[3,3];
y=[1,5];
display(x>y);
display(x<y);
display(x==y);
display(x~=y);
Output:
1×2 logical array
1 0
1×2 logical array
0 1
1×2 logical array
0 0
1×2 logical array
1 1
Experiment 3
Aim: Generating a set of Commands on a given Vector (Example: X = [1 8 3 9 0 1]) to
(A). Add up the values of the elements (Check with sum)
(B). Compute the Running Sum (Check with sum), where Running Sum for element j = the
sum of the elements from 1 to j, inclusive.
(C) Generating a Random Sequence using rand() / randn() functions and plot them.
Introduction:
1.Sum: sum(a),returns a row vector containing sum of each columns in matrix;
sum(a,2),returns a column vector containing sum of each row of matrix
4.Rand: it is also a predefined function in MATLAB which return any number between 0 and
1, rand(n,m), will return a n*m matrix having random numbers between 0 and 1
5.Randi(max): returns any integer between 1 and max; randi(10,3); return a 3*3 matrix
containing random integers less than 10
6.Randn: randn returns a random scalar drawn from the standard normal
distribution,randn(n) returns a n*n matrix
A. Sum
Code:
clc
clear all
close all
a=[1,2,3;4,5,6;7,8,9];
display(a);
s1=sum(a);
display(s1);
s2=sum(a,2);
display(s2);
s3=cumsum(a);
s4=cumsum(a,2);
display(s3);
display(s4);
Output:
>> deepak65
a=
1 2 3
4 5 6
7 8 9
s1 =
12 15 18
s2 =
15
24
s3 =
1 2 3
5 7 9
12 15 18
s4 =
1 3 6
7 9 15
7 15 24
Running sum
Code:
a=[4,5,6,8,5,6,8,5,2];
l=length(a);
sum=0;
for i=1:l
sum=sum+a(i);
end
display(sum);
Output:
>> deepakrunsum
sum =
49
Random Num
Code:
r1=rand(3,3);
display(r1);
r2=randi(4,3);
display(r2);
r3=randperm(6,3);
display(r3);
r4=randn(3);
display(r4);
subplot(2,2,1);
plot(r1);
title('Rand');
subplot(2,2,2);
plot(r2);
title('Randi');
subplot(2,2,3);
plot(r3);
title('Randperm');
subplot(2,2,4);
plot(r4);
title('Randn');
Output:
>> deepakrndm
r1 =
0.2373 0.5468 0.4889
0.4588 0.5211 0.6241
0.9631 0.2316 0.6791
r2 =
2 1 4
2 4 1
4 4 2
r3 =
1 3 2
r4 =
Aim: 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.
Introduction:
clc
clear all
close all
a=[-0.2,5.5,1.9,0,5.7];
display(a);
r1=round(a);
display(r1);
display(ceil(a));
display(floor(a));
display(fix(a));
Output:
a=
r1 =
0 6 2 0 6
ans =
0 6 2 0 6
ans =
-1 5 1 0 5
ans =
0 5 1 0 5
A. Trignometric Functions:
x1=-2*pi:pi/10:2*pi;
y1=sin(x);
subplot(2,3,1);
plot(x1,y1,'r');
xlim([-5,5]);
ylim([-2,2]);
grid on;
title('Sin function');
x2=-2*pi:pi/10:2*pi;
y2=cos(x2);
subplot(2,3,2);
plot(x2,y2,'m*');
xlim([-5,5]);
ylim([-2,2]);
title('Cos function');
x3=-2*pi:pi/10:2*pi;
y3=tan(x3);
subplot(2,3,3);
plot(x3,y3,'k-');
xlim([-5,5]);
ylim([-2,2]);
title('Tan function');
x4=-2*pi:pi/10:2*pi;
y4=cot(x4);
subplot(2,3,4);
plot(x4,y4,'g');
xlim([-5,5]);
ylim([-2,2]);
title('Cot Function');
x5=-2*pi:pi/10:2*pi;
y5=csc(x5);
subplot(2,3,5);
plot(x5,y5,'b');
xlim([-5,5]);
ylim([-2,2]);
title('Cosec function');
x6=-2*pi:pi/10:2*pi;
y6=sec(x6);
subplot(2,3,6);
plot(x6,y6,'c');
xlim([-5,5]);
ylim([-2,2]);
title('Sec function');
grid on;
Output:
clc
clear all
close all
x=-2*pi:pi/10:2*pi;
y=sin(x);
plot(x,y,'k');
xlim([-5,5]);
ylim([-2,2]);
xlabel('time');
hold on
y2=cos(x);
plot(x,y2,'r');
y3=sec(x);
plot(x,y3,'g');
y4=csc(x);
plot(x,y4,'y');
y5=cot(x);
plot(x,y5,'C');
y6=tan(x);
plot(x,y6,'m');
grid on;
legend('sin','cos','sec','cosec','cot','tan');
hold off;
Output:
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: In MATLAB a vector is a matrix with either one row or one column. The distinction
between row vectors and column vectors is essential. Many programming errors are caused
by using a row vector where a column vector is required, and vice versa. MATLAB vectors
are used in many situations, e.g., creating x-y plots, that do not fall under the rubric of linear
algebra. In these contexts a vector is just a convenient data structure. MATLAB still enforces
the rules of linear algebra so paying attention to the details of vector creation and
manipulation is always important.
POWER:
C = A.^B raises each element of A to the corresponding powers in B. The sizes of A and B
must be the same or be compatible. If the sizes of A and B are compatible, then the two
arrays implicitly expand to match each other. For example, if one of A or B is a scalar, then
the scalar is combined with each element of the other array. Also, vectors with different
orientations (one row vector and one column vector) implicitly expand to form a matrix.
Code:
clc;
clear;
close;
s=0;
for n=1:100;
x=(-1)^(n+1)/(2*n-1);
s=s+x;
end
display(s);
x=0:4;
y1=x;
y2=x.^3;
y3=exp(x);
y4=exp(x.^2);
subplot(2,2,1);
plot(x,y1,'r');
xlim([0,4]);
title('y=x');
grid on;
subplot(2,2,2);
plot(x,y2,'g');
xlim([0,4]);
title('x^3');
grid on;
subplot(2,2,3);
plot(x,y3,'k');
xlim([0,4]);
title('e^x');
grid on;
subplot(2,2,4);
plot(x,y4,'m');
xlim([0,4]);
title('e^x^2');
grid on;
Output:
s=
0.7829
Experiment- 6
Aim: : Generating a Sinusoidal Signal of a given frequency with Titling, Labeling, Adding
Text, Adding Legends, Printing Text in Greek Letters, Plotting as Multiple and
Subplot. Time scale the generated signal for different values. E.g. 2X, 4X, 0.25X, 0.0625X.
Theory:
text(x,y,str) adds a text description to one or more data points in the current axes using the
text specified by str. To add text to one point, specify x and y as scalars in data units. To add
text to multiple points, specify x and y as vectors with equal length.
text(___,Name,Value) specifies text object properties using one or more name-value pairs.
For example, 'FontSize',14 sets the font size to 14 points. You can specify text properties
with any of the input argument combinations in the previous syntaxes. If you specify the
Position and String properties as name-value pairs, then you do not need to specify the x, y,
z, and str inputs.
Code:
t=-2*pi:pi/10:2*pi;
f=3;
y=sin(2*pi*f*t);
plot(t,y,'r');
xlabel('time');
ylabel('sin(t)');
text(-1.570796,1,'Max Value','color','magenta','fontsize',15);
legend('Sin Function');
Output:
Experiment- 7
Aim: Solving First, Second and third Order Ordinary Differential Equation using Built-in
Functions and plot.
Theory:
S = dsolve(eqn) solves the differential equation eqn, where eqn is a symbolic equation. Use
diff and == to represent differential equations. For example, diff(y,x) == y represents the
equation dy/dx = y. Solve a system of differential equations by specifying eqn as a vector of
those equations.
Code:
clc
clear all
close all
syms y(t) a b c
p=diff(y,t)==a*y
dsolve(p)
x=0:4;
plot(x,p,'r')
q=diff(y,t,2)==b*y
cond=[y(0)==1 p(0)==2]
dsolve(q,cond)
r=diff(y,t,3)==c*y
cond2=[q(0)==3 y(0)==1]
dsolve(r,cond2)
Output:
p(t) =
diff(y(t), t) == a*y(t)
ans =
C1*exp(a*t)
q(t) =
diff(y(t), t, t) == b*y(t)
cond =
ans =
r(t) =
diff(y(t), t, t, t) == c*y(t)
cond2 =
ans =
THEORY:
OUTPUT:
Experiment 9
Aim: Generating a Square Wave from sum of Sine Waves of certain Amplitude and
Frequencies.
Theory:
➢ SQUARE WAVE
x = square(t) generates a square wave with period 2π for the elements of the time array t.
square is similar to the sine function but creates a square wave with values of –1 and 1. x =
➢ SINE WAVE
A sine wave or sinusoid is a mathematical curve that describes a smooth periodic oscillation.
A sine wave is a continuous wave. It is named after the function sine, of which it is the
graph. It occurs often in pure and applied mathematics, as well as physics, engineering,
signal processing and many other fields.
Code:
clc
clear all
close all
f=5;
w=2*pi*f;
t=0:0.001:1;
y=0;
for n=1:2:99;
y=y+(1/n)*sin(n*w*t);
end
subplot(2,2,1);
plot(t,y,'m');
xlabel('Time');
ylabel('Amplitude');
legend('Sin Wave');
title('Square Wave');
subplot(2,2,2);
stem(t,y);
xlabel('Time');
ylabel('Amplitude');
legend('Sin Wave');
title('Discrete wave');
Output:
Experiment-10
Aim: Basic 2D and 3D plots: parametric space curve. polygons with vertices. 3D contour lines, pie
and bar charts.
Theory:
To plot multiple sets of coordinates on the same set of axes, specify at least one of X,
Y, or Z as a matrix and the others as vectors.
2. Pie3(X) draws a three-dimensional pie chart using the data in X. Each element in X is
represented as a slice in the pie chart.
clc
clear all
close all
t=0:pi/50:10*pi;
st=sin(t);
ct=cos(t);
subplot(2,3,1);
plot3(st,ct,t);
title('3-D Plot');
x=0:pi/10:2*pi;
y=sin(x);
subplot(2,3,2);
pie(x,y);
title('Pie Chart');
subplot(2,3,3);
pie3(x,y);
title('Pie 3D Chart');
subplot(2,3,4);
bar(x,y);
title('Bar Graph');
subplot(2,3,5);
bar3(x,y);
title('Bar 3D Graph');
Output:
OPEN ENDED EXPERIMENT
Course Title: Basic Simulation Lab
Course Code: ES-204
Credit Units:01
Aim: To covert coloured image to grayscale and then to black and white.
Theory:
RGB CHART creates a figure window showing all the available colors with
their names.