Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1. Overview 2. plot in 2D 3. Plot in 3D 4. Other possible charts 5. Engineers: label your plots! 6. Plots & Polynomial Plotting 11."— Presentation transcript:

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

2 1. Plots & Charts, overview Plotting functions plot(), plot3(), polar(), meshgrid() Charting functions pie(), pie3(), bar(), bar3(), bar3h(), hist(), errorbar() Plot-related functions polyfit(), polyval(), text(), title(), xlabel(), ylabel() 22

3 2. Plots Create a graph of y vs. x plot(x, y) %order of arguments matters Example: 100 data-points x = linspace(-pi, pi, 100); y = sin(x); plot(x, y) Note that plot() connects the data points by default. 33

4 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. 44

5 2. Plots: line specifier 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 characters: 1 st character: the line’s color 2 nd character: the line style that connects the data points (dashed, dotted, full line, no line..) 3 rd character: the symbol at each data point (□ ◊ ○ ∆) 55

6 2. Plots: line specifier, cont. Specify the color only: Color plot(x, y, ‘r’); 66

7 2. Plots: line specifier, cont. Color and line style: plot(x, y, ‘r:’); 7 Line style 7

8 2. Plots: line specifier, cont. Color, type of line and data marker: Data marker style 8 plot(x, y, ‘r:d’); 8

9 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 99

10 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 not required. MATLAB will rotate through the default colors to make sure each plot has a different color. The other 4 arguments are MANDATORY. 10

11 3. Plots: 3 dimensions plot3() makes a 3D plot – requires x, y, and z data. 11

12 meshgrid() 12 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. In other words, I need to evaluate z at every pair of points between x and y.

13 3. Plots: 3 dimensions, cont. x = linspace(-pi, pi, 100); y = linspace(-pi, pi, 100); [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 13

14 4. Other Possible Charts polar() creates polar coordinate plots: x = linspace(-pi, pi, 100); y = cos(x) - sin(x).^2; polar(x, y) 14

15 4. Other Possible Charts, cont. pie(), pie3(), bar(), bar3(), bar3h(), hist(), errorbar() pie3() 15 Much like Excel offers: 15

16 4. Other Possible Charts, cont. pie(), pie3(), bar(), bar3(), bar3h(), hist(), errorbar() 16 Much like Excel offers: bar3h() 16

17 4. Other Possible Charts, cont. As with all the MATLAB possibilities, use… F1 = Help 17

18 5. Engineers: Complete Plots The following built-in functions should be applied to any graph created: title() %title on figure xlabel() %x-axis label ylabel() %y-axis label zlabel() %z-axis label Each function takes 1 'string' argument only and has no return value. 18

19 5. Engineers: Complete Plots Additional built-in function: text() Example: text(1, -2, -2, 'Cool plot!') 19

20 5. Engineers: Complete Plots Final built-in function: grid on 20

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

22 6. Plotting & Polynomials, cont. clear clc %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 line plot(x, y, 'd') hold on %combine future plots on this one %find best-fit polyn. of order 3 c = polyfit(x, y, 3) px = linspace(min(x), max(x), 100); py = polyval(c, px); plot(px, py) Remember that fitting a curve does NOT mean hitting every data-point! 22

23 Wrapping Up Plotting any kind of graphs mostly requires vectors of identical dimensions: (x,y) (x, y, z) (r, theta, z)... To plot density (on the y-axis) vs. altitude (on the x-axis), the syntax is: plot(altitude, density). The independent variable is the first argument. All functions are explained in the help, usually with examples that show how to place legends. As engineer, remember all graphs must be labeled correctly. For mathematical analysis, polyfit() and polyval() allow to fit a curve to data points. 23


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

Similar presentations


Ads by Google