Plotting Mat Lab
Plotting Mat Lab
Graphing Using
MATLAB
2
LINE GRAPHS
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
18
16
14
(1,10)
12
(3,12)
10
1 2 3 4 5
6
SIMPLE EXAMPLES
20
18
16
14
12
10
1 2 3 4 5
7
SIMPLE EXAMPLES
1
1 1.5 2 2.5 3 3.5 4
FORMAT OPTIONS (COLOR, LINESTYLE,
8
…)
At the command prompt, type: >> help plot
>> t = -2:0.01:7;
% Generates a vector of 901 t-values from -2
to +7 spaced apart by 0.01
GRAPHING FUNCTIONS
Graph the polynomial function 𝑦 = 𝑡 2 + 3𝑡 + 10
PLOT TOOLS
Plot Tools is another nice option for editing graphs.
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
𝑉𝑐 = 12(1 − 𝑒 −10𝑡 )
>> t = 0:0.01:0.5;
>> y = 12*(1-exp(-10*t));
>> plot(t,y);xlabel('time');ylabel('Voltage');
17
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.