Cours 4 Plotting in MATLAB (1)
Cours 4 Plotting in MATLAB (1)
in MATLAB
A picture is worth a thousand words
• MATLAB allows you to plot data sets for better
visualization and interpretation.
• There are different types of plots available in
MATLAB including 2D and 3D plots.
• You can control all aspects of the plot: lines,
colors, grids, labels, etc.
• Plotting clear and easy-to-read figures is an
important skill, which you gain from experience.
2
Example of a Figure window
5-11 3
Nomenclature for a typical xy two-dimensional plot.
4
Example: Plot y 8x for x , where
y represents the height of a rocket after launch, in
miles, and x is the horizontal (downrange) distance
in miles.
>> x = 0:0.1:52;
>> y = 0.4*sqrt(1.8*x);
>> plot(x,y);
>> xlabel(’Distance (miles)’);
>> ylabel(’Height (miles)’);
>> title(’Rocket Height vs. Distance’);
Notice that for each x there is y; so MATLAB plots
one array against another.
Also notice how we added the axes labels and plot title.
The resulting plot is shown on the next slide.
5
The autoscaling feature in MATLAB selects tick-mark
spacing.
6
The plot will appear in the Figure window. You can use the
plot in other applications in several ways:
8
One Data Set: plot
x = 0:2*pi/100:2*pi;
y1 = sin(x);
plot(x,y1); 1
Example
xlabel('x'); 0.8
0.6
ylabel('y'); 0.4
title('Example'); 0.2
0
y
-0.2
-0.8
if y1 is a vector. -1
0 1 2 3 4 5 6 7
x
9
Multiple Data Sets: plot, hold
x = 0:2*pi/100:2*pi;
y1 = sin(x); 1.5
Example
y2 = cos(x);
y3 = sin(x)+cos(x); 1
plot(x,y1); 0.5
hold on; 0
y
plot(x,y2);
-0.5
plot(x,y3);
xlabel('x'); -1
ylabel('y'); -1.5
0 1 2 3 4 5 6 7
title('Example'); x
hold off;
10
Or better use one plot command
x = 0:2*pi/100:2*pi;
y1 = sin(x); 1.5
Example
y2 = cos(x); 1
y3 = sin(x)+cos(x);
0.5
plot(x,y1,x,y2,x,y3);
0
xlabel('x');
y
ylabel('y'); -0.5
title('Example'); -1
% by MATLAB
11
Colors, Data Markers & Line Types
• You can also specify your own line styles in the plot
command.
• For full details enter help plot in MATLAB.
12
x = 0:2*pi/100:2*pi;
y1 = sin(x);
y2 = cos(x);
y3 = sin(x)+cos(x);
plot(x,y1,'r-.',x,y2,'g-x',x,y3,'b+');
xlabel('x'); 1.5
ylabel('y'); 1
0.5
0
y
-0.5
-1
-1.5
0 1 2 3 4 5 6 7
x
13
Exercise: How did we use different data markers below?
14
Legends
• With multiple lines on the same plot it is a good idea to add a legend.
legend('sin','cos','sin + cos');
legend('sin','cos','sin+cos','Location','North');
1.5 1.5
sin sin
cos cos
1 sin + cos 1 sin + cos
0.5 0.5
0 0
y
-0.5 -0.5
-1 -1
-1.5 -1.5
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
x x
15
Labeling Curves and Data
The legend command automatically obtains from the
plot the line type used for each data set and displays a
sample of this line type in the legend box next to the
string you selected. The following script file produced
the plot in the next slide.
x = 0:0.01:2;
y = sinh(x);
z = tanh(x);
plot(x,y,x,z,'--');
legend('sinh(x)', 'tanh(x)');
gtext(‘text’): Places a string in the Figure
window at a point specified by the mouse.
text(x,y,’text’): Places a string in the Figure
window at a point specified by coordinates x, y.
16
Application of the legend command.
I moved the legend to an empty space using the mouse.
3.5
3
sinh(x)
tanh(x)
2.5
1.5
0.5
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
17
The grid and axis Commands
18
axis and grid commands
axis([0 9 -2 2]); grid on;
axis([0 6 -2 2]); grid off;
2 2
sin sin
cos 1.5 cos
1.5
sin+cos sin+cos
1 1
0.5 0.5
0 0
y
y
-0.5 -0.5
-1 -1
-1.5 -1.5
-2 -2
0 1 2 3 4 5 6 0 1 2 3 4 5 6
x x
19
Homework #1
Plotting Polynomials with the polyval Function.
>> x = -6:0.01:6;
>> p = [3,2,-100,2,-7,90];
>> plot(x,polyval(p,x)); 5000
2000
1000
p 0
-1000
-2000
-3000
-6 -4 -2 0 2 4 6
x
20
Homework # 2
• The polyfit function is based on the least-
squares method. It fits a polynomial of
degree n to data described by the vectors x
and y, where x is the independent variable.
• Syntax: p = polyfit(x,y,n)
• It returns a row vector p of length n+1 that
contains the polynomial coefficients in order
of descending powers.
• For the following census data, draw the
actual points and the best 5th order
polynomial fit for such data.
21
year = 1810:10:2010;
population = 1e6*[3.9 5.3 7.2 9.6 12.9 17.1
23.1 31.4 38.6 50.2 62.9 76. 92. 105.7 122.8
131.7 150.7 179. 205. 226.5 248.7];
coeff = polyfit(year, population, 5)
f = polyval(coeff, year);
plot(year, population, 'bo', year, f, 'r--');
8
x1
0
3
2.5
1.5
0.5
0
1800 1850 1900 1950 2000 2050
22
More Than One Figure Window
• What happens if you enter the following?
x = 0:2*pi/100:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x,y1);
title('Plot #1');
plot(x,y2);
title('Plot #2');
23
More Than One Figure Window
• … you end up with one figure window and it
contains a plot of y = cos(x).
• To open a new figure window enter the command
figure before making the second plot.
plot(x,y1);
title('Plot #1');
figure;
plot(x,y2);
title('Plot #2');
24
Subplots
25
Subplots
• subplot(m,n,p)
26
Example
x = 0:2*pi/100:2*pi; sin(x) cos(x)
1 1
y1 = sin(x);
0.5 0.5
y2 = cos(x);
0 0
y3 = sin(x)+cos(x);
subplot(2,2,1); -0.5 -0.5
plot(x,y1,'r-.'); -1
0 2 4 6 8
-1
0 2 4 6 8
title('sin(x)'); sin(x)+cos(x)
2
subplot(2,2,2);
1
plot(x,y2,'go');
0
title('cos(x)');
-1
subplot(2,2,3);
-2
0 2 4 6 8
plot(x,y3,'b+');
title('sin(x)+cos(x)');
27
Homework:
The following script file shows two plots of the functions
y e12x sin(10x 5) for 0 x 5
and y x3 100for 6 x 6.
x = 0:0.01:5;
y = exp(-1.2*x).*sin(10*x+5);
subplot(1,2,1);
plot(x,y);
axis([0 5 -1 1]);
x = -6:0.01:6;
y = abs(x.^3-100);
subplot(1,2,2);
plot(x,y);
axis([-6 6 0 350]) The figure is shown
on the next slide.
28
Application of the subplot command.
1 350
0.8
300
0.6
0.4 250
0.2
200
0
150
-0.2
-0.4 100
-0.6
50
-0.8
-1 0
0 1 2 3 4 5 -5 0 5
29
Source: