Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

23
Sept. 26, 2005 Sept. 26, 2005 Lecture 6 - By Paul Lin Lecture 6 - By Paul Lin 1 CPET 190 CPET 190 Lecture 6 Lecture 6 Problem Solving with Problem Solving with MATLAB MATLAB http://www.etcs.ipfw.edu/ http://www.etcs.ipfw.edu/ ~lin ~lin

Transcript of Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Page 1: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 11

CPET 190 CPET 190

Lecture 6Lecture 6

Problem Solving with MATLABProblem Solving with MATLAB

http://www.etcs.ipfw.edu/~linhttp://www.etcs.ipfw.edu/~lin

Page 2: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 22

Lecture 6: MATLAB PlottingLecture 6: MATLAB Plotting

6-1 Introduction to Plotting6-1 Introduction to Plotting

6-2 Simple X-Y Plots6-2 Simple X-Y Plots• Printing a PlotPrinting a Plot• Exporting a Plot as a Graphical ImageExporting a Plot as a Graphical Image• Line Color, Style, Marker and LegendsLine Color, Style, Marker and Legends

6-3 Multiple Plot6-3 Multiple Plot

6-4 Logarithmic Scales6-4 Logarithmic Scales

6-5 Plotting Applications6-5 Plotting Applications

Page 3: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 33

6.1 Introduction to Plotting6.1 Introduction to Plotting

Data or Numerical factsData or Numerical facts• For making decision makingFor making decision making

Pictures, Graphs, or PlotsPictures, Graphs, or Plots• Describe dataDescribe data• Ohms Laws: V = R * IOhms Laws: V = R * I

MATLAB Plotting CapabilitiesMATLAB Plotting Capabilities• 2-D XY Plot2-D XY Plot• Semilog PlotSemilog Plot• 3-D Plot3-D Plot• MoreMore

Page 4: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 44

6.2 Simple XY Plots6.2 Simple XY Plots

Basic Steps for Preparing a XY GraphBasic Steps for Preparing a XY Graph1.1. Prepare data for x-axis and y-axis in row Prepare data for x-axis and y-axis in row

vector or column vector formatvector or column vector format2.2. Select a window with a figure number and Select a window with a figure number and

plot position (if multiple sub-plot)plot position (if multiple sub-plot)3.3. Call plot() functionCall plot() function4.4. Select plot line, color, etcSelect plot line, color, etc5.5. Set Axis range and gridSet Axis range and grid6.6. Annotate the plotAnnotate the plot7.7. Export graphExport graph

MATLAB XY Plot Related FunctionsMATLAB XY Plot Related Functions• figure, plot, subplot, set, grid on, axis, xlabel, figure, plot, subplot, set, grid on, axis, xlabel,

ylabel, title, legend, textylabel, title, legend, text

Page 5: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 55

6.2 Simple XY Plots 6.2 Simple XY Plots (cont.)(cont.)

Example 6.1 Plot the function y = xExample 6.1 Plot the function y = x22 – 10x + – 10x + 1515 (from page 56-57 of MATLAB Programming for Engineers, 3(from page 56-57 of MATLAB Programming for Engineers, 3rdrd, by , by Stephen J. Chapman, published by Thomson-Learning)Stephen J. Chapman, published by Thomson-Learning)

SolutionSolution PlanningPlanning

1.1. Prepare x vector, with reasonable range, Prepare x vector, with reasonable range, and compute y vectorand compute y vector

2.2. Use a default window ( without figure), Use a default window ( without figure), and call plot(x,y) functionand call plot(x,y) function

3.3. Add title(), xlabel(), ylabel(), grid on, etc to Add title(), xlabel(), ylabel(), grid on, etc to fine-tune the graphfine-tune the graph

Page 6: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 66

6.2 Simple XY Plots 6.2 Simple XY Plots (cont.)(cont.)

Example 6-1 Example 6-1 (cont.)(cont.)

MATLAB Solution:MATLAB Solution: Start MATLAB, and invoke Start MATLAB, and invoke

M-File EditorM-File Editor Write the following Write the following

MATLAB statementsMATLAB statements%plotxy_p56.mx = 0:1:10;% NOT y = x^2 - 10*x + 15y = 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

15y = x.2 - 10.*x + 15

x axis

y ax

is

Page 7: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 77

6.2 Simple XY Plots 6.2 Simple XY Plots (cont.)(cont.)

Example 6-1 Example 6-1 (cont.)(cont.)

