Professional Documents
Culture Documents
Lab Objectives:
To understand M-files principle.
To plot multiple plots on a single graph.
To use different parameters on the graph.
To plot multiple graphs in a single graph window.
Requirement:
MATlab 7.0 or later
Script/M-file: One can place all commands you want to execute in a file with extension .m at
the end, and you run the script file. MATLAB will run all of these commands in sequence for
you. MATLAB has a M-file editor that you can use where you can create your scripts.
Basic Graphic Commands:
2D plotting: If x and y is array of elements that are the same size, you can plot them using this
data with the following command:
plot(x, y);
x=0:0.1:10;
y1=0.1.*x; y2=0.5.*x; y3=2.*x;
y4=5.*x; y5=10.*x;
plot(x,y1,x,y2,x,y3,x,y4,x,y5);
Labelling Axes:
grid puts a grid on the graph. The spacing for the grid is automatically figured out by
MATLAB
title(‘…’) lets your graph have a title.
xlabel(‘…’), ylabel(‘…’) labels the x and y axes accordingly. Put the labels inside the
quotations
legend(‘…’, ‘…’, …, ‘…’) produces a legend, labeling what each plot is on the graph
Example #03:
x=0:0.1:10;
y1=0.1.*x;
y2=0.5.*x;
y3=2.*x;
y4=5.*x;
y5=10.*x;
plot(x,y1,x,y2,x,y3,x,y4,x,y5);
grid; % To grid up the graph
title(‘Multiple Plots example’);
xlabel(‘X points’);
ylabel(‘Y points’);
legend(‘y1=0.1x’,’y2=0.5x’, ‘y3=2x’, ‘y4=5x’, ‘y5=10x’);
Example#04:
x = 0:0.1:10;
y = x;
plot(x,y,’g.’); %This will plot a green line with dots at each point
plot(x,y,’bo’); %This will plot a blue line with circles at each point
plot(x,y,'rx’); %This will plot a red line with crosses at each point
8|Page
Multiple plots in different Graph windows: The figure command can be used to display
multiple plots in separate figure windows.
Multiple Graphs in One Window: Multiple plots can be displayed within a single figure
window using the subplot function, which can be called as follows:
subplot(rows, cols, whichArea)
Example#05: If I wanted to make a window that has 4 plots, 2 plots in each row à 2 rows,
here’s what I’d do:
Do subplot(221) à Specify that we want to work on the top left corner. Next, code the
syntax to plot normally. The plot will appear on the top left corner
Do subplot(222) à Specify that we want to work on the top right corner. Next, code the
syntax to plot normally. The plot will appear on the top right corner
Do subplot(223) à Specify that we want to work on the bottom left corner. Next, code the
syntax to plot normally. The plot will appear on the bottom left corner
Do subplot(224) à Specify that we want to work on the bottom right corner. Next, code
the syntax to plot normally. The plot will appear on the bottom right corner
Lab Tasks:
1. Plot the following:
A) y1=2x B) y2=x+3 C) y3=2x+2z
2. Plot following vectors in a single graph and embellish with custom colors. Also display
legend of the graph.
X = [3 9 27]; % dependent vectors of interest
Y = [10 8 6];
Z = [4 4 4];
t = [1 2 3]; % independent vector
3. Plot following 4 outputs on a single graph window. Mention x-label, y-label on each.
And display your Roll no: on top of the graph window.
y1=2x,
y2=4x,
y3=6x,
y4=8x, x= -5:0.1:5
9|Page