Presentation is loading. Please wait.

Presentation is loading. Please wait.

Plotting II: By the end of this class you should be able to: Create a properly formatted engineering graph Create graphs of a function Place multiple plots.

Similar presentations


Presentation on theme: "Plotting II: By the end of this class you should be able to: Create a properly formatted engineering graph Create graphs of a function Place multiple plots."— Presentation transcript:

1 Plotting II: By the end of this class you should be able to: Create a properly formatted engineering graph Create graphs of a function Place multiple plots in one figure Plot a polynomial functions Find polynomial roots by graph and by command Change the tick marks and tick labels on a graph Text: §§ 5.1 - 5.3

2 Review Exercise: Please complete in pairs Download the water vapor pressure data file from Data Files page This file contains two columns of data: Column 1 = Temperature (K) Column 2 = Vapor Pressure of Water (Torr) Prepare a Script that will: Load this file into MATLAB Plot Temp (x-axis) vs Vapor Pressure (y-axis) Label the axes Copy the script and the resulting graph into word and print out

3 Review: Plot commands Simple x/y plot >> plot(x,y)or >> plot(x,y,’*’) Plot multiple data >> plot(x1,y1,’+’,x2,y2,’p’) Labeling & Adjusting the graph >> xlabel(‘x-axis title’), ylabel(‘y-axis label’) >> legend(‘label 1’, ‘label 2’, …) >> gtext (‘text’) >> grid on/off >> axis([xlow, xhigh, ylow, yhigh])

4 Graphing Requirements ( pg 262) 1. axis label with quantity and units 2. regularly spaced, easy to interpret tick marks 3. label multiple curves 4. use title to distinguish as necessary 5. plot data using a symbol (6. avoid connection lines) 7. Plot functions using lines

5 Plan an x range to plot Create an x vector Calculate the y value for each x Plot x vs. y Revise x range and vector if necessary Review: General Procedure for plotting functions:

6 A projectile is thrown straight upward. It’s height is governed by the formula: where: h = height of the projectile v 0 = the initial velocity of the projectile = 50 m/s t = the elapsed time in seconds g = the acceleration of gravity = 9.8 m/s 2 How would you plot the height of the projectile vs. time?

7 Steps to plot projectile need to figure out how long it will be until the projectile hits the ground (so we know how long the x vector should be). –set height equal to zero –will equal zero if either term is zero, t=0 is one solution –for the other solve for time end now set up the x vector >> t = 0:1:tend; plug into function to get heights >> h = 50*t – 0.5*9.8*t.^2; now plot the result >> plot(t,h), xlabel(‘time (s)’), ylabel(‘height (m)’)

8 Steps to plot projectile (cont.) Resulting plot Plot is adequate but could be better. it does not actually reach the ground the curve is a little choppy. fix these problems with a new x vector >> t = linspace(0, tend, 40); or >> t = 0:0.025*tend:tend; calculate h and plot as before.

9 Write a function to plot height of projectile vs. time (complete a function development worksheet in the process) It’s height is governed by the formula : where: h = height of the projectile v 0 = the initial velocity of the projectile = 50 m/s t = the elapsed time in seconds g = the acceleration of gravity = 9.8 m/s 2 Also Remember:

10 1.Problem Statement: We wish to create a function that will plot the height vs. time of a projectile thrown straight up with an initial velocity of v 0. The graph should cover from initial launch through when the projectile returns to the ground. Inputs Variable NameDescriptionUnitsInput Source * v0the initial velocitym/sec.command line Outputs Variable NameDescriptionUnitsOutput type * NAA plot of height vs. timem, sec.display figure Handout summary sheet – have students work on function

11 4.Solution Steps/Test Calculation solution steps: calculate total time (tend): (tend = 2v0/g) create an x vector (x = linspace(0, tend, 50)) plug into formula to create a y vector (previous formula) plot with proper labels test calculation: we do need some key points to validate the result first we can check if the curve hits zero at 10.2 sec. next we can check the maximum height (when will that occur?) which comes at ½ the total time (5.2 sec) h max = (50 m/)(5.2 s) – 0.5*9.8 m/s 2 )(5.2 s) 2 = 127.6 m;