•Verifying Data:Verifying Data:

X = 0, Y = 15X = 0, Y = 15

Y = 0 – 0 + 15 = 15Y = 0 – 0 + 15 = 15

X = 10, Y = 15X = 10, Y = 15

Y = 10^2 - 10*10 + 15 = 15Y = 10^2 - 10*10 + 15 = 15

•Exporting FigureExporting Figure

•Copy Figure for ExportingCopy Figure for Exporting

Page 8: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 88

6.2 Simple XY Plots 6.2 Simple XY Plots (cont.)(cont.)

Example 6-1 Example 6-1 (cont.)(cont.)

Exporting TIFF Figure to Exporting TIFF Figure to PowerPoint applications like PowerPoint applications like this onethis one

1. On MATLAB Command 1. On MATLAB Command Windows: we typeWindows: we type

>> print –dtiff plotxy_56.tif %for >> print –dtiff plotxy_56.tif %for creating graphic filescreating graphic files

2. We locate the plotxy_56.tif in 2. We locate the plotxy_56.tif in the current directory – OK.the current directory – OK.

3. We start and setup PowerPoint, 3. We start and setup PowerPoint, then Insert -> Picture to locate then Insert -> Picture to locate plotxy_56.tif file.plotxy_56.tif file.

Page 9: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 99

6.3 Multiple Plots6.3 Multiple Plots Multiple Plots Multiple Plots

• On the same Graph using plot function, orOn the same Graph using plot function, or

• Arranged in m x n matrix formatArranged in m x n matrix format subplot(2,1,1) – 2 subplots, two rows, one subplot(2,1,1) – 2 subplots, two rows, one

column, the first plotcolumn, the first plot subplot(2,2,1) – 4 subplots, two rows, two subplot(2,2,1) – 4 subplots, two rows, two

column, the first plotcolumn, the first plot

• >> help subplot>> help subplot

ExamplesExamples>> subplot(2,1,1), plot(sine)>> subplot(2,1,1), plot(sine)

>> subplot(2,1,2), plot(cosine)>> subplot(2,1,2), plot(cosine)

Page 10: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 1010

6.3 Multiple Plots6.3 Multiple Plots

Example 6-2. Multiple plots of sine and cosine Example 6-2. Multiple plots of sine and cosine from the equation from the equation (from page 56-57 of MATLAB Programming (from page 56-57 of MATLAB Programming for Engineers, 3for Engineers, 3rdrd, by Stephen J. Chapman, published by Thomson-, by Stephen J. Chapman, published by Thomson-Learning)Learning)

AnalysisAnalysis• Data preparationData preparation

X vector from 0 to 2piX vector from 0 to 2pi Y1 = sin2xY1 = sin2x Y2 = 2cos2xY2 = 2cos2x

• Two plots subplot(2,1,1), and subplot(2,1,2)Two plots subplot(2,1,1), and subplot(2,1,2)

xxdx

d2cos22sin

Page 11: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 1111

6.3 Multiple Plots6.3 Multiple Plots

Example 6-2 (cont.)Example 6-2 (cont.) MATLAB ProgramMATLAB 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%plot_sine_cosine.mx = 0:pi/100:2*pi;x = 0:pi/100:2*pi;y1 = sin(2*x);y1 = sin(2*x);y2 = 2*cos(2*x)y2 = 2*cos(2*x)figure(1), plot(x,y1, x, y2)figure(1), plot(x,y1, x, y2)figure(2),subplot(2,1,1),plot(x,y1),figure(2),subplot(2,1,1),plot(x,y1),title('sin 2x');title('sin 2x');figure(2),subplot(2,1,2),plot(x,y2),figure(2),subplot(2,1,2),plot(x,y2),title('2cos 2x');title('2cos 2x');

0 1 2 3 4 5 6 7-1

-0.5

0

0.5

1sin 2x

0 1 2 3 4 5 6 7-2

-1

0

1

22cos 2x

Page 12: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 1212

6.3 Multiple Plots6.3 Multiple Plots Distinguish different Distinguish different

plotsplots• Line ColorLine Color

• Line StyleLine Style

• Marker Style and Marker Style and

• LegendsLegends

help plot for more infohelp plot for more info Example 6-3Example 6-3

%plot_p60.m%plot_p60.m

x = 0: 1: 10;x = 0: 1: 10;

y = x.^2 - 10.*x + 15;y = x.^2 - 10.*x + 15;

plot(x,y,'r--', x, y, 'bo');plot(x,y,'r--', x, y, 'bo');

