Вы находитесь на странице: 1из 41

Practical File

Course Title: Basic Simulation Lab


Course Code: ES-204
Credit Units:01

Department of Computer Science and Engineering


Amity Institute of Engineering and Technology
Uttar Pradesh, Noida

Name: Hardik Garg


Class:4CSE1Y
Enrolment Number: A2305218045
Experiment-1
Aim: 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.
Software Used: MATLAB 7.0
Introduction:
1.Addition: C=A+B, this will first add two matrices A and B and then return the result to the
matrix C
2.Subtraction: C=A-B, this will return the result of diffrence of two matrices A and B to the
matrix C
3.Multiplication: C=A.*B, it multiplies the corrosponding elements of matrices A and B
4.Division: C=A./B, it divides corrosponding elements of A and B
5.Exponentiation: exp(A), computes the matrix exponential of A
6.Inverse: inv(A), returns inverse of square matrix A
7.Transpose: transpose(A), simply returns a matrix in which rows and columns are
interchanged with each other
8. Rank: rank(A), returns number of independent rows or columns in a matrix
Code:
clc
clear all
close all
x=[1,4,7;2,5,8;3,6,9];
display(x);
y=[1,2,3;4,5,6;7,8,9];
display(y);
add=x+y;
display(add);
sub=x-y;
display(sub);
a=[1,2,3;4,5,6;7,8,9];
display(a);
b=[7,8,9;6,5,4;1,2,3];
display(b);
mul=a.*b;
display(mul);
div=a./b;
display(div);
e1=exp(a);
display(e1);
Output:
x=
1 4 7
2 5 8
3 6 9
y=
1 2 3
4 5 6
7 8 9
add =
2 6 10
6 10 14
10 14 18
sub =
0 2 4
-2 0 2
-4 -2 0
a=
1 2 3
4 5 6
7 8 9
b=
7 8 9
6 5 4
1 2 3
mul =
7 16 27
24 25 24
7 16 27
div =
0.1429 0.2500 0.3333
0.6667 1.0000 1.5000
7.0000 4.0000 3.0000
e1 =
1.0e+03 *
0.0027 0.0074 0.0201
0.0546 0.1484 0.4034
1.0966 2.9810 8.1031

Code:
clc
clear all
close all

x=[12,41,73;25,54,84;32,61,29];
display(x);
y=[51,92,83;64,85,66;97,58,29];
display(y);
i=inv(x);
display(i);
a=inv(y);
display(a);
t=transpose(x);
display(t);
z=transpose(y);
display(z);
r=rank(x);
display(r);
f=rank(y);
display(f);
Output
x=
12 41 73
25 54 84
32 61 29
y=
51 92 83
64 85 66
97 58 29
i=
-0.1549 0.1421 -0.0217
0.0855 -0.0866 0.0356
-0.0088 0.0253 -0.0164
a=
0.0495 -0.0780 0.0357
-0.1652 0.2388 -0.0707
0.1647 -0.2168 0.0564
t=
12 25 32
41 54 61
73 84 29
z=
51 64 97
92 85 58
83 66 29
r=3
f=3
Experiment-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 (A). Relational Operations - >, <, ==, <=, >=, ~= (B).
Logical Operations - ~, &, |, XOR
Software Used: MATLAB 7.0
Introduction:
1.Concatenating: C = cat(dim,A,B) concatenates B to the end of A along dimension dim
when A and B have compatible sizes
2.Indexing: used to access any random elements of a matrix
3.Sorting: sort(A), sorts elements of A in ascending order, sort(A,2) will sort elements of A in
ascending order row wise
4.Sortrows: sort elements of matrix row wise, by default it compare elements of first
column
5.Reshaping: done using function rot90(A,n), it rotates the rows of A counterclockwise by
(90*n)degrees, by default n=1
6.Shifting: circshift(A,1), shifts rows of A anticlockwise or downwards if we write (A,[0,-1]), it
will shift columns of A clockwise or upwards and if write (A,[1,1]), first shifts rows and then
shifts columns of resultant matrix
7.Fliping: flips rows about a horizontal axis or flips columns about a vertical axis
Code:
clc
clear all
close all
a=[1,2,3;4,5,6;7,8,9];
display(a);
b=[7,8,9;6,5,4;1,2,3];
display(b);
c=cat(2,a,b);
display(c);
display(a(3,2:3));
s1=sort(a);
s2=sort(a,2);
display(s1);
display(s2);
s3=sortrows(a);
s4=sortrows(b,3);
display(s3);
display(s4);
r1=rot90(a);
r2=rot90(b,-1);
r3=rot90(a,2);
display(r1);
display(r2);
display(r3);
c1=circshift(a,1);
c2=circshift(b,-1);
c3=circshift(a,[0,-1]);
c4=circshift(a,[1,1]);
display(c1);
display(c2);
display(c3);
display(c4);
f1=flipud(a);
display(f1);
f2=fliplr(b);
display(f2);
Output:
a=
1 2 3
4 5 6
7 8 9
b=
7 8 9
6 5 4
1 2 3
c=

1 2 3 7 8 9
4 5 6 6 5 4
7 8 9 1 2 3
ans =
8 9
s1 =
1 2 3
4 5 6
7 8 9
s2 =
1 2 3
4 5 6
7 8 9
s3 =
1 2 3
4 5 6
7 8 9
s4 =
1 2 3
6 5 4
7 8 9
r1 =
3 6 9
2 5 8
1 4 7
r2 =
1 6 7
2 5 8
3 4 9
r3 =
9 8 7
6 5 4
3 2 1
c1 =
7 8 9
1 2 3
4 5 6
c2 =
6 5 4
1 2 3
7 8 9
c3 =
2 3 1
5 6 4
8 9 7
c4 =
9 7 8
3 1 2
6 4 5
f1 =
7 8 9
4 5 6
1 2 3
f2 =
9 8 7
4 5 6
3 2 1
Logical Operation:
1.OR: a|b, produces a matrix in which element is 1 only when there is one in any of two
matrices or in both the matrices else gives zero
2.AND: a&b,produces a matrix in which element is one if both elements in a and b is one,
otherwise gives zero
3.NOT: ~a, it changes the 1 by 0 and 0 by 1 in the matrix a and generates a new matrix
4.XOR: xor(a,b), gives 1 as output when both elements in a and b are not nonzero or when
both elements are not zero I.e. when both are not same
Code:
clc
clear all
close all
x=[1,0,1;0,0,1;1,1,1];
y=[0,1,0;1,1,1;0,0,1];
I=xor(x,y);
display(x);
display(y);
display(I);
o=or(y,x);
display(o);
a=and(x,y);
display(a);
n1=not(x);
n2=not(y);
display(n1);
display(n2);
Output:
x=
1 0 1
0 0 1
1 1 1

y=
0 1 0
1 1 1
0 0 1
I=
1 1 1
1 1 0
1 1 0
o=
1 1 1
1 1 1
1 1 1
a=
0 0 0
0 0 1
0 0 1
n1 =
0 1 0
1 1 0
0 0 0
n2 =
1 0 1
0 0 0
1 1 0
Relational Operation:
Code:
clc
clear all
close all
x=[3,3];
y=[1,5];
display(x>y);
display(x<y);
display(x==y);
display(x~=y);

Output:
1×2 logical array
1 0
1×2 logical array
0 1
1×2 logical array
0 0
1×2 logical array
1 1
Experiment 3
Aim: 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 7.0

Introduction:
1.Sum: sum(a),returns a row vector containing sum of each columns in matrix;
sum(a,2),returns a column vector containing sum of each row of matrix

2.Cumsum: cumsum(a),returns a matrix which contains the cummulative sum of elements


of each column in the matrix; cumsum(a,2),returns a matrix containing cummulative sum of
elements of each rows in a matrix

3.Length: length() is a predefined function which returns the size of an array

