0% found this document useful (0 votes)
11 views

Matlab Final

The document describes programs written in MATLAB to perform various tasks like sorting arrays, displaying graphs, image processing, and finding system details. It includes algorithms and code snippets to sort arrays in ascending and descending order, display different graph types using functions like mesh, surf etc. and to convert images between formats and extract RGB layers. It also shows how to get the host name and IP address programmatically in MATLAB.

Uploaded by

Reshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Matlab Final

The document describes programs written in MATLAB to perform various tasks like sorting arrays, displaying graphs, image processing, and finding system details. It includes algorithms and code snippets to sort arrays in ascending and descending order, display different graph types using functions like mesh, surf etc. and to convert images between formats and extract RGB layers. It also shows how to get the host name and IP address programmatically in MATLAB.

Uploaded by

Reshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

MAT LAB MCA 2016-2019, CMS CBE

EX.NO:1 SORT IN ASCENDING AND DESCENDING ORDER


DATE: 14.12.17

AIM

CREATE A PROGRAM TO SORT ROW, COLUMN WISE ASCENDING AND DECENDING


ORDER.

ALGORITHM

STEP 1: START THE PROCESS.

STEP 2: WINDOWS=>RUN=>CMD=>MATLAB.

STEP 3: GET THE VALUES IN THE 3 X 3 MATRIX AND PRINT IT THROUGH

VARIABLE

STEP 4: USE SORT(A,1) FUNCTION TO SORT THE NUMBER IN COLUMN WISE

STEP 5: SORT THE MATRIX IN ROW WISE USING SORT(A, 2) FUNCTION

STEP 6:TO SORT IN THE DESCENDING ORDER USE NEGATIVE SYMBOL FOR

ROW, COLUMN WISE.

STEP 7:SAVE AND RUN THE PROGRAM.

STEP 8: DISPLAY THE RESULT.

STEP 9:STOP THE PROCESS.

1
MAT LAB MCA 2016-2019, CMS CBE

CODING

fprintf('sorting a single dimensional array\n')

A = [78 23 10 100 45 5 6];


fprintf('original values of A \n ')
A
fprintf('Sorted list in ascending order\n ')
sort(A)

fprintf('Sorted list in desending order\n ')


%sort(A,'descend')
flipud(A)

fprintf('Sorting a 3x3 matrix \n ')


A=[3 7 5
6 8 3
0 4 2 ];
fprintf('original values of the matrix \n ')
A
fprintf('sorted matrix \n ')
sort(A)

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

fprintf('Sorted matrix column wise\n ')


sort(A,1)

fprintf('Sorted matrix row wise\n ')


sort(A,2)

fprintf('matrix sorted in descending order \n ')


sortrows(A,-2)

2
MAT LAB MCA 2016-2019, CMS CBE

OUTPUT

Sorting a single dimensional array

Original values of A

A=

78 23 10 100 45 5 6

Sorted list in ascending order

Ans =

5 6 10 23 45 78 100

Sorted list in desending order

Ans =

78 23 10 100 45 5 6

Sorting a 3x3 matrix

Original values of the matrix

A=

3 7 5

6 8 3

0 4 2

Sorted matrix

ans =

0 4 2

3 7 3

6 8 5

3
MAT LAB MCA 2016-2019, CMS CBE

Sorted matrix column wise

ans =

0 4 2

3 7 3

6 8 5

Sorted matrix row wise

ans =

3 5 7

3 6 8

0 2 4

Matrix sorted in descending order

ans =

6 8 3

3 7 5

0 4 2

>>

RESULT:
The above program is executed successfully and output is obtained

4
MAT LAB MCA 2016-2019, CMS CBE

EX.NO:2 DISPLAY THE DIFFERENT TYPE OF GRAPH USING MATLAB

DATE: 20.12.17

AIM

CREATE A PROGRAM TO DISPLAY THE DIFFERENT TYPE OF GRAPH USING


MATLAB.

ALGORITHM

STEP 1: START THE PROCESS.

STEP 2: WINDOWS=>RUN=>CMD=>MATLAB.

STEP 3: DECLARE TWO VARIABLE FOR X, Y CO-ORDINATE AND PASS THE