0 1 2 3 4 5 6 7 8 9 10-10

-5

0

5

10

15

Page 13: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 1313

6.3 Multiple Plots6.3 Multiple Plots LegendsLegends

• Name and its line styleName and its line style

Example 6-4Example 6-4

0 1 2 3 4 5 6 7-2

-1.5

-1

-0.5

0

0.5

1

1.5

2f(x) = sin(2x) and its Derivative

x

y

f(x)d/dx (fx)

%plot_sine_cosine_legend.mx = 0:pi/100:2*pi;y1 = sin(2*x);y2 = 2*cos(2*x);plot(x,y1,'k-', x, y2,'b--'), …grid ontitle('f(x) = sin(2x) and its Derivative');xlabel('x');ylabel('y');legend('f(x)', 'd/dx (fx)')

Page 14: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 1414

6.4 Logarithmic Scales6.4 Logarithmic Scales

Log Scale plots Log Scale plots • Decibel Db = 10log(Pout/Pin)Decibel Db = 10log(Pout/Pin)

• Frequency response plotFrequency response plot

MATLAB FunctionsMATLAB Functions• semilogsemilogxx(x,y) – plot x data on log scale and y (x,y) – plot x data on log scale and y

data on linear scaledata on linear scale

• semilogsemilogyy(x,y) – plot x data on linear scale and (x,y) – plot x data on linear scale and y on log scaley on log scale

• Loglog(x,y) – plot both x and y data on log Loglog(x,y) – plot both x and y data on log scalescale

Page 15: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 1515

6.5 Plotting Applications6.5 Plotting Applications

Example 6-5: Mr. A performed an electrical Example 6-5: Mr. A performed an electrical circuit experiment and collected the circuit experiment and collected the following measured resistance values following measured resistance values (ohms) and current values (amperes):(ohms) and current values (amperes):

R = 0.5, 0.75, 1.0, 2.0, 3.0, 4.0R = 0.5, 0.75, 1.0, 2.0, 3.0, 4.0

I = 10, 7, 5.2, 3, 1.8, 1.5I = 10, 7, 5.2, 3, 1.8, 1.5

You are asked to help Mr. A to prepare data You are asked to help Mr. A to prepare data and generate a plot that shows red circles and generate a plot that shows red circles on all data pointson all data points

Page 16: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 1616

6.5 Plotting Applications 6.5 Plotting Applications (continue)(continue)

Example 6-5: SolutionExample 6-5: Solution

%plotRI.m%plotRI.m%Author: M. Lin%Author: M. Lin%Date: 9/1/04%Date: 9/1/04%Version: 1.0%Version: 1.0%Description:%Description:% R = 0.5 0.75 1.0 2.0 3.0 4.0% R = 0.5 0.75 1.0 2.0 3.0 4.0% I = 10 7 5.2 3 1.8 1.5% I = 10 7 5.2 3 1.8 1.5R = [0.5 0.75 1.0 2.0 3.0 4.0];R = [0.5 0.75 1.0 2.0 3.0 4.0];I = [10 7 5.2 3 1.8 1.5];I = [10 7 5.2 3 1.8 1.5];plot(R,I, 'ro--')plot(R,I, 'ro--') % show red circles at the data points% show red circles at the data pointsxlabel('Resistance'), ylabel('Current')xlabel('Resistance'), ylabel('Current')title('R vs I Plot')title('R vs I Plot')

0.5 1 1.5 2 2.5 3 3.5 41

2

3

4

5

6

7

8

9

10

Resistance

Cur

rent

R vs I Plot

Page 17: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 1717

6.5 Plotting Application 6.5 Plotting Application (continue)(continue)

Example 6-6: Mr. A performed an Ohm’s Example 6-6: Mr. A performed an Ohm’s experiment and collected the following experiment and collected the following measured voltage values (volts) and current measured voltage values (volts) and current values (milli-amperes):values (milli-amperes):

V = 0, 1, 2, 3, 4, 5, 6, 7, 8V = 0, 1, 2, 3, 4, 5, 6, 7, 8

I = 10, 8, 7, 7, 6, 4. 3, 2, 0I = 10, 8, 7, 7, 6, 4. 3, 2, 0

You are asked to help Mr. A to prepare data You are asked to help Mr. A to prepare data and generate a plot that shows red-lines and and generate a plot that shows red-lines and blue circles on all data pointsblue circles on all data points

Page 18: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 1818

