0% found this document useful (0 votes)
54 views

MATLAB Graphics MATLAB Graphics

MATLAB can be used to create various types of plots and graphs. The plot() function is used to create basic 2D and 3D plots with at least two input parameters for x and y coordinates. Additional parameters can specify properties like color, line style, titles, labels, legends, etc. The hold on command allows plotting multiple graphs on the same figure. Multiple plots can also be generated on separate axes using plotyy. Other plot types include single lines/vectors and using the line command.

Uploaded by

naeem ullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

MATLAB Graphics MATLAB Graphics

MATLAB can be used to create various types of plots and graphs. The plot() function is used to create basic 2D and 3D plots with at least two input parameters for x and y coordinates. Additional parameters can specify properties like color, line style, titles, labels, legends, etc. The hold on command allows plotting multiple graphs on the same figure. Multiple plots can also be generated on separate axes using plotyy. Other plot types include single lines/vectors and using the line command.

Uploaded by

naeem ullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

MATLAB Graphics

MATLAB Graphics

1
MATLAB Graphics

MATLAB Graphics
For Academic use only. No commercial use without prior permission of

 Create plots in MATLAB


 Function: plot()
• e.g. plot(x,y) % For 2D plot, minimum 2 parameters
o Code: x=0:pi/10:4*pi; y=sin(x) and plot(x,y)
o % Use other parameters
o plot(x, y, 'r') or
o plot(x, y, 'r:')
• For more color options, see slide # 5.
• title (‘My plot Title')
Javaid.Ahmad@nu.edu.pk

• xlabel (‘my plot x label’);


•ylabel (‘my plot y label’)
• grid on/off
• legend ('sin(x) Curve’)
• text (x pos, y pos, ‘Your text’

2
MATLAB Graphics

MATLAB Graphics (Cont’d) Legend


For Academic use only. No commercial use without prior permission of

Plot title
Light Intensity as a Function of Distance
1200
Theory
Experiment

y axis 1000
label
Text
Tick-mark
800
INTENSITY (lux)

Comparison between theory and experiment.

600

400
Javaid.Ahmad@nu.edu.pk

Data symbol

200

0
8 10 12 14 16 18 20 22 24
DISTANCE (cm)
x axis Tick-mark label
label
3
MATLAB Graphics

MATLAB Graphics (Cont’d)


For Academic use only. No commercial use without prior permission of

 For multiple plots, use hold on command


• e.g. x=0:pi/10:4*pi;
• y=4*sin(x);
• z=3*cos(x);
• yz = 3*cos(x) + 4*sin(x);
• plot(x,y,'r-*')
• % specify color in RGB format
• plot(x,y, 'color', [1 0 1], 'linestyle', ':')
• hold on;
Javaid.Ahmad@nu.edu.pk

• plot(x,z, 'g:s')
• hold on;
• plot(x,yz, ‘b-’)

4
MATLAB Graphics

MATLAB Graphics (Cont’d)


For Academic use only. No commercial use without prior permission of

 Multiple plots on same figure,


 x = 0:pi/100:2*pi;
 y1 = sin(x);
 y2 = sin(x-0.25);
 y3 = sin(x-0.5);
 figure
 plot(x,y1,x,y2,'--',x,y3,':')
 Plot with two Y-axes
•e.g. x=0:pi/10:10*pi; y1=sin(x);
Javaid.Ahmad@nu.edu.pk

• y2=exp(x);
• plotyy(x,y1,x,y2)

5
MATLAB Graphics

MATLAB Graphics (Cont’d)


For Academic use only. No commercial use without prior permission of

 Other plots:
 To plot single line/vector,
 use plot(x)
• e.g. x=0:pi/10:10*pi;
• plot(x)
 use Line command
• e.g. x = 0:pi/100:2*pi;
• y1 = sin(x);
• line(x, y1)
Javaid.Ahmad@nu.edu.pk

• x = [1 9];
•y = [2 12];
• line(x,y, ‘color', ‘green', ‘lineStyle','--')

6
MATLAB Graphics

MATLAB Graphics (Cont’d)


For Academic use only. No commercial use without prior permission of

 More simple plots:


 To plot single line/vector,
 use plot(x)
• e.g. x=0:pi/10:10*pi;
• plot(x)
 use Line command
• e.g. x = 0:pi/100:2*pi;
• y1 = sin(x);
• line(x, y1)
Javaid.Ahmad@nu.edu.pk

• x = [1 9];
•y = [2 12];
• line(x,y, ‘Color', ‘green', ‘LineStyle','—’)

7
MATLAB Graphics

MATLAB Graphics (Cont’d)


For Academic use only. No commercial use without prior permission of

 More simple plots (Cont’d):


 fplot() option or ezplot():
• Used to plot a function
• e.g.
• fplot('3*x^3 - x^2 + 2*x +cos(pi*x) - 1', [-4*pi 4*pi])
• fplot('3cos(x) + 4*sin(x)', [-4*pi 4*pi])
• fplot('3*x^3 - x^2 + 2*x +cos(pi*x) - 1', [-4*pi 4*pi])
•fplot('(1+0.9*cos(8*x))*(1+0.1*cos(24*x))*(0.9+0.05*c
os(200*x))*(1+sin(1*x))', [-1*pi 1*pi])
Javaid.Ahmad@nu.edu.pk

• oplot=ezplot('(x^2+y^2-1)^3 - x^2*y^3',[-2,2,-1.5,1.5])
• Also have fun with ezsurf()

8
MATLAB Graphics

MATLAB Graphics (Cont’d)


For Academic use only. No commercial use without prior permission of

 Sub plots: subplot(rows, columns,No.) : create a subplot in


an array

>> subplot(2,3,1); P=1 P=2 P=3

>> subplot(2,3,4);
m
P=4
Javaid.Ahmad@nu.edu.pk

9
MATLAB Graphics

MATLAB 3D Plot & Contour plot


For Academic use only. No commercial use without prior permission of

 surf command
• e.g.
• dx = linspace(0,20,3);
• Lx = linspace(0,20,5);
• M = [0 10 20; 10 40 50; 0 100 150; 195 250 300; 0 150
800];
• surf(dx,Lx,M) % Column of dx must match with
columns of M and columns of Lx must match with rows
of M
Javaid.Ahmad@nu.edu.pk

800

600

400

200

0
20
15 20
10 15
10
5 5
0 0

10
MATLAB Graphics

MATLAB 3D Plot & Contour plot


For Academic use only. No commercial use without prior permission of

 contour command
• e.g.
• dx = linspace(0,20,3);
• Lx = linspace(0,20,5);
• M = [0 10 20; 10 40 50; 0 100 150; 195 250 300; 0 150
800];
• contour(dx,Lx,M)
Javaid.Ahmad@nu.edu.pk

11
For Academic use only. No commercial use without prior permission of
Javaid.Ahmad@nu.edu.pk

 Line style and colour options


MatLab Graphics (Cont’d)
MATLAB Graphics

12
For Academic use only. No commercial use without prior permission of
Javaid.Ahmad@nu.edu.pk

 Plot Marker options


MatLab Graphics (Cont’d)
MATLAB Graphics

13
MATLAB Graphics

Plot Formatting
For Academic use only. No commercial use without prior permission of

 MATLAB Figure editable.


• plot(x, y, 'r') or
• plot(x, y, 'r:')
• For more color options, see slide # 5.
• title (‘My plot Title')
• xlabel (‘my plot x label’);
•ylabel (‘my plot y label’)
• grid on/off
• legend ('sin(x) Curve’)
Javaid.Ahmad@nu.edu.pk

• text (x pos, y pos, ‘Your text’


 Other option is to get plot as object
• oplot=plot(x,y)
• set(oplot, ‘Color',’red’)
• set(oplot, 'LineWidth',2)

14
MATLAB Graphics

MATLAB Figure
For Academic use only. No commercial use without prior permission of

 MATLAB Figure editable.


• Open figure >> Edit menu and edit all possible options
• Save file for later use
• In order to copy in MS word,
o Edit menu >> click ‘Copy Figure’ and paste in MS
word.
 You can also generate code of the figure.
• Open figure, File menu >> ‘Generate Code’
 In
Javaid.Ahmad@nu.edu.pk

15

You might also like