Basic Simulation Lab: Amity School of Engineering Amity University - UTTAR PRADESH
Basic Simulation Lab: Amity School of Engineering Amity University - UTTAR PRADESH
Mr. Yogesh
INDEX
BASIC SIMULATION LAB
1 Creating a One-Dimensional Array (Row / Column Vector) ; Creating a Two- Dimensional PRACTICAL
Array (Matrix of given size) and (A). Performing Arithmetic Operations - Addition,
Subtraction, Multiplication and Exponentiation. (B). Performing Matrix operations - Inverse,
Transpose, Rank.
(B). Compute the Running Sum (Check with sum), where Running Sum for element j = the
sum of the elements from 1 to j, inclusive.
Also, Generating a Random Sequence using rand() / randn() functions and plotting them.
4 Evaluating a given expression and rounding it to the nearest integer value using Round, Floor, PRACTICAL
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 PRACTICAL
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 (say, 100Hz) and Plotting with Graphical PRACTICAL
Enhancements - Titling, Labeling, Adding Text, Adding Legends, Adding New
Plots to Existing Plot, Printing Text in Greek Letters, Plotting as Multiple and Subplot.
8
7
Solving First Order Ordinary Differential Equation using Built-in Functions. PRACTICAL
9
Writing brief Scripts starting each Script with a request for input (using input) to Evaluate PRACTICAL
the function h(T) using if-else statement, where
1
0 h(T) = (T - 10) for 0 < T < 100
Exercise : Testing the Scripts written using A). T = 5, h = -5 and B). T = 110, h =949.5
PRACTICAL
9: Generating a Square Wave from sum of Sine Waves of certain Amplitude and
Frequencies.
PRACTICAL
OPEN ENDED
EXPERIMENT
EXPERIMENT NO.1
AIM:Creating a One-Dimensional Array (Row / Column Vector); Creating a Two- Dimensional
Array (Matrix of given size) and
(A). Performing Arithmetic Operations - Addition, Subtraction, Multiplication and Exponentiation.
(B). Performing Matrix operations - Inverse, Transpose, Rank.
THEORY
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.
PROCEDURE:
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)
EXPERIMENT NO. 2
AIM: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
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
Arithmetic Operators
PROCEDURE:
Operator Description
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.
.* For no scalar 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.
Backslash or matrix left division. If A is a square matrix, A\B is roughly the same s
\ inv(A)*B, except it is computed in a different way. If A is an n-by-n matrix and B is
column vector with n components, or a matrix with several such columns, then X
= \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.
.
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.
EXPERIMENT NO. 3
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 andpseudo independent. You can use these numbers as if they are
truly random and independent. One benefit of using pseudorandom, pseudo independent
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 ran perm, 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
:
There are four fundamental random number functions: rand, Randi, randn,
Andran perm. The rand function returns real numbers between 0 and 1 that are drawn from a
uniform Distribution. For example, r1 = rand (1000, 1); r1 is a 1000-by-1 column vector
containing real floating-point numbers drawn from a uniform distribution. All the values in r1
are in the open interval, (0, and 1). A histogram of these values is roughly flat, which indicates a
fairly uniform sampling of numbers.
The Randi function returns double integer values drawn from a discrete uniform
Distribution. For example,
r2 = Randi (10, 1000, 1);
r2 is a 1000-by-1 column vector containing integer values drawn from a discrete uniform
distribution whose range is 1, 2, and 10. A histogram of these values is roughly flat, which
indicates a fairly uniform sampling of integers between 1 and 10.
The randn function returns arrays of real floating-point numbers that are drawn from a standard
normal distribution. For example, r3 = randn (1000, 1); r3 is a 1000-by-1 column vector containing
numbers drawn from a standard normal distribution. A histogram of r3looks like a roughly
normal distribution whose mean is 0 and standard deviation is 1.
You can use the ran perm function to create arrays of random integer values that have no repeated
values. For example, r4 = ran perm (15, 5); r4 is a 1-by-5 array containing randomly selected integer
values on the closed interval, [1, 15]. Unlike Randi, which can return an array containing repeated
values, the array returned by ran perm has no repeated values.
Successive calls to any of these functions return different results. This behavior is useful for creating
several different arrays of random values.
EXPERIMENT NO. 4
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) (B) Logarithmic and other Functions - log (A), log10 (A), Square root of A, Real nth root of A.
THEORY:
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.
Y = round(X) rounds the elements of X to the nearest integers. For complex X, the imaginary and real
parts are rounded independently.
Examples
a = [-1.9, -0.2, 3.4, 5.6, 7.0, 2.4+3.6i]
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
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
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
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.
Examples
a=
Columns 1 through 4
-1.9000 -0.2000 3.4000 5.6000
Columns 5 through 6
7.0000 2.4000 + 3.6000i
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:
To plot the graph of a function, you need to take the following steps:
1. Define x, by specifying the range of values for the variable x, for which
the function is to be plotted.
2. Define the function, y = f(x).
3. Call the plot command, as plot(x, y).
EXPERIMENT NO. 5
THEORY:
The exp function is an elementary function that operates element-wise on arrays. Its domain includes
complex numbers.Y = exp(X) returns the exponential for each element of X. For complex, it returns
the complex exponential
PROCEDURE:
To plot the graph of a function, you need to take the following steps:
1. Define x, by specifying the range of values for the variable x, for which the function is to be
plotted.
2. Define the function, y = f(x).
3. Call the plot command, as plot(x, y).
EXPERIMENT 5B
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.
EXPERIMENT NO. 6
AIM: Generating a Sinusoidal Signal of a given frequency (say, 100Hz) and 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.
THEORY:
The sin function operates element-wise on arrays. The function's domains and ranges include
complex values. All angles are in radians. Y = sin(X) returns the circular sine of the
elements of X.
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.
1. The xlabel and ylabel commands generate labels along x-axis and y-axis.
2. The title command allows you to put a title on the graph.
3. The grid on command allows you to put the grid lines on the graph.
4. The axis equal command allows generating the plot with the same scale
factors and the spaces on both axes.
5. The axis square command generates a square plot.
6. Setting Colors on Graph
MATLAB provides eight basic color options for drawing graphs. The
following table shows the colors and their codes:
Color Code
White w
Black k
Blue b
Red r
Cyan c
Green g
S
e
t
t
i
n
g
A
x
i
s
S
c
a
l
e
s
The axis command allows you to set the axis scales. You can provide minimum
and maximum values for x and y axes using the axis command in the following
way:
axis ( [xminxmaxyminymax] )
Generating Sub-Plots
When you create an array of plots in the same figure, each of these plots is called
a subplot. Thesubplot command is for creating subplots.
Syntax for the command is:
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
To plot the graph of a function, you need to take the following steps:
1. Define x, by specifying the range of values for the variable x, for which the function is
to be plotted.
2. Define the function, y = f(x).
3. Call the plot command, as plot(x, y).
EXPERIMENT NO-7
AIM:Solving First, Second and third Order Ordinary Differential Equation using Built-in
Functions and plot.
THEORY:
MATLAB provides thedsolve command for solving differential equations symbolically.
The most basic form of thedsolve command for finding the solution to a single equation is :
dsolve('eqn')
whereeqn is a text string used to enter the equation.
It returns a symbolic solution with a set of arbitrary constants that MATLAB labels C1, C2,
and so on. 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)'
Higher derivatives are indicated by following D by the order of the derivative.
For example the equation f"(x) + 2f'(x) = 5sin3x should be entered as:
s = dsolve('Dy = 5*y')
s=
C2*exp(5*t)
PROCEDURE:
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).
EXPERIMENT NO. 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, h(T) = (T – 10) for 0 < T < 100 = (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
THEORY:
Program files can be scripts that simply execute a series of MATLAB® statements, or they can
be functions that also accept input arguments and produce output.
Both scripts and functions contain MATLAB code, and both are stored in text files with
a .m extension. However, functions are more flexible and more easily extensible. MATLAB
expression, usually consist of variables or smaller expressions joined by relational operators
(e.g., count < limit), or logical functions (e.g., isreal(A)).
Simple expressions can be combined by logical operators (&&, ||, ~) into compound
expressions such as the following. MATLAB evaluates compound expressions from
left to right, adhering to operator precedence rules.
(count < limit) && ((height - offset) >= 0)
Nested if statements must each be paired with a matching end. The if function can be used alone
or with the else and elseif functions. When using elseif and/or else within an if statement, the
general form of the statement is
if expression1
statements1
elseif expression2
statements2
else
statements3
end
PROC
EDUR
E:
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.
Once an else if succeeds, none of the remaining else if's or else's will be
tested.
EXPERIMENT NO. 09
AIM:Generating a Square Wave from sum of Sine Waves of certain Amplitude and
Frequencies