6.5 Plotting Applications 6.5 Plotting Applications (continue)(continue)

Example 6-6: SolutionExample 6-6: Solution

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

milliamps')milliamps')title(‘E - I Plot')title(‘E - I Plot')

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

9

10

Voltage - Volt

Curre

nt -

milli

amps

E - I Plot

Page 19: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 1919

6.5 Plotting Applications6.5 Plotting Applications

Example 6-7: Maximum Power Transfer. A Example 6-7: Maximum Power Transfer. A voltage source E = 120 v with an internal voltage source E = 120 v with an internal resistance of Rs of 50 ohms supplying a load of resistance of Rs of 50 ohms supplying a load of resistance RL . resistance RL .

Q1 - Find the value of load resistance RL that Q1 - Find the value of load resistance RL that will result in the maximum possible power will result in the maximum possible power being supplied by the source to the load.being supplied by the source to the load.

Q2 – How much power be supplied in this Q2 – How much power be supplied in this case?case?

Q3 – Plot the power supply to the load as a Q3 – Plot the power supply to the load as a

function of the load resistance RLfunction of the load resistance RL..

Page 20: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 2020

6.5 Plotting Applications6.5 Plotting Applications

Example 6-7: Solution:Example 6-7: Solution: Circuit DiagramCircuit Diagram EquationsEquations

PL = IPL = I22 RL, RL,

where I is the current where I is the current passing through the circuit.passing through the circuit.

I = E/RI = E/Rtotal total = E/(Rs + RL) = E/(Rs + RL)

Array data for MALAB Array data for MALAB plottingplotting• RL – a vectorRL – a vector

• E and Rs are scalarsE and Rs are scalars

RL = ? ohms

Rs = 50 ohm

E = 120VI

Page 21: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 2121

6.5 Plotting Applications6.5 Plotting Applications

Example 6-7: Solution:Example 6-7: Solution: MATLAB ProgramMATLAB Program

0 10 20 30 40 50 60 70 80 90 1000

10

20

30

40

50

60

70

80Max Power Transfer

Load resistance RL

Powe

r - W

atts

%maxPower_p67.m%maxPower_p67.mRs = 50;Rs = 50;E = 120;E = 120;RL = 1:1:100;RL = 1:1:100;I = E./(Rs + RL);I = E./(Rs + RL);% NOT I = R/(Rs + RL)% NOT I = R/(Rs + RL)% -- for Scalar calculation% -- for Scalar calculationPL = (I.^2) .* RL;PL = (I.^2) .* RL;% NOT PL = I^2 * RL % NOT PL = I^2 * RL % -- for Scalar calculation% -- for Scalar calculationplot(RL, PL), grid onplot(RL, PL), grid ontitle('Max Power Transfer');title('Max Power Transfer');xlabel('Load resistance RL');xlabel('Load resistance RL');ylabel('Power - Watts');ylabel('Power - Watts');

Page 22: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 2222

6.5 Plotting Applications6.5 Plotting Applications Example 6-7: Solution:Example 6-7: Solution: AnswersAnswers Q1 – Find the value of RL Q1 – Find the value of RL

for Pmax to occurfor Pmax to occur• RL = 50 ohms results a PL RL = 50 ohms results a PL

max of 72 wattsmax of 72 watts Q2 – How much power be Q2 – How much power be

supplied in this case?supplied in this case?• PL consume 72 Watt,PL consume 72 Watt,• Rs will also receive the Rs will also receive the

same 72 Watts inside the same 72 Watts inside the power supplypower supply

• Power supplied is 144 wattsPower supplied is 144 watts Q3 – Plot the PL(RL)Q3 – Plot the PL(RL)

0 10 20 30 40 50 60 70 80 90 1000

10

20

30

40

50

60

70

80Max Power Transfer

Load resistance RL

Powe

r - W

atts

72 watts

Page 23: Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB lin.

Sept. 26, 2005Sept. 26, 2005 Lecture 6 - By Paul LinLecture 6 - By Paul Lin 2323

SummarySummary

Introduction to PlottingIntroduction to Plotting Simple X-Y PlotsSimple X-Y Plots

• Printing a PlotPrinting a Plot• Exporting a Plot as a Graphical ImageExporting a Plot as a Graphical Image• Line Color, Style, Marker and LegendsLine Color, Style, Marker and Legends

Multiple PlotMultiple Plot Logarithmic ScalesLogarithmic Scales Plotting ApplicationsPlotting Applications