You are on page 1of 6

NAME: MUHAMMAD HANNAN AYUB KHAN

SECTION: ME-12-C
CMS ID: 335935

LAB NO 07
TITLE:
Introduction to 2D and 3D plotting with MATLAB.

OBJECTIVES:
• Introduction to basic plotting functions of MATLAB.
• To learn how plot in 2D and 3D with MATLAB.

SOFTWARE USED:
1. MATLAB.

THEORETICAL BACKGROUND:
INTRODUCTION TO MATLAB:
MATLAB is a high-performance language for technical computing. It integrates
computation, visualization, and programming in an easy-to-use environment where
problems and solutions are expressed in familiar mathematical notation. The name
MATLAB stands for matrix laboratory. MATLAB was originally written to provide easy
access to matrix.
CREATING A PLOT USING PLOT FUNCTION
The plot function has different forms, depending on the input arguments. If y is a vect or,
plot(y) produces a piecewise linear graph of the elements of y versus the index of the
elements of y. If you specify two vectors as arguments, plot (x, y) produces a graph of y
versus x.
SPECIFYING LINE STYLES AND COLORS
Various line types, plot symbols and colors may be obtained with plot (x, y, s) where s is a
character string made from one element from any or all the following 3 columns:
Color Marker Line
Style
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magent * star (none) no line
a
y yellow s square
NAME: MUHAMMAD HANNAN AYUB KHAN
SECTION: ME-12-C
CMS ID: 335935

k black d diamond
v triangle (down)
> triangle (left)
p pentagram
h hexagram

ADDING PLOTS TO AN EXISTING GRAPH


The hold command enables you to add plots to an existing graph. When you type Hold on
MATLAB does not replace the existing graph when you issue another plotting command;
it adds the new data to the current graph, rescaling the axes if necessary.
MULTIPLE PLOTS IN ONE FIGURE:
The subplot command enables you to display multiple plots in the same window or print
them on the same piece of paper. Typing
Subplot (m, n, p)
Partitions the figure window into an m-by-n matrix of small subplots and selects the pth
subplot for the current plot. The plots are numbered along first the top row of the figure
window, then the second row, and so on
SETTING AXIS LIMITS:
By default, MATLAB finds the maxima and minima of the data to choose the axis limits
to span this range. The axis command enables you to specify your own limits
axis ([ xmin xmax ymin ymax])
AXIS LABELS AND TITLES
The xlabel, ylabel, and zlabel commands add x-, y-, and z-axis labels. The title command
adds a title at the top of the figure and the text function inserts text anywhere in the figure.

PARACTICE EXERCISES:
EXERCISE NO 01:
Plot Sinc function, where Sinc (x) = sin(x) / x, -2π ≤ x ≤ 2π
CODE:
clear %Removes all variables from workspace
clc %Clears the command window and homes the cursor
x=-2*pi:.05:2*pi; %Assigning an array of values to x
y=sinc(x); %Assigning value to y function
plot(x,y,'r-','LineWidth',2); %Plotting graph
xlabel('x axis'); %Labelling X axis
ylabel('y axis'); %Labelling y axis
NAME: MUHAMMAD HANNAN AYUB KHAN
SECTION: ME-12-C
CMS ID: 335935

title('Sinc Function Plot'); %Assigning title to graph


legend('Sinc Graph'); %Plotting legend on graph
OUTPUT:

EXERCISE NO 02:
Plot sin(x) and cos(x) on the same axis using different colors. -2π ≤ x ≤ 2π
CODE:
clear %Removes all variables from workspace
clc %Clears the command window and homes the cursor
x=-2*pi:.1:2*pi; %Assigning array of values to x
y=sin(x); %Assigning values to y
y_1=cos(x); %Assigning values to y_1
plot(x,y,'b*-'); %Plotting graph for sine function
hold on; %Allowing another function to be plotted on the
same graph
plot(x,y_1,'ro-'); %Plotting graph for sine function
xlabel('x axis'); %Labelling x axis
ylabel('y axis'); %Labelling y axis
title('Sine and Cosine Relation'); %Assiging title to
graph
legend('Sine Graph','Cosine Graph') %Plotting legend on
graph
NAME: MUHAMMAD HANNAN AYUB KHAN
SECTION: ME-12-C
CMS ID: 335935

OUTPUT:

EXERCISE NO 03:
Plot the following waveforms: y = A*square (2*pi*f*t) and y_1 = A*sawtooth (2*pi*f*t) in
the same figure. Ask user to input values for amplitude (A) and frequency (f). Take
t = 0: 0.001:1 s
CODE:
clear %Removes all variables from workspace
clc %Clears the command window and homes the cursor
t=0:0.001:1; %Assigning an array of values to variable to
t
A=input('Enter the value of Amplitude: '); %Asking user
to input value of A
f=input('Enter the value of Frequency: '); %Asking user
to input value of f
y=A*square(2*pi*f*t); %Assigning square function values
to y
y_1=A*sawtooth(2*pi*f*t); %Assigning square function
values to y_1
subplot(2,1,1);plot(t,y,'b-'); %Plotting multiple graphs
starting with square wave
xlabel('Time'); %Labelling x axis
ylabel('Square Function'); %Labelling y axis
title('Square Graph Plot'); %Assigning title to square
wave graph
NAME: MUHAMMAD HANNAN AYUB KHAN
SECTION: ME-12-C
CMS ID: 335935

legend('Square Wave Graph'); %Plotting legend on graph


subplot(2,1,2);plot(t,y_1,'r-'); %Plotting sawtooth wave
graph alongside square wave graph
xlabel('Time'); %Labelling x axis
ylabel('Sawtooth Function'); %Labelling y axis
title('Sawtooth Graph Plot'); %Assigning title to
sawtooth wave graph
legend('Sawtooth Graph'); %Plotting legend on graph

OUTPUT:

EXERCISE NO 04:
Plot the 3-D helix. Define t as a vector of values between 0 and 10π. Define st and ct as
vectors of sine and cosine values. Then plot st, ct, and t.
CODE:
clear %Removes all variables from workspace
clc %Clears the command window and homes the cursor
t=0:.1:10*pi; %Assiging an array of values to variable t
st=sin(t); %Assiging values to variable st
ct=cos(t); %Assiging values to variable ct
plot3(st,ct,t,'r*-','LineWidth',2); % Plotting a 3
dimensional graph
xlabel('st axis'); % Labelling x axis
NAME: MUHAMMAD HANNAN AYUB KHAN
SECTION: ME-12-C
CMS ID: 335935

ylabel('ct axis'); % Labelling y axis


zlabel('t axis'); % Labelling z axis
title('Helix Graph'); %Assigning title to graph
legend('Helix Plot'); %Plot legend for graph
OUTPUT:

CONCLUSIONS AND DISCUSSION:


This lab was aimed toward familiarizing us with fundamentals of plotting graphs on MATLAB
software. Knowledge regarding command lines, functions and basic modification operations was
also imparted during this lab session. Initially we were familiarized with basic command lines such as
“plot” and “subplot”, and how these two commands aid in plotting a graph or multiple. These two
commands are a must before running any script. From creation of graphs to labelling axis and then
displaying the graph’s title. Square and Sawtooth functions were introduced, and knowledge
regarded changing line type, marker and colour was also imparted. In addition to this, performing
basic operations on graphs such as plotting another function on same graph, plotting 3-dimensional
graph, creation of a surface plot, and plotting a colour bar alongside.

You might also like