MAT LAB Record Final
MAT LAB Record Final
ARTS COLLEGE(AUTONOMOUS)
VELLORE-2
PG & Research Department of Computer Science
M.Sc., COMPUTER SCIENCE
PRACTICAL RECORD
2024-2025
SUB.CODE : …………………………………………………………………………………
SUBJECT : ………………………………………………………………………………….
CLASS :………………………………………………………………………………….
MUTHURANGAM GOVERNMENT
BONAFIDE CERTIFICATE
SI. PAGE
DATE TITLE SIGN
NO. NO.
1. ARRAY FUNCTION
2. VECTOR MANIPULATION
TWO-DIMENSIONAL PLOTTING
5. TRIGONOMETRIC FUNCTIONS
6. SINE WAVE
THREE-DIMENSIONAL PLOTTING
AIM
To Implement the MAT LAB built-in array functions for creating zero, one, and identity
matrices and retrieving array dimensions.
DESCRIPTION
size(arr) Return two values specifying the number of rows and columns in arr.
MATLAB PROGRAM
% Display results
disp('Zero Matrices:');
disp(zeroMatrix1);
disp(zeroMatrix2);
disp(zeroMatrix3);
disp('One Matrices:');
disp(oneMatrix1);
disp(oneMatrix2);
disp(oneMatrix3);
disp('Identity Matrices:');
disp(identityMatrix1);
disp(identityMatrix2);
OUTPUT
>> Array
Zero Matrices:
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
One Matrices:
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
Identity Matrices:
1 0 0
0 1 0
0 0 1
1 0 0
0 1 0
RESULT
The MAT LAB built-in array functions have been successfully implemented.
Ex. No: 02
VECTOR MANIPULATION
Date:
AIM
DESCRIPTION
Cross Product (cross(A, B)) Computes the cross product (only for 3D vectors).
% Vector Addition
vectorSum = A + B;
% Vector Subtraction
vectorDiff = A - B;
% Vector Transpose
A_transpose = A';
% Dot Product
dotProduct_AB = dot(A, B);
dotProduct_CD = dot(C, D);
% Element-wise Multiplication
elementWiseMult = A .* B;
% Element-wise Division
elementWiseDiv = A ./ B;
% Display results
disp('Vector A:');
disp(A);
disp('Vector B:');
disp(B);
disp('Transpose of A:');
disp(A_transpose);
disp('Vector C:');
disp(C);
disp('Vector D:');
disp(D);
OUTPUT
>> Vector
Vector A:
1 2 3 4 5
Vector B:
5 4 3 2 1
Transpose of A:
1
2
3
4
5
Vector C:
1 2 3
Vector D:
4 5 6
RESULT
AIM
DESCRIPTION
disp('Matrix A:');
disp(A);
disp('Matrix B:');
disp(B);
% Matrix Addition
matrixSum = A + B;
disp('Matrix Addition (A + B):');
disp(matrixSum);
% Matrix Subtraction
matrixDiff = A - B;
disp('Matrix Subtraction (A - B):');
disp(matrixDiff);
% Matrix Multiplication
matrixProd = A * B;
disp('Matrix Multiplication (A * B):');
disp(matrixProd);
% Transpose of A
transposeA = A';
disp('Transpose of A (A''):');
disp(transposeA);
% Determinant of A
detA = det(A);
disp('Determinant of A:');
disp(detA);
OUTPUT
>> Matrix
Matrix A:
1 2 3
4 5 6
7 8 9
Matrix B:
9 8 7
6 5 4
3 2 1
Transpose of A (A'):
1 4 7
2 5 8
3 6 9
Determinant of A:
6.6613e-16
RESULT
AIM
To Implement 2D plot for Pie chart and Histogram using MAT LAB Procedure.
DESCRIPTION
pie(data, labels) Creates a pie chart using data and optional labels.
plot(x, y)
where,
2. Grid
To give better illustration of the coordinates on the plot, a grid is used. The command
grid is used to show the grid lines on the plot. The grid on the plot can be added or removed
by using the on and off with the grid command. When grid command without any argument
is used alternatively, it toggles the grid drawn. By default, MATLAB starts with grid off.
3. Printing Labels
To make the plot more understandable, the two axes are labelled and a title is provided above
the top of the plot by using the following commands.
xlabel(‘string’) %Statement 1
ylabel(‘string’) %Statement 2
title(‘string’) %Statement
where, Statement 1 uses the command xlabel to print label on the x - axis. The label to be
printed is enclosed in single quotes. Similarly, Statement 2 uses the command ylabel to print
the label along y - axis. The command title in Statement 3 is used to provide a title above the
top of the graph.
PROCEDURE
Use pie() to generate a pie chart with the defined data and labels.
MATLAB PROGRAM
pieLabels = {'A', 'B', 'C', 'D', 'E'}; % Labels for the pie chart
histData = randi([1, 100], 1, 100); % 100 random values between 1 and 100
figure;
subplot(1, 2, 1); % Divide the figure into 1 row and 2 columns, use 1st plot
xlabel('Data Values');
ylabel('Frequency');
title('Histogram Representation');
OUTPUT
RESULT
MAT LAB Procedure to Plot the 2D Graphs for Pie chart and Histogram has been
Successfully implemented.
Ex. No: 05 TWO-DIMENSIONAL PLOTTING
AIM
To Plot the 2D Graphs of the trigonometric functions sin(t), cos(t), sec(t), and tan(t)
over the interval [-2π, 2π] in MATLAB and analyze their behavior.
DESCRIPTION
To draw a two - dimensional plot in MATLAB, the plot command is used. The syntax is given
as follows:
plot(x, y)
2. Grid
To give better illustration of the coordinates on the plot, a grid is used. The
command grid is used to show the grid lines on the plot. The grid on the plot can be added or
removed by using the on and off with the grid command. When grid command without any
argument is used alternatively, it toggles the grid drawn. By default, MATLAB starts with grid
off.
3. Printing Labels
To make the plot more understandable, the two axes are labelled and a title is provided above
the top of the plot by using the following commands.
xlabel(‘string’) %Statement 1
ylabel(‘string’) %Statement 2
title(‘string’) %Statement
where, Statement 1 uses the command xlabel to print label on the x - axis. The label to be
printed is enclosed in single quotes. Similarly, Statement 2 uses the command ylabel to print
the label along y - axis. The command title in Statement 3 is used to provide a title above the
top of the graph.
PROCEDURE
y1 = sin(t)
y2 = cos(t)
y3 = sec(t) = 1/cos(t)
y4 = tan(t) = sin(t)/cos(t)
4. Limit y-axis: To avoid infinite values of tan(t) and sec(t), we set ylim([-5,5]).
5. Add labels, grid, and legend for clarity.
MATLAB PROGRAM
y1 = sin(t);
y2 = cos(t);
y3 = sec(t);
y4 = tan(t);
figure;
hold on; % Hold the graph to plot multiple functions on the same figure
ylim([-5, 5]); % Limit y-axis to avoid large tan(t) and sec(t) values
xlabel('t (radians)');
ylabel('Function Values');
grid on;
hold off;
OUTPUT
RESULT
The MAT LAB 2D Graphs for trigonometric functions sin(t), cos(t), sec(t), and tan(t)
over the interval [-2π, 2π] has been implemented succssfully.
Ex. No: 06 TWO-DIMENSENTIONAL PLOTTING
AIM
To Plot a sine wave in MATLAB by defining a time variable and computing the sine
function, then visualizing it using the plot function.
DESCRIPTION
A sine wave is a periodic function that represents oscillatory motion, commonly used in
signal processing, physics, and engineering. The general mathematical equation for a sine
wave is:
y=Asin(ωt+ϕ)
Where,
Syntax
Y = sin(x)
Description
y = sin(x) returns the sine of the elements of X. The sin function operates element-wise on
arrays. The function accepts both real and complex inputs. For real values of X in the interval
[-int. int], sin returns real values in the interval [-1,1]. For complex values of X, sin returns
complex values. All angles are in radians.
PROCEDURE
linspace(0, 2*pi, 1000) generates 1000 points from 0 to 2π for smooth plotting.
MATLAB PROGRAM
y = sin(t);
figure;
xlabel('Time (t)');
ylabel('Amplitude');
title('Sine Wave');
% Display legend
legend('sin(t)');
OUTPUT
RESULT
AIM
To Generate a 3D Surface plot in MATLAB using the function sin(√(x² + y²)) and
visualize the relationship between X, Y, and Z using the surf function.
DESCRIPTION
Used to plot 3D surfaces where the color represents the function value.
Example: surf(X, Y, Z)
PROCEDURE
MATLAB PROGRAM
OUTPUT
RESULT
The MATLAB 3D Plot using the surf function has been successfully implemented.
Ex. No: 08 THREE-DIMENSIONAL POLLTING
AIM
To Plot a 3D mesh surface of the function 𝑐𝑜𝑠 𝑥 +𝑦 in MATLAB using the mesh
function, visualizing its oscillatory nature over a defined 2D grid.
DESCRIPTION
Example: mesh(X, Y, Z)
PROCEDURE
MATLAB PROGRAM
OUTPUT
RESULT
The 3D mesh surface of the function 𝑐𝑜𝑠 𝑥 +𝑦 in MATLAB using the mesh
function has been implemented successfully.
Ex. No: 09 THREE-DIMENSIONAL PLOTTING
AIM
DESCRIPTION
surf(Xq, Yq, Zq) Creates a smooth surface plot of the interpolated data.
MATLAB PROGRAM
x = rand(100,1)*4 - 2;
y = rand(100,1)*4 - 2;
z = peaks(x,y);
Zq = griddata(x,y,z,Xq,Yq,'cubic');
figure;
hold on;
hold off;
xlabel('X');
ylabel('Y');
zlabel('Z');
RESULT
The 3D Interpolating Scattered Surface plot in MATLAB using griddata, scatter, surf
and meshgrid function has been implemented successfully.
Ex. No: 10
SIMPLE CALCULATOR USING GUI
Date:
AIM
DESCRIPTION
A Graphical User Interface (GUI) in MATLAB allows users to interact with a program
using graphical components instead of command-line inputs. MATLAB provides App
Designer and GUIDE (deprecated) for creating GUIs.
function simpleCalculator
displayBox = uieditfield(fig, 'text', 'Position', [20, 320, 260, 40], 'Editable', 'off', 'FontSize',
16);
expression = "";
% Create buttons
for i = 1:4
for j = 1:4
btnIndex = (i-1) * 4 + j;
end
end
function buttonCallback(btnText)
switch btnText
expression = "";
displayBox.Value = "";
try
displayBox.Value = num2str(result);
catch
displayBox.Value = 'Error';
expression = "";
end
displayBox.Value = expression;
end
end
end
OUTPUT
RESULT
The Simple calculator using GUI Components in MAT LAB has been implemented
successfully.