0% found this document useful (0 votes)
6 views

Cours 4 Plotting in MATLAB (1)

Thanks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Cours 4 Plotting in MATLAB (1)

Thanks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Lecture 4: Plotting

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:

1. You can print a hard copy of the figure by selecting


File | Print menu item in the Figure window.
2. You can save the plot to a file to be used later. You can
save the plot by selecting File | Save As menu item.
Possible file formats include: *.fig (MATLAB format),
*.bmp, *.eps, *.jpg, *.png, *.tif, *.pdf, …. Another way
to save is File | Export Setup that allows specifying
options for the output file, then selecting Export.
3. You can copy a figure to the clipboard and then paste it
into another application using the Edit | Copy Figure
menu item. For options, use Edit | Copying Options
menu item.
7
When you have finished with the plot, close the figure
window by selecting File | Close menu item in the
figure window.

If you do not close the window, it will not re-appear


when a new plot command is executed. However,
the figure will still be updated.

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

plot(y1): Plots values -0.4

of y1 versus their indices


-0.6

-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

% Notice the auto coloring -1.5


0 1 2 3
x
4 5 6 7

% 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');

• You can also move the legend with the mouse.

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

MATLAB will automatically determine the maximum and


minimum values for the axes. You can use the axis
command to override the MATLAB selections for the axis
limits. The syntax is axis([xmin xmax ymin ymax]).
This command sets the scaling for the x- and y-axes to the
minimum and maximum values indicated.

The grid command displays gridlines at the tick marks


corresponding to the tick labels. Type grid on to add
gridlines; type grid off to stop plotting gridlines. When
used by itself, grid toggles this feature on or off, but you
might want to use grid on and grid off to be sure.

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.

To plot the polynomial 3x5 + 2x4 – 100x3 + 2x2 – 7x + 90 over the


range –6  x  6 with a spacing of 0.01, you type

>> x = -6:0.01:6;
>> p = [3,2,-100,2,-7,90];
>> plot(x,polyval(p,x)); 5000

>> xlabel('x'); 4000

>> ylabel('p'); 3000

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

You can use the subplot command to obtain several


smaller “subplots” in the same figure. The syntax is
subplot(m,n,p). This command divides the Figure
window into an array of rectangular panes with m rows and
n columns. The variable p tells MATLAB to place the output
of the plot command following the subplot command
into the pth pane.

For example, subplot(3,2,5) creates an array of six


panes, three panes deep and two panes across, and directs
the next plot to appear in the fifth pane (in the bottom-left
corner).

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  e12x sin(10x  5) for 0  x  5
and y  x3  100for 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:

Dr. Mohammed Hawa Electrical


Engineering Department
University of Jordan

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy