You are on page 1of 23

Sept.

26, 2005 Lecture 6 - By Paul Lin 1


CPET 190
Lecture 6
Problem Solving with MATLAB

http://www.etcs.ipfw.edu/~lin
Sept. 26, 2005 Lecture 6 - By Paul Lin 2
Lecture 6: MATLAB Plotting
6-1 Introduction to Plotting
6-2 Simple X-Y Plots
Printing a Plot
Exporting a Plot as a Graphical Image
Line Color, Style, Marker and Legends
6-3 Multiple Plot
6-4 Logarithmic Scales
6-5 Plotting Applications
Sept. 26, 2005 Lecture 6 - By Paul Lin 3
6.1 Introduction to Plotting
Data or Numerical facts
For making decision making
Pictures, Graphs, or Plots
Describe data
Ohms Laws: V = R * I
MATLAB Plotting Capabilities
2-D XY Plot
Semilog Plot
3-D Plot
More
Sept. 26, 2005 Lecture 6 - By Paul Lin 4
6.2 Simple XY Plots
Basic Steps for Preparing a XY Graph
1. Prepare data for x-axis and y-axis in row
vector or column vector format
2. Select a window with a figure number and
plot position (if multiple sub-plot)
3. Call plot() function
4. Select plot line, color, etc
5. Set Axis range and grid
6. Annotate the plot
7. Export graph
MATLAB XY Plot Related Functions
figure, plot, subplot, set, grid on, axis, xlabel,
ylabel, title, legend, text
Sept. 26, 2005 Lecture 6 - By Paul Lin 5
6.2 Simple XY Plots (cont.)
Example 6.1 Plot the function y = x
2
10x +
15 (from page 56-57 of MATLAB Programming for Engineers, 3
rd
, by
Stephen J. Chapman, published by Thomson-Learning)
Solution
Planning
1. Prepare x vector, with reasonable range,
and compute y vector
2. Use a default window ( without figure),
and call plot(x,y) function
3. Add title(), xlabel(), ylabel(), grid on, etc to
fine-tune the graph
Sept. 26, 2005 Lecture 6 - By Paul Lin 6
6.2 Simple XY Plots (cont.)
Example 6-1 (cont.)
MATLAB Solution:
Start MATLAB, and invoke
M-File Editor
Write the following
MATLAB statements
%plotxy_p56.m
x = 0:1:10;
% NOT y = x^2 - 10*x + 15
y = x.^2 - 10.*x + 15;
plot(x, y);
title('y = x.^2 - 10.*x + 15');
xlabel('x axis'), ylabel('y axis');
grid on;
0 1 2 3 4 5 6 7 8 9 10
-10
-5
0
5
10
15
y = x.
2
- 10.*x + 15
x axis
y

a
x
i
s
Sept. 26, 2005 Lecture 6 - By Paul Lin 7
6.2 Simple XY Plots (cont.)
Example 6-1 (cont.)
Verifying Data:
X = 0, Y = 15
Y = 0 0 + 15 = 15
X = 10, Y = 15
Y = 10^2 - 10*10 + 15 = 15
Exporting Figure
Copy Figure for Exporting
Sept. 26, 2005 Lecture 6 - By Paul Lin 8
6.2 Simple XY Plots (cont.)
Example 6-1 (cont.)
Exporting TIFF Figure to
PowerPoint applications like
this one
1. On MATLAB Command
Windows: we type
>> print dtiff plotxy_56.tif %for
creating graphic files
2. We locate the plotxy_56.tif in
the current directory OK.
3. We start and setup PowerPoint,
then Insert -> Picture to locate
plotxy_56.tif file.
Sept. 26, 2005 Lecture 6 - By Paul Lin 9
6.3 Multiple Plots
Multiple Plots
On the same Graph using plot function, or
Arranged in m x n matrix format
subplot(2,1,1) 2 subplots, two rows, one
column, the first plot
subplot(2,2,1) 4 subplots, two rows, two
column, the first plot
>> help subplot
Examples
>> subplot(2,1,1), plot(sine)
>> subplot(2,1,2), plot(cosine)
Sept. 26, 2005 Lecture 6 - By Paul Lin 10
6.3 Multiple Plots
Example 6-2. Multiple plots of sine and cosine
from the equation (from page 56-57 of MATLAB Programming
for Engineers, 3
rd
, by Stephen J. Chapman, published by Thomson-
Learning)


Analysis
Data preparation
X vector from 0 to 2pi
Y1 = sin2x
Y2 = 2cos2x
Two plots subplot(2,1,1), and subplot(2,1,2)
x x
dx
d
2 cos 2 2 sin
Sept. 26, 2005 Lecture 6 - By Paul Lin 11
6.3 Multiple Plots
Example 6-2 (cont.)
MATLAB Program
0 1 2 3 4 5 6 7
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
%plot_sine_cosine.m
x = 0:pi/100:2*pi;
y1 = sin(2*x);
y2 = 2*cos(2*x)
figure(1), plot(x,y1, x, y2)
figure(2),subplot(2,1,1),plot(x,y1),
title('sin 2x');
figure(2),subplot(2,1,2),plot(x,y2),
title('2cos 2x');
0 1 2 3 4 5 6 7
-1
-0.5
0
0.5
1
sin 2x
0 1 2 3 4 5 6 7
-2
-1
0
1
2
2cos 2x
Sept. 26, 2005 Lecture 6 - By Paul Lin 12
6.3 Multiple Plots
Distinguish different
plots
Line Color
Line Style
Marker Style and
Legends
help plot for more info
Example 6-3
%plot_p60.m
x = 0: 1: 10;
y = x.^2 - 10.*x + 15;
plot(x,y,'r--', x, y, 'bo');
0 1 2 3 4 5 6 7 8 9 10
-10
-5
0
5
10
15
Sept. 26, 2005 Lecture 6 - By Paul Lin 13
6.3 Multiple Plots
Legends
Name and its line style
Example 6-4
0 1 2 3 4 5 6 7
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
f(x) = sin(2x) and its Derivative
x
y
f(x)
d/dx (fx)
%plot_sine_cosine_legend.m
x = 0:pi/100:2*pi;
y1 = sin(2*x);
y2 = 2*cos(2*x);
plot(x,y1,'k-', x, y2,'b--'),
grid on
title('f(x) = sin(2x) and its
Derivative');
xlabel('x');
ylabel('y');
legend('f(x)', 'd/dx (fx)')
Sept. 26, 2005 Lecture 6 - By Paul Lin 14
6.4 Logarithmic Scales
Log Scale plots
Decibel Db = 10log(Pout/Pin)
Frequency response plot
MATLAB Functions
semilogx(x,y) plot x data on log scale and y
data on linear scale
semilogy(x,y) plot x data on linear scale and
y on log scale
Loglog(x,y) plot both x and y data on log
scale
Sept. 26, 2005 Lecture 6 - By Paul Lin 15
6.5 Plotting Applications
Example 6-5: Mr. A performed an electrical
circuit experiment and collected the
following measured resistance values
(ohms) and current values (amperes):
R = 0.5, 0.75, 1.0, 2.0, 3.0, 4.0
I = 10, 7, 5.2, 3, 1.8, 1.5
You are asked to help Mr. A to prepare data
and generate a plot that shows red circles
on all data points
Sept. 26, 2005 Lecture 6 - By Paul Lin 16
6.5 Plotting Applications (continue)
Example 6-5: Solution
%plotRI.m
%Author: M. Lin
%Date: 9/1/04
%Version: 1.0
%Description:
% R = 0.5 0.75 1.0 2.0 3.0 4.0
% I = 10 7 5.2 3 1.8 1.5
R = [0.5 0.75 1.0 2.0 3.0 4.0];
I = [10 7 5.2 3 1.8 1.5];
plot(R,I, 'ro--')
% show red circles at the data points
xlabel('Resistance'), ylabel('Current')
title('R vs I Plot')
0.5 1 1.5 2 2.5 3 3.5 4
1
2
3
4
5
6
7
8
9
10
Resistance
C
u
r
r
e
n
t
R vs I Plot
Sept. 26, 2005 Lecture 6 - By Paul Lin 17
6.5 Plotting Application (continue)
Example 6-6: Mr. A performed an Ohms
experiment and collected the following
measured voltage values (volts) and current
values (milli-amperes):
V = 0, 1, 2, 3, 4, 5, 6, 7, 8
I = 10, 8, 7, 7, 6, 4. 3, 2, 0
You are asked to help Mr. A to prepare data
and generate a plot that shows red-lines and
blue circles on all data points
Sept. 26, 2005 Lecture 6 - By Paul Lin 18
6.5 Plotting Applications (continue)
Example 6-6: Solution

%plotVI.m
%Author: M. Lin
%Date: 9/1/04
%Version: 1.0
%Description:
% V (volts) = 0 1 2 3 4 5 6 7 8
% I (milliamps) = 10 8 7 7 6 4 3 2 0
V = [0 1 2 3 4 5 6 7 8];
I = [10 8 7 7 6 4 3 2 0];
plot(V,I, 'r--', V, I, 'bo')
% show red cicrles at the data points
xlabel('Voltage - Volt'), ylabel('Current -
milliamps')
title(E - I Plot')
0 1 2 3 4 5 6 7 8
0
1
2
3
4
5
6
7
8
9
10
Voltage - Volt
C
u
r
r
e
n
t

-

m
i
l
l
i
a
m
p
s
E - I Plot
Sept. 26, 2005 Lecture 6 - By Paul Lin 19
6.5 Plotting Applications
Example 6-7: Maximum Power Transfer. A
voltage source E = 120 v with an internal
resistance of Rs of 50 ohms supplying a load
of resistance RL .
Q1 - Find the value of load resistance RL that
will result in the maximum possible power
being supplied by the source to the load.
Q2 How much power be supplied in this
case?
Q3 Plot the power supply to the load as a
function of the load resistance RL.
Sept. 26, 2005 Lecture 6 - By Paul Lin 20
6.5 Plotting Applications
Example 6-7: Solution:
Circuit Diagram
Equations
PL = I
2
RL,
where I is the current
passing through the circuit.
I = E/R
total
= E/(Rs + RL)
Array data for MALAB
plotting
RL a vector
E and Rs are scalars
RL = ? ohms
Rs = 50
ohm
E = 120V
I
Sept. 26, 2005 Lecture 6 - By Paul Lin 21
6.5 Plotting Applications
Example 6-7: Solution:
MATLAB Program
0 10 20 30 40 50 60 70 80 90 100
0
10
20
30
40
50
60
70
80
Max Power Transfer
Load resistance RL
P
o
w
e
r

-

W
a
t
t
s
%maxPower_p67.m
Rs = 50;
E = 120;
RL = 1:1:100;
I = E./(Rs + RL);
% NOT I = R/(Rs + RL)
% -- for Scalar calculation
PL = (I.^2) .* RL;
% NOT PL = I^2 * RL
% -- for Scalar calculation
plot(RL, PL), grid on
title('Max Power Transfer');
xlabel('Load resistance RL');
ylabel('Power - Watts');
Sept. 26, 2005 Lecture 6 - By Paul Lin 22
6.5 Plotting Applications
Example 6-7: Solution:
Answers
Q1 Find the value of RL
for Pmax to occur
RL = 50 ohms results a PL
max of 72 watts
Q2 How much power be
supplied in this case?
PL consume 72 Watt,
Rs will also receive the
same 72 Watts inside the
power supply
Power supplied is 144 watts
Q3 Plot the PL(RL)
0 10 20 30 40 50 60 70 80 90 100
0
10
20
30
40
50
60
70
80
Max Power Transfer
Load resistance RL
P
o
w
e
r

-

W
a
t
t
s
72 watts
Sept. 26, 2005 Lecture 6 - By Paul Lin 23
Summary
Introduction to Plotting
Simple X-Y Plots
Printing a Plot
Exporting a Plot as a Graphical Image
Line Color, Style, Marker and Legends
Multiple Plot
Logarithmic Scales
Plotting Applications

You might also like