You are on page 1of 16

CHAPTER FIVE : GRAPHICS IN MATLAB

1. Basic 2-D Plots


The basic command for producing a simple 2-D plot is

plot(x values, y values, 'style option')


where
x values and y values are vectors containing the x- and y-coordinates of points on
the graph.
Style option is an optional argument that specifies the color, line-style and the
point-marker style.

The style option in the plot command is a character string that consists of 1, 2 or 3
characters that specify the color and/or the line style. The different color, line-style
and marker-style options are summarized in Table 1.

Table 1 Color, line-style and marker-style options

Example 1.
Plot the function y=sin(x) for 0=<x <= 2pi with 100 points, using MATLAB

Solution
x=linspace(0,2*pi,100);
plot(x,sin(x),'r')
The program plot the function with red color ('r')
Or using the loop ( for … end) statement as

for x=0:pi/50:2*pi
plot (x,sin(x),'g*')
end

The program plot the function with green color ('g') and asterisk ('*') symbol

Example 2
Plot a sine wave signal and the output of uncontrolled rectifier on the same plot
for x=0:pi/100:2*pi
plot (x,sin(x),'g*')
plot (x,abs(sin(x)),'k>')
plot (x,0,'r-')
end

 Put the text on each signal in the plot

Example 3
Plot a sine wave signal and the output of uncontrolled rectifier on the same plot and
put the text on each signal like "input signal" and "rectifier output"

for x=0:pi/100:2*pi
plot (x,sin(x),'g*')
plot (x,abs(sin(x)),'k>')
plot (x,0,'r-')
end
gtext('input signal')
gtext('rectifier output')

Note that when you put the gtext function the program gives you the flexibility about
where you can put the text on the figure.

 Determine or change the Scaling of the axes of a plot

Syntax : axis([minx maxx miny maxy])

Example 4
for x=0:pi/100:2*pi
plot (x,sin(x),'g*')
plot (x,abs(sin(x)),'k>')
plot (x,0,'r-')
hold on
end
axis([0 20 -1.5 1.5])
gtext('input signal')
gtext('rectifier output')
Note that the we change the minimum and maximum values of x-axis to 0 and 20
respectively; and the minimum and maximum values of y-axis to -1.5 and 1.5
respectively.

 Put a Label on the plot axis

Syntax : xlabel('\fontsize{…} the x-axis label')


ylabel('\fontsize{…} the y-axis label')

Example 5
for x=0:pi/100:2*pi
plot (x,sin(x),'g*')
plot (x,abs(sin(x)),'k>')
plot (x,0,'r-')
hold on
end
axis([0 20 -1.5 1.5])
gtext('input signal')
gtext('rectifier output')
xlabel('\fontsize{16} theta in rad.')
ylabel('\fontsize{14} Vin and vout')

 Put a title on the plot