4.Rand: it is also a predefined function in MATLAB which return any number between 0 and
1, rand(n,m), will return a n*m matrix having random numbers between 0 and 1

5.Randi(max): returns any integer between 1 and max; randi(10,3); return a 3*3 matrix
containing random integers less than 10

6.Randn: randn returns a random scalar drawn from the standard normal
distribution,randn(n) returns a n*n matrix

7.Randperm: randperm(n,k) returns a row vector containing k unique integers selected


randomly from 1 to n

A. Sum
Code:

clc

clear all

close all

a=[1,2,3;4,5,6;7,8,9];
display(a);

s1=sum(a);

display(s1);

s2=sum(a,2);

display(s2);

s3=cumsum(a);

s4=cumsum(a,2);

display(s3);

display(s4);

Output:

>> deepak65

a=

1 2 3

4 5 6

7 8 9

s1 =

12 15 18

s2 =

15

24

s3 =

1 2 3

5 7 9

12 15 18

s4 =
1 3 6

7 9 15
7 15 24

Running sum
Code:

a=[4,5,6,8,5,6,8,5,2];

l=length(a);

sum=0;

for i=1:l

sum=sum+a(i);

end

display(sum);

Output:

>> deepakrunsum

sum =

49

Random Num
Code:

r1=rand(3,3);

display(r1);

r2=randi(4,3);

display(r2);

r3=randperm(6,3);

display(r3);

r4=randn(3);

display(r4);
subplot(2,2,1);

plot(r1);

title('Rand');

subplot(2,2,2);

plot(r2);

title('Randi');

subplot(2,2,3);

plot(r3);

title('Randperm');

subplot(2,2,4);

plot(r4);

title('Randn');

Output:

>> deepakrndm

r1 =
0.2373 0.5468 0.4889
0.4588 0.5211 0.6241
0.9631 0.2316 0.6791

r2 =
2 1 4
2 4 1
4 4 2

r3 =
1 3 2

r4 =

-0.0044 1.0826 0.2571


0.6524 1.0061 -0.9444
0.3271 -0.6509 -1.3218
Experiment – 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) Logarithmic and other Functions – log(A), log10(A), Square root of A, Real nth root of A.

Introduction:

1. Round: Round to nearest decimal or integer


2. Floor: Round toward negative infinity; Y = floor(X) rounds each element of X to the
nearest integer less than or equal to that element.
3. Ceil: ceil(X) rounds each element of X to the nearest integer greater than or equal to
that element
4. Fix: fix(X) rounds each element of X to the nearest integer toward zero.
Code:

clc

clear all

close all

a=[-0.2,5.5,1.9,0,5.7];

display(a);

r1=round(a);

display(r1);

display(ceil(a));

display(floor(a));

display(fix(a));

Output:

a=

-0.2000 5.5000 1.9000 0 5.7000

r1 =

0 6 2 0 6
ans =

0 6 2 0 6

ans =

-1 5 1 0 5

ans =

0 5 1 0 5

A. Trignometric Functions:
x1=-2*pi:pi/10:2*pi;

y1=sin(x);

subplot(2,3,1);

plot(x1,y1,'r');

xlim([-5,5]);

ylim([-2,2]);

grid on;

title('Sin function');

x2=-2*pi:pi/10:2*pi;

y2=cos(x2);

subplot(2,3,2);

plot(x2,y2,'m*');

xlim([-5,5]);

ylim([-2,2]);

title('Cos function');

x3=-2*pi:pi/10:2*pi;

y3=tan(x3);

subplot(2,3,3);
plot(x3,y3,'k-');

xlim([-5,5]);

ylim([-2,2]);

title('Tan function');

x4=-2*pi:pi/10:2*pi;

y4=cot(x4);

subplot(2,3,4);

plot(x4,y4,'g');

xlim([-5,5]);

ylim([-2,2]);

title('Cot Function');

x5=-2*pi:pi/10:2*pi;

y5=csc(x5);

subplot(2,3,5);

plot(x5,y5,'b');

xlim([-5,5]);

ylim([-2,2]);

