You are on page 1of 42

LAB RECORD

BACHELOR OF TECHNOLOGY

B.Tech CSE (Section: “C”) Semester (4th )


(Academic Session – 2019 - 2023 )

Course Title : Basic Simulation Lab


Course Code : ES-204
Enrollment No. :
Name of Student :
Date of Submission : / /2023
Signature of Student :
Grade/Marks Obtained :
Faculty Name & Signature : Mrs. Pallavi Asthana
Department of Computer Science & Engineering
Amity School of Engineering & Technology
Amity University, Lucknow Campus

Department of Computer Science & Engineering


Amity School of Engineering & Technology
Amity University, Lucknow Campus
Department of Computer Science & Engineering
Amity School of Engineering & Technology
Amity University, Lucknow Campus

Index
S.No EXPERIMENTS Date Sign Remark
INTRODUCTION TO MATLAB
1.
Creating a One and Two-Dimensional Array
(Row / Column Vector) (Matrix of given size)
then, (A). Performing Arithmetic Operations -
Addition, Subtraction, Multiplication and
Exponentiation. (B). Performing Matrix
operations - Inverse, Transpose, Rank with
plots.

2. Performing Matrix Manipulations -


Concatenating, Indexing, Sorting, Shifting,
Reshaping, Resizing and Flipping about a
Vertical Axis / Horizontal Axis; Creating
Arrays X & Y of given size (1 x N) and
Performing (A). Relational Operations - >, <,
==, <=, >=, ~= (B). Logical Operations - ~, &,
|, XOR

3. 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.

4. 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.

5. 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

6. 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.

7. Solving First, Second and third Order


Ordinary Differential Equation using Built-
in Functions and plot.

8. Writing brief Scripts starting each Script with


a request for input (using input) to Evaluate
the function h(T) using if-else statement,
where, h(T) = (T – 10) for 0 < T < 100 h(T) =
(0.45 T + 900) for T > 100. Exercise: Testing
the Scripts written using A). T = 5, h = -5 and
B). T = 110, h =949.5

9. Generating a Square Wave from sum of


Sine Waves of certain Amplitude and
Frequencies.

10. Basic 2D and 3D plots: parametric space


curve, polygons with vertices, 3D contour
lines and pie and bar chart.
INTRODUCTION TO MATLAB

MATLAB is a high-performance language for technical computing. It integrates


computation, visualization, and programming in an easy-to-use environment where
problems and solutions are expressed in familiar mathematical notation. Typical uses
include Math and computation, Algorithm development, Data acquisition, Modeling,
simulation, and prototyping, Data analysis, exploration, and visualization, Scientific and
engineering graphics, Application development, including graphical user interface
building.

MATLAB is an interactive system whose basic data element is an array that does not
require dimensioning. This allows you to solve many technical computing problems,
especially those with matrix and vector formulations, in a fraction of the time it would take
to write a SUGGESTED PROGRAM: in a scalar non interactive language such as C or
Fortran. The name MATLAB stands for matrix laboratory.
EXPERIMENT NO. 01

OBJECTIVE: Creating a 1-D array and 2-D array(row/column vector) and then
performing
a) Arithmetic Operations; Addition, Subtraction, Multiplication, Exponentiation
b) Matrix Operations; Inverse, Transpose, Rank with plots.

SOFTWARE USED: MATLAB

THEORY: MATLAB allows two different types of arithmetic operations-:

Matrix arithmetic operations

Matrix arithmetic operations are same as defined in linear algebra. Array operations are
executed element by element, both on one dimensional and multi-dimensional array.

ARITHMETIC OPERATIONS:

Addition (+): Addition or unary plus. A+B adds the values stored in variables A and B. A and
B must have the same size.

Subtraction (-): Subtraction or unary minus. A-B subtracts the value of B from A. A and B must
have the same size.

Matrix Multiplication (*): Matrix multiplication. C = A*B is the linear algebraic product of the
matrices A and B. For non-scalar A and B, the number of columns of A must be equal to the
columns of B.