Syntax : title('\fontsize{…} the title of the figure)

Example 6

for x=0:pi/100:2*pi
plot (x,sin(x),'g*')
plot (x,abs(sin(x)),'k>')
plot (x,0,'r-')
hold on
end
axis([0 20 -1.5 1.5])
gtext('input signal')
gtext('rectifier output')
xlabel('\fontsize{16} theta in rad.')
ylabel('\fontsize{14} Vin and vout')
title('\fontsize{20} waveforms of full wave rectifier')
 Put a legend on the plot

Syntax : legend('sign for curve 3' , 'sign for curve 2', ….. , 'sign for curve n'
for x=0:pi/100:2*pi
plot (x,sin(x),'g*')
plot (x,abs(sin(x)),'k>')
plot (x,0,'r-')
hold on
end
axis([0 20 -1.5 1.5])
gtext('input signal')
gtext('rectifier output')
xlabel('\fontsize{16} theta in rad.')
ylabel('\fontsize{14} Vin and vout')
title('\fontsize{20} waveforms of full wave rectifier')
legend('x-axis ','input','output')
 Put a grid on the plot
Syntax : grid on to put the grid
grid off to omit the grid

Example 7.
for x=0:pi/100:2*pi
plot (x,sin(x),'g*')
plot (x,abs(sin(x)),'k>')
plot (x,0,'r-')
hold on
end
axis([0 20 -1.5 1.5])
gtext('input signal')
gtext('rectifier output')
xlabel('\fontsize{16} theta in rad.')
ylabel('\fontsize{14} Vin and vout')
title('\fontsize{20} waveforms of full wave rectifier')
legend('x-axis ','input','output')
grid on

 Zoom on and Zoom off

Syntax : zoom on to activate the zooming.


zoom off to inactivate the zooming.
Example 8.
for x=0:pi/100:2*pi
plot (x,sin(x),'g*')
plot (x,abs(sin(x)),'k>')
plot (x,0,'r-')
hold on
end
axis([0 20 -1.5 1.5])
gtext('input signal')
gtext('rectifier output')
xlabel('\fontsize{16} theta in rad.')
ylabel('\fontsize{14} Vin and vout')
title('\fontsize{20} waveforms of full wave rectifier')
legend('x-axis ','input','output')
grid on
zoom on
 Plot more than one window in the screen
In MATLAB you can plot four curves independently (each curve or plot in window).
The screen is divided in four sub screen (or window or subplot) as follow:

Syntax : subplot(n,m,p)
Where:
n is the number of row.
m is the number of column.
p is the number of window.

If we want to plot graphs in window 1 and 3 we have to write

Subplot (2,1,1) % for the first graph (this graph is plotted in the first window ), and
Subplot (2,1,3) % for the second graph (this graph is plotted in the third window )
Because we determine a screen consists of 2 rows and 1 column (which is 2,1)

If we want to plot graphs in window 1 and 2 we have to write

Subplot (1,2,1) % for the first graph (this graph is plotted in the first window ), and
Subplot (1,2,2) % for the second graph (this graph is plotted in the second window )
Because we determine a screen consists of 1 row and 2 columns (which is 1,2)

If we write the following:


Subplot(1,2,3) this is not correct because we determined the first row and two
columns (1,2) and window 3 does not situated in the region of the first row.(neither in
1st row, 1st column nor 1st row, 2nd column).

If you want to plot in the window no. 3 you have to write

Subplot(2,1,3) or subplot(2,2,3)

If we want to plot graphs in windows 1, 2, 3, and 4 we have to write

Subplot (2,2,1) % for the first graph (this graph is plotted in the window no. 1).
Subplot (2,2,2) % for the second graph (this graph is plotted in the window no. 2).
Subplot (2,2,3) % for the third graph (this graph is plotted in the window no. 3).
Subplot (2,2,4) % for the fourth graph (this graph is plotted in the window no. 4 ).
Because we determine a screen consists of 2 rows and 2 columns (which is 2,2)

NOTE: we have to put the subplot function before the plot function
Example 9.

for t=0:pi/300:2*pi
va=sin(t);
vb=sin(t-2*pi/3);
vc=sin(t-4*pi/3);
subplot(2,1,1)
plot (t,va,'r')
plot (t,vb,'b')
plot (t,vc,'k')
plot (t,0,'k')
hold on
subplot (2,1,2)
plot (t,va-vb,'r')
plot (t,vb-vc,'b')
plot (t,vc-va,'k')
plot (t,vb-va,'r')
plot (t,vc-vb,'b')
plot (t,va-vc,'k')
plot (t,0,'k')
hold on
end
gtext('Va'):gtext('Vb'):gtext('Vc'):gtext('Vab'):gtext('Vbc')
gtext('Vca'):gtext('Vba'):gtext('Vcb'):gtext('Vac')
axis([0 8 -2.5 2.5])
xlabel('\fontsize{16} theta in rad.')
ylabel('\fontsize{14}signals')
title('\fontsize{20} waveforms of AC')
 Plot in stair and bar forms
Syntax : bar(y) and stairs(y) where y is a vector to be plot.

Example 10.
y=[5:5:100]; %y starts with 5 and end with 100 with increasing of 5
subplot(1,2,1)
bar(y);
title('Bar Graph')
subplot (1,2,2)
stairs(y)
title('Stairs Graph')

SOLVED PROBLEMS.

1. Generate an overlay plot for plotting three functions:

Solution:
% using the plot command
clc
t=linspace(0,2*pi,100);
y1=sin(t);y2=t;
y3=t-(t.^3)/6+(t.^5)/120-(t.^7)/5040;
plot(t,y1,t,y2,'-',t,y3,'o')
axis([0 5 -1 5])
xlabel('t')
ylabel('sin(t) approximation')
title('sin(t) function')
text(3.5,0, 'sin(t)')
gtext('Linear approximation')
gtext('4-term approximation')

Note : the command text(3.5,0,'sin(t)') means the text 'sin(t)' is put at the point (3.5,0)
of the curve.
2. Using the functions for plotting x-y data, plot the following functions:

Solution
(a)
% using the plot command
clc
t=linspace(0,10*pi,1000);
y1=t.*sin(t);
plot(t,y1,'ro')
xlabel('t')
ylabel('function of t')
title('(t) function')
(b)
% using the plot command
clc
t=linspace(0,2*pi,100);
x=exp(-2*t);y=t;
plot(t,x,'ro',t,y,'b>')
xlabel('t')
ylabel('function of t')
title('(t) function')

(c)
% using the plot command
clc
t=linspace(0,2*pi,100);
x=exp(2*t);y=t;
plot(t,x,'ro',t,y,'b>'),grid
xlabel('t')
ylabel('function of t')
title('(t) function')

(d)
% using the plot command
clc
t=linspace(0,2*pi,100);
x=exp(t);y=50+x;
plot(t,x,'ro',t,y,'b>'),grid
xlabel('t')
ylabel('function of t')
title('(t) function')

(e)
% using the plot command
clc
t=linspace(0,2*pi,100);
r=sqrt(3*sin(7.*t));y=r.*sin(t);
plot(t,x,'ro',t,y,'b^'),grid
xlabel('t')
ylabel('function of t')
title('(t) function')

(f)
% using the plot command
clc
t=linspace(0,2*pi,100);
r=sqrt(3*sin(4.*t));y=100*sin(t);
plot(t,x,'ro',t,y,'b.'),grid
xlabel('t')
ylabel('function of t')
title('(t) function')

3. Do a program to plot the input and output of an half-wave controlled rectifier with
variable firing angle (alpha); the value of alpha must be do it as an input variable to
the program and in degree.

Solution
clc
clear all
th1=0;ii=0;
ss=pi/5000; %sampling time
alpha=input ('give the value of alpha in deg. ');
alpha=alpha*pi/180;
for t=ss:ss:2*pi
ii=ii+1;
control(ii)=sin(t);
if control(ii)>0 & t>alpha
vout(ii)=control(ii);
else
vout(ii)=0;
end
end
subplot(2,1,1);
plot(control,'r')
axis([0 12000 -1.2 1.2])
title('\fontsize {16} input voltage')
hold on
subplot(2,1,2)
plot(vout)
axis([0 12000 -.2 1.2])
title('\fontsize{16} output voltage')
hold on
4. For a bidirectional sinusoidal PWM inverter, do a Matlab program to execute the
following:

a) Enter the value of frequency modulation ration (FMR).

b) Enter the value of modulation index (MI) = 0.6.

