Basic Simulation Lab Practical
Name :
Programme : Btech -
Submitted to : Dr. Neha Gupta
Batch : 2024 - 2028
ER no. :
Index
Matrix Operations, Manipulations and Analysis
Experiment Title 1: Performing matrix operations and matrix manipulations in MATLAB.
Objective: Create a 1D and a 2D array of the given size and perform arithmetic operations and
matrix operations in MATLAB.
Software Required: MATLAB
Theory:
1. Arithmetic Operation
Arithmetic is divided into Matrix-wise (Linear Algebra) and Element-wise (Array math).
Matrix-wise Operations
• Addition/Subtraction (+, -): The sum or difference of corresponding elements of
matrix a and b. Matrices must have identical dimensions.
• Matrix Multiplication (*): The dot product of rows and columns.
• Matrix Power (^): Repeated matrix multiplication (A * A). It requires a square matrix.
Element-wise Operations
• Array Multiplication (.*): Each element in A is multiplied only by the element in the
same position in B.
• Array Division (./): Each element in A is divided by the element in the same position
in B.
• Array Power (.^): Each element in the matrix is raised to the specified power.
2. Special Matrices:
These are pre-defined structures used to initialise data or perform specific algebraic tasks.
• Identity Matrix (eye(n)): A square matrix where all elements are 0 except for the main
diagonal, which contains 1s. It acts as the multiplicative identity,
• Zero Matrix (zeros(m,n)): A matrix where every element is 0. It acts as the additive
identity and is often used to pre-allocate memory for speed.
• Ones Matrix (ones(m,n)): A matrix where every element is 1. It is used for calculating
sums.
• Random Matrix (rand(m,n)): A matrix associated with random values (usually
between 0 and 1). It is used for simulations, noise generation, and weight initialisation
in machine learning.
3. Matrix Operations
These functions extract properties or transform the matrix using Linear Algebra.
• Transpose (a'): Reflects the matrix over its main diagonal. Rows become columns and
columns become rows.
• Determinant (det(a)): A scalar value that indicates if a system of linear equations has
a unique solution. If det = 0, the matrix is singular (non-invertible).
• Inverse (inv(a)): Calculates A^-1, the matrix that, when multiplied by A, results in the
Identity Matrix.
• Adjoint (adjoint(a)): The transpose of the cofactor matrix. Used in the analytical
calculation of the inverse.
• Rank (rank(a)): The number of linearly independent rows or columns in a matrix. It
represents the system's "true" dimension.
• Eigenvalues (eig(a)): The set of scalars lambda associated with a linear system of
equations. They define the scale of transformation along eigenvectors.
Function Table:
Functions Name
eye Identity Matrix
zeros Zero Matrix
ones Ones Matrix
rand Random Matrix
+ Matrix Addition
- Matrix Subtraction
* Matrix Multiplication
.* Element-wise Multiplication
./ Element-wise Division
^ Matrix Power
.^ Element-wise Power
' Matrix Transpose
det Determinant
inv Matrix Inverse
adjoint Adjoint Matrix
eig Eigenvalues
rank Matrix Rank
Result: The experiment is successfully performed and verified.
1. Basic Arithmetic Operations(Element-Wise)
2. Special Matrices
3. Matrix Operations
Matrix Operations, Manipulations, and Signal Plotting
Experiment Title: Performing matrix operations, manipulations, and signal plotting in
MATLAB.
Objective:
1. The equation of the line is given by y=mx+c. Compute the y coordinate of a line with
slope m=0.5 and intercept c=2.
2. To create a vector t with 10 elements and compute:
• x = t sin(t)
• y = (t-1)/(t+1)
• z = (sin^2(t))/(t^2)
3. To verify that the points x = r cos(θ), y = r sin(θ) satisfy x^2 + y^2 = r^2.
4. To create a column vector θ = [0,π/4, π/2, 3π/4, π]. Set r = 2 and compute the column
vectors x and y. Now check that x and y satisfy the circle's equation by computing the
radius.
5. To create a vector of 11 elements from 0 to 10; take r = 0.5 and create another vector
equal to r^0, r^1, r^2,………., r^n. Now take the sum of this vector.
Software Required: MATLAB
Theory:
MATLAB utilizes vectorization to process entire datasets through a single command,
eliminating the need for manual loops. By using linspace to define input ranges and element-
wise operators, the software can evaluate multiple complex functions simultaneously across
every point in a vector. This approach ensures that calculations involving trigonometric
transformations and power sequences are applied consistently to all individual values, making
it highly efficient for verifying geometric identities and managing large numerical arrays.
Procedure:
1. Plot for Line
m=0.5 ………..Declare slope
c=2 ………..Declare intercept
x= linspace(0;0.001;100) ………. Declare the range of x
y=mx+c ………..Equation of a line
plot(x,y) ………..Plot for the line
2. Vector Operations and Mathematical Expressions
t = linspace(1, 10, 10); → Create vector with 10 elements
x1 = t .* sin(t); → Compute t*sin(t) using element-wise math
y1 = (t - 1) ./ (t + 1); → Compute fractional expression
z1 = (sin(t).^2) ./ (t.^2); → Compute sin^2(t)/t^2
3. Geometric Verification (Circle)
theta = (0:0.01:2*pi);
r = 2;
x = r * cos(theta);
y = r * sin(theta);
plot(x, y);
4. Sum of Vectors
a= 0;1;10
r=0.5
x=r^.a;
s= sum(a)
Result: The experiment is successfully performed and verified.
1. Plot for line
2. Vector operations
3. Plot of circle and equation verification
[Link] sum
Matrix Manipulations and Logical Operations
Experiment Title3: Performing Matrix Manipulations and Logical Operations in MATLAB
Objective:
To perform matrix manipulations such as concatenation, indexing, sorting, shifting, reshaping,
resizing, and flipping, and to apply relational and logical operations on arrays using MATLAB.
Software Required: MATLAB
Theory:
MATLAB provides powerful functions for matrix manipulation and logical evaluation, which
are essential for numerical computation and engineering applications.
Procedure:
1. Matrix Structural Manipulation
These operations change how the data is organised or viewed without changing the values
themselves.
Function Name Description
[A B] Horizontal Joins matrices side-by-side.
Concatenation
[A; B] Vertical Concatenation Stack matrices on top of each other.
reshape(A, r, c) Reshaping Changes dimensions to r rows and c
columns.
fliplr(A) Flip Left-Right Reverses the order of columns.
flipud(A) Flip Up-Down Reverses the order of rows.
circshift(A, k) Circular Shift Shifts elements and wraps them around.
2. Data Indexing and Sorting
These functions are used to extract specific data points or organise them in a specific order.
Function Name Description
A(r, c) Element Indexing Accesses the value at a specific Row and Column.
A(r, :) Row Slicing Extracts an entire row using the colon operator.
A(:, c) Column Slicing Extracts an entire column.
sort(A) Column Sort Sorts each column in ascending order (default).
sort(A, 2) Row Sort Sorts each row independently across columns.
3. Relational and Logical Operations
These are used for comparison and decision-making. They always return a Logical Array (1
for True, 0 for False).
Function Name Mathematical Logic
>/< Greater/Less Than Compares if element $A$ is larger or smaller than $B$.
== Equal To Checks for exact equality.
~= Not Equal To Checks if two values are different.
& Logical AND True only if both inputs are True.
| Logical OR True if at least one input is True.
~ Logical NOT Reverses the logic (Inverts 1 to 0 and 0 to 1).
xor(X, Y) Exclusive OR True only if inputs are different.
Result: Matrix manipulations and logical operations were successfully performed in
MATLAB.
Evaluation, Rounding and Mathematical Functions
Experiment Title 4: Evaluation, Rounding and Plotting of Mathematical Functions in
MATLAB
Objective:
1. To evaluate a given mathematical expression and round it using round, floor, ceil and fix
functions.
2. To generate and plot trigonometric functions for a given duration.
3. To evaluate logarithmic and other mathematical functions.
Software Required: MATLAB
Theory:
MATLAB simplifies complex engineering calculations through its extensive library of built-
in functions. These functions are categorised based on how they process numerical data:
• Numerical Approximation (Rounding): These functions map real numbers to integers.
While round follows standard arithmetic rules, floor and ceil provide directional
rounding (down or up), and fix acts as a truncator, removing decimal values toward
zero.
• Trigonometric Evaluation: MATLAB processes periodic waveforms using functions
like sin and cos. By default, these functions use radians. To generate a smooth plot, a
small step size (like 0.01) is used to create a continuous-looking curve from discrete
data points.
• Logarithmic functions are the mathematical inverse of exponential functions. In
MATLAB, they are used to compress large dynamic ranges (like sound decibels or
earthquake intensity) into manageable scales.
Procedure:
Part A: Numerical Evaluation & Rounding
Here, we define a scalar A and observe how different rounding logic affects the resulting
integer.
Function Command Logic Result (A=12.78)
Round round(A) Rounds to the nearest integer 13
Floor floor(A) Rounds down toward -infinity 12
Ceil ceil(A) Rounds up toward +infinity 13
Fix fix(A) Truncates decimal toward 0 12
Part B: Trigonometric Signal Generation
We define a time vector t from 0 to 2pi to cover one full cycle of the fundamental trigonometric
functions.
t = 0 : 0.01: 2*pi;
Calculate Primary Functions
y1 = sin(t); // Sine Wave
y2 = cos(t); //Cosine Wave
y3 = tan(t); // Tangent (Contains asymptotes)
Calculate Reciprocal Functions
y4 = sec(t); //Secant (1/cos)
y5 = csc(t); // Cosecant (1/sin)
y6 = cot(t); //Cotangent (1/tan)
Part C: Logarithmic and Power Evaluations
Here, we process a scalar A to determine its logarithmic properties and root values.
Function Command Result (A=25)
Natural Log log(A) 3.2189
Common Log log10(A) 1.3979
Square Root sqrt(A) 5
n-th Root nthroot(A,3) 2.9240
Result: The given expression was evaluated and rounded successfully. Trigonometric,
logarithmic and root functions were generated and plotted.
Part A: Numerical Evaluation & Rounding
Part B: Trigonometric Signal Generation
Part C: Logarithmic and Power Evaluations
Signal Visualisation and Figure Controls
Experiment Title 5: Signal Visualisation and Figure Control in MATLAB
Objective:
Generating a sinusoidal signal of a given frequency with titling, labelling and adding legends,
plotting as a multi-plot and subplot and scaling the generated signal for different values.
Software Required: MATLAB
Theory:
In MATLAB, plotting is a layered process. While plot generates the visual representation of
data, control functions like hold and subplot manage the environment where those
visualisations appear. These tools are essential for comparing multiple signals or analysing how
changes in frequency and time affect a waveform.
1. Figure Management Functions
Function Name Purpose
plot(t, y) 2D Plot Draws a line graph of vector $y$ versus vector $t$.
hold on Retain Figure Prevents the figure from being cleared, allowing
multiple plot commands to stack on one graph.
hold off Release Figure Resets the behaviour so that the next plot command
clears the current screen (default).
subplot(m, n, p) Sub-window Divides a figure into an m times n grid and selects the
p-th area for the next plot.
grid on Grid Lines Adds horizontal and vertical lines to the plot to make
reading coordinates easier.
2. Labelling and Formatting
Function Name Purpose
xlabel('...') X-Axis Label Label the horizontal axis (usually "time" in your
code).
ylabel('...') Y-Axis Label Label the vertical axis (usually "amplitude" in your
code).
title('...') Plot Title Adds a heading above the individual plot or subplot.
linspace(a, b, n) Linearly Spaced Generates n points between a and b to create a smooth
time domain vector.
Result: The experiment is successfully performed and verified.