Array Multiplication (.*): Array multiplication. A.*B is the element-by-element product of the
The matrix is called the inverse of X.

Inverse: A matrix X is invertible if there exists a matrix of the same size such that
XY=YX.
The matrix is called the inverse of X.

Transpose: B =A’ returns the non-conjugate transpose of A, that is, interchanges the
row and
column index for each element.
Rank: The number of linearly independent columns in a matrix is the rank of the
matrix. The
rank gives a measure of the dimension of the range or column space of the matrix,
which is the
collection of all linear combinations of the columns.
Commands:
Clc - Clears command windows

Clear all - Deletes variables, scripts in workspace.

Program :

Clc
Clear all

Creating 1D arrays
a= [1 2 3]
b= [4 5 6]
c= [7;8;9]
Addition of 1D arrays
Sum=a+b

Subtraction of 1D arrays
Sub=a-b

Multiplication of 1D arrays
Mul=a*c

Array element multiplication


Element mul=a.*b

Exponentiation of a matrix
expo_a=exp(a)

Creating 2D arrays
a2=[2 1 2; 7 8 9; 6 5 4]
b2=[2 2 2; 3 3 3; 4 4 4]

Addition of 2D arrays
c2=a2+b2

Subtraction of 2D arrays
c3=a2-b2

Multiplication of 2D arrays
c4=a2*b2

Inverse of Matrix
inversea2=inv(a2)

Transpose of Matrix
Transpose_a2=transpose(a2)
Rank of Matrix
Rank_a2=rank(a2)

RESULT:

1) ARITHMETIC OPERATIONS:
2) Matrix Operations
EXPERIMENT NO.02

OBJECTIVE: Performing Matrix Manipulations - Concatenating, Indexing, Sorting,


Shifting, Reshaping, Resizing and Flipping about a Vertical Axis / Horizontal Axis; Creating
Arrays X & Y of given size (1 x N) and Performing (A). Relational Operations - >, <,
==, <=, >=, ~= (B). Logical Operations - ~, &, |, XOR

SOFTWARE USED: MATLAB

THEORY:

CONCATENATION: Just like in actual world, where concatenation is used to join smaller
things to make something bigger, concatenation has the same application in MATLAB. Using
concatenation we can join smaller matrices to produce bigger matrices. As in a matrix, its most
fundamental unit is an element of that matrix, an entire matrix can be seen as an amalgamation
of all the elements in your matrix. In other words, a matrix can be viewed as a product of
concatenation of all the different elements of that matrix.

INDEXING: Indexing on a matrix means selecting a subset of elements from itself. Indexing
as a function is used as a first or primary operation before many advanced operations. There
are many types of indexing possible as well, depending upon whether you want to select
individual elements out of a given matrix or a set or range of elements.

SORTING: Just like we operated while indexing matrices, in a column major order, sorting
of matrices works in the same manner.

SHIFTING: Shifting of matrices takes place in term of rows and/or columns. MATLAB
allows us to shift rows and/or columns simultaneously in individual commands.

RESHAPING: Reshaping a matrix means changing the order of the matrix. It is done when
for example we create a matrix of the order (m x n) while we actually wanted it to be of the
order (p x q) or just of any other order but (m x n).

RESIZING:

FLIPPING: To understand flipping about axes, first we should be able to understand axes. In
terms of MATLAB, up-down form the vertical axis while left-right form the horizontal axis.

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.

LOGICAL OPERATORS:
MATLAB offers two types of logical operators and functions −
• Element-wise − These operators operate on corresponding elements of logical arrays.
• Short-circuit − These operators operate on scalar and, logical expressions.
Element-wise logical operators operate element-by-element on logical arrays. The symbols
&, |, and ~ are the logical array operators AND, OR, and NOT.
Short-circuit logical operators allow short-circuiting on logical operations. The symbols &&
and || are the logical short-circuit operators AND and OR.

