1. overview 2. plot in 2d 3. plot in 3d 4. other possible charts 5. engineers: label your plots! 6....

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

Upload: teresa-wilkinson

Post on 17-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

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

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

Page 5: 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. 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 10: 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. 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: 1. Overview 2. plot in 2D 3. Plot in 3D 4. Other possible charts 5. Engineers: label your plots! 6. Plots & Polynomial Plotting 11

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 22: 1. Overview 2. plot in 2D 3. Plot in 3D 4. Other possible charts 5. Engineers: label your plots! 6. Plots & Polynomial Plotting 11

5. Engineers: Complete Plots

The following built-in functions should be applied to any graph 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.

2222

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

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

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

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)

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

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

Wrapping Up 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.

2828