June 2 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

June 2

Description:

For example, define your x-array as cos(2(pi)t) and y-array as sin(2(pi)t) ... Do the same for 2cos(2(pi)t) vs. 4sin(2(pi)t) 1999 BG Mobasseri 5 * Plots via a matrix ... – PowerPoint PPT presentation

Number of Views:193
Avg rating:3.0/5.0
Slides: 25
Provided by: ece12
Category:
Tags: june | pi

less

Transcript and Presenter's Notes

Title: June 2


1
June 2
  • GRAPHICS IN MATLAB- PART I
  • BASIC PLOTTING

2
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
  • t00.011
  • ycos(2pit)
  • plot(y)
  • Notice numbers on x-axis. They are not time

3
PLOTTING ONE ARRAY AGAINST ANOTHER
  • If t is one array and y another, plot(t,y) plots
    y vs. t.
  • Do this
  • t00.011
  • ycos(t)
  • plot(t,y)

4
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)

5
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
  • t0pi/16/2pi
  • xcos(t),sin(t)
  • plot(x)

each column is plotted separately vs. position
6
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
  • t0pi/162pi
  • Ycos(t),sin(t)
  • plot(t,Y)

each column plotted separately vs. another array
7
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
  • Yy1,y2,y3
  • plot(t,Y)
  • Method 2
  • plot(t,y1,t,y2,t,y3)
  • Method 3
  • Use hold command

8
RC circuit response
  • Capacitors charge and discharge following an
    exponential curve. Plot the following 3 voltages
    on the same graph for 0

9
Try it!
10
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

11
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

12
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 cosine

x range
function
13
MULTIPLE PLOTS USING fplot
  • fplot(sinc(x),sinc(2x),-2 2)

14
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, sr, puts red plus() marks for
    every data point.
  • MATLAB accepts many other choices.
  • Redo the exponentials using different styles

15
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

16
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

17
bar
  • Usage
  • bar(x,y)
  • plot the shape of a normal distribution given by
    yexp(-x2) in the range (-2,2).

18
fill
  • Usage
  • fill(x,y,b) where b indicates a blue fill.
  • plot the function exp(-t2), in the range (-2,2)

19
polar
  • Usage
  • polar(theta,rho)
  • Plot

20
stairs
  • Usage
  • stairs(x,y)
  • plot the function exp(-x2), in the range (-4,4)

21
ADDING LABELS AND TITLES
  • There are numerous ways the appearance of a plot
    can be controlled. The most obvious ones are
    through
  • axis labels
  • figure title
  • legend
  • grid

22
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

23
Try it!
24
Homework 3
  • Over a time range -3, 3, plot the following 3
    functions simultaneously using the matrix method
    of slide 7
  • ye-4xcos(2pix)
Write a Comment
User Comments (0)
About PowerShow.com