RESULT:

1) Concatenating:
2) Indexing:
3) Sorting:
4) Shifting:
5) Reshaping:
6) Flipping:
Creating Arrays X & Y of given size (1 x N):
(A): Relational Operations ( >, <, = =, <=, >=, ~= )
(B): Logical Operations ( ~, &, |, XOR )
EXPERIMENT NO. 03

OBJECTIVE: 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.

SOFTWARE USED: MATLAB

THEORY:
Matrices with one dimension equal to one and the other greater than one are called
vectors. Here is an example of a numeric vector:
A = [5.73 2-4i 9/7 25e3 .046 sqrt(32) 8j];
size(A) % Check value of row and column
dimensions ans =
1 7
We can construct a vector out of other vectors, as long as the critical dimensions agree. All
components of a row vector must be scalars or other row vectors. Similarly, all components
of a column vector must be scalars or other column vectors: A = [29 43 77 9
21]; B = [0 46 11];

C = [A 5 ones(1,3) B]
C=
29 43 77 9 21 5 1 1 1 0 46 11

RANDOM:

rand
:

Uniformly distributed random numbers and arrays SyntaxY = rand(n)


Y = rand(m,n)
Y = rand([m n])
Y = rand(m,n,p,...)
Y = rand([m n
p...])
Y =
rand(size())
s=rand('state')
The rand function generates arrays of random numbers whose elements are uniformly
distributed in the interval (0,1).

Y = rand(n) returns an n-by-n matrix of random entries.An error message appears if n


is not a scalar.

Y = rand(m,n) or Y = rand([m n]) returns an m-by-n matrix of random entries.


Y = rand(m,n,p,...) or Y = rand([m n p...]) generates random arrays.

Matrices with one dimension equal to one and the other greater than one are called vectors.
Here is an example of a numeric vector:
A = [5.73 2-4i 9/7 25e3 .046 sqrt(32) 8j];
size(A) % Check value of row and column
dimensions ans = 1 7
We can construct a vector out of other vectors, as long as the critical dimensions agree. All
components of a row vector must be scalars or other row vectors. Similarly, all components
of a column vector must be scalars or other column vectors: A = [29 43 77 9
21]; B = [0 46 11];

C = [A 5 ones(1,3) B]
C = 29 43 77 9 21 5 1 1 1 0 46 11

RESULT:

(A). Add up the values of the elements (Check with sum)


(B). Compute the Running Sum (Check with sum)
(C) Generating a Random Sequence using rand() / randn() functions and plot them.
EXPERIMENT NO. 04
OBJECTIVE: 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.

SOFTWARE USED: MATLAB

THEORY:
Round - Round to nearest integer
Syntax: Y = round(X)
Description: Y = round(X) rounds the elements of X to the nearest integers. For complex X,
the imaginary and real parts are rounded independently.

Floor - Round towards minus infinity


Syntax B = floor(A)
Description B = 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.

Ceil - Round toward infinity


Syntax: B = ceil(A)
Description: B = ceil(A) 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.

Fix - Round towards zero Syntax: B = fix(A)


Description: B = fix(A) rounds the elements of A toward zero, resulting in an array of
integers. For complex A, the imaginary and real parts are rounded independently.

Trigonometric Functions
The trigonometric functions are used along with their names and have the angle value as the
parameters in them. A range for the value of the parameter is defined to attain their graphical
representations.
Syntax: A = ‘trigonometric function name’(Value)
Ex. sin(30), cos(115), tan(-45)
RESULT:
OPEN ENDED QUESTIONS

QUESTION 1

OBJECTIVE: Write a function called flip-it that has one input argument, a row vector v,
and one output argument, a row vector w that is of the same length as v. The vector w
contains all the elements of v, but in the exact opposite order. For example, if v is equal to [1
2 3] then w must be equal to [3 2 1]. You are not allowed to use the built-in function flip. Use
function rot90.

SOFTWARE USED: MATLAB

