31
1 Matlab Graphics Introduction to Matlab 5 Omed Ghareb Abdullah Sulaimani University Sulaimani University College of Sciences College of Sciences Physics Department Physics Department 1 MATLAB provides a variety of functions for Basic Plotting Commands displaying vector data as line plots, as well as functions for annotating and printing these graphs. The following table summarizes the functions that produce basic line plots. These functions differ in the way they scale the plot's axes. Each accepts input in the form of vectors or matrices and automatically scales the axes to accommodate the data. 2

05 Matlab Graphics

Embed Size (px)

DESCRIPTION

Lecture (5): Matlab Graphics - Sulaimani University - College of Science - Physics Department

Citation preview

Page 1: 05 Matlab Graphics

1

Matlab Graphics

Introduction to Matlab 5

Omed Ghareb AbdullahSulaimani UniversitySulaimani UniversityCollege of SciencesCollege of SciencesPhysics DepartmentPhysics Department

1

MATLAB provides a variety of functions for

Basic Plotting Commands

p ydisplaying vector data as line plots, as well as functionsfor annotating and printing these graphs. Thefollowing table summarizes the functions that producebasic line plots. These functions differ in the way theyscale the plot's axes. Each accepts input in the form ofvectors or matrices and automatically scales the axesto accommodate the data.

2

Page 2: 05 Matlab Graphics

2

Graphics

Function Description l t G h 2 D d t ith li l f b thplot Graph 2-D data with linear scales for both axes

plot3 Graph 3-D data with linear scales for both axes

loglog Graph with logarithmic scales for both axes

semilogx Graph with a logarithmic scale for the x-axis and a linear scale for the y-axis

semilogy Graph with a logarithmic scale for the y-axis and a linear scale for the x-axis

plotyy Graph with y-tick labels on the left and right side

3

plotLinear 2-D plot

plot(Y) plots the columns of Y versus their index if Y is a real number. IfY is complex, plot(Y) is equivalent to plot(real(Y),imag(Y)). In allother uses of plot, the imaginary component is ignored.

plot(X1,Y1,...) plots all lines defined by Xn versus Yn pairs. If onlyXn or Yn is a matrix, the vector is plotted versus the rows orcolumns of the matrix, depending on whether the vector's row orcolumn dimension matches the matrix.

plot(X1,Y1,LineSpec,...) plots all lines defined by the Xn,Yn,LineSpectriples, where LineSpec is a line specification that determines linetype, marker symbol, and color of the plotted lines. You can mixXn,Yn,LineSpec triples with Xn,Yn pairs:plot(X1,Y1,X2,Y2,LineSpec,X3,Y3).

4

Page 3: 05 Matlab Graphics

3

Example

Plot of a sine waves in the range

plot

x = - pi : 0.1 : pi ;

y = sin ( x ) ;

plot (x , y)0

0.2

0.4

0.6

0.8

1

-4 -3 -2 -1 0 1 2 3 4-1

-0.8

-0.6

-0.4

-0.2

0

5

plotAdding Titles, Axis Labels, and Line Colors

MATLAB enables you to add axis labels and titles ForMATLAB enables you to add axis labels and titles. For example, using the graph from the previous example, add an x- and y-axis label:

plot (x , y , ' ro- ')

xlabel('-\pi \leq \Theta \leq \pi')( p q q p )

ylabel('sin(\Theta)')

title('Plot of sin(\Theta)')

6

Page 4: 05 Matlab Graphics

4

plot

0.8

1Plot of sin(Θ)

-0.2

0

0.2

0.4

0.6

sin(Θ

)

-4 -3 -2 -1 0 1 2 3 4-1

-0.8

-0.6

-0.4

-π ≤ Θ ≤ π7

b blue           .      point               ‐ solidg      green          o      circle              :      dotted      d                  k                   d hd t 

colure marker symbol line style

r      red            x      x‐mark              ‐.     dashdot c      cyan           +      plus                ‐‐ dashed   m      magenta        *      stary      yellow         s      squarek      black          d      diamond

v      triangle (down)g ( )^      triangle (up)< triangle (left)> triangle (right)p pentagramh      hexagram 8

Page 5: 05 Matlab Graphics

5

You can use Greek letters in your labels by putting a backslash (\) before the name of the letter

Improving your labels

( )

\\sigmasigma σσ \\SigmaSigma ΣΣ \\PhiPhi ΦΦ