title('Cosec function');

x6=-2*pi:pi/10:2*pi;

y6=sec(x6);

subplot(2,3,6);

plot(x6,y6,'c');

xlim([-5,5]);

ylim([-2,2]);

title('Sec function');

grid on;
Output:

Code( 2nd Option):

clc

clear all

close all

x=-2*pi:pi/10:2*pi;

y=sin(x);

plot(x,y,'k');

xlim([-5,5]);

ylim([-2,2]);

xlabel('time');

hold on
y2=cos(x);

plot(x,y2,'r');

y3=sec(x);

plot(x,y3,'g');

y4=csc(x);

plot(x,y4,'y');

y5=cot(x);

plot(x,y5,'C');

y6=tan(x);

plot(x,y6,'m');

grid on;

legend('sin','cos','sec','cosec','cot','tan');

hold off;

Output:
Experiment-5

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

Software Used: Matlab R2015b

Theory: In MATLAB a vector is a matrix with either one row or one column. The distinction
between row vectors and column vectors is essential. Many programming errors are caused
by using a row vector where a column vector is required, and vice versa. MATLAB vectors
are used in many situations, e.g., creating x-y plots, that do not fall under the rubric of linear
algebra. In these contexts a vector is just a convenient data structure. MATLAB still enforces
the rules of linear algebra so paying attention to the details of vector creation and
manipulation is always important.

POWER:

C = A.^B raises each element of A to the corresponding powers in B. The sizes of A and B
must be the same or be compatible. If the sizes of A and B are compatible, then the two
arrays implicitly expand to match each other. For example, if one of A or B is a scalar, then
the scalar is combined with each element of the other array. Also, vectors with different
orientations (one row vector and one column vector) implicitly expand to form a matrix.

Code:
clc;

clear;

close;

s=0;

for n=1:100;

x=(-1)^(n+1)/(2*n-1);

s=s+x;

end

display(s);

x=0:4;
y1=x;

y2=x.^3;

y3=exp(x);

y4=exp(x.^2);

subplot(2,2,1);

plot(x,y1,'r');

xlim([0,4]);

title('y=x');

grid on;

subplot(2,2,2);

plot(x,y2,'g');

xlim([0,4]);

title('x^3');

grid on;

subplot(2,2,3);

plot(x,y3,'k');

xlim([0,4]);

title('e^x');

grid on;

subplot(2,2,4);

plot(x,y4,'m');

xlim([0,4]);

title('e^x^2');

grid on;

Output:
s=
0.7829
Experiment- 6
Aim: : 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.

Software Used: MATLAB R2015b

Theory:

text(x,y,str) adds a text description to one or more data points in the current axes using the
text specified by str. To add text to one point, specify x and y as scalars in data units. To add
text to multiple points, specify x and y as vectors with equal length.

text(___,Name,Value) specifies text object properties using one or more name-value pairs.
For example, 'FontSize',14 sets the font size to 14 points. You can specify text properties
with any of the input argument combinations in the previous syntaxes. If you specify the
Position and String properties as name-value pairs, then you do not need to specify the x, y,
z, and str inputs.

Code:

t=-2*pi:pi/10:2*pi;

f=3;

y=sin(2*pi*f*t);

plot(t,y,'r');

xlabel('time');

ylabel('sin(t)');

text(-1.570796,1,'Max Value','color','magenta','fontsize',15);

legend('Sin Function');

Output:
Experiment- 7

Aim: Solving First, Second and third Order Ordinary Differential Equation using Built-in
Functions and plot.

Software Used: MATLAB R2019b

Theory:

Syms- create symbolic variables and functions

Y = diff(X,n) calculates the nth difference by applying the diff(X) operator


recursively n times. In practice, this means diff(X,2) is the same as diff(diff(X)).

S = dsolve(eqn) solves the differential equation eqn, where eqn is a symbolic equation. Use
diff and == to represent differential equations. For example, diff(y,x) == y represents the
equation dy/dx = y. Solve a system of differential equations by specifying eqn as a vector of
those equations.

