You are on page 1of 25

Topic 4

Simple Two Dimensional Plot


Single plot
Formatting Plot
Multiple plot
Formatting Text in Plot
Formatting Plot with Plot Editor

1
Introduction
• Graphs are useful tool for presenting information.
• MATLAB has many commands that can be used for creating
different types of plots.
– Standard plots with linear axes, log and semi log,
– Bar, stair, polar plots
– Three dimensional contour surface, mesh plots, etc.

This course only discuss standard and bar plots.


2
plot Command
plot command is used to create 2D plots

plot (x,y)
vector vector
Horizontal axis Vertical axis

• x and y vectors must have the same number of elements.


• Straight-line drawn connecting the points.
• When the plot command is executed, a figure is created in
the Figure Window.

3
plot Command
Figure Window
>> t=[1 2 4 7 10];
>> v=[2 5 3 6 5];
>> plot(t,v)

• Default line colour is blue.


• Axes are set up according to
the data.

The plot can be copied and then pasted into other application.
{Edit/Copy Figure}

4
Practice Problem
Draw the bending moment diagram of a simply supported beam
fully loaded uniformly distributed load.
Plot the BMD using segment length of;
a) span/100
b) span/10.

Use linspace function to create x vector (horizontal axis) then calculate bm


(vertical axis) at every x point.

5
plot Command - optional arguments

Additional optional arguments can be used to specify ;


• colour and style of lines
• type of markers
Optional arguments

plot(x,y,’line specifiers’,’property name’,property value)

Properties with values to specify


Define the type, the line width, marker ‘s size
colour and marker and, edge and fill colours

6
plot Command - line specifiers
plot(x,y, ’line specifiers’,’property name’,’property value’)

1) Line Style Specifiers 2) Line Colour Specifiers 3) Marker Type Specifiers


Line Specifier Marker Type Specifier
Line Style Specifier
Colour Plus sign +
solid (default) - red r cross *
dashed -- green g point .
dotted : blue b circle o
Dash-dot -. cyan c square s
magenta m diamond d
yellow y Five-pointed star p
black k six-pointed star h
white w triangle (pointed up) ^(v,>,<)

The three specifiers (style, colour, type) can be typed in any order. 7
plot Command - line specifiers
Command Result
example
plot(x,y) A blue line connects the points with no
markers (default)
plot(x,y,’r’) A red line connects the points
plot(x,y,’--g’) A green dashed line connects the points

plot (x,y,’*’) The points are marked with * ,


but no line between the points
plot(x,y,’g:d’) A green dotted line connects the points
that are marked with diamond markers
8
plot Command
Time 6 7 8 9 10 11 12
Average speed 85 70 50 65 85 75 60

>> time=[6:12];
>> speed=[85 70 60 65 85 75 60];
>> plot(time,speed,'--m^‘)

Line
specifiers

9
plot Command - property name & value
plot(x,y,’line specifiers’,’property name’,property value)

Property Name Description Property Values


LineWidth Specifies the width Units of points
(linewidth) of the line (default 0.5)
MarkerSize Specifies the size of Units of points
(markersize) marker
MarkerEdgeColor Specifies the colour Colour specifiers,
(markeredgecolor) of the marker , or typed as string
the edge line for
filled markers
MarkerFaceColor Specifies the Colour specifiers ,
(markerfacecolour) colours of the filling typed as string
in filled markers
10
plot Command
Time 6 7 8 9 10 11 12
Average speed 85 70 50 65 85 75 60

>> time=[6:12];
>> speed=[85 70 60 65 85 75 60];
>> plot(time,speed,'--m^','linewidth',2,
'markersize',10,'markerfacecolor','b')

line colour & type, point


marker type and colour set
in the plot command

The plot can be copied


and then pasted into
other application.
{Edit/Copy Figure}
11
Formatting Plot
Labeling axes,
putting title and legends,
controlling x and y axes
displaying grid.

12
Formatting Plot
• plot only create bare plot.
• Other information such as;
 axis labels, plot title, legend, text can be added.
 grid and axis range can be specified

title x
legend
text
x x
Y label

x
13
X label
Formatting Plot - cont’d
• help function will show the many options.
• Simple plot can be done in command window (as
shown in the previous slide)
• However, it is desirable to customised the plot with
labels, title, legend, etc; so it make more sense to
do this in a script.

• Formatting of graph can be done in;


a) MATLAB command using;
i. plot
ii. fplot (to plot mathematical functions).
b) plot editor (interactively).
14
Formatting Plot - using commands