\\deltadelta δδ \\DeltaDelta ∆∆ \\inftyinfty ∞∞

\\leqleq ≤≤ \\geqgeq ≥≥ \\neqneq ≠≠

To create a superscript use curly bracketsTo create a superscript use curly brackets

title(‘x^{2}’)gives x2

9

plotAdding Titles, Axis Labels, and Line Colors

Th  i t ti

0.25

0.3

0.35

0.4

0.45

0.5

0.55

The instructionstitle, ylabel, xlabel and text are used to add text 

0.5 1 1.5 2 2.5 3

0.05

0.1

0.15

0.2to a figure in several positions

10

Page 6: 05 Matlab Graphics

6

plotAdding Titles, Axis Labels, and Line Colors

MATLAB enables you to add axis labels and titles ForMATLAB enables you to add axis labels and titles. For example, using the graph from the previous example, add an x- and y-axis label:

plot (x , y , ' ro- ' , 'linewidth' ,3)

xlabel('-\pi \leq \Theta \leq \pi' , 'FontSize',18)

Line Width (default: 0.5)Line Width (default: 0.5)

( p q q p , , )

ylabel('sin(\Theta)' , 'FontSize',18)

title('Plot of sin(\Theta)' , 'FontSize',18)

text(0,0,' \leftarrow sin(\pi)' , 'FontSize',18)11

plot

0.8

1Plot of sin(Θ)

-0.2

0

0.2

0.4

0.6

sin(Θ

)

← sin(π)

-4 -3 -2 -1 0 1 2 3 4-1

-0.8

-0.6

-0.4

-π ≤ Θ ≤ π 12

Page 7: 05 Matlab Graphics

7

plotPlotting two curves on the same figures.

x = - pi : 0.1 : pi ;

2

3Plot of two carves

p p ;

y = sin ( x ) ;

z = tan(sin(x)) - sin(tan(x));

plot (x , y , ' ro- ', x , z , ' b.: ')

-4 -3 -2 -1 0 1 2 3 4-3

-2

-1

0

1

-π ≤ Θ ≤ π

y &

z

xlabel(' -\pi \leq \Theta \leq \pi ')

ylabel(' y & z ')

title(' Plot of two carves ')13

plotx = - pi : 0.1 : pi ;

y = sin ( x ) ;

2

3Plot of two carves

y sin ( x ) ;

z = tan(sin(x)) - sin(tan(x));

plot (x , y , ' ro- ', x , z , ' b.: ')

xlabel(' -\pi \leq \Theta \leq \pi ')

-4 -3 -2 -1 0 1 2 3 4-3

-2

-1

0

1

-π ≤ Θ ≤ π

y &

z

y-functionz-function

ylabel(' y & z ')

title(' Plot of two carves ')

legend('y-function','z-function')14

Page 8: 05 Matlab Graphics

8

0.2

0.4

0.6

0.8

1Plot of sin(Θ)

plotx = - pi : 0.1 : pi ;

y = sin ( x ) ;

z = tan(sin(x)) - sin(tan(x));

-4 -3 -2 -1 0 1 2 3 4-1

-0.8

-0.6

-0.4

-0.2

0

-π ≤ Θ ≤ π

sin(Θ

)

2

3Plot of two carves

plot (x , y , ' ro- ')

xlabel(' -\pi \leq \Theta \leq \pi ')

ylabel(' y & z ')

title(' Plot of two carves ')

-4 -3 -2 -1 0 1 2 3 4-3

-2

-1

0

1

-π ≤ Θ ≤ π

y &

z

y-functionz-function

pause

hold

plot( x , z , ' b.: ')

legend('y-function','z-function')15

plotx = - pi : 0.1 : pi ;

y = sin ( x ) ;

z = tan(sin(x)) - sin(tan(x));

0.4

0.6

0.8

1 Plot of two carves

-2

0

2

4

plot (x , y , ' ro- ')

xlabel(' -\pi \leq \Theta \leq \pi ')

ylabel(' y & z ')

title(' Plot of two carves ')

-4 -3 -2 -1 0 1 2 3 4-1

-0.8

-0.6

-0.4

-0.2

0

0.2

-π ≤ Θ ≤ π

y &

z

-4 -2 0 2 4-4axes('position',[0.2 0.6 0.3 0.25]);

plot( x , z , ' b.: ')