Code:

clc

clear all

close all

syms y(t) a b c

p=diff(y,t)==a*y

dsolve(p)

x=0:4;

plot(x,p,'r')

q=diff(y,t,2)==b*y

cond=[y(0)==1 p(0)==2]

dsolve(q,cond)

r=diff(y,t,3)==c*y
cond2=[q(0)==3 y(0)==1]

dsolve(r,cond2)

Output:

p(t) =

diff(y(t), t) == a*y(t)

ans =

C1*exp(a*t)

q(t) =

diff(y(t), t, t) == b*y(t)

cond =

[ y(0) == 1, (subs(diff(y(t), t), t, 0) == a*y(0)) == 2]

ans =

(exp(b^(1/2)*t)*(a + b^(1/2)))/(2*b^(1/2)) - (exp(-b^(1/2)*t)*(a - b^(1/2)))/(2*b^(1/2))

r(t) =

diff(y(t), t, t, t) == c*y(t)

cond2 =

[ (subs(diff(y(t), t, t), t, 0) == b*y(0)) == 3, y(0) == 1]

ans =

C1*exp(c^(1/3)*t) - (exp(-t*((3^(1/2)*c^(1/3)*1i)/2 + c^(1/3)/2))*(3^(1/2)*b*2i + 3*C1*c^(2/3) +


3^(1/2)*c^(2/3)*1i - 3*c^(2/3) - 3^(1/2)*C1*c^(2/3)*3i))/(6*c^(2/3)) +
(3^(1/2)*exp(t*((3^(1/2)*c^(1/3)*1i)/2 - c^(1/3)/2))*(2*b - 3*C1*c^(2/3) - 3^(1/2)*c^(2/3)*1i +
c^(2/3) + 3^(1/2)*C1*c^(2/3)*1i)*1i)/(6*c^(2/3))
EXPERIMENT 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

TOOLS USED: MATLAB 2019b

THEORY:

• Input can be taken from a user using the input function.


Syntax: X = input (‘Enter value of X:’);
It prompts user to enter value of X and takes input into variable X
• Conditions can be checked using if-elseif-else statement.
Syntax:
if (X<0)
disp(‘Negative’);
elseif (X>0)
disp(‘Positive’);
else
disp(‘Zero’);
end
MATLAB PROGRAM:
clc;
clear;
close;
T = input('Enter the value of T:'); %Taking Input from User
if (T>0) && (T<=100)
H = T-10;
disp('H=');
disp(H); %Displaying Value of H
elseif (T>100)
H = 0.45*T + 900;
disp('H=');
disp(H);
else
disp('Incorrect Value of T entered');
end % Ending if else statement

OUTPUT:
Experiment 9
Aim: Generating a Square Wave from sum of Sine Waves of certain Amplitude and
Frequencies.

Software Used: MATLAB 2019b

Theory:

➢ SQUARE WAVE

x = square(t) generates a square wave with period 2π for the elements of the time array t.
square is similar to the sine function but creates a square wave with values of –1 and 1. x =

square(t,duty) generates a square wave with specified duty cycle duty.

➢ SINE WAVE

A sine wave or sinusoid is a mathematical curve that describes a smooth periodic oscillation.
A sine wave is a continuous wave. It is named after the function sine, of which it is the
graph. It occurs often in pure and applied mathematics, as well as physics, engineering,
signal processing and many other fields.

Code:
clc

clear all

close all

f=5;

w=2*pi*f;

t=0:0.001:1;

y=0;

for n=1:2:99;

y=y+(1/n)*sin(n*w*t);

end

subplot(2,2,1);

plot(t,y,'m');
xlabel('Time');

ylabel('Amplitude');

legend('Sin Wave');

title('Square Wave');

subplot(2,2,2);

stem(t,y);

xlabel('Time');

ylabel('Amplitude');

legend('Sin Wave');

title('Discrete wave');

Output:
Experiment-10
Aim: Basic 2D and 3D plots: parametric space curve. polygons with vertices. 3D contour lines, pie
and bar charts.

