# „ 1999 bg mobasseri1 9/18/2015 june 2 graphics in matlab- part i basic plotting

of 24 /24
1999 BG Mobasseri 1 03/25/ June 2 GRAPHICS IN MATLAB- PART I BASIC PLOTTING

Post on 28-Dec-2015

222 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

1999 BG Mobasseri 1 04/19/23

June 2

GRAPHICS IN MATLAB- PART I

BASIC PLOTTING

1999 BG Mobasseri 2 04/19/23

PLOTTING A SINGLE ARRAY

The most basic command that puts a graphic on screen is plot

If y is an array, plot(y) simply plots values of y vs. their array index position

Do this:

– t=[0:0.01:1];

– y=cos(2*pi*t);

– plot(y)

Notice numbers on x-axis. They are not time

1999 BG Mobasseri 3 04/19/23

PLOTTING ONE ARRAY AGAINST ANOTHER

If t is one array and y another, plot(t,y) plots y vs. t.

Do this:

– t=[0:0.01:1];

– y=cos(t);

– plot(t,y)

1999 BG Mobasseri 4 04/19/23

Try it!

Plotting one array against another generates creative figures

For example, define your x-array as cos(2(pi)t) and y-array as sin(2(pi)t). Then plot y vs.x. Let t run from 0 to 1 as before

Do the same for 2cos(2(pi)t) vs. 4sin(2(pi)t)

1999 BG Mobasseri 5 04/19/23

Plots via a matrix

The argument of plot can be a matrix. In this case, each column is plotted separately on the same axis. This is a compact way of plotting several graphs on the same axis

Try

– t=0:pi/16/:2*pi;

– x=[cos(t)’,sin(t)’]

– plot(x)

5 8 9 0

3 5 2 3

6 6 5 4

8 4 6 8

⎢⎢⎢

⎥⎥⎥

each column is plottedseparately vs. position

1999 BG Mobasseri 6 04/19/23

Plotting a matrix against another array

Let Y be a matrix and x be the vector of variables , then

– plot(x,Y) plots columns of Y vs. x on the same graph

Try this

– t=0:pi/16:2*pi;

– Y=[cos(t)’,sin(t)’]

– plot(t’,Y)

5 8 9 0

3 5 2 3

6 6 5 4

8 4 6 8

⎢⎢⎢

⎥⎥⎥

each column plottedseparately vs. anotherarray

1999 BG Mobasseri 7 04/19/23

Putting multiple plots on the same graph

There are 3 ways to do this. Want to plot 3 data vectors y1,y2 and y3 vs. t

Method 1:

– Y=[y1,y2,y3]

– plot(t,Y) Method 2

– plot(t,y1,t,y2,t,y3) Method 3

– Use hold command

1999 BG Mobasseri 8 04/19/23

RC circuit response

Capacitors charge and discharge following an exponential curve. Plot the following 3 voltages on the same graph for 0<t<5.

v1 =1−e−t

v2 =1−e−2t

v3 =1−e−3t

⎨⎪

⎩⎪

1999 BG Mobasseri 9 04/19/23

Try it!

1999 BG Mobasseri 10 04/19/23

hold COMMAND

Once hold is turned on, all subsequent plot commands are superimposed on the same axis

hold cycles between on and off.

For example,– plot(t,y1)– hold on– plot(t,y2)– plot(t,y3)– hold off

1999 BG Mobasseri 11 04/19/23

Homework #1

Plot y vs. x over the range x=[-2,2]. In addition to your m-file, save your figure and send it to me

y=1

x−0.3( )2 + 0.01+

1x−0.9( )2 + 0.04

−6

1999 BG Mobasseri 12 04/19/23

Plotting parametric functions- fplot.

You may want to plot a function, rather than an array of numebrs, against a variable.

Example is plotting cos(x). The command is

fplot(‘cos’,[-pi pi])

Note that the function must be known to MATLAB such as sine and cosinefunction x range

1999 BG Mobasseri 13 04/19/23

MULTIPLE PLOTS USING fplot

fplot(‘[sinc(x),sinc(2*x)]’,[-2 2])

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

1999 BG Mobasseri 14 04/19/23

CONTROLLING LINE TYPES

We can select line styles and colors in a graph

Simply pass a character string to plot

– plot(t,x,’s’)

For example, s=r+, puts red plus(+) marks for every data point.

MATLAB accepts many other choices.

Redo the exponentials using different styles

1999 BG Mobasseri 15 04/19/23

Homework #2

Plot the 3 exponentials on slide 8 simultaneously.

– 1. Use red, green and blue solid lines respectively

– 2. Use red plus sign(+), green star(*) and blue dots (.) for each graph

1999 BG Mobasseri 16 04/19/23

SPECIALIZED 2D PLOTTING FUNCTIONS

For each item write an m-code to see the plots. Read the example slides

bar - creates a, well, bar graph polar - creates a (plot hist - creates a histogram of data fill - fills the curve with solid colors stairs - similar to bar but without internal

lines

1999 BG Mobasseri 17 04/19/23

bar

Usage:

– bar(x,y)

– plot the shape of a normal distribution given by y=exp(-x2) in the range (-2,2).

-3 -2 -1 0 1 2 30

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

1999 BG Mobasseri 18 04/19/23

fill

Usage:

– fill(x,y,’b’) where b indicates a blue fill.

– plot the function exp(-t^2), in the range (-2,2)

-2 -1.5 -1 -0.5 0 0.5 1 1.5 20

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

1999 BG Mobasseri 19 04/19/23

polar

Usage:

– polar(theta,rho)

Plot =sin 2θ( )cos 2θ( )

0.1

0.2

0.3

0.4

0.5

30

210

60

240

90

270

120

300

150

330

180 0

1999 BG Mobasseri 20 04/19/23

stairs

Usage

– stairs(x,y)

– plot the function exp(-x^2), in the range (-4,4)

-4 -3 -2 -1 0 1 2 3 40

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

1999 BG Mobasseri 21 04/19/23

There are numerous ways the appearance of a plot can be controlled. The most obvious ones are through

– axis labels

– figure title

– legend

– grid

1999 BG Mobasseri 22 04/19/23

LABEL USAGE

axis labels

– xlabel(‘time’)

– ylabel(‘voltage’) Figure title

– title(‘Capacitor voltage’) Legend

– legend(‘first’,’second’,’third’) Grid

– grid - places a grid over the plot

1999 BG Mobasseri 23 04/19/23

Try it!

1999 BG Mobasseri 24 04/19/23

Homework #3

Over a time range [-3, 3], plot the following 3 functions simultaneously using the matrix method of slide 7

– y=e-4xcos(2*pi*x);