12 5.Coding (continues in next frame): function projectile(v0) % This function calculates the height of a projectile versus % time when launched upward with an initial speed of v0. It % plots ten points on the graph of height vs time. % % function projectile(v0) % input variables: v0 = initial speed (m/sec) % output: a graph of height vs time. % other variables: % g = acceleration of gravity (m/sec2) % h = vector of heights of the projectile (m) % t = vector of times corresponding to above heights (m) % tend = time until projectile hits the ground again (s) % S. Scott Moor February 2006

13 % Calculate final time g=9.8; tend = 2*v0/g; % Set up the time vector t=0:0.02*tend:tend; % Calculate the corresponding heights h=v0*t - 0.5*g*t.^2; % Ploting the results plot(t,h) xlabel 'time (s)', ylabel 'height (m)' V = num2str(v0); b =['Projectile height, v_0 = ', V, 'm/sec']; title(b)

14 Validation: 6.Test case check: Run function with v0 = 50, does it match test calculation? 7.Reality Check: Now we need this step, we have only checked the curve at a couple of points. Look at the overall curve – Does it look like what you would expect (i.e., does it look like a parabola)? 8.Test on wider cases: Try the function several times with different inputs. Does it continue to give the graphs you would expect?

15 Graphing Hints ( pg 281, 282) Plan Axes Use sensible tick mark spacing Avoid trailing zeros Start Axes from zero whenever possible Use different line types for each curve (plots must be “xeroxible” ) Use same axis range and tick mark spacing for graphs that will be compared Avoid clutter (too many curves in one area)

16 www.engr.ipfw.edu/~moor/121 Go to ENGR 121 page Download: –analyze.m and –piano.wav Run >> analyze(‘piano’)

17

18 Analyze.m [y, Fs] = wavread(file); % y is sound data, Fs is sample frequency. t = (1:length(y))/Fs; % time ind = find(t>0.1 & t<0.12); % set time duration for waveform plot figure; subplot(1,2,1) plot(t(ind),y(ind)) axis tight title(['Waveform of ' file]) N = 2^12; % number of points to analyze c = fft(y(1:N))/N; % compute fft of sound data p = 2*abs( c(2:N/2)); % compute power at each frequency f = (1:N/2-1)*Fs/N; % frequency corresponding to p subplot(1,2,2) semilogy(f,p) axis([0 4000 10^-4 1]) title(['Power Spectrum of ' file])

19 To notice in Analyze.m subplot command – creates multiple plots on one figure >> subplot( rows, columns, current location) figure command – creates a new figure window (instead of overwriting an existing plot) >> figure Shape of figure grid # indicating the space on the grid for the next plot

20 Complete the Exercises: “Plotting Polynomials and other graphing stuff” (see handout) 1.Plotting polynomials 2.Adjusting tick marks 3.Plotting Polynomials with Polyval 4.Finding roots graphically 5.Roots command 6.Using the “hold” command

21 Review: Zooming and holding plots Zooming in on a plot (>> poly1 % plot set up script): >> axis([-1.5 10 -25 25])% adjust the axis Using data curser: –Select from tool bar, place near zero of curve –move right/left with curser keys, find point nearest zero –right click and choose “Create New Datatip” to point to other zeros Adding a line at zero >> hold on% holds existing plot >> x = [-1.5 10];% set x high & low limits >> y0 =[0, 0];% set y equal to zero >> plot(x,y0)% add to plot >> hold off% turn hold off Adjust plot axis interactively Choose: Edit  Axis Properties..., change values in dialogue


Download ppt "Plotting II: By the end of this class you should be able to: Create a properly formatted engineering graph Create graphs of a function Place multiple plots."

Similar presentations


Ads by Google