ma/cs375 fall 2002 1 ma/cs 375 fall 2002 lecture 4

32
MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

Upload: leslie-hall

Post on 18-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 1

MA/CS 375

Fall 2002

Lecture 4

Page 2: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 2

Plotting in Matlab

Page 3: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 3

Example 5 (building and plotting a function of a vector)

1) create a vector x, with 50 equally spaced entries from 0 to 1.0

2) create a vector f, with entries of cosine evaluated at entries of 2*pi*x

3) plot f as a function of x, using + symbols

Page 4: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 4

Example 5(figure created by Matlab)

Page 5: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 5

Example 5 (building and plotting a function of a vector)

1) create a vector x, with 50 equally spaced entries from 0 to 1.0

2) create a vector f, with entries of cos evaluated at entries of x

3) plot f as a function of x, using a blue line

Page 6: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 6

Example 5(figure created by Matlab)

Page 7: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 7

Example 6 (building and plotting two

functions of a vector)1) create a vector x, with

50 entries from 0.0 to 1.0

2) create a vector f, with cos(2*pi*x).

3) create a vector g with entries sin(2*pi*x)

4) plot f as a function of x, using a blue line

5) on same graph plot g as a function of x, using red + symbols.

Page 8: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 8

Example 6

Page 9: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 9

Close Up of Example 6 code

Instruct Matlab to keep the current graph window and its contents for the next plot.

Page 10: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 10

Close Up of Example 6 code

Instruct Matlab that you have finished plotting in the current graph window.

Page 11: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 11

Quick Reference For Plot(Taken from Matlab: > help plot)

Various line types, plot symbols and colors may be obtained with PLOT(X,Y,S) where S is a character string made from one element from any or all the following 3 colunms:

y yellow . point - solid

m magenta o circle : dotted

c cyan x x-mark -. dashdot

r red + plus -- dashed

g green * star

b blue s square

w white d diamond

k black v triangle (down)

^ triangle (up)

< triangle (left)

> triangle (right)

p pentagram

h hexagram

Page 12: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 12

Adding a Title, Labeling Axes and Legends

• Ok – so we have made a cool graph

• But it isn’t much use to anybody else unless you label everything in sight!

• The following functions will modify the current figure: title, xlabel, ylabel, legend

Page 13: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 13

Example 7

The label commands

Page 14: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 14

Details:

• Set up the legend box with descriptions of each curve:>> legend('f=cos(2*pi*x)', 'g=sin(2*pi*x)');

• Label the axis>> xlabel('x');

• Label the y-axis>> ylabel('f,g');

• Give the graph a title>> title('The very boring graphs');

Page 15: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 15

Page 16: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 16

Modifying the Figure Properties

So I am not so pleased by the default fontsused by Matlab.

• It is easy to change the font:

• In the figure window click on the text object you wish to change

Page 17: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 17

Page 18: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 18

Click on “Edit/Current Object Properties”

Page 19: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 19

I prefer a larger Arial font

Page 20: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 20

Page 21: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 21

The Final Product

Page 22: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 22

Alternative Ways To Change Fonts..

Page 23: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 23

Page 24: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 24

Individual Exercise1) Create two curves on the same graph:

F = 1/(1+x^2) G = exp(x^3)

2) Plot the points at 33 points equally spaced between 0 and 1

3) Use black *’s for F and green o’s for G4) Label the horizontal and vertical axes5) Create a title (including your name)6) Create a legend7) Make sure the font’s are legible for my old eyes

(also figure out how to change the axis fonts)8) Print out your graph and hand it in.

Page 25: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 25

Plotting Two-Dimensional Data

Page 26: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 26

Example 7(Plotting Two-dimensional Data)

1) create two vectors x1d and y1d, with 20 entries from 0.0 to 1.0

2) create matrices of values using the x1d and y1d to distribute points in the unit square with 20 points in the x and y directions.

3) plot results in a graph.

Page 27: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 27

Results From Example 7

Page 28: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 28

Where are we going with this?

• We can evaluate a function at the two dimensional points (x2d,y2d)

• We can plot contours of this function where: a contour is a curve in the domain of the function where the value of the function is constant.

Page 29: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 29

Example 8(plotting contours of a two-dimensional function)

Page 30: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 30

Example 9(plotting two-dimensional function as a surface)

Page 31: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 31

Example 9 cont.(plotting two-dimensional function as a smooth surface)

Page 32: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 4

MA/CS375 Fall 2002 32

Summary of Lecture 4

• We have learnt how to:– plot a graph – plot multiple graphs in the same window– improve the fonts– label everything in sight….– plot contours of a two-dimensional function– plot a two-dimensional function as a surface