33
Plotting 1. Overview 2. plot in 2D 3. Plot in 3D 4. Other possible charts 5. Engineers: label your plots! 6. Plots & Polynomial

Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

Embed Size (px)

Citation preview

Page 1: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

Plotting

1. Overview2. plot in 2D3. Plot in 3D4. Other possible charts5. Engineers: label your plots!6. Plots & Polynomial

Page 2: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

1. Plots & Charts, overview

• Plotting functionsplot(), plot3(), polar(), meshgrid()

• Charting functionspie(), pie3(), bar(), bar3(), bar3h(), hist(), errorbar()

• Plot-related functionspolyfit(), polyval(), text(), title(),xlabel(), ylabel()

2

Page 3: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

2. Plots

• Create a graph of y vs. x plot(x, y) %order of arguments

matters

• Example: 100 data-pointsx = linspace(-pi, pi);y = sin(x);plot(x, y)

• By default, plot() connects the data-points with a solid line without markers.

33

Page 4: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

2. Plots, cont.

• Let us try with less data-points:x = linspace(-pi, pi, 10);y = sin(x);plot(x, y);

• Notice that the curve is less smooth

• This is another reason why linspace() is friendly to use: easily fixable.

4

Page 5: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

2. Plots: line specifiers

• A third argument can be added in the plot function-call:plot(x,y, _____)

