Professional Documents
Culture Documents
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.
plot (x,y)
vector vector
Horizontal axis Vertical axis
3
plot Command
Figure Window
>> t=[1 2 4 7 10];
>> v=[2 5 3 6 5];
>> plot(t,v)
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.
5
plot Command - optional arguments
6
plot Command - line specifiers
plot(x,y, ’line specifiers’,’property name’,’property value’)
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
>> 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)
>> time=[6:12];
>> speed=[85 70 60 65 85 75 60];
>> plot(time,speed,'--m^','linewidth',2,
'markersize',10,'markerfacecolor','b')
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.
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
-1
For mathematical function, use fplot
-1.5
-2
1 2 3 4 5 6 7 8 9 18
fplot Function
fplot function
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)
21
Plot a Function using fplot command
fplot(’function’,limits,‘line specifiers’)
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.
25