Basic Simulation Lab: Amity University
Basic Simulation Lab: Amity University
TECHNOLOGY
AMITY UNIVERSITY
----------- UTTAR PRADESH-----------
Shubhangi Singhal
A2305216321
4CSE5-X
INDEX
EXPERIMENT NO 1
Theory:
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).
Suggested Program:
b = [10;20;30;40;50;60;70]
c = [1 2 3; 4 5 6; 7 8 9];
d = c;
a = b;
c + d;
c - d;
c * d;
inv(c);
rank(c);
rank(a);
rank(b);
rank(d);
2^5;
1000000000 ^1000000;
a = [1 2 3 4 5];
b = a;
b.^2;
b.^3;
Output:
Ans (c+d) = 2 4 6
8 10 12
14 16 18
Ans (c-d) = 000
000
000
ans (c*d) = 30 36 42
66 81 96
102 126 150
Ans (inv(c)) = 1.0e+016 *
-0.4504 0.9007 -0.4504
0.9007 -1.8014 0.9007
-0.4504 0.9007 -0.4504
Ans (rand(c)) = 2
Ans (rank(a)) = 1
Ans (rank(b)) = 1
Ans (rank(d)) = 2
Ans (2^5) = 32
Ans (100… ^ 100…)= Inf
Ans (b.^2) = 1 4 9 16 25
Ans (b.^3) = 1 4 27 256 3125
EXPERIMENT NO 2
*
For nonscalar 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.
Operator Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== 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.
● Short-circuit - these operators operate on scalar, 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.
Bitwise Operations
Bitwise operator works on bits and performs bit-by-bit operation. The truth
tables for &, |, and ^ are as follows:
p Q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume if A = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
MATLAB provides various functions for bit-wise operations like 'bitwise and',
'bitwise or' and 'bitwise not' operations, shift operation, etc.
The following table shows the commonly used bitwise operations:
Function Purpose
bitand(a, b) Bit-wise AND of integers a and b
bitcmp(a) Bit-wise complement of a
bitget(a,pos) Get bit at specified position pos, in the integer array a
bitor(a, b) Bit-wise OR of integers a and b
bitset(a, pos) Set bit at specific location pos of a
Returns a shifted to the left by k bits, equivalent to
multiplying by 2k. Negative values of k correspond to shifting
bitshift(a, k) bits right or dividing by 2|k| and rounding to the nearest
integer towards negative infinite. Any overflow bits are
truncated.
bitxor(a, b) Bit-wise XOR of integers a and b
swapbytes Swap byte ordering
Set Operations
MATLAB provides various functions for set operations, like union,
intersection and testing for set membership, etc.
The following table shows some commonly used set operations:
Function Description
Set intersection of two arrays; returns the values common
intersect(A,B)
to both A and B. The values returned are in sorted order.
Treats each row of A and each row of B as single entities
intersect(A,B,'rows') and returns the rows common to both A and B. The rows
of the returned matrix are in sorted order.
Returns an array the same size as A, containing 1 (true)
ismember(A,B) where the elements of A are found in B. Elsewhere, it
returns 0 (false).
Treats each row of A and each row of B as single entities
ismember(A,B,'rows') and returns a vector containing 1 (true) where the rows of
matrix A are also rows of B. Elsewhere, it returns 0 (false).
Returns logical 1 (true) if the elements of A are in sorted
order and logical 0 (false) otherwise. Input A can be a
issorted(A) vector or an N-by-1 or 1-by-N cell array of strings. A is
considered to be sorted if A and the output of sort(A) are
equal.
Returns logical 1 (true) if the rows of two-dimensional
matrix A are in sorted order, and logical 0 (false)
issorted(A, 'rows')
otherwise. Matrix A is considered to be sorted if A and the
output of sortrows(A) are equal.
Set difference of two arrays; returns the values in A that
setdiff(A,B) are not in B. The values in the returned array are in sorted
order.
Treats each row of A and each row of B as single entities
and returns the rows from A that are not in B. The rows of
setdiff(A,B,'rows') the returned matrix are in sorted order.
The 'rows' option does not support cell arrays.
Output:
>> c = [a;b]
c=
1 2 3
4 5 6
4 5 6
7 8 9
>> c = [a b]
c=
1 2 3 4 5 6
4 5 6 7 8 9
>> c(1)
ans =
1
>> c(1,6)
ans =
6
>> magic(4)
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> x = magic(4);
>> y = rand(4,4)
y=
0.9501 0.8913 0.8214 0.9218
0.2311 0.7621 0.4447 0.7382
0.6068 0.4565 0.6154 0.1763
0.4860 0.0185 0.7919 0.4057
>> z = rand(4,4)
z=
0.9355 0.0579 0.1389 0.2722
0.9169 0.3529 0.2028 0.1988
0.4103 0.8132 0.1987 0.0153
0.8936 0.0099 0.6038 0.7468
>> y1 = rand(4,4)
y1 =
0.4451 0.8462 0.8381 0.8318
0.9318 0.5252 0.0196 0.5028
0.4660 0.2026 0.6813 0.7095
0.4186 0.6721 0.3795 0.4289
>> w = magic(4)
w=
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> x-w
ans =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> y-y1
ans =
0.5050 0.0451 -0.0167 0.0900
-0.7007 0.2369 0.4251 0.2354
0.1408 0.2538 -0.0658 -0.5332
0.0673 -0.6536 0.4125 -0.0232
>> x(1,:)
ans =
16 2 3 13
>> x(2:3,2:4)
ans =
11 10 8
7 6 12
>> sort(x)
ans =
4 2 3 1
5 7 6 8
9 11 10 12
16 14 15 13
>> sort(x,1)
ans =
4 2 3 1
5 7 6 8
9 11 10 12
16 14 15 13
>> sort(x,2)
ans =
2 3 13 16
5 8 10 11
6 7 9 12
1 4 14 15
>> sort(x,'descend')
ans =
16 14 15 13
9 11 10 12
5 7 6 8
4 2 3 1
>> reshape(x,2,8)
ans =
16 9 2 7 3 6 13 12
5 4 11 14 10 15 8 1
>> reshape(x,8,2)
ans =
16 3
5 10
9 6
4 15
2 13
11 8
7 12
14 1
>> reshape(x,1,16)
ans =
16 5 9 4 2 11 7 14 3 10 6 15 13 8 12 1
>> reshape(x,16,1)
ans =
16
5
9
4
2
11
7
14
3
10
6
15
13
8
12
1
>> x
x=
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> rot90(x)
ans =
13 8 12 1
3 10 6 15
2 11 7 14
16 5 9 4
>> rot90(x,1)
ans =
13 8 12 1
3 10 6 15
2 11 7 14
16 5 9 4
>> rot90(x,2)
ans =
1 15 14 4
12 6 7 9
8 10 11 5
13 3 2 16
>> rot90(x,3)
ans =
4 9 5 16
14 7 11 2
15 6 10 3
1 12 8 13
>> fliplr(x)
ans =
13 3 2 16
8 10 11 5
12 6 7 9
1 15 14 4
>> flipud(x)
ans =
4 14 15 1
9 7 6 12
5 11 10 8
16 2 3 13
>> flipdim(x,1)
ans =
4 14 15 1
9 7 6 12
5 11 10 8
16 2 3 13
>> flipdim(x,2)
ans =
13 3 2 16
8 10 11 5
12 6 7 9
1 15 14 4
>> x > y
ans =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
>> x < y
ans =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> x >= y
ans =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
>> x <= y
ans =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> x == y
ans =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> x ~= y
ans =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
>> and(x,y)
ans =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
>> x | y
ans =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
>> ~x
ans =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> xor(x,y)
ans =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
EXPERIMENT 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 and pseudoindependent. You can use these numbers
as if they are truly random and independent. One benefit of using
pseudorandom, pseudoindependent 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 randperm, 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,
and randperm. 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, 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);
SUGGESTED PROGRAM:
x = rand(1,100);
plot(x)
x = magic(4)
sum(x)
sum(x,1)
sum(x,2)
y = [1:10]
cumsum(y)
cumsum(x)
cumsum(x,2)
mn = size(x);
SUM = 0;
for i = 1:mn(2)
ROWSUM = 0;
for j = 1:mn(1)
ROWSUM = x(i,j) + ROWSUM;
SUM = x(i,j) + SUM;
end
ROWSUM
end
SUM
OUTPUT
x=
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
ans =
34 34 34 34
ans =
34 34 34 34
ans =
34
34
34
34
y=
1 2 3 4 5 6 7 8 9 10
ans =
1 3 6 10 15 21 28 36 45 55
ans =
16 2 3 13
21 13 13 21
30 20 19 33
34 34 34 34
ans =
16 18 21 34
5 16 26 34
9 16 22 34
4 18 33 34
ROWSUM =
34
ROWSUM =
34
ROWSUM =
34
ROWSUM =
34
SUM =
136