xlabel(‘string’)
ylabel(‘string’)
title(‘string’)
1st text character
is at x,y
text(x,y,’string’)
Text position specified by
gtext(‘string’) the user using the mouse

legend(‘string’,’string’,...,pos)

Strings are placed next to the line sample (optional) a number that
in the order in which they were created specifies the position to be
placed. {-1,0,1,2,3,4} 15
Formatting a Plot - using commands - cont’d
axis and grid
Command Description
axis([xmin,xmax,ymin,ymax]) Sets x and y axis limits
axis equal Sets same scale for both axes
axis square Sets axes region to be square
axis tight Sets axis limits to the range of
the data
grid on Adds grid line
grid off Removes grid line

grid can also be used as toggle, instead of grid on and grid off.
16
Plot formatting - Try this!!!
Draw BDM of a simply supported beam fully loaded with udl. The
BDM should have all the information as shown in the sample below.
Bending Moment Diagram
80
Maximum BM is 73.4
Load intensity q = 12.0
70 Beam span l = 7.0

60

50
BM value

40

30

20

10 This plot copied from


MATLAB figure using
0
{Edit/Copy Figure}
0 1 2 3 4 5 6 7
distance from left support
Plot a mathematical function using fplot
𝒙𝟑 +𝟓𝒙
Plot the function, y = from x = 1,2,3,4, …. 9
𝟒𝒙𝟐 −𝟕
>> x=1:9;
>> y=(x.^3+5*x)./(4*x.^2-7);
>> plot(x,y)
2.5
The step value in vector x is
2 too large resulting
incorrect /misleading plot
1.5

1 plot the function again


0.5
using x interval of 0.1
0 This new plot is much
better than the previous
-0.5

-1
For mathematical function, use fplot
-1.5

-2
1 2 3 4 5 6 7 8 9 18
fplot Function
fplot function

MATLAB can plot a function, e.g. y = f(x),


by using the ;
• plot command (element-by-element) - done
• fplot command

20
Plot a Function using plot command
To plot y= 3.5-0.5xcos(6x) for x between -2 and 4.
% Plot of function: 3^(-0.5x) *
Script cos(6x)
File
x=[-2:0.01:4];
y=3.^(-0.5*x).*cos(6*x); Element by element operation
plot(x,y)

The plot is made up of


straight line segments
Figure What if the larger
Window spacing is used , say
0.3 instead of 0.01?

21
Plot a Function using fplot command

fplot command plots a function, y = f(x), between


specified limits.

fplot(’function’,limits,‘line specifiers’)

Function to plot Domain of x and, Specifiers that define


optionally, limits of the type & colour and
y axis markers(optional)

1. Type function directly


or
2.use anonymous function

22
Plot a Function using fplot command
fplot(’function’,limits,‘line specifiers’)

limits
• vector of two elements to specify the domain of x
[xmin,xmax]
• vector of four elements to specify the domain of x
and limits the y axis
[xmin,xmax,ymin,ymax]

line specifier
Same as plot command
23
Plot a Function using fplot command
fplot(’function’,limits,‘line specifiers’)

function
1. function can be typed directly .
e.g. 3^(-0.5*x)*cos(6*x)
The function can be typed as a function of any letter;
e.g. 3^(-0.5*b) * cos(6*b)
• Cannot include previously defined variables.
• Not possible to assign 3 to a variable in the above
function, and then use the variable in the function.

2. Use anonymous function


e.g. t=@(x) 3^(-0.5*x)*cos(6*x)
24
fplot Command - Example
Directly typed function
>> fplot('x^2+4*sin(2*x)-1',[-3 4],’k')
Anonymous function
>> t=@(x) x^2+4*sin(2*x)-1;
>> fplot(t,[-3 4])

25

You might also like