MAT LAB
MAT LAB
No: 01
BUILT-IN ARRAY FUNCTION
Date:
To write a program to demonstrates the use of built-in array functions in MAT LAB for
creating zero, one, and identity matrices and retrieving array dimensions.
EQUIPMENT REQUIRED
Personal computer
Operating system windows 10
MATLAB Software-version R2007b
THEORY
% 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
EQUIPMENTS REOUIRED:
Personal computer
Operating system windows 10
MATLAB Software-version R2007b
THEORY:
% 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
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.
EOUIPMENTS REOUIRED
Personal computer
Operating system windows 10
MATLAB Software-version R2007b
THEORY
To draw a two - dimensional plot in MATLAB, the plot command is used. The syntax is given
as follows:
plot(x, y)
where,
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.
3. 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.
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 EXPERIMENT
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
Ex. No: 04
THREE-DIMENSIONAL PLOTTING
Date:
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.
EOUIPMENTS REOUIRED
Personal computer
Operating system windows 10
MATLAB Software-version IU007b
THEORY
Used to plot 3D surfaces where the color represents the function value.
Example: surf(X, Y, Z)
PROCEDURE
MATLAB PROGRAMMING
OUTPUT
Ex. No: 05
SINE WAVE PLOTTING
Date:
To plot a sine wave in MATLAB by defining a time variable and computing the sine
function, then visualizing it using the plot function.
EQUIPMENT REOUIRED:
Personal computer
Operating system windows 10
MATLAB Software-version R2007b
THEORY:
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 PROGRAMMING:
y = sin(t);
% Plot the sine wave
figure;
xlabel('Time (t)');
ylabel('Amplitude');
title('Sine Wave');
% Display legend
legend('sin(t)');
OUTPUT
Ex. No: 06
MESH SURFACE PLOTTING
Date:
To plot a 3D mesh surface of the function 𝑐𝑜𝑠 𝑥 +𝑦 in MATLAB using the mesh
function, visualizing its oscillatory nature over a defined 2D grid.
EOUIPMENTS REOUIRED
Personal computer
Operating system windows 10
MATLAB Software-version IU007b
THEORY
Example: mesh(X, Y, Z)
PROCEDURE
MATLAB PROGRAMMING
OUTPUT