Introduction to
Graphing Using
MATLAB
2
LINE GRAPHS
▪ Useful for graphing functions
▪ Useful for displaying data trends over time
▪ Useful for showing how one variable depends on another
3
LINE GRAPHS
The MATLAB command for creating a line graph is plot.
General Form:
% A single Function
plot(x-coordinates, y-coordinates, optional formatting)
% Multiple Functions
plot(x-values of f1, y-values of f1, formatting for f1, x-values of f2, y-
values of f2, formatting for f2, …)
4
SIMPLE EXAMPLES
X-Coordinates
Y-Coordinates
Formatting
4.5
>> plot(3,4,'r*') 4
3.5
3
2 2.5 3 3.5 4
5
SIMPLE EXAMPLES
X-Coordinates Y-Coordinates
>> plot([1 3 5],[10 12 20])
(5,20)
20
18
16
14
(1,10)
12
(3,12)
10
1 2 3 4 5
6
SIMPLE EXAMPLES
>> plot([1 3 5],[10 12 20],‘rd--')
20
18
16
14
12
10
1 2 3 4 5
7
SIMPLE EXAMPLES
>> plot([1 2 4 1],[1 6 2 1],'m*-','LineWidth',5)
1
1 1.5 2 2.5 3 3.5 4
FORMAT OPTIONS (COLOR, LINESTYLE,
8
…)
At the command prompt, type: >> help plot
Scroll up to see this table of options:
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v triangle (down)
GRAPHING FUNCTIONS 9
Let us Graph the polynomial function 𝑦 = 𝑡 2 + 3𝑡 + 10
Must generate a set of t-values to go on the x-axis then calculate
the corresponding y-values.
Option 1: Write the t-values as a vector
>> t = [-2 -1.5 -1 -0.5 0 0.5 1 1.5 2]
% This works OK if you have only a few t-values
Option 2: Generate values by defining a range
START INCREMENT MAX
>> t = -2:0.01:7;
% Generates a vector of 901 t-values from -2
to +7 spaced apart by 0.01
Option 3: Using the function linspace
>> t = linspace(-2,7,500); % Generates a vector
of 500 t-values evenly spaced from -2 to +7
10
GRAPHING FUNCTIONS
Graph the polynomial function 𝑦 = 𝑡 2 + 3𝑡 + 10
% Generate a vector of t-values for x-axis
>> t = -2:0.01:7;
% Plug each t value into the equation to get
a vector of y-values
>> y = t.^2 + 3*t + 10;
>> plot(t,y)
GRAPHING FUNCTIONS 11
ADDING LABELS AND TITLES
Plots should be labeled and titled. This can be done using
MATLAB commands or by using plot tools.
Commands:
>> xlabel(‘time, t’); ylabel(‘y-values’);
>> title('y = t^2+3t+10'); grid
12
PLOT TOOLS
Plot Tools is another nice option for editing graphs.
You can add labels,
title, grids, legends
and other things to
the graph. You can
also edit properties
of the graph
13
SAVING A PLOT
After editing the graph, you can save it by clicking
on file, then save As. Select the format and
filename, then save the plot.
14
COMMON ERRORS
Choosing the x-axis values poorly.
Increment too large: Poor choice for range of x-axis:
>> t = -2:2:7; >> t = -2:0.01:100;
>> y = t.^2+3*t+10; >> y = t.^2+3*t+10;
>> plot(t,y) >> plot(t,y)
15
EXERCISE 1
Open a new Matlab script for this exercise.
Graph the following functions:
1. 𝑦 = 3𝑡 + 4 use the range 0 ≤ 𝑡 ≤ 10
2. 𝑦 = 𝑡 2 + 2 use the range −5 ≤ 𝑡 ≤ 5
3. 𝑦 = 𝑒 −𝑡 use the range 0 ≤ 𝑡 ≤ 10
In each case, use an increment value of 0.01
Endeavour to label the graphs appropriately.
Save the plot as a Matlab Figure in your folder.
16
SOLVING EQUATIONS GRAPHICALLY
Suppose a capacitor is charging in an RC circuit and the
voltage across the capacitor is given by:
𝑉𝑐 = 12(1 − 𝑒 −10𝑡 )
Vc is in volts and t is in seconds. Plot the voltage across the
capacitor versus time then determine the time at which the
capacitor voltage reaches 9 volts.
>> t = 0:0.01:0.5;
>> y = 12*(1-exp(-10*t));
>> plot(t,y);xlabel('time');ylabel('Voltage');
17
SOLVING EQUATIONS GRAPHICALLY
In the Figure Window, Click on Tools then select Data Tips.
Click on graph – move data cursor if needed using arrow keys.
To add additional datatips, right click on an existing datatip and
select add new datatip.
The capacitor reaches 9 volts
between t = 0.13 seconds and
t = 0.14 seconds.
Note: Our precision is limited
by the increment chosen for t
which was 0.01 seconds in
this example.
18
MULTIPLE PLOTS ON A SINGLE GRAPH
You can Plot more than one function on the same graph using
the ‘hold’ function.
For example, plot each of the following functions on the same
graph and use legends to indicate which line represent which
function
𝑓1 = 𝑡 2 + 2; 𝑓2 = 𝑡 2 − 3; 𝑓3 = 𝑡 2 ;
19
SUBPLOT COMMAND
subplot(m,n,k)
The subplot command splits the figure window into several sub-
windows. The first two entries in subplot show how the window
is to be split up by specifying number of rows and number of
columns. The third entry points to a particular sub-window.
Subplot(3,2,4) would divide the subplot(3,2,1) subplot(3,2,2)
plot window into 3 rows and 2
columns allowing for 6 smaller subplot(3,2,3) subplot(3,2,4)
plot windows and would point
to the 4th sub-window as shown
subplot(3,2,5) subplot(3,2,6)
in the diagram.
20
MULTIPLE PLOTS USING SUBPLOT
Repeat the previous example but put each plot in a separate
sub-window of the figure using subplot.
21
NEW FIGURE WINDOW
If you want your plot on a new figure window, then use the
function ‘figure’ before the code that plots the graph.
22
SOME USEFUL COMMANDS
FOR PLOTTING
plot(x-coordinates, y-coordinates, formatting)
title(‘Insert Desired Title for Plot’)
xlabel(‘Insert label for x-axis’)
ylabel(‘Insert label for y-axis’)
legend(‘Plot1 Label’,’Plot2 Label’, …)
grid % Adds a grid
close % Closes the current figure window
figure % Creates a new figure window
subplot(m,n,k) %Subdivides a figure window into
m by n subwindows & points to the kth subwindow
axis([xmin xmax ymin ymax]) %Set axis scale
hold on %Holds current plot on & allows add-ons
hold off % Turns off the hold