• The third argument must be a string, made of up to three components:– One component for the line’s color– One component for the line-style– And one component for the marker symbol (the symbol at each data

point

55

Page 6: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

2. Plots: lineSpecs - color

• Specify the color only:

6

plot(x, y, ‘r’);

6

Page 7: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

2. Plots: lineSpecs – line style

• Color and line-style:

7

plot(x, y, ‘r:’);

7

(none): no line

Page 8: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

2. Plots: lineSpecs - marker

• Color, type of line and data-marker:

8

plot(x, y, ‘r:d’);

Page 9: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

2. Plots: line specifier, cont.

• If only the data-points must show, leave out the line-style:plot(x, y, 'rd')

• Forgot all the options?>> doc plot <enter>

99

Page 10: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

2. Plots: multiple plots

• Repeat the series of 3 arguments to combine multiple plots on 1 graph.

• Example:X = linspace(-2*pi,2*pi,50);Ysine = sin(X);Ycosine = cos(X);plot(X,Ysine,'r:o',X,Ycosine,'b--d')

• The string argument is unnecessary. MATLAB will rotate through the default colors to make sure each plot has a different color. The other 4 arguments are MANDATORY.

10

Page 11: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

11

2. Plots: hold on/off

• At default mode, the plot() command erases the previous plots before drawing new plots.

• If subsequent plots are meant to add to the existing graph, use: hold on, which holds the current plot.

• When you are done, use hold off to return to default mode.

• hold, by itself, toggles the hold modes.

Page 12: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

12

>> hold on

>> plot(x1,sin(2*x1),'g’)

2. Plots: hold on/off, cont.

>> x1 = linspace(0,pi,25);

>> plot(x1,sin(x1),'r')

* Range of the axis will be automatically adjusted.

Page 13: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

Example: Airfoil Lift

• Vortex Panel Method + Boundary Layer analysis– A numerical Computational fluid dynamics– Models the lifting surface of a wing as an infinitely thin sheet of

discrete vortices– Used to compute lift and induced drag & predict separation/stall

13

Page 14: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

3. Plots: 3 dimensions

• plot3() makes a 3D plot – requires x, y, and z data.

1414

Page 15: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

15

Meshgrid()

Given: x = [1 2 3];y = [4 5];

Evaluate: f(x,y) = x + y

In other words, I need to evaluate (x+y) at every pair of points between x and y.

Page 16: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

16

Meshgrid(), cont.

16

To make a useful plot, it is necessary to match up each x with each y before computing z. The function meshgrid() makes this easy to do.

Page 17: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

3. Plots: 3 dimensions, cont.

x = linspace(-pi, pi);y = linspace(-pi, pi);[X, Y] = meshgrid(x, y);Z = sin(X).^3 - cos(Y).^2;plot3(X, Y, Z)

Creates two arrays X, Y where each value of x is matched with each value of y

1717

Page 18: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

4. Other Possible Charts

• polar() creates polar coordinate plots:

x = linspace(-pi, pi);y = cos(x) - sin(x).^2;polar(x, y)

1818

Page 19: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

4. Other Possible Charts, cont.

• pie(), pie3(), bar(), bar3(), bar3h(), hist(), errorbar()

19

pie3()

19

Much like Excel offers:

>>x = 38 54 8 54 48

>> pie3(x)

Page 20: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

4. Other Possible Charts, cont.

• pie(), pie3(), bar(), bar3(), bar3h(), hist(), errorbar()

2020

Much like Excel offers:

bar3h()x = 8 9 1 9 6

>> bar3h(x)

Page 21: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

4. Other Possible Charts, cont.

• pie(), pie3(), bar(), bar3(), bar3h(), hist(), errorbar()

2121

errorbar()

Page 22: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

Example: Flight Delays

1. Percentage of flights delayed1. Percentage of those flights delayed due to carrier’s fault2. Percentage of those flights delayed due to weather 3. Percentage of those flights delayed due to NAS4. Percentage of those flights delayed due to security5. Percentage of those flights delayed due to late flights

2. Percentage of flights arrived on time

3. Present result in table4. If time, present results in pie chart

22

Page 23: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

Example: Plot results

• Plot the results for 2010 and 2012

23

Page 24: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

4. Other Possible Charts, cont.

• As with all the MATLAB possibilities, use…

24

F1 = Help

24

Page 25: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

5. Engineers: Complete Plots

• The following built-in functions should be applied to ALL graphs created:

title() %title on figurexlabel() %x-axis labelylabel() %y-axis labelzlabel() %z-axis label

• Each function takes 1‘string’ argument only and has no return-value.

2525

Page 26: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

5. Engineers: Complete Plots

• Additional built-in function: text()Example: text(1, -2, -2, 'Cool plot!')

2626

Page 27: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

5. Engineers: Complete Plots

• Built-in function: grid onCOMMAND line typed in the script, after a plot command.

• It stands alone on one line, requires no arguments, and returns no value.

2727

Page 28: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

5. Engineers: Complete Plots

• Additional built-in functions: legend(), xlim, ylim, axis

2828

Page 29: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

Plots

• All plots MUST have a title and axis labels!• All plots with more than 1 line should have a legend with appropriate

labeling.• Grids should be appropriate for the problem• Plot limits should be appropriate for the problem

– Is there a “0” value?

• As with using fprintf and input, a happy grader is a generous grader – make your plots easy to read/understand and your grader will be happier.

• If you see the phrase “suitable for technical presentation,” assume that means title, axis labels, grid (where appropriate), legend (where appropriate), and appropriate axis limits.

Page 30: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

6. Plotting & Polynomials• Plot-related functions that try to find an equation that links

data-points:polyfit(), polyval()

• polyfit() is like linear regression which finds the curve that best fits the data-points. polyfit() attempts to fit a polynomial – not a line. It mainly finds the coefficients of the polynomial that best fits the data given.

• polyval() is used to evaluate the polynomial at specified points. It makes use of the coefficients generated by polyfit(). – It is frequently used to generate a plot.

3030

Page 31: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

6. Plotting & Polynomials, cont.

clearclc

%generate tables of x, and y data = [1, 50; 4, 4900; 7, 4600; 10, 3800; 70, 1300; 100, 850; 300, 0.2e9;

700, 1.2e9; 1000, 1.2e9];x = data(:, 1)';y = data(:, 2)'; %plot data points, omit lineplot(x, y, 'd')hold on %combine future plots on this one

%find best-fit polyn. of order 3coeff = polyfit(x, y, 3)px = linspace(min(x), max(x), 100);py = polyval(coeff, px);plot(px, py)

31

Remember that fitting a curve does NOT mean hitting every data-point! 31

Page 32: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

Example: Beam Deflection

• Cantilever Beam Deflection– Determine the equation of the polynomialfrom experimental data.

32

Page 33: Plotting 1.Overview 2.plot in 2D 3.Plot in 3D 4.Other possible charts 5.Engineers: label your plots! 6.Plots & Polynomial

Key Ideas

• Plotting any kind of graphs mostly requires vectors of identical dimensions: (x,y) (x, y, z) (r, theta, z)...

• hold on allows multiple plots to be combined together.• The independent variable is the first argument. IT IS A COMMON MISTAKE

TO SWAP THEM.• All functions are easily explained in the help, usually with examples that

show how to place arguments.• As engineer, remember all graphs must be labeled correctly.• For mathematical analysis, polyfit() and polyval() allow to fit a curve

through data-points.

3333