Basic Simulation Lab
Basic Simulation Lab
Experiment 1
Date- 14 December 2023
Aim - Creating a one dimensional array (Row/Column Vector).
● Creating a Two Dimensional Array (Matrix of a given size).
● Performing Arithmetic operations - Addition, Subtraction, Multiplication.
● Performing Matrix operations Inverse, Transpose, Rank.
INPUT
%creating a one dimensional array- row/column%
a=[1;2;3;4;5]
x=[3 4 5 6 7]
b=[1,2,3,4,5]
%creating a multidimensional array%
c=[7,5,1;7,3,9;10,1,12]
OUTPUT
a =
1
2
3
4
5
x =
3 4 5 6 7
b =
1 2 3 4 5
c =
7 5 1
7 3 9
10 1 12
Addition of Matrix
f =
4 6 8 10 12
Subtraction of Matrix
g =
-2 -2 -2 -2 -2
Multiplication of Matrix
h =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Rank of Matrix
t =
Transpose of Matrix
y =
7 7 10
5 3 1
1 9 12
Inverse of Matrix
n =
Determinant of Matrix
u =
196
Exponent of Matrix
z =
Theory-An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulations. MATLAB is designed to operate primarily on whole matrices and
arrays. Therefore, operators in MATLAB work both on scalar and non-scalar data.
MATLAB allows the following types of elementary operations:
● Arithmetic Operators
● Relational Operators
● Logical Operators
● Bitwise Operations
● Set Operations
MATLAB allows two different types of arithmetic operations:
● Matrix arithmetic operations
● Array arithmetic operations
Matrix arithmetic operations are the same as defined in linear algebra. Array operations are
executed element by element, both on one-dimensional and multidimensional arrays.
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.
The following table shows the relational operators available in MATLAB:
Operator Description
== Equal to
~= Not equal to
Logical Operators:- MATLAB offers two types of logical operators and functions:
● Element-wise : these operators operate on corresponding elements of logical arrays.
The symbols & ; | and ~ are the logical array operators AND, OR and NOT.
● Short-circuit - these operators operate on scalar, logical expressions. The symbols
&& and || are the logical short-circuit operators AND and OR.
INPUT
%Concatenation%
disp('The concatenating')
B= [A,A+1;A+5,A+3]
%Index operations%
disp('The indexing')
A(5)
A(2,3)
%Sorting%
disp('The sorting')
sort(A,1)
sort(A,2,"descend")
sort(A,'descend')
sort(A,"ascend")
%Shifting operations%
disp('The shifting')
circshift(A,[1,0])
circshift(A,[0,1])
%Reshaping%
disp('Reshaping')
reshape(A,1,9)
%Flipping%
disp('Flipping the matrix vertically')
fliplr(A)
disp('Flipping the matrix vertically')
flipud(A)
flipud(B)
%Relational Operations%
disp('Array X')
X=[1,2,3]
disp('Array X')
Y=[89 88 90]
disp('Equal Operations')
X=Y
X>Y
X<Y
X>=Y
X~=Y
%Logical operations%
disp('Logical Operation AND')
and (X,Y)
disp('Logical Operation OR')
or (X,Y)
disp('Logical Operation OR')
not (X)
disp('Logical Operation XOR')
xor(X,Y)
OUTPUT
Array is 3*3
A =
1 2 3
4 5 6
6 7 8
The concatenating
B =
1 2 3 2 3 4
4 5 6 5 6 7
6 7 8 7 8 9
6 7 8 4 5 6
9 10 11 7 8 9
11 12 13 9 10 11
The indexing
ans =
5
ans =
6
The sorting
ans =
1 2 3
4 5 6
6 7 8
ans =
3 2 1
6 5 4
8 7 6
ans =
6 7 8
4 5 6
1 2 3
ans =
1 2 3
4 5 6
6 7 8
The shifting
ans =
6 7 8
1 2 3
4 5 6
ans =
3 1 2
6 4 5
8 6 7
Reshaping
ans =
1 4 6 2 5 7 3 6 8
3 2 1
6 5 4
8 7 6
6 7 8
4 5 6
1 2 3
ans =
11 12 13 9 10 11
9 10 11 7 8 9
6 7 8 4 5 6
6 7 8 7 8 9
4 5 6 5 6 7
1 2 3 2 3 4
Array X
X =
1 2 3
Array Y
Y =
89 88 90
Equal Operations
X =
89 88 90
ans =
0 0 0
ans =
0 0 0
ans =
1 1 1
ans =
0 0 0
1 1 1
Logical Operation OR
ans =
1 1 1
0 0 0
0 0 0
Experiment 3
Date: 04 January 2024
Aim: To generate Random Sequence and plot them
● To calculate sum matrix, cumulative sum matrix and plot sum matrix
Tool Used: MATLAB 7.0
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.
All the random number functions 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.
INPUT
A = [1 4 7; 2 5 8; 3 6 9]
%graph%
plot(x)
title('random function');
xlabel('random variable');
ylabel('f(x)');
OUTPUT
A =
1 4 7
2 5 8
3 6 9
S1 =
6 15 24
S2 =
12
15
18
C1 =
1 4 7
3 9 15
6 15 24
C2 =
1 5 12
2 7 15
3 9 18
x =
Theory- Ceil function rounds the elements of A to the nearest integers greater than or
equal to A. For complex A, the imaginary and real parts are rounded independently.
● Round function rounds the elements of X to the nearest integers.
● Floor function 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.
● Fix function rounds the elements of A toward zero, resulting in an array of integers.
For complex A, the imaginary and real parts are rounded independently.
To plot the graph of a function, you need to take the following steps:
● Define x, by specifying the range of values for the variable x, for which the function
is to be plotted.
● Define the function, y = f(x).
● Call the plot command, as plot(x, y).
INPUT
clc
A = [-2.7 -0.8 0.7 2.3]
%Trigonometric operations%
t = 0 : pi/100 : 2*pi;
y = sin(t);
subplot(3, 3, 1)
plot(t, y)
xlabel('Time')
ylabel('Amplitude')
title('Sine graph')
z = cos(t);
subplot(3, 3, 2)
plot(t, z)
xlabel('Time')
ylabel('Amplitude')
title('Cosine graph')
w = tan(t);
subplot(3, 3, 3)
plot(t, z)
xlabel('Time')
ylabel('Amplitude')
title('Tan graph')
u = csc(t);
subplot(1, 3, 1)
plot(t, u)
xlabel('Time')
ylabel('Amplitude')
title('Cosec graph')
v = sec(t);
subplot(1, 3, 2)
plot(t, v)
xlabel('Time')
ylabel('Amplitude')
title('Secant graph')
b = csc(t);
subplot(1, 3, 3)
plot(t, b)
xlabel('Time')
ylabel('Amplitude')
title('Cot graph')
%Logarithmic operations%
T = 0 : 0.1 : 10;
P = exp(-T);
subplot(3, 3, 4)
plot(T, P)
xlabel('Time')
ylabel('Amplitude')
title('Exponential graph')
q = log(T);
subplot(3, 3, 5)
plot(T, q)
xlabel('Time')
ylabel('Amplitude')
title('Logarithm graph')
r = log10(T);
subplot(3, 3, 6)
plot(T, r)
xlabel('Time')
ylabel('Amplitude')
title("Logarithm 10 graph")
s = log(T);
%Square Root%
subplot(3, 3, 7)
plot(T, s)
xlabel('Time')
ylabel('Amplitude')
title('Square-root function graph')
OUTPUT
A =
Ceil A:
ans =
-2 0 1 3
Floor A:
ans =
-3 -1 0 2
Round A:
ans =
-3 -1 1 2
Fix A:
ans =
-2 0 0 2
Experiment 5
Date- 18 January 2023
Aim - Creating a vector X with elements 𝑋𝑛 = (− 1)𝑛 + 1/(2𝑛 − 1) up to 100 elements of
the vector X and plotting the functions x2,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.
Tool Used- MATLAB 7.0
Theory- Vector Creation (X): The vector X is created using the given formula.The n
variable represents the index of the vector, ranging from 1 to 100. The (-1).^n term alternates
the sign of each element in the vector. The division by (2*n - 1) ensures that the denominator
follows the pattern 1,3,5,… as n increases.
Function Plotting: The linspace function is used to create a smooth range of values for x
between 0 and 4. The functions 2 x2 , x3, ex and ex2 are calculated for the specified x values.
The plot function is employed to plot each function on the same figure with appropriate
line widths and labels.
INPUT
%declaring n%
n=1:100;
%declaring Xn%
Xn=((-1).^(n+1)./(2*n-1));
plot(n,Xn)
subplot(5,1,1)
title('Xn')
xlabel('n')
ylabel('Xn')
sum(Xn)
%declaring x%
x=0:0.1:4;
%declaring function%
Z1=x.^2;
plot(x,Z1)
subplot(5,1,2)
title('X^2')
xlabel('x')
ylabel('x^2')
Z2=x.^3;
plot(x,Z2)
subplot(5,1,3)
title('x^3')
xlabel('x')
ylabel('x^3')
Z3=exp(x);
plot(x,Z3)
subplot(5,1,4)
title('Exponential(X)')
xlabel('x')
ylabel('exp(x)')
Z4=exp(Z1);
plot(x,Z4)
subplot(5,1,5)
title('Exponential(X^2)')
xlabel('x')
ylabel('exp(x^2)')
OUTPUT
Sum is:
ans =
0.7829
Experiment 6
Date- 25 January 2023
Aim - Generating a Sinusoidal Signal of a given frequency (say,100Hz) and plotting with
Graphical Enhancements - titling, labelling, adding text , adding legends, adding new plots
to existing plot, printing text in greek letters, plotting as multiple and subplot.
Theory- To generate a sinusoidal signal of a given frequency and plot it with graphical
enhancements, including titling, labelling, adding text, legends, and plotting multiple
subplots, you would typically follow these steps:
-> Generate the Signal: Use a mathematical function to generate the sinusoidal signal. For a
sine wave, the function is typically f(t)=Asin(2πft+ϕ), where A is the amplitude, f is the
frequency in Hz, t is time, and ϕ is the phase angle.
-> Plotting: Utilise a plotting library such as Matplotlib in Python to create the plot.
Matplotlib provides extensive functionality for customising plots.
-> Enhancements:
● Titling: Add a title to the plot using plt.title("Title").
● Labelling: Label the x and y axes using plt.xlabel("X Label") and plt.ylabel("Y Label").
● Adding Text: Use plt.text(x, y, "Text") to add text at specific coordinates on the plot.
● Adding Legends: If plotting multiple signals, use plt.legend(["Signal 1", "Signal 2"])
to add a legend.
● Plotting Multiple Signals: Use plt.plot() multiple times with different data to plot
multiple signals on the same plot.
● Plotting as Subplots: Use plt.subplot(rows, cols, index) to create subplots.
● Printing Greek Letters: Matplotlib supports LaTeX rendering, so you can use LaTeX
syntax to print Greek letters. For example, plt.xlabel("/pi") will label the x-axis with
the Greek letter pi.
● Plotting in Multiple Subplots: Use plt.subplots(rows, cols) to create a grid of
subplots, then plot each signal on its respective subplot.
Plotting such signals allows us to visualise their behaviour over time. Enhancements like
titling, labelling, adding text, legends, and subplots improve the readability and
interpretation of the plot. Utilising these graphical enhancements, along with the ability to
print Greek letters for additional clarity or mathematical notation, enables effective
communication of the signal's characteristics and behaviour.
INPUT
%plot sine wave%
t = 0 : 1/100 : 1;
f0 = 1
s1 = sin(2 * pi * f0 * t);
figure;
plot(t, s1);
xlabel('Time(s)');
ylabel('Amplitude');
title('Sine curve (t ranges from 0 to 2\pi)');
text(01, 0, '\leftarrow')
s2 = cos(2 * pi * f0 * t);
figure;
plot(t, s1, t, s2);
legend('sine','cos');
xlabel('Time(s)');
ylabel('Amplitude');
title('Sine and Cos curve (t ranges from 0 to 2\pi)');
text(01, 0, '\leftarrow');
text(01, 1, '\leftarrow');
figure;
subplot(2, 1, 1); plot(t, s1);
xlabel("Time(s");
ylabel("Amplitude");
title("Sine curve");
text(01, 0, '\leftarrow');
figure;
subplot(2, 1, 2); plot(t, s2);
xlabel("Time(s)");
ylabel("Amplitude");
title("Cosine Curve");
text(01, 1, '\leftarrow');
s3 = sin(2 * pi * f0 * 2 * t);
figure;
subplot(4, 1, 1); plot(t, s3);
xlabel("Time(s)");
ylabel("Amplitude");
text(01, 0, '\leftarrow');
s4 = sin(2 * pi * f0 * 4 * t);
subplot(4, 1, 2); plot(t, s4);
xlabel("Time(s)");
ylabel("Amplitude");
text(01, 0, '\leftarrow');
s5 = sin(2 * pi * f0 * 0.25 * t);
subplot(4, 1, 3); plot(t, s5);
xlabel("Time(s)");
ylabel("Amplitude");
text(01, 1, '\leftarrow');
s6 = sin(2 * pi * f0 * 0.0625 * t);
subplot(4, 1, 4); plot(t, s6);
xlabel("Time(s)");
ylabel("Amplitude");
text(01, 1, '\leftarrow');
Experiment 7
Date- 01 February 2023
Aim - Generating a square wave form sum of Sine waves of certain amplitude and
frequencies. Fourier expansion of square wave =1/3sin3x+1/5sin5x……….
Theory- Generating a square wave form as the sum of sine waves involves utilizing the
concept of Fourier series. The Fourier series states that any periodic waveform can be
expressed as the sum of sinusoidal functions (sine and cosine waves) of different
frequencies, each with its own amplitude and phase.
The general form of a Fourier series representation for a periodic function f(t) with period T
is given by:
For a square wave, which is a periodic waveform with a duty cycle of 50%, the Fourier series
simplifies to include only odd harmonics. The amplitude of each harmonic decreases as the
frequency increases, with the fundamental frequency (the frequency of the square wave)
having the largest amplitude.
To generate a square wave as the sum of sine waves, follow these steps:
● Determine the Frequencies: Decide the frequencies of the sine waves to be included
in the Fourier series. For a square wave, typically only odd harmonics are included,
with the fundamental frequency being the lowest.
● Calculate the Amplitudes: Determine the amplitudes of each sine wave component.
The amplitude of each harmonic is inversely proportional to its frequency.
● Sum the Sine Waves: Add up the sine waves with their respective frequencies and
amplitudes according to the Fourier series equation.
● Normalize: Normalize the resultant waveform to ensure it falls within the desired
range, typically between -1 and 1 for a normalized square wave.
● Plotting: Utilize a plotting library such as Matplotlib to visualize the generated
square wave.
By summing sine waves of appropriate frequencies and amplitudes, the resulting waveform
will approximate a square wave. This approach demonstrates the power of Fourier series in
decomposing complex signals into simpler sinusoidal components, enabling the synthesis
of various waveforms for practical applications in signal processing and communication
systems.
INPUT
clc
theta=0:pi/20:2*pi;
sine=sin(theta);
sq=sine;
for n=3:2:100
sq=sq+sin(n*theta)./n;
end
figure;
plot(theta,sine,theta,sq);
xlabel('time');
ylabel('amplitude');
legend('sine','square')
OUTPUT