VALUES.

STEP 4: FIND THE SQUARE ROOT OF X,Y AND STORE THE RESULT IN A

SEPARATE VARIABLE R.

STEP 5: FIND THE SIN VALUE OF R AND PUT IT IN A NEW VARIABLE Z.

STEP 6: CREATE A WINDOW WITH 4 SUB PLOTS.

STEP 7: FIND THE SURFACE OF THOSE VALUES USING SURF () FUNCTION.

STEP 8: RUN THE PROGRAM.

STEP 9: DISPLAY THE GRAPH IN THE PLOT WITH DIFFERENT COLOR.

STEP10: STOP THE PROCESS.

5
MAT LAB MCA 2016-2019, CMS CBE

CODING

figure
[x,y]=meshgrid(-8:.5:8);
R=sqrt(x.^2+y.^2)+eps;
z=sin(R)./R;
subplot(2,2,1)
mesh(x,y,z,'EdgeColor','black')
subplot(2,2,2)
surf(x,y,z)
colormap cool
subplot(2,2,3)
surf(x,y,z)
colormap cool%
alpha(.4)
subplot(2,2,4)
surf(x,y,z,'faceColor','magenta','EdgeColor','none')
camlight left% headlight|right|left
lighting phong%or flat|goraud|phong

6
MAT LAB MCA 2016-2019, CMS CBE

OUTPUT

RESULT:
The above program is executed successfully and output is obtained

7
MAT LAB MCA 2016-2019, CMS CBE

EX.NO:3 SINE WAVE FUNCTION

DATE: 05.01.18

AIM:

CREATE A PROGRAM TO DISPLAY SINE WAVE FUNCTION USING MATLAB.

ALGORITHM

STEP 1: START THE PROCESS.

STEP 2: WINDOWS=>RUN=>CMD=>MATLAB.

STEP 3: DECLARE TWO VARIABLE X, Y;

STEP4: CALCULATE THE VALUE OF X, Y USING THE FORMULA X=-PI:PI/20:PI

AND Y=SIN(X).

STEP5: CREATE PLOT WITH AND PASS CALCULATED VALUES TO THE GRAPH.

STEP6:DISPLAY SIN VALUES AND PI VALUES IN A GRAPH

STEP 7:CALCULATE SIN2X VALUE AND PASS IT TO THE GRAPH TO DISPLAY

STEP 8:RUN THE PROGRAM.

STEP9:DISPLAY THE RESULT.

STEP10:STOP THE PROCESS.

8
MAT LAB MCA 2016-2019, CMS CBE

CODING