16

Page 9: 05 Matlab Graphics

9

stemA two-dimensional stem plot displays data as lines extending from a baseline along the x-axis.

stem(Y) plots the data sequence Y as stems that extend from equallystem(Y) plots the data sequence Y as stems that extend from equally spaced and automatically generated values along the x-axis. When Y is a matrix, stem plots all elements in a row against the same x value.

stem(X,Y) plots X versus the columns of Y. X and Y are vectors or matrices of the same size. Additionally, X can be a row or a column vector and Y a matrix with length(X) rows.

stem(...,'fill') specifies whether to color the circle at the end of the stem.

stem(...,LineSpec) specifies the line style, marker symbol, and color for the stem and top marker (the base line is not affected).

17

0.2

0.4

0.6

0.8

1

z

Plot of two carves stemx = - pi : 0.1 : pi ;

y = sin ( x ) ;

z = tan(sin(x)) - sin(tan(x));

2

3 Plot of two carves

-4 -3 -2 -1 0 1 2 3 4-1

-0.8

-0.6

-0.4

-0.2

0

-π ≤ Θ ≤ π

y &

z

stem(x , y , ' ro- ')

xlabel(' -\pi \leq \Theta \leq \pi ')

ylabel(' y & z ')

title(' Plot of two carves ')

-4 -3 -2 -1 0 1 2 3 4-3

-2

-1

0

1

-π ≤ Θ ≤ π

y &

z

y-functionz-function

pause

hold

stem( x , z , ' b.: ')

legend('y-function','z-function')18

Page 10: 05 Matlab Graphics

10

fillfills the 2-D polygon

fill(X,Y,C) creates filled polygons from the data in X and Y with vertex color specified by C C is a vector or matrix used as anvertex color specified by C. C is a vector or matrix used as an index into the colormap. If C is a row vector, length(C) must equal size(X,2) and size(Y,2); if C is a column vector, length(C) must equal size(X,1) and size(Y,1). If necessary, fill closes the polygon by connecting the last vertex to the first.fill(X,Y,ColorSpec) fills two-dimensional polygons specified by X and Y with the color specified by ColorSpec.p y pfill(X1,Y1,C1,X2,Y2,C2,...) specifies multiple two-dimensional filled areas.

19

x = - pi : 0.1 : pi ;

y = sin ( x ) ;

z = tan(sin(x)) - sin(tan(x)); 0.2

0.4

0.6

0.8

1 Plot of two carves

fill

fill(x , y , ' r ')

xlabel(' -\pi \leq \Theta \leq \pi ')

ylabel(' y & z ')

title(' Plot of two carves ')

-4 -3 -2 -1 0 1 2 3 4-1

-0.8

-0.6

-0.4

-0.2

0

-π ≤ Θ ≤ π

y &

z

2

3 Plot of two carves

y-functionz-function

pause

hold

fill( x , z , ' b ')

legend('y-function','z-function') -4 -3 -2 -1 0 1 2 3 4-3

-2

-1

0

1

-π ≤ Θ ≤ π

y &

z

20

Page 11: 05 Matlab Graphics

11

x = - pi : 0.1 : pi ;

y = sin ( x ) ;

z = tan(sin(x)) - sin(tan(x));

stairs

0.2

0.4

0.6

0.8

1 Plot of two carves

stairs(x , y , ' r ')

xlabel(' -\pi \leq \Theta \leq \pi ')

ylabel(' y & z ')

title(' Plot of two carves ')

-4 -3 -2 -1 0 1 2 3 4-1

-0.8

-0.6

-0.4

-0.2

0

-π ≤ Θ ≤ π

y &

z

2

3 Plot of two carves

y-functionz-function

pause

hold

stairs( x , z , ' b ')

legend('y-function','z-function') -4 -3 -2 -1 0 1 2 3 4-3

-2

-1

0

1

-π ≤ Θ ≤ π

y &

z

21

Function PlotsFunction plots allow you to use a function as input to a plot command, instead of a set of ordered pairs of x-y values

fplot('sin(x)',[‐2*pi,2*pi])

function input as a

range of the independent p

stringp

variable – in this case x

22

Page 12: 05 Matlab Graphics

12

Plots with Error Barserrorbar(X,Y,E) plots X versus Y with symmetric error bars 2*E(i)