c) Sketch the waveforms of triangle and reference in addition to the waveform of


the inverter output.
clc
clear all
th1=0;ii=0;
ss=pi/5000; %sampling time
FMR=input ('give the value of FMR '); % frequency modulation ratio
(no of multiple of the reference input sine signal
mi=input ('give the value of mod. index (mi) 0=<mi<=1 '); % frequency
modulation ratio (no of multiple of the reference input sine signal
for t=ss:ss:2*pi
ii=ii+1;
control(ii)=mi*sin(t);
suma=0;
for k=0:10
suma=suma+(-1)^(k)*(sin((2*k+1)*FMR*(t-0*pi/4)))/((2*k+1)^2);
end
tri(ii)=(8/pi^2)*suma;
if control(ii)>tri(ii)
th1(ii)=1;
else
th1(ii)=-1;
end
end
subplot(2,1,1);
plot(tri,'r')
axis([0 12000 -1.2 1.2])
hold on
plot(control,'b')
subplot(2,1,2)
plot(th1)
axis([0 12000 -1.2 1.2])
hold on
ylabel('the output voltage X Vs/2')

Example:
1
𝑓(𝑠) = 𝐿 = .1 𝑅 = 5
.1 𝑆 2 + 𝐿𝑆+𝑅

R=5; L=.1
num=[1];
den=[.1 L R];
sys=tf(num,den)
step (sys)
Example:
𝑤
𝑓(𝑠) = 𝑤 = 0.9
𝑆 2 + 2 0.7∗𝑤∗𝑆+𝑤 2

clc
w=.9;
ww=w^2;
num=[w];
den=[1 2*0.7*w ww];
sys=tf(num,den);
step (sys)
title('step response of second order system')

You might also like