%subplot(2,1,1)
title('3 different waves')
x=-pi:pi/20:pi;
y=sin(x);
plot(x,y,'--gd')
hold on
x=-pi:pi/20:pi;
y=sin(x-pi);
plot(x,y,':bs')
grid on;
hold off
%subplot(2,1,2)
figure
title('sin wave with line properties')
x=-pi:pi/20:pi;
y=sin(2*x);
plot(x,y,'-mo',...
'LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)

9
MAT LAB MCA 2016-2019, CMS CBE

OUTPUT:

FIGURE 1

FIGURE 2

RESULT:
The above program is executed successfully and output is obtained

10
MAT LAB MCA 2016-2019, CMS CBE

EX.NO:4 DISPLAY IMAGE AS RGB LAYERS


DATE:20.01.18

AIM

CREATE A PROGRAM TO DISPLAY THE RED,GREEN,BLUE LAYERS OF AN


IMAGE.

ALGORITHM

STEP 1: START THE PROCESS.

STEP 2: WINDOWS=>RUN=>CMD=>MATLAB.

STEP 3: DECLARE A VARIABLE AND READ AN IMAGE USING IMREAD()

FUNCTION.

STEP 4:CREATE A WINDOW AND MAKE IT 4 PLOTS USING SUBPLOT().

STEP 5:CONVERT INTO THE LAYER WISE USING IMAGE(VARIABLENAME(:,:,1)

STEP 6:IMAGE(VARIABLENAME(:,:,1) FOR RED LAYER AND 2 FOR GREEN

LAYER AND 3 FOR BLUE LAYER.

STEP7:DISPLAY THE LAYER BASED IMAGE IN THE SEPARATE PLOT WITH

RELEVANT TITLE.

STEP 8:RUN THE PROGRAM.

STEP 9:DISPLAY THE RESULT.

STEP10:STOP THE PROCESS.

11
MAT LAB MCA 2016-2019, CMS CBE

CODING

myimage=imread('a.jpg');
subplot(2,2,1),image(myimage),title('it is a original image');
subplot(2,2,2),image(myimage(:,:,1)),title('red layer here');
subplot(2,2,3),image(myimage(:,:,2)),title('green layer here');
subplot(2,2,4),image(myimage(:,:,3)),title('blue layer here');

OUT PUT

RESULT:
The above program is executed successfully and output is obtained

12
MAT LAB MCA 2016-2019, CMS CBE

EX.NO:5 CONVERT COLOR IMAGE TO GRAYSCALE IMAGE

DATE:03.02.18

AIM

CREATE A PROGRAM TO CNVERT THE “BMP FILE” TO “JPEG FILE” USING


MATLAB

ALGORITHM

STEP 1: START THE PROCESS.

STEP 2: WINDOWS=>RUN=>CMD=>MATLAB.

STEP 3: DECLARE A VARIABLE AND LINK A STORED BMP IMAGE WITH PATH.

STEP 4:DECLARE ANOTHER VARIABLE AND READ THE IMAGE USINGIMREAD().

STEP 5: DISPLAY THE SAME PICTURE.

STEP 6: CONVERT THE PICTURE FROM COLOR TO BLACK & WHITE USING

RGB2 GRAY ().

STEP 7: FOLLOW THE ABOVE STEPS FOR JPG FILE WITH DIFFERENT VARIABLE.

STEP 8: DISPLAY THE CONVERTED IMAGE AS A DIFFERENT FILES.

STEP 9: DISPLAY THE RESULT.

STEP10: STOP THE PROCESS.

13
MAT LAB MCA 2016-2019, CMS CBE

CODING

% converting .bmp and .jpg file into greyscale and save the new file
A = 'D:\a.bmp';
B = imread(A,'bmp');
figure(1),imshow(B);
C = rgb2gray(B);
figure(2), imshow(C);
imwrite(C,'D:\ converted1.bmp','bmp')

D = 'D:\b.bmp';
E = imread(D,'bmp');
figure(3),imshow(E);
F = rgb2gray(E);
figure(4), imshow(F);

imwrite(F,'D:\converted.jpg','jpg')

14
MAT LAB MCA 2016-2019, CMS CBE

OUTPUT:

JPG IMAGE-FIGURE 1JPG IMAGE CONVERTED-FIGURE 2

JPG IMAGE-FIGURE 3JPG IMAGE CONVERTED-FIGURE4

RESULT:
The above program is executed successfully and output is obtained

15
MAT LAB MCA 2016-2019, CMS CBE

EXNO: 6 SYSTEM HOST NAME AND IP ADDRESS

DATE:15.02.18

AIM

CREATE A MATLAB PROGRAM TO FIND HOSTNAME AND IP ADDRESS

ALGORITHM

STEP 1: START THE PROCESS.

STEP 2: WINDOWS=>RUN=>CMD=>MATLAB.

STEP3: CREATE A USER DEFINED FUNCTION AND ALSO CREATE A THREAD TO

THROW IF ERROR OCCUR.

STEP 4: DECLARE A VARIABLE AND FIND THE HOST NAME USING.GETHOSTNAME

STEP 5:DECLARE ANOTHER VARIABLE AND FIND THE IP ADDRESS USING

GETHOSTADDRES.

STEP 6:STORE IP ADDRESS AND HOST NAME IN THE RELEVANT VARIABLE

STEP 7:RUN THE PROGRAM.

STEP 8:DISPLAY THE RESULT.

STEP9:STOP THE PROCESS.

16
MAT LAB MCA 2016-2019, CMS CBE

CODING:

function whoami

try
me = java.net.InetAddress.getLocalHost;
catch
error('unable to get local host address');
end

myname = me.getHostName;
myip = me.getHostAddress;

disp('My host name is');


disp(myname);
disp('My IP address is');
disp(myip);

17
MAT LAB MCA 2016-2019, CMS CBE

OUTPUT

RESULT:
The above program is executed successfully and output is obtained

18
MAT LAB MCA 2016-2019, CMS CBE

EX.NO: 7 DISPLAY THE JAVA DIALOGUE BOX

DATE: 21.02.18

AIM

CREATE A PROGRAM TO CREATE JAVA WINDOW ONE MAKE IT RESPOND FOR ON


CLICK

ALGORITHM

STEP 1: START THE PROCESS.

STEP 2: WINDOWS=>RUN=>CMD=>MATLAB.

STEP 3: IMPORT APPLET VIEWER IN JAVA AND STORE IT INTO THE DECLARED

VARIABLE.

STEP 4: SET THE LAYOUT OF THE FRAME WITH GRID AND LOCATION.

SETP 5:NAME THE DISPLAY BOX USING LABEL PROPERTY AND WRITE CODE

TO EXIT WHEN BUTTON CLICK

STEP 6:DISPLAY DIALOGUE BOX USING SHOW() FUNCTION.

STEP 7:RUN THE PROGRAM.

STEP 8:DISPLAY THE RESULT.

STEP9:STOP THE PROCESS.

19
MAT LAB MCA 2016-2019, CMS CBE

CODING

dbox=java.awt.Frame('hi folks');
dbox.setLayout(java.awt.GridLayout(2,1));
dbox.setLocation(250,250);
resize(dbox,200,100);
set(dbox,'background',[1,1,1]);
txt=java.awt.Label('click the button to exit',1);
but=java.awt.Button('exit button');
set(but,'background',[0.7,0.5,0.5]);
set(but,'MouseClickedCaLLbACK','dbox.dispose')
dbox.add(txt);
dbox.add(but);
dbox.show;

20
MAT LAB MCA 2016-2019, CMS CBE

OUT PUT:

RESULT:
The above program is executed successfully and output is obtained

21
MAT LAB MCA 2016-2019, CMS CBE

EX.NO: 8 ARITHMETIC OPERATIONS AND TRANSPOSE OF MATRIX

DATE:27.02.18

AIM

CREATE APROGRAM FOR DIFFERENT ARITHMETIC OPERATION FOR GIVEN MATRIX


AND DISPLAY IT’S TRANSPOSE OF MATRIX

ALGORITHM

STEP 1: START THE PROCESS.

STEP 2: WINDOWS=>RUN=>CMD=>MATLAB.

STEP 4: DECLARE TWO VARIABLES AND PASS 2 X 2 MATRIX VALUE.

STEP 5:DECLARE VARIABLES TO STORE 3 X 3, 2X3 , 3X2 MATRIX VALUE.

STEP 6: CALCULATE ADDITION, MULTIPLICATION AND DIVISION , DISPLAY IT.

STEP 7:FIND THE TRANSPOSE OF MATRIX USING TRANSPOSE() FUNCTION.

STEP 8:RUN THE PROGRAM.

STEP 9: DISPLAY ALL THE CALCULATE VALUES IN THE MATRIX FORM.

STEP 10:STOP THE PROCESS.

22
MAT LAB MCA 2016-2019, CMS CBE

CODING:
%Use the following matrices for questions 3 and 4
a = [1 2
3 4];
b = [8 -1
2 -3];
c = [3 -3
14
0 7];
d = [0 1 2
304
1 -2 -1];
e = [6 5
0 -3
2 1];
f = [2 5 -1
0 3 3];

%perform the following

p=a+b
q=d*c

%matrix tranpspose
r = transpose(d)
s = e.'

%Solve the following systems of equations using Matlab


%3x + 2y -z = 10
%-1x + 3y +2z = 5
%x - y - z = -1

A = [3 2 -1
-1 3 2
1 -1 -1];
C = [10
5
-1];

result =A\C

23
MAT LAB MCA 2016-2019, CMS CBE

OUT PUT

create an array from 2 to 7


x=
2 3 4 5 6 7

create an array from 0 to 30 increasing by 5


y=
0 5 10 15 20 25 30
r=
4 16 5
12 14 7
1 3 11
8 9 15
13 6 2

select element from 3,4specific element

ans =
7

create A as third row of matrix of r

A=
1 3 11

create A as 2 column of matrix of r

A=
16
14
3
9
6

create B selecting 3 to 5 rows , 1 to 3 column

B=
1 3 11

24
MAT LAB MCA 2016-2019, CMS CBE

8 9 15
13 6 2

create C selecting 2 to 4 rows , 1 , 3 columns

C=
12 7
1 11
8 15
p=
9 1
5 1
q=
1 18
9 19
1 -18

r=
0 3 1
1 0 -2
2 4 -1
s=
6 0 2
5 -3 1

result =

-2.0000
5.0000
-6.0000

>>

RESULT:
The above program is executed successfully and output is obtained

25
MAT LAB MCA 2016-2019, CMS CBE

EX.NO:9 CIRCLE BASED GRAPH USING MAT LAB

DATE:12.03.18

AIM

CREATE A PROGRAM TO CIRCLE BASED GRAPH USING MAT LAB.

ALGORITHM

STEP 1: START THE PROCESS.

STEP 2: WINDOWS=>RUN=>CMD=>MATLAB.

STEP 3: DECLARE TWO VARIABLE AT PASS THE VALUES THROUGH ARRAY

STEP 4:CREATE GRID USING MESHGRID() FUNCTION

STEP 5:DECLARE A VARIABLE AND CALCULATE ADDITION OF SQUARE OF

THE NUMBER(X2+Y2) AND STORE IT.

STEP 6:USE CONTOUR() FUNCTION TO GENERATE THE CIRCLE GRAPH.

STEP 7:DISPLAY THE GRAPH.

STEP 8: STOP THE PROCESS.

26
MAT LAB MCA 2016-2019, CMS CBE

CODING:

[x,y] = meshgrid(-5:0.1:5,-3:0.1:3);
g = x.^2 + y.^2;
contour(x,y,g)
print -depsgraph.eps

OUTPUT:

RESULT:
The above program is executed successfully and output is obtained

27
MAT LAB MCA 2016-2019, CMS CBE

EX.NO: 10 VARIOUS SUBSET OF VECTORS

DATE:23.03.18

AIM

CREATE A PROGRAM CREATING VARIOUS SUBSET OF VECTORS AND


MATRIXES

ALGORITHM

STEP 1: START THE PROCESS.

STEP 2: WINDOWS=>RUN=>CMD=>MATLAB.

STEP 3: DECLARE AN ARRAY VARIABLE AND PASS SIX ELEMENTS.

STEP4: DECLARE ANOTHER ARRAY VARIABLE AND ASSIGN VAULES FROM 0

TO 30 INCREASING BY 5.

STEP 5: DECLARE AN ARRAY VARIABLE AND PASS THE VALUES IN 5 X 3 MATRIX.

STEP 6:DISPLAY THE VALUES FROM THE ARRAY LIKE SUB SET LIKE ANY

ROW OR COLUMN OR N X N MATRIX.

STEP 7: PASS N VALUES TO DISPLAY THE SUBSET OF THE MATRIX.

STEP 8: RUN THE PROGRAM.

STEP 9: DISPLAY ALL THE CALCULATE VALUES IN THE MATRIX FORM.

STEP 10: STOP THE PROCESS.

28
MAT LAB MCA 2016-2019, CMS CBE

CODING
%Creating various subset of vectors and matrixes

disp('create an array from 2 to 7')

x = 2:7

disp('create an array from 0 to 30 increasing by 5')


y = 0:5:30

r = [4,16,5
12,14,7
1,3,11
8,9,15
13,6,2]

disp('select element from 3,4specific element')


r(2,3)

% create a as first column of matrix of r'


disp('create A as third row of matrix of r')
A = r(3,:)

disp('create A as 2 column of matrix of r')


A = r(:,2)

disp('create B selecting 3 to 5 rows , 1 to 3 column')


B = r(3:5,1:3)

disp('create C selecting 2 to 4 rows , 1 , 3 columns')


C = r(2:4,1:2:3)

29
MAT LAB MCA 2016-2019, CMS CBE

OUTPUT

RESULT:
The above program is executed successfully and output is obtained

30

You might also like