XY Plotting in MATLAB
· To see of the types of xy plots MATLAB will generate use the demo page >> demo.
demo à Visualization à 2D Plots
· The most common types of
plots are scatter plots and line plots.
Both of these are generated using the MATLAB’s function plot(
). For details on this function
use
>> help plot
· Let’s generate some data to
plot…
t = linspace(0,1,100);
T1 = .1;
V1 = exp(-t/T1 );
T2 = .3;
V2 = exp(-t/T2 );
· Let’s take a closer look at
the plot( ) command options…
figure(1)
plot(t,V1,'ro')
legend('T1')
xlabel('Time (seconds)')
ylabel('Voltage (volts)')
title('Exponential
Decay');
grid
· The code above produces a scatter plot.
Try a few other options by changing the text string following the data
arrays ('ro'). MATLAB is looking for up to 3
characters. One character representing
the color, one the data symbol, and one for the line type. Look at the help text for a full list of
options.
figure; plot(t,V1,'ro-')
figure; plot(t,V1,'r-')
figure; plot(t,V1,'b--')
figure; plot(t,V1, 'g:')
·
The command axis equal forces the plot
to have equal scale on the vertical and horizontal axes. This is nice here because in will make the
initial angle look correct on the plot.
The command axis tight gets rid of any
space in the plot outside the range of the data. Using axis([startx,endx,starty,endy]) forces the horizontal axis
to go from startx to endx. It goes from starty to endy on the vertical axis. See the help text for more details.
Adding Text to Your Plot
· The function text(x,y,
'stuff you want printed'), lets you put text at the coordinates x,y on your plot. If
you want to get fancy and put numerical variable values along with text, you
need to use sprintf( )
to create your text string. Look at how
it is used below…
figure(1)
plot(t,V1,'r-o')
legend('T1')
xlabel('Time (seconds)')
ylabel('Voltage (Volts)')
title('Exponential
Decay');
grid
S=sprintf('T=%.2f s',T1);
title(S)
legend(S)
text(.5,.5,S);
· As you have already seen, we
can put multiple curves on the same axes…
figure(2)
plot(t,V1,'b-',t,V2,'r--')
legend(sprintf('T=%.2f s',T1), sprintf('T=%.2f s',T2))
xlabel('Time (seconds)')
ylabel('Voltage (Volts)')
grid
· The function subplot( ) lets you put multiple plots on one figure
window. Try this…
figure(3)
subplot(211)
plot(t,V1,'bo-')
legend(sprintf('T=%.2f s',T1))
xlabel('Time (seconds)')
ylabel('Voltage (Volts)')
subplot(212)
plot(t,V2,'r-s')
legend(sprintf('T=%.2f s',T2))
xlabel('Time (seconds)')
ylabel('Voltage (Volts)')
· When the range of data you
are trying to plot spans several orders of magnitude, a log scale on an axis
offers a good way to visualize the data.
·
Note that y=log10(x) à 10^y=x
· Try this…
log10([1,10,100,1000,10000])
· On a log axis, each order of
magnitude (1,10,100,1000,…) moves you the same amount
on the curve.
· MATLAB has the following
commands for plotting on log axes. They
are used just like plot( ):
o
semilogx( ) – logarithmic in x, linear in y
o
semilogy( ) – logarithmic in y, linear in x
o
loglog( ) – logarithmic in x and y
· Here is an example of data
in the range 0-1 plus an outlier with a value of 100.
data=rand(1,100);
data(50)=100;
x=linspace(1,100,100);
figure(4)
subplot(211)
plot(x,data)
title('Data Plotted on
Linear Axes')
subplot(212)
semilogy(x,data)
title('Same Data
Plotted on Semilogy Axes')
· Here is our exponential data
plotted on semilogy
figure(5)
plot(t,V1,'bo-')
legend(sprintf('T=%.2f s',T1))
xlabel('Time (seconds)')
ylabel('Voltage (Volts)')
grid
figure(6)
semilogy(t,V1,'r-s')
legend(sprintf('T=%.2f s',T1))
xlabel('Time (seconds)')
ylabel('Voltage (Volts)')
grid
· Log scales are also a good
way to visualize quantities that we perceive logarithmically. In sound for example, continuously doubling
frequency (moving up octaves) sounds like a linear progression. This represents a logarithmic
relationship. In fact, sound volume
works the same way. You have to keep
doubling the volume to make it sound like it is increasing linearly. The volume control on your stereo uses what
is called an audio-taper variable resistor. This resistor (which determines the output
amplitude) varies logarithmically with a linear adjustment.
· When you adjust the tone
controls on your stereo, you are changing the frequency
response of your system.
Frequency response is an important concept in most engineering
fields. In simple terms, frequency
response is how much amplification (or attenuation) a system provides for a
certain sinusoidal frequency input. For
many systems (linear systems), if the input is
,
then the output is
,
where H(f) is the amplitude frequency response function.
· When you turn down the
treble on your stereo, you are effectively passing the audio voltage signal
through a circuit with an amplitude frequency response such as

where f is frequency in Hz,
and c is a constant which depends on your treble setting.
· Let’s try to visualize this
frequency response function using various axes options…
clear;close all;clc
f=linspace(20,20000,1000);
c=.001;
H=1./(sqrt((2*pi*c*f).^2+1));
figure
subplot(221)
plot(f,H)
xlabel('f (Hz)');
ylabel('H(f)');
title('Treble Cut
Frequency Response (linear axes)');
axis tight
subplot(222)
semilogx(f,H);
xlabel('f (Hz)');
ylabel('H(f)');
title('RC Circuit
Frequency Response (semilogx)');
axis tight
subplot(223)
semilogy(f,H);
xlabel('f (Hz)');
ylabel('H(f)');
title('RC Circuit
Frequency Response (semilogy)');
axis tight
subplot(224)
loglog(f,H);
xlabel('f (Hz)');
ylabel('H(f)');
title('RC Circuit
Frequency Response (loglog)');
axis tight