Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB"— Presentation transcript:

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

2 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 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 Plot 6-4 Logarithmic Scales 6-5 Plotting Applications

3 Sept. 26, 2005 Lecture 6 - By Paul Lin 3 6.1 Introduction to Plotting Data or Numerical facts Data or Numerical facts For making decision makingFor making decision making Pictures, Graphs, or Plots Pictures, Graphs, or Plots Describe dataDescribe data Ohms Laws: V = R * IOhms Laws: V = R * I MATLAB Plotting Capabilities MATLAB Plotting Capabilities 2-D XY Plot2-D XY Plot Semilog PlotSemilog Plot 3-D Plot3-D Plot MoreMore

4 Sept. 26, 2005 Lecture 6 - By Paul Lin 4 6.2 Simple XY Plots Basic Steps for Preparing a XY Graph 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 MATLAB XY Plot Related Functions figure, plot, subplot, set, grid on, axis, xlabel, ylabel, title, legend, textfigure, plot, subplot, set, grid on, axis, xlabel, ylabel, title, legend, text

5 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 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

6 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 Start MATLAB, and invoke M-File Editor Write the following MATLAB statements 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;

7 Sept. 26, 2005 Lecture 6 - By Paul Lin 7 6.2 Simple XY Plots (cont.) Example 6-1 (cont.) Verifying Data:Verifying Data: X = 0, Y = 15 Y = 0 – 0 + 15 = 15 Y = 0 – 0 + 15 = 15 X = 10, Y = 15 Y = 10^2 - 10*10 + 15 = 15 Y = 10^2 - 10*10 + 15 = 15 Exporting FigureExporting Figure Copy Figure for ExportingCopy Figure for Exporting

8 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.

9 Sept. 26, 2005 Lecture 6 - By Paul Lin 9 6.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 column, the first plot 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 subplot(2,2,1) – 4 subplots, two rows, two column, the first plot >> help subplot>> help subplot Examples Examples >> subplot(2,1,1), plot(sine) >> subplot(2,1,2), plot(cosine)

10 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) 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 Analysis Data preparationData preparation X vector from 0 to 2pi X vector from 0 to 2pi Y1 = sin2x Y1 = sin2x Y2 = 2cos2x Y2 = 2cos2x Two plots subplot(2,1,1), and subplot(2,1,2)Two plots subplot(2,1,1), and subplot(2,1,2)

11 Sept. 26, 2005 Lecture 6 - By Paul Lin 11 6.3 Multiple Plots Example 6-2 (cont.) MATLAB Program MATLAB Program %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');

12 Sept. 26, 2005 Lecture 6 - By Paul Lin 12 6.3 Multiple Plots Distinguish different plots Distinguish different plots Line ColorLine Color Line StyleLine Style Marker Style andMarker Style and LegendsLegends help plot for more info help plot for more info Example 6-3 Example 6-3%plot_p60.m x = 0: 1: 10; y = x.^2 - 10.*x + 15; plot(x,y,'r--', x, y, 'bo');

13 Sept. 26, 2005 Lecture 6 - By Paul Lin 13 6.3 Multiple Plots Legends Legends Name and its line styleName and its line style Example 6-4 Example 6-4 %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)')

14 Sept. 26, 2005 Lecture 6 - By Paul Lin 14 6.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 Functions MATLAB Functions semilogx(x,y) – plot x data on log scale and y data on linear scalesemilogx(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 scalesemilogy(x,y) – plot x data on linear scale and y on log scale Loglog(x,y) – plot both x and y data on log scaleLoglog(x,y) – plot both x and y data on log scale

15 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): 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 You are asked to help Mr. A to prepare data and generate a plot that shows red circles on all data points

16 Sept. 26, 2005 Lecture 6 - By Paul Lin 16 6.5 Plotting Applications (continue) Example 6-5: Solution 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')

17 Sept. 26, 2005 Lecture 6 - By Paul Lin 17 6.5 Plotting Application (continue) Example 6-6: Mr. A performed an Ohm’s experiment and collected the following measured voltage values (volts) and current values (milli-amperes): Example 6-6: Mr. A performed an Ohm’s 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 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

18 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')

19 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. 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. 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? 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. Q3 – Plot the power supply to the load as a function of the load resistance RL.

20 Sept. 26, 2005 Lecture 6 - By Paul Lin 20 6.5 Plotting Applications Example 6-7: Solution: Example 6-7: Solution: Circuit Diagram Circuit Diagram Equations 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 Array data for MALAB plotting RL – a vectorRL – a vector E and Rs are scalarsE and Rs are scalars

21 Sept. 26, 2005 Lecture 6 - By Paul Lin 21 6.5 Plotting Applications Example 6-7: Solution: Example 6-7: Solution: MATLAB Program MATLAB Program %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');

22 Sept. 26, 2005 Lecture 6 - By Paul Lin 22 6.5 Plotting Applications Example 6-7: Solution: Example 6-7: Solution: Answers Answers Q1 – Find the value of RL for Pmax to occur Q1 – Find the value of RL for Pmax to occur RL = 50 ohms results a PL max of 72 wattsRL = 50 ohms results a PL max of 72 watts Q2 – How much power be supplied in this case? Q2 – How much power be supplied in this case? PL consume 72 Watt,PL consume 72 Watt, Rs will also receive the same 72 Watts inside the power supplyRs will also receive the same 72 Watts inside the power supply Power supplied is 144 wattsPower supplied is 144 watts Q3 – Plot the PL(RL) Q3 – Plot the PL(RL) 72 watts

23 Sept. 26, 2005 Lecture 6 - By Paul Lin 23 Summary Introduction to Plotting Introduction to Plotting Simple X-Y Plots 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 Multiple Plot Multiple Plot Logarithmic Scales Logarithmic Scales Plotting Applications Plotting Applications


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

Similar presentations


Ads by Google