matlab plotting with matlab 1. two dimensional plots the xy plot is the most commonly used plot by...

65
MATLAB Plotting With MATLAB 1

Upload: edith-doyle

Post on 29-Dec-2015

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

MATLAB

Plotting With MATLAB

1

Page 2: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Two Dimensional Plots

• The xy plot is the most commonly used plot by engineers

• The independent variable is usually called x• The dependent variable is usually called y

2

Page 3: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Consider This xy Data

3

time, sec Distance, Ft

0 0

2 0.33

4 4.13

6 6.29

8 6.85

10 11.19

12 13.19

14 13.96

16 16.33

18 18.17

Time is the independent variable and distance is the dependent variable

Page 4: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Define x and y and call the plot function

4

You can use any variable name that is convenient for the dependent and independent variables

time, sec Distance, Ft

0 0

1 0.33

3 4.13

5 6.29

7 6.85

9 11.19

11 13.19

13 13.96

15 16.33

17 18.17

Page 5: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

5

Page 6: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Engineers always add …• Title• X axis label, complete with units• Y axis label, complete with units• Often it is useful to add a grid

6

Page 7: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Creating Multiple Plots

• MATLAB overwrites the figure window every time you request a new plot

• To open a new figure window use the figure function – for example figure(2)

7

Page 8: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Plots With Multiple Lines

• hold on – Freezes the current plot, so that an additional plot

can be overlaid

• When you use this approach the additional line is drawn in blue – the default drawing color

8

Page 9: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

9

The first plot is drawn in blue

Page 10: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

10

The hold on command freezes the plot

The second line is also drawn in blue, on top of the original plot

The second line is also drawn in blue, on top of the original plot

Page 11: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Plots With Multiple Lines

• You can also create multiple lines on a single graph with one command

• Using this approach each line defaults to a different color

11

Each set of ordered pairs will produce a new line

Page 12: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Variations

• If you use the plot command with a single matrix, MATLAB plots the values versus the index number

• Usually this type of data is plotted on a bar graph

• When plotted on an xy grid, it is often called a line graph

12

Page 13: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

13

Page 14: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

If you want to create multiple plots, all with the same x value you can

• Use alternating sets of ordered pairs– plot(x,y1,x,y2,x,y3,x,y4)

• Or group the y values into a matrix– z=[y1,y2,y3,y4]– plot(x,z)

14

Page 15: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

15

Alternating sets of ordered pairs

Matrix of Y values

Define x first

Page 16: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

16

The peaks(100) function creates a 100x100 array of values. Since this is a plot of a single variable, we get 100 different line plots

Page 17: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Plots of Complex Arrays• If the input to the plot command is a single array of

complex numbers, MATLAB plots the real component on the x-axis and the imaginary component on the y-axis

17

Page 18: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Multiple arrays of complex numbers

• If you try to use two arrays of complex numbers in the plot function, the imaginary components are ignored

18

Page 19: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

19

Page 20: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Line, Color and Mark Style

• You can change the appearance of your plots by selecting user defined – line styles– color– mark styles

• Try using “help plot” for a list of available styles

20

Page 21: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Available choices

21

Table 5. 2 Line, Mark and Color Options

Line Type Indicator Point Type Indicator

Color Indicator

solid - point . blue b

dotted : circle o green g

dash-dot -. x-mark x red r

dashed -- plus + cyan c

star * magenta m

square s yellow y

diamond d black k

triangle down

v

triangle up ^

triangle left <

triangle right

>

pentagram p

hexagram h

Page 22: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Specify your choices in a string

• For example• plot(x,y,':ok')

– strings are identified with a tick mark– if you don’t specify style, a default is used

• line style – none• mark style – none• color - blue

22

Page 23: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

plot(x,y,':ok')

• In this command– the : means use a dotted line– the o means use a circle to mark each point– the letter k indicates that the graph should be

drawn in black

23

Page 24: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

24

dotted line

circles

black

Page 25: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

25

specify the drawing parameters for each line after the ordered pairs that define the line

Page 26: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Axis scaling

• MATLAB automatically scales each plot to completely fill the graph

• If you want to specify a different axis – use the axis command

axis([xmin,xmax,ymin,ymax])• Lets change the axes on the graph we just

looked at

26

Page 27: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

27

Use the axis function to override the automatic scaling

Page 28: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Annotating Your Plots

• You can also add– legends– textbox

• Of course you should always add – title– axis labels

28

Page 29: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

29

Page 30: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Improving your labels

You can use Greek letters in your labels by putting a backslash (\) before the name of the letter. For example:title(‘\alpha \beta \gamma’)

creates the plot title α β γ

To create a superscript use curly brackets title(‘x^{2}’)gives x2

30

Page 31: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Subplots

31

• The subplot command allows you to subdivide the graphing window into a grid of m rows and n columns

• subplot(m,n,p)

rows columns location

Page 32: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

subplot(2,2,1)

32

2 rows

2 columns

1 2

3 4

-20

2-2

02

-5

0

5

x

Peaks

y

Page 33: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

33

2 rows and 1 column

Page 34: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Other Types of 2-D Plots

• Polar Plots• Logarithmic Plots• Bar Graphs• Pie Charts• Histograms• X-Y graphs with 2 y axes• Function Plots