THEORY: A vector is a one-dimensional array of numbers. MATLAB allows creating two


types of vectors − Row vectors are created by enclosing the set of elements in square
brackets, using space or comma to delimit the elements. Column vectors are created by
enclosing the set of elements in square brackets, using semicolon to delimit the elements.
Eg: A = [1 2 3]

Flipping a vector means flipping the elements of the vector, i.e., reversing the order in which
the elements are stored in the vector.

rot90: Matlab built_in function rot90 (A,k) can be used to rotate images in 90 degrees. Here
is an example using rot90: Assign K=1 for 90 degree, 2 for 180, 3 for 270 and 4 for 360. The
output image will be rotated 90 degrees. Another matlab built_in function flipud (A) can be
used to rotate the image 90 degrees.

FUNCTION:

RESULT:

>> A = [1 2 3]

A =

1 2 3

>> B = flip_it(A)

B =

3 2 1
QUESTION 2
OBJECTIVE: Write a function called top_right that takes two inputs: a matrix N and a
scalar non-negative integer n, in that order, where each dimension of N is greater than or
equal to n. The function returns the n-by-n square subarray of N located at the top right
corner of N.

SOFTWARE USED: MATLAB

THEORY: This function takes in a matrix N and a scalar n, and returns the n-by-n square
subarray located at the top right corner of N. The function first computes the dimensions of N
using the size function, and stores the number of rows in ~ and the number of columns in k.
Then, it uses indexing to extract the subarray at the top right corner, which starts at row 1 and
goes to row n, and starts at column k-n+1 and goes to column k. The subarray is stored in the
output variable T.

RESULT:

>> A = [4 5 6; 9 2 7; 5 4 3; 8 3 2]

A =

4 5 6
9 2 7
5 4 3
8 3 2

>> n = 2

n =

>> top_right(A,n)

ans =

5 6
2 7
QUESTION 3
OBJECTIVE: Input a one-dimensional array x of 10 elements with linspace command and
find y = x2.

SOFTWARE USED: MATLAB

THEORY: The linspace function in MATLAB is used to generate a sequence of evenly


spaced numbers over a specified interval. It takes three arguments: linspace(start, end, num),
where start is the first value in the sequence, end is the last value in the sequence, and num is
the number of evenly spaced values to generate. The linspace function returns a row vector
with num equally spaced values between start and end.
For example:
>> linspace(0,1,5)

Will return the row vector:

0 0.25 0.5 0.75 1

which are 5 evenly spaced values between 0 and 1.

RESULT:

>> x = linspace(0,9,10)

x =

0 1 2 3 4 5 6 7 8 9

>> y = x.^2

y =

0 1 4 9 16 25 36 49 64 81

>> plot(x,y)
QUESTION 4
25 1 −1
OBJECTIVE: Compute the following and compare results 25 −1
and (1 − 25 ) .

SOFTWARE USED: MATLAB

THEORY AND RESULT:

To compute the given expressions in MATLAB, we can simply type the expressions into the
command window and press enter. MATLAB will automatically calculate the results.

As we can see, the results of the two expressions are the same, with the value of
approximately 1.0323.
Q.1- Draw a circle of unit Radius.
Q.2- Plot y=sin x, 0 ≤ x ≤ 2π, taking 100 linearly spaced points in
the given interval. Label the axes and put ‘Sine Plot’ in the title.
Q.3- Make the same plot as above , but rather than displaying the
graph as a curve , show the unconnected data points . To display the
data points with small circles , use plot (x, y,’o’). Two plots can also be
combined with the commands plot(x,y,x,y,’o’).
Q.4- Plot y=e-0.4x sinx, 0 ≤ x ≤ 4π, taking 10, 50, and 100 points interval.

For 10
For 50
For 100
Q.5- Use the command plot3(x,y,z)to plot the circular helix x(t)=sin t,
y(t)= cos t, z(t)=t, 0≤ t≤ 20.

You might also like