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

MAT LAB

The document outlines a series of MATLAB experiments focused on built-in array functions, vector manipulation, and plotting techniques. It includes detailed procedures for creating matrices, performing vector operations, and generating 2D and 3D plots of mathematical functions. Each experiment specifies the aim, required equipment, theoretical background, MATLAB programming code, and expected output.

Uploaded by

subapalani2001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

MAT LAB

The document outlines a series of MATLAB experiments focused on built-in array functions, vector manipulation, and plotting techniques. It includes detailed procedures for creating matrices, performing vector operations, and generating 2D and 3D plots of mathematical functions. Each experiment specifies the aim, required equipment, theoretical background, MATLAB programming code, and expected output.

Uploaded by

subapalani2001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Ex.

No: 01
BUILT-IN ARRAY FUNCTION
Date:

AIM OF THE EXPERIMENT

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

zeros(n) Creates a n x n matrix of zeros.


zeros(m,n) Creates a m x n matrix of zeros
zeros(size(arr)) Create a matrix of zeros of the same size as arr.
ones(n) Creates a n x n matrix of ones.
ones(m,n) Creates a m x n matrix of ones.
ones(size(arr)) Creates a matrix of ones of the same size as arr.
eye(n) Creates a n x n identity matrix.
eye(m,n) Creates an m x n identity matrix.
length(arr) Return the length of a vector, or the longest dimension of a 2D array.
size(arr) Return two values specifying the number of rows and columns in arr.
MAT LAB PROGRAMMING

% MATLAB Program to Demonstrate Built-in Array Functions


arr = [1 2 3; 4 5 6; 7 8 9]; % Define a sample matrix for size reference

% Creating matrices of zeros


n = 3;
m = 2;
zeroMatrix1 = zeros(n); % n x n matrix of zeros
zeroMatrix2 = zeros(m, n); % m x n matrix of zeros
zeroMatrix3 = zeros(size(arr)); % Matrix of zeros of the same size as arr

% Creating matrices of ones


oneMatrix1 = ones(n); % n x n matrix of ones
oneMatrix2 = ones(m, n); % m x n matrix of ones
oneMatrix3 = ones(size(arr)); % Matrix of ones of the same size as arr

% Creating identity matrices


identityMatrix1 = eye(n); % n x n identity matrix
identityMatrix2 = eye(m, n); % m x n identity matrix

% Finding length and size of an array


vec = [1 2 3 4 5]; % Example vector
arrLength = length(arr); % Longest dimension of the array
vecLength = length(vec); % Length of a vector
[arrRows, arrCols] = size(arr); % Number of rows and columns in arr

% 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);

disp('Array Length and Size:');


disp(['Length of arr: ', num2str(arrLength)]);
disp(['Length of vec: ', num2str(vecLength)]);
disp(['Size of arr: ', num2str(arrRows), ' x ', num2str(arrCols)]);

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

Array Length and Size:


Length of arr: 3
Length of vec: 5
Size of arr: 3 x 3
Ex. No: 02
VECTOR MANIPULATION
Date:

AIM OF TITE EXPERIMENT

To implement and demonstrate various vector operations in MATLAB, including


addition, subtraction, transpose, appending elements, dot product, cross product, and element-
wise operations.

EQUIPMENTS REOUIRED:

 Personal computer
 Operating system windows 10
 MATLAB Software-version R2007b

THEORY:

Addition (A + B) Adds corresponding elements of two vectors.

Subtraction (A - B) Subtracts elements of B from A.