long. X Y E must be the same size When they are vectors each errorX, Y, E must be the same size. When they are vectors, each error bar is a distance of E(i) above and below the point defined by (X(i),Y(i)). When they are matrices, each error bar is a distance of E(i,j) above and below the point defined by (X(i,j),Y(i,j)).

errorbar(X,Y,L,U) plots X versus Y with error bars L(i)+U(i) long specifying the lower and upper error bars. X Y L and U must be the same size When they are vectorsX, Y, L, and U must be the same size. When they are vectors, each error bar is a distance of L(i) below and U(i) above the point defined by (X(i),Y(i)). When they are matrices, each error bar is a distance of L(i,j) below and U(i,j) above the point defined by(X(i,j),Y(i,j)).

23

12

Plots with Error Bars

x=linspace(1,10,20);

10 /

4

6

8

10y=10./x;

er=randn(20,1);

errorbar(x,y,er);

0 2 4 6 8 10 12-2

0

2

‘‘erer’ is the array containing the error on the ’ is the array containing the error on the data. The data. The error bar is drawn from ‘yerror bar is drawn from ‘y--erer’ to ‘’ to ‘y+ery+er’’

24

Page 13: 05 Matlab Graphics

13

plot3The plot3 function displays a three-dimensional plot of a set of data points.

plot3(X1,Y1,Z1,...), where X1, Y1, Z1 are vectors or matrices, plots one or more lines in three-dimensional space through the points whose coordinates are the elements of X1, Y1, and Z1.

plot3(X1,Y1,Z1,LineSpec,...) creates and displays all lines defined by the Xn Yn Zn LineSpec quads where LineSpecdefined by the Xn,Yn,Zn,LineSpec quads, where LineSpecis a line specification that determines line style, marker symbol, and color of the plotted lines.

25

plot3Example

Plot a three-dimensional helix.

20

25

30

35

t = 0:pi/50:10*pi;

plot3(sin(t),cos(t),t)

-1

-0.5

0

0.5

1

-1

-0.5

0

0.5

10

5

10

15

26

Page 14: 05 Matlab Graphics

14

plot3Example

Plot a three-dimensional helix.

15

20

25

30

35t = 0:pi/50:10*pi;

plot3(sin(t),cos(t),t)

grid on

-1

-0.5

0

0.5

1

-1

-0.5

0

0.5

10

5

10axis square

27

10

stem3Example

10 5

10

2

4

6

8

10

T=0:0.1:10;

-1

-0.5

0

0.5

1

-1

-0.5

0

0.5

S=0.1+i;

Y=exp(-S*T);

stem3(real(Y),imag(Y),T,'fill');

28

Page 15: 05 Matlab Graphics

15

loglogloglog(Y) plots the columns of Y versus their index if Y contains

real numbers. If Y contains complex numbers, loglog(Y)and loglog(real(Y) imag(Y)) are equivalent loglog ignoresand loglog(real(Y),imag(Y)) are equivalent. loglog ignores the imaginary component in all other uses of this function.

loglog(X1,Y1,...) plots all Xn versus Yn pairs. If only Xn or Ynis a matrix, loglog plots the vector argument versus the rows or columns of the matrix, depending on whether the vector's row or column dimension matches the matrix.

29

loglogExample

Create a simple loglog plot with square markers.

20

1025

1030

1035

1040

1045

x = logspace(-1,2);

loglog(x,exp(x),'-s')

grid on

10-1 100 101 102100

105

1010

1015

1020

default

50 points

30

Page 16: 05 Matlab Graphics

16

linspace (X1, X2) generates a row vector of (100) linearly equally d i b 1 d 2

linspace & logspace

spaced points between X1 and X2.linspace (X1, X2, N) generates N points between X1 and X2.

For N < 2, linspace returns X2.

logspace (X1, X2) generates a row vector of (50) logarithmically equally spaced points between decades 10^X1 and 10^X2 If X2equally spaced points between decades 10 X1 and 10 X2. If X2 is pi, then the points are between 10^X1 and pi.

logspace (X1, X2, N) generates N points.For N < 2, logspace returns 10^X2.

31

semilogx , semilogysemilogx and semilogy plot data as logarithmic scales for the x- and y-axis,

respectively, logarithmic.

