Professional Documents
Culture Documents
Lab 7 Report
Lab 7 Report
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
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
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
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