Transpose (A') Converts a row vector to a column vector.

Append ([A, value]) Adds an element to the vector.


Computes the sum of element-wise
Dot Product (dot(A, B))
multiplication.
Computes the cross product (only for 3D
Cross Product (cross(A, B))
vectors).
Element-wise Multiplication (A .* B) Multiplies corresponding elements.

Element-wise Division (A ./ B) Divides corresponding elements.


MATLAB PROGRAMMING

% Define two 5D vectors


A = [1 2 3 4 5];
B = [5 4 3 2 1];

% Define two 3D vectors for cross product demonstration


C = [1 2 3];
D = [4 5 6];

% Vector Addition
vectorSum = A + B;

% Vector Subtraction
vectorDiff = A - B;

% Vector Transpose
A_transpose = A';

% Append an element to vector A


A_appended = [A, 6];

% Dot Product
dotProduct_AB = dot(A, B);
dotProduct_CD = dot(C, D);

% Cross Product for 3D vectors


crossProduct_CD = cross(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('Vector Sum (A + B):');


disp(vectorSum);

disp('Vector Difference (A - B):');


disp(vectorDiff);

disp('Transpose of A:');
disp(A_transpose);

disp('Vector A after appending 6:');


disp(A_appended);

disp('Dot Product of A and B:');


disp(dotProduct_AB);

disp('Dot Product of C and D:');


disp(dotProduct_CD);

disp('Vector C:');
disp(C);
disp('Vector D:');
disp(D);

disp('Cross Product of C and D:');


disp(crossProduct_CD);

disp('Element-wise Multiplication (A .* B):');


disp(elementWiseMult);

disp('Element-wise Division (A ./ B):');


disp(elementWiseDiv);

OUTPUT
>> Vector
Vector A:
1 2 3 4 5

Vector B:
5 4 3 2 1

Vector Sum (A + B):


6 6 6 6 6

Vector Difference (A - B):


-4 -2 0 2 4

Transpose of A:
1
2
3
4
5

Vector A after appending 6:


1 2 3 4 5 6

Dot Product of A and B:


35

Dot Product of C and D:


32

Vector C:
1 2 3

Vector D:
4 5 6

Cross Product of C and D:


-3 6 -3

Element-wise Multiplication (A .* B):


5 8 9 8 5

Element-wise Division (A ./ B):


0.2000 0.5000 1.0000 2.0000 5.0000
Ex. No: 03
TWO-DIMENSIONAL PLOTTING
Date:

AIM OF THE EXPERIMENT

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

Sine (sin t) A smooth wave oscillating between -1 and 1.

Cosine (cos t) Similar to sine but phase-shifted.

Secant (sec t) Has asymptotes where cos(t) = 0 (π/2, 3π/2, ...).

Tangent (tan t) Also has asymptotes where cos(t) = 0.

1. Two - dimensional Plots

To draw a two - dimensional plot in MATLAB, the plot command is used. The syntax is given
as follows:

plot(x, y)

where,

x is a vector containing the x - coordinates of the plot and

y is a vector containing the y - coordinates of the plot.


2. 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.

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

1. Define t: The values of t are taken from -2π to 2π using linspace.


2. Calculate function values:

 y1 = sin(t)

 y2 = cos(t)

 y3 = sec(t) = 1/cos(t)

 y4 = tan(t) = sin(t)/cos(t)

3. Plot the functions:

 plot(t, y1, 'r', 'LineWidth', 1.5); → Red for sin(t)


 plot(t, y2, 'b', 'LineWidth', 1.5); → Blue for cos(t)

 plot(t, y3, 'g', 'LineWidth', 1.5); → Green for sec(t)

 plot(t, y4, 'm', 'LineWidth', 1.5); → Magenta for tan(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

% Define time variable t from -2π to 2π

t = linspace(-2*pi, 2*pi, 1000);

% Calculate trigonometric functions

y1 = sin(t);

y2 = cos(t);

y3 = sec(t);

y4 = tan(t);

% Plot the functions

figure;

hold on; % Hold the graph to plot multiple functions on the same figure

plot(t, y1, 'r', 'LineWidth', 1.5); % Sine function in red

plot(t, y2, 'b', 'LineWidth', 1.5); % Cosine function in blue

plot(t, y3, 'g', 'LineWidth', 1.5); % Secant function in green

plot(t, y4, 'm', 'LineWidth', 1.5); % Tangent function in magenta


% Set axis limits

ylim([-5, 5]); % Limit y-axis to avoid large tan(t) and sec(t) values

% Labels and title

xlabel('t (radians)');

ylabel('Function Values');

title('2D Plot of sin(t), cos(t), sec(t), and tan(t)');

legend('sin(t)', 'cos(t)', 'sec(t)', 'tan(t)');

% Grid and axes

grid on;

ax = gca; % Get current axes

ax.XAxisLocation = 'origin'; % Set x-axis at origin

ax.YAxisLocation = 'origin'; % Set y-axis at origin

hold off;
OUTPUT
Ex. No: 04
THREE-DIMENSIONAL PLOTTING
Date:

AIM OF THE EXPERIMENT

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

3D plotting in MATLAB allows visualization of functions and data in three dimensions


by representing relationships between X, Y, and Z coordinates, helping to analyze surfaces,
curves, and spatial relationships. MATLAB provides various functions for 3D plots, including
surface plots, mesh plots, contour plots, and scatter plots. These plots help in understanding
mathematical functions, engineering designs, and scientific data.

3D plotting in MATLAB is a powerful tool for visualizing and analyzing multi-


dimensional data. By using functions like surf, mesh, and plot3, users can explore
mathematical functions, data distributions, and scientific models effectively.

Surface Plot (surf)

 Used to plot 3D surfaces where the color represents the function value.

 Example: surf(X, Y, Z)
PROCEDURE

1. Define the X and Y grid:


 meshgrid(-5:0.5:5, -5:0.5:5) creates a grid of values from -5 to 5 for both x and y.
2. Define the function for Z:
 z = sin(sqrt(x.^2 + y.^2)) creates a wave-like surface.
3. Plot the 3D surface using surf(x, y, z);
 surf creates a smooth surface connecting the (x, y, z) points.
4. Enhancements:

 xlabel, ylabel, zlabel → Adds axis labels.

 title → Provides a title for the plot.

 colormap jet → Uses the "jet" color scheme.

 colorbar → Displays a color scale.

 grid on → Enables the grid.

MATLAB PROGRAMMING

% Define the range for X and Y


[x, y] = meshgrid(-5:0.5:5, -5:0.5:5);

% Define the function for Z


z = sin(sqrt(x.^2 + y.^2)); % Example function: sin(sqrt(x^2 + y^2))

% Create a 3D Surface Plot


figure;
surf(x, y, z);

% Labels and Title


xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Surface Plot of sin(sqrt(x^2 + y^2))');

% Color and Grid


colormap jet; % Apply a colormap
colorbar; % Show color scale
grid on; % Turn on grid

OUTPUT
Ex. No: 05
SINE WAVE PLOTTING
Date:

AIM OF THE EXPERIMENT

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,

 A = Amplitude (peak value of the wave)

 ω = Angular frequency (determines how fast the wave oscillates)

 t = Time (independent variable)

 φ = Phase shift (determines horizontal shift)

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

1. Define the time variable t

 linspace(0, 2*pi, 1000) generates 1000 points from 0 to 2π for smooth plotting.

2. Define the sine wave function y

 y = sin(t) computes the sine of each value in t.

3. Plot the sine wave using plot

 'r' → Red color


 'LineWidth', 2 → Thicker line for better visibility

4. Add labels, title, and grid

 xlabel, ylabel, and title describe the graph.


 grid on enables a background grid for clarity.
 legend('sin(t)') adds a legend.

MATLAB PROGRAMMING:

% Define time variable t from 0 to 2π with small increments

t = linspace(0, 2*pi, 1000);

% Define sine wave function

y = sin(t);
% Plot the sine wave

figure;

plot(t, y, 'r', 'LineWidth', 2); % Red line with width 2

% Labels and Title

xlabel('Time (t)');

ylabel('Amplitude');

title('Sine Wave');

% Grid and Axis

grid on; % Enable grid

ax = gca; % Get current axes

ax.XAxisLocation = 'origin'; % Set x-axis at origin

ax.YAxisLocation = 'origin'; % Set y-axis at origin

% Display legend

legend('sin(t)');
OUTPUT
Ex. No: 06
MESH SURFACE PLOTTING
Date:

AIM OF THE EXPERIMENT

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

3D plotting in MATLAB allows visualization of functions and data in three dimensions


by representing relationships between X, Y, and Z coordinates, helping to analyze surfaces,
curves, and spatial relationships. MATLAB provides various functions for 3D plots, including
surface plots, mesh plots, contour plots, and scatter plots. These plots help in understanding
mathematical functions, engineering designs, and scientific data.

3D plotting in MATLAB is a powerful tool for visualizing and analyzing multi-


dimensional data. By using functions like surf, mesh, and plot3, users can explore
mathematical functions, data distributions, and scientific models effectively.

Mesh Plot (mesh)

 Similar to surf, but displays only wireframes without filled colors.

 Example: mesh(X, Y, Z)
PROCEDURE

1. Define the X and Y grid:


 meshgrid(-5:0.5:5, -5:0.5:5) creates a grid of values from -5 to 5 for both x and y.
2. Define the function for Z:
 z = cos(sqrt(x.^2 + y.^2)) creates a wave-like surface.
3. Plot the 3D surface using mesh(x, y, z);
 surf creates a smooth surface connecting the (x, y, z) points.
4. Enhancements:

 xlabel, ylabel, zlabel → Adds axis labels.

 title → Provides a title for the plot.

 colormap jet → Uses the " parula" color scheme.

 colorbar → Displays a color scale.

 grid on → Enables the grid.

MATLAB PROGRAMMING

% Define the range for X and Y


[X, Y] = meshgrid(-5:0.5:5, -5:0.5:5);

% Define the function for Z


Z = cos(sqrt(X.^2 + Y.^2)); % Example function for mesh surface

% Create a 3D Mesh Surface Plot


figure;
mesh(X, Y, Z);

% Labels and Title


xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Mesh Surface Plot of cos(sqrt(X^2 + Y^2))');

% Grid and Color


colormap parula; % Apply a colormap
colorbar; % Show color scale
grid on; % Enable grid

OUTPUT

You might also like