semilogx(Y) t l t i b 10 l ith i l f th i dsemilogx(Y) creates a plot using a base 10 logarithmic scale for the x-axis anda linear scale for the y-axis. It plots the columns of Y versus their index if Ycontains real numbers. semilogx(Y) is equivalent to semilogx(real(Y),imag(Y)) if Y contains complex numbers. semilogx ignores the imaginarycomponent in all other uses of this function.

semilogx(X1,Y1,...) plots all Xn versus Yn pairs. If only Xn or Yn is amatrix, semilogx plots the vector argument versus the rows or columns ofthe matrix, depending on whether the vector's row or column dimensionmatches the matrix.

semilogx(X1,Y1,LineSpec,...) plots all lines defined by theXn,Yn,LineSpec triples. LineSpec determines line style, marker symbol, andcolor of the plotted lines.

32

Page 17: 05 Matlab Graphics

17

semilogx , semilogy

Example

C i l il l

102

103

Create a simple semilogy plot.

x = 0:.1:5;

semilogy(x,10.^x)

0 0.5 1 1.5 2 2.5 3100

101grid on

33

plotyyCreate graphs with y-axes on both left and right side

plotyy(X1 Y1 X2 Y2) plots X1 versus Y1 with y-axis labelingplotyy(X1,Y1,X2,Y2) plots X1 versus Y1 with y-axis labeling on the left and plots X2 versus Y2 with y-axis labeling on the right.

plotyy(X1,Y1,X2,Y2,function) uses the specified plotting function to produce the graph. 'function' can be plot, stem , semilogx, semilogy, loglog

plotyy(X1,Y1,X2,Y2,'function1','function2') uses function1(X1,Y1) to plot the data for the left axis and function2(X2,Y2) to plot the data for the right axis.

34

Page 18: 05 Matlab Graphics

18

plotyyExampleThis example graphs two mathematical functions using plot as the plotting function. The two y-axes enable you to display both sets of data on one graph even though relative values of the data are quite different.

x = 0:0.01:20;

y1 = 200*exp(-0.05*x).*sin(x);

y2 = 0.8*exp(-0.5*x).*sin(10*x);

plotyy(x,y1,x,y2,'plot')35

150

200

0.6

0.8

plotyy

-50

0

50

100

-0.2

0

0.2

0.4

0 2 4 6 8 10 12 14 16 18 20-200

-150

-100

0 2 4 6 8 10 12 14 16 18 20-0.8

-0.6

-0.4

36

Page 19: 05 Matlab Graphics

19

You can make indications on the plot, like title and labels and so on, make the following changes to previous example;

plotyy

x = 0:0.01:20;

y1 = 200*exp(-0.05*x).*sin(x);

y2 = 0.8*exp(-0.5*x).*sin(10*x);

[AX,H1,H2] = plotyy(x , y1 , x , y2 , ' plot ' );

l b l(' Z t 20 \ ')xlabel(' Zero to 20 \musec. ')

title(' Labeling plotyy ')

set(H1, ' LineStyle ', ' - - ')

set(H2, ' LineStyle ', ' : ' )37

150

200Labeling plotyy

0.6

0.8

plotyy

-50

0

50

100

-0.2

0

0.2

0.4

0 2 4 6 8 10 12 14 16 18 20-200

-150

-100

Zero to 20 µsec.0 2 4 6 8 10 12 14 16 18 20

-0.8

-0.6

-0.4

38

Page 20: 05 Matlab Graphics

20

Basic PlottingFigure Control☺ pause, pause(n)☺ figure, figure(n)

Multiple Plots ☺ hold on, hold off ☺ plot(X, Y, W, Z)☺ plotyy(X, Y, W, Z)

39

polarPolar coordinate plot.

POLAR(THETA, RHO) makes a plot using polar coordinates of the angle THETA in radians versus the radius RHO

0.6

0.8

1

30

60

90

120

150

theta=linspace(0,2*pi,100);

the angle THETA, in radians, versus the radius RHO.

POLAR(THETA,RHO,S) uses the linestyle specified in string S. (description of legal linestyles).

0.2

0.4

210

240

270

300

330

180 0

r=1./(1+theta.^2);

polar(theta,r,’.r:’);

40

Page 21: 05 Matlab Graphics

21

polarPolar coordinate plot.

POLAR(THETA, RHO) makes a plot using polar coordinates of the angle THETA in radians versus the radius RHO

t=0:.01:2*pi;

the angle THETA, in radians, versus the radius RHO.