Software Used: MATLAB 2019b

Theory:

1. Plot3(X,Y,Z) plots coordinates in 3-D space.


To plot a set of coordinates connected by line segments, specify X, Y, and Z as
vectors of the same length.

To plot multiple sets of coordinates on the same set of axes, specify at least one of X,
Y, or Z as a matrix and the others as vectors.

2. Pie3(X) draws a three-dimensional pie chart using the data in X. Each element in X is
represented as a slice in the pie chart.

3. Bar3 draws a three-dimensional bar graph.


bar3(Z) draws a three-dimensional bar chart, where each element in Z corresponds
to one bar. When Z is a vector, the y-axis scale ranges from 1 to length(Z). When Z is
a matrix, the y-axis scale ranges from 1 to the number of rows in Z.
Code:

clc

clear all

close all

t=0:pi/50:10*pi;

st=sin(t);

ct=cos(t);

subplot(2,3,1);

plot3(st,ct,t);

title('3-D Plot');

x=0:pi/10:2*pi;

y=sin(x);

subplot(2,3,2);
pie(x,y);

title('Pie Chart');

subplot(2,3,3);

pie3(x,y);

title('Pie 3D Chart');

subplot(2,3,4);

bar(x,y);

title('Bar Graph');

subplot(2,3,5);

bar3(x,y);

title('Bar 3D Graph');

Output:
OPEN ENDED EXPERIMENT
Course Title: Basic Simulation Lab
Course Code: ES-204
Credit Units:01

Department of Computer Science and Engineering


Amity Institute of Engineering and Technology
Uttar Pradesh, Noida

Name: Hardik Garg


Class:4CSE1Y
Enrolment Number: A2305218045
OPEN ENDED EXPERIMENT

Aim: To covert coloured image to grayscale and then to black and white.

Software Used: MATLAB R2019b

Theory:

RGB = RGB('COLORNAME') returns the red-green-blue triple


corresponding to the color named COLORNAME by the CSS3 proposed
standard [1], which contains 139 different colors (an rgb triple is a 1x3 vector of
numbers between 0 and 1). The color names are the ones accepted by almost all
web browsers, for example Brown, DarkRed, SlateGray.

RGB CHART creates a figure window showing all the available colors with
their names.

COLORNAME = RGB(r,g,b) and COLORNAME = RGB([r,g,b]) both find the


name of the color with the triple that is closest to [r,b,g] (measured by sum of
squares).

A = imread(filename) reads the image from the file specified by filename,


inferring the format of the file from its contents. If filename is a multi-image
file, then imread reads the first image in the file.

rgb2gray converts RGB images to grayscale by eliminating the hue and


saturation information while retaining the luminance. If the input is an RGB
image, it can be of class uint8, uint16, single, or double. The output image I is
of the same class as the input image. If the input is a colormap, the input and
output colormaps are both of class double.

BW = im2bw(I,level) converts the grayscale image I to binary image BW, by


replacing all pixels in the input image with luminance greater than level with
the value 1 (white) and replacing all other pixels with the value 0 (black).
This range is relative to the signal levels possible for the image's class.
Therefore, a level value of 0.5 corresponds to an intensity value halfway
between the minimum and maximum value of the class.
Program:
clc
clear all
close all
%AIM:To read an image from folder and display in RGB,
Gray and Binary form.
img =
imread('C:\Users\HARDIK\Desktop\MATLAB\158840.jpg');
subplot(3,1,1);
imshow(img)
title('Color');
gray = rgb2gray(img);
subplot(3,1,2);
imshow(gray)
title('Grasyscale');
bin_img = im2bw(gray);
subplot(3,1,3);
imshow(bin_img);
title('Binary');
% Irgb =
%imread('C:\Users\HARDIK\Desktop\MATLAB\158840.jpg');
%threshold = 128;
%Igray = rgb2gray(Irgb);
%Ibw = Igray>threshold;
%in case image processing toolbox is not installed
Output: