Title: CPET 190
1CPET 190
- Lecture 6
- Problem Solving with MATLAB
- http//www.etcs.ipfw.edu/lin
2Lecture 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
36.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
46.2 Simple XY Plots
- Basic Steps for Preparing a XY Graph
- Prepare data for x-axis and y-axis in row vector
or column vector format - Select a window with a figure number and plot
position (if multiple sub-plot) - Call plot() function
- Select plot line, color, etc
- Set Axis range and grid
- Annotate the plot
- Export graph
- MATLAB XY Plot Related Functions
- figure, plot, subplot, set, grid on, axis,
xlabel, ylabel, title, legend, text
56.2 Simple XY Plots (cont.)
- Example 6.1 Plot the function y x2 10x 15
(from page 56-57 of MATLAB Programming for
Engineers, 3rd, by Stephen J. Chapman, published
by Thomson-Learning) - Solution
- Planning
- Prepare x vector, with reasonable range, and
compute y vector - Use a default window ( without figure), and call
plot(x,y) function - Add title(), xlabel(), ylabel(), grid on, etc to
fine-tune the graph
66.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 0110 NOT y x2 - 10x
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
76.2 Simple XY Plots (cont.)
- Verifying Data
- X 0, Y 15
- Y 0 0 15 15
- X 10, Y 15
- Y 102 - 1010 15 15
- Exporting Figure
- Copy Figure for Exporting
86.2 Simple XY Plots (cont.)
Exporting TIFF Figure to PowerPoint applications
like this one 1. On MATLAB Command Windows we
type gtgt 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 -gt Picture to locate
plotxy_56.tif file.
96.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 - gtgt help subplot
- Examples
- gtgt subplot(2,1,1), plot(sine)
- gtgt subplot(2,1,2), plot(cosine)
106.3 Multiple Plots
- Example 6-2. Multiple plots of sine and cosine
from the equation (from page 56-57 of MATLAB
Programming for Engineers, 3rd, 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)
116.3 Multiple Plots
- Example 6-2 (cont.)
- MATLAB Program
plot_sine_cosine.m x 0pi/1002pi y1
sin(2x) y2 2cos(2x) 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),titl
e('2cos 2x')
126.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')
136.3 Multiple Plots
- Legends
- Name and its line style
- Example 6-4
plot_sine_cosine_legend.m x 0pi/1002pi y1
sin(2x) y2 2cos(2x) 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)')
146.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
156.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
166.5 Plotting Applications (continue)
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')
176.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
186.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')
196.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.
206.5 Plotting Applications
- Example 6-7 Solution
- Circuit Diagram
- Equations
- PL I2 RL,
- where I is the current passing through the
circuit. - I E/Rtotal E/(Rs RL)
- Array data for MALAB plotting
- RL a vector
- E and Rs are scalars
216.5 Plotting Applications
- Example 6-7 Solution
- MATLAB Program
maxPower_p67.m Rs 50 E 120 RL 11100 I
E./(Rs RL) NOT I R/(Rs RL) -- for
Scalar calculation PL (I.2) . RL NOT PL
I2 RL -- for Scalar calculation plot(RL,
PL), grid on title('Max Power Transfer') xlabel('
Load resistance RL') ylabel('Power - Watts')
226.5 Plotting Applications
72 watts
- 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)
23Summary
- 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