POLAR(THETA,RHO,S) uses the linestyle specified in string S. (description of legal linestyles).

0.3

0.4

0.5

30

60

90

120

150

r=abs(sin(2*t).*cos(2*t));

polar(t,r,’-r’);

0.1

0.2

210

240

270

300

330

180 0

41

subplot subplot divides the current figure into rectangular panes that are numbered row-wise. Each pane contains an axes. Subsequent plots are output to the current paneSubsequent plots are output to the current pane.

subplot(m,n,p) creates an axes in the p-th pane of a figure divided into an m-by-n matrix of rectangular panes. The new axes becomes the current axes. If p is a vector, it specifies an axes having a position that covers all the subplot positions listed in p.

subplot(m,n,p,'replace') If the specified axes already exists, delete it and tcreate a new axes.

subplot(m,n,p,'align') positions the individual axes so that the plot boxes align, but does not prevent the labels and ticks from overlapping.

subplot(h) makes the axes with handle h current for subsequent plotting commands.

42

Page 22: 05 Matlab Graphics

22

subplot Example

To plot income in the top half of a figure and outgo in

3.5

4

4.5

5

5.5

6

To plot income in the top half of a figure and outgo in the bottom half,

income = [3.2 4.1 5.0 5.6];

outgo = [2 5 4 0 3 35 4 9];1 1.5 2 2.5 3 3.5 4

3

1 1.5 2 2.5 3 3.5 42.5

3

3.5

4

4.5

5

outgo = [2.5 4.0 3.35 4.9];

subplot(2,1,1); plot(income)

subplot(2,1,2); plot(outgo)

43

subplot

44

Page 23: 05 Matlab Graphics

23

The following illustration shows four subplot regions and indicates the command used to create each.

subplot

45

The following combinations produce asymmetrical arrangements of subplots.

subplot

subplot(2,2,[1 3])

subplot(2,2,2)

subplot(2,2,4)

46

Page 24: 05 Matlab Graphics

24

You can also use the colon operator to specify multiple locations if they are in sequence.

subplot

subplot(2,2,1:2)

subplot(2,2,3)

subplot(2,2,4)

47

Write a script file to show the beat phenomena in waves% Ploting Wave Profile

Example

a1 = 1; a2 = 1.5; c1 = 2.0; c2 = 1.8;

time = 0:0.1:100;

wave1 = a1 * sin(c1*time);

wave2 = a2 * sin(c2*time);

wave3 wave1 + wave2;wave3 = wave1 + wave2;

plot(time,wave1,time,wave2,time,wave3)

xlabel ('time'); ylabel ('wave elevation');

title ('Wave Profile')48

Page 25: 05 Matlab Graphics

25

Example

2

2.5Wave Profile

-0.5

0

0.5

1

1.5

wav

e el

evat

ion

0 10 20 30 40 50 60 70 80 90 100-2.5

-2

-1.5

-1

time49

clf % clear the graphics window

a1 = 1; a2 = 1.5; c1 = 2.0; c2 = 1.8;

% Display the results in three subplots

AX

IS([

time = 0:0.1:100;

wave1 = a1 * sin(c1*time);

wave2 = a2 * sin(c2*time);

wave3 = wave1 + wave2;

subplot(3,1,1) % top figure

plot(time,wave1,'m'); axis([0 100 -3 3]); ylabel('wave 1');

[XM

IN X

MA

X Y

MINp y

subplot(3,1,2) % middle figure

plot(time,wave2,'g'); axis([0 100 -3 3]); ylabel('wave 2');

subplot(3,1,3) % bottom figure

plot(time,wave3,'r'); axis([0 100 -3 3]); ylabel('waves 1&2');

xlabel('time');

N Y

MA

X])

50

Page 26: 05 Matlab Graphics

26

0

2

ve 1

% Display the results in three subplots

0 10 20 30 40 50 60 70 80 90 100

-2

0w

av

-2

0

2

wav

e 2

0 10 20 30 40 50 60 70 80 90 100

0 10 20 30 40 50 60 70 80 90 100

-2

0

2

wav

es 1

&2

time

51

x=0:0.1:10;

Practic

Subplot (m,n,p) Multiple plots

x 0:0.1:10;

y1=sin(pi*x); y2=sin(0.5*pi*x); y3=y1+y2;

z1=cos(pi*x); z2=cos(0.5*pi*x); z3=z1-z2;

subplot(3,2,1); plot(x,y1,'b');

subplot(3,2,2); plot(x,z1,'b');

subplot(3 2 3); plot(x y2 'm');subplot(3,2,3); plot(x,y2,'m');

subplot(3,2,4); plot(x,z2,'m');

subplot(3,2,5); plot(x,y3,'r');

subplot(3,2,6); plot(x,z3,'r');

52

Page 27: 05 Matlab Graphics

27

Subplot (m,n,p) Multiple plots

0

0.5

1

0

0.5

1

0 2 4 6 8 10-1

-0.5

0

0 2 4 6 8 10-1

-0.5

0

-1

-0.5

0

0.5

1

-1

-0.5

0

0.5

1

0 2 4 6 8 101

0 2 4 6 8 101

0 2 4 6 8 10-2

-1

0

1

2

0 2 4 6 8 10-2

-1

0

1

2

53

x=0:0.1:10;

Figure

Create figure window

x 0:0.1:10;

y1=sin(pi*x); y2=sin(0.5*pi*x); y3=y1+y2;

z1=cos(pi*x); z2=cos(0.5*pi*x); z3=z1-z2;

figure(1); plot(x,y1,'b');

figure(2); plot(x,z1,'b');

figure(3); plot(x y2 'm');figure(3); plot(x,y2,'m');

figure(4); plot(x,z2,'m');

figure(5); plot(x,y3,'r');

figure(6); plot(x,z3,'r');

54

Page 28: 05 Matlab Graphics

28

Exercise and HomeworkUse graphical method to find the root for the following equations :

3 2

4 3 2

2

1 1 25 8 2025 2 11312 2 75 19 18 14 29 3 64563

. ( ) . . .. ( ) . . . .. ( ) cos ( )

f x x x xf x x x x xf x x x

= − − +

= − − − +

= −

following equations :

55

-10

-5

0

5

10

15

1. x=-3:0.5:4;

y=x.^3-1.2500*x.^2-8.2025*x+2.1131;

plot(x,y,'--rs')

Answers

-3 -2 -1 0 1 2 3 4-300

-250

-200

-150

-100

-50

0

50

-3 -2 -1 0 1 2 3 4-15

plot(x,y, rs )

grid on

2. x=-3:0.5:4;

y=x.^4-2.75*x.^3-19.18*x.^2-14.29*x+3.6456;

plot(x,y,'--rs')

grid on

Roots are -2.45 0.25 3.45

2 4 1 2 0 2 6 2

-3 -2 -1 0 1 2 3-10

-8

-6

-4

-2

0

2

3. x=-3:0.5:3;

y=cos(x)-x.^2;

plot(x,y,'--s')

grid on

gRoots are -2.45 -1.2 0.2 6.2

Roots are -0.8 0.8556

Page 29: 05 Matlab Graphics

29

Bitmap images can also be visualized

Graphics

load earth

image(X)

colormap(map)

57

Colormaps50

100

150

200

50

100

150

200

50

100

150

200

50

100

150

200

50 100 150 200 250

50

100

150

200

250

50 100 150 200 250

250

50 100 150 200 250

250

50 100 150 200 250

250

50 100 150 200 250

250

50 100 150 200 250

50

100

150

200

250

50 100 150 200 250

50

100

150

200

250

colormap(map) colormap(gray) colormap(hot) colormap(hot(256))

l ( l) l ( i k) l ( )50 100 150 200 250

50

100

150

200

250

l (b )

50 100 150 200 250

50

100

150

200

25050 100 150 200 250

50

100

150

200

250

50 100 150 200 250

50

100

150

200

250

colormap(cool) colormap(pink) colormap(copper)

colormap(flag) colormap(hsv)50 100 150 200 250

50

100

150

200

250

colormap(lines) colormap(summer)58

colormap(bone)

Page 30: 05 Matlab Graphics

30

Bitmap images can also be visualized

Graphics

load mandrill

image(X)

colormap(map)

59

Reading & WritingA=imread(filename,format)

A=imread('zanko.jpg')

image(A)

colormap(map)

60

Page 31: 05 Matlab Graphics

31

Handling Graphics Images• Reading/Writing Graphics Images

– imread , imwrite– load , save ( if only the image was saved as a MAT file)

• Displaying Graphics Images– image– Imagesc

• Utilities• Utilities– imfinfo– ind2rgb

61