Professional Documents
Culture Documents
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.
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
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.
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.
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')
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
Syntax : subplot(n,m,p)
Where:
n is the number of row.
m is the number of column.
p is the number of window.
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)
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)
Subplot(2,1,3) or subplot(2,2,3)
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.
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:
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')