34

Page 35: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Polar Plots

• Some functions are easier to specify using polar coordinates than by using rectangular coordinates

• For example the equation of a circle is– y=sin(x)in polar coordinates

35

Page 36: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

36

Page 37: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Logarithmic Plots

• A logarithmic scale (base 10) is convenient when – a variable ranges over many orders of magnitude, because

the wide range of values can be graphed, without compressing the smaller values.

– data varies exponentially.• Aplot – uses a linear scale on both axes• semilogy – uses a log10 scale on the y axis• semilogx – uses a log10 scale on the x axis• loglog – use a log10 scale on both axes

37

Page 38: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

38

Page 39: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

39

Page 40: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Bar Graphs and Pie Charts

• MATLAB includes a whole family of bar graphs and pie charts– bar(x) – vertical bar graph– barh(x) – horizontal bar graph– bar3(x) – 3-D vertical bar graph– bar3h(x) – 3-D horizontal bar graph– pie(x) – pie chart– pie3(x) – 3-D pie chart

40

Page 41: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

41

Page 42: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

42

Page 43: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Histograms• A histogram is a plot showing the distribution

of a set of values

43

Page 44: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

X-Y Graphs with Two Y Axes

• Scaling Depends on the largest value plotted• Its difficult to see how the blue line behaves, because the scale isn’t appropriate

44

Page 45: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

45

The plotyy function allows you to use two scales on a single graph

Page 46: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Function Plots

46

• Function plots allow you to use a function as input to a plot command, instead of a set of ordered pairs of x-y values

• fplot('sin(x)',[-2*pi,2*pi])

function input as a string

range of the independent variable – in this case x

Page 47: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

47

Page 48: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Three Dimensional Plotting

• Line plots• Surface plots• Contour plots

48

Page 49: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Three Dimensional Line Plots• These plots require a set of order triples ( x-y-z values) as input

49

The z-axis is labeled the same way the x and y axes are labeled

Page 50: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

50

MATLAB uses a coordinate system consistent with the right hand rule

Page 51: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Surface Plots• Represent x-y-z data as a surface then use surf(z)

51

%surfplotdiary surf.datz=[1,2,3,4,5,6,7,8,9,10; 2,4,6,8,10,12,14,16,18,20; 3,4,5,6,7,8,9,10,11,12]x=linspace(1,50,10)y=linspace(500,1000,3)surf(z)xlabel('x-axis')ylabel('y-axis')zlabel('z-axis')diary

02

46

810

1

1.5

2

2.5

30

5

10

15

20

x-axisy-axis

z-ax

is

Page 52: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Bode Plots

52

Page 53: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

A Resistor in the Laplace (S) Domain

53

1K

R1

1K

R2+ +

__

vV

i I

a a

b bTime s

v = iR V = IR

Page 54: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

An Inductor in the Laplace (s) Domain

54

L1 L2+

_

v

i I0 = 0

LL+

_

VsLsL

I

Time s

L

Page 55: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

A Capacitor in the Laplace (s) Domain

55

C1 C1+

v

-

i

+V0

_

+

v

-

ICC 1/sC1/sC

Time s

Page 56: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Low Pass filter

56

Page 57: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Circuit Transfer Function H(s)

57

+v1(t)_

2 2

¼ F 4/s

R1

C1

+

v2(t)

_

+v1(s)_

R1

C1

+

V2(s)

_

V2(s)

V1(s)

=4/s

2 + 4/s=

2

S + 2H(s) =

Page 58: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

MATLAB Software – Bode Plot Template

58

H(s) = 2 / (s + 2)

n=[0 2];d=[1 2];bode (n,d);w=logspace (-1, 2, 200);[mag,pha]=bode (n,d,w);semilogx (w,20*log10 (mag));gridtitle(‘Bode Plot’)xlabel (‘w (Rad/sec)’)ylabel(‘Decibels (dB)’)

Start at 10-1

Stop at 102

generates 200 points between 100 & 104

Page 59: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Bode Plot

59

10-1

100

101

102

-35

-30

-25

-20

-15

-10

-5

0Bode Plot

w (Rad/sec)

Dec

ibel

s (d

b)

Page 60: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Bode Plot

60

10-1

100

101

102

-20

-15

-10

-5

0

5

10Bode Plot

w(Rad/sec)

Dec

ibel

s (d

B)

X: 2.026Y: -3.066

Page 61: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

61

Page 62: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

62

+

Vo

_

+

Vi

_

50K

R1

0.1uF

C1+

Vo

_

+

Vi

_

50K

R1

C1

1/RC = 1 / (50 x 103)(0.1 x 10-6) = 200

1/0.1s1/sC =

Page 63: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

MATLAB Software For Semi-log Paper

n=[1];d=[1];bode (n,d);w=logspace (0, 4, 200);[mag,pha]=bode (n,d,w);semilogx (w,20*log10 (mag));gridtitle(‘Bode Plot’)xlabel (‘w (Rad/sec)’)xlabel(‘Decibels (dB)’)

63

Start at 100

Stop at 104

generates 200 points between 100 & 104

Page 64: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

Semi-log Paper

64

100

101

102

103

104

-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1Bode Plot

w (Rad/sec)

Dec

ibel

s (d

b)

Page 65: MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called

65