You are on page 1of 7

1

Chapter 2: Introduction to MATLAB

MAS162 Foundations of Discrete Mathematics


Semester 1, 2016
Dr Amy Glen

Chapter 2: Introduction to MATLAB

School of Engineering & Information Technology

(continued)

Murdoch University, Perth, Australia


A.Glen@murdoch.edu.au
http://amyglen.wordpress.com

LECTURE 7
Plotting in two and three dimensions in MATLAB (ctd.)
+ Intro to for loops
Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

Chapter 2: Introduction to MATLAB

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Chapter 2: Introduction to MATLAB

Lecture 7 S1 2016

Plotting in two dimensions

2
4

Example
I

Last week, we were looking at array operations and 2D plotting


commands in MATLAB.

Today, well learn about the plot-related command ginput (used by


setting [xin,yin]=ginput) that records the coordinates of any
clicked positions of the cursor in the column vectors xin and yin.

Well also look at plotting in three dimensions.

And if time permits, well move onto using for loops in MATLAB.

Lets begin with a plotting example involving the zoom command that
we learnt about last lecture.

The next program allows the user to zoom in on the graphs of the two
functions f (x) = sin(x) and g(x) = x near the origin.
% Zoom in on two graphs
x=-10:0.01:10;
f=sin(x);
g=x;
plot(x,f,x,g,--)
xlabel(x)
ylabel(f and g)
title(Graphs of f(x)=sin(x) and g(x)=x)
zoom on
(Ex2pt11.m)

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

Chapter 2: Introduction to MATLAB

Plotting in two dimensions

Example . . .

Chapter 2: Introduction to MATLAB

Plotting in two dimensions

Example . . .
Graphs of f(x)=sin(x) and g(x)=x

10
8
6

We notice that as we zoom in, the graphs become closer and closer.

This means that for x near 0, sin(x) is close to x.


And the nearer x is to 0, the closer sin(x) is to x.

f and g

This is an important fact about the sin function.

What function do you think h(x) = 3 sin(x 2 ) is close to near x = 0?

0
2
4

Guess: 3x 2 .

We modify the program to graphically verify this answer . . .

8
10
10

(Ex2pt11ver2.m)
8

Dr Amy Glen (Murdoch University)

0
x

MAS162 Foundations of Discrete Maths

Chapter 2: Introduction to MATLAB

10

Lecture 7 S1 2016

Plotting in two dimensions

Dr Amy Glen (Murdoch University)

Chapter 2: Introduction to MATLAB

Lecture 7 S1 2016

Plotting in two dimensions

Another useful command to interact with a 2D graph window is


ginput.

This command records the coordinates of any desired position of the


cursor, which can then be printed or operated on in any way.

In its simplest form, the command is used as:

70

60

50

40

30

[xin yin]=ginput

20

where the variable names (xin and yin) can be chosen as desired.
I

10

10
5

ginput

Graphs of f(x)=3sin(x2) and g(x)=3x2


80

f and g

MAS162 Foundations of Discrete Maths

Then, with each click of the mouse button, the x and y coordinates
of the cursor (cross) are saved in the column vectors xin and yin.
To stop this process, press the Enter key.

Dr Amy Glen (Murdoch University)

0
x

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

Next, well see the usefulness of the ginput command for finding the
coordinates of the point(s) where the graphs of two different
functions intersect.

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

Chapter 2: Introduction to MATLAB

Plotting in two dimensions

Chapter 2: Introduction to MATLAB

Finding simultaneous solutions to two equations

Graphs of y=2x and y=1x

15

Example: Solve y = 2x and y = 1 x.

10

This simple example can be solved algebraically by writing

y = 2x = 1 x.
Thus 3x = 1 and therefore x = 1/3.

10

y = 2 1/3 = 2/3.

15

So these equations have a unique solution: x = 1/3, y = 2/3.

20
10

Graphically, this means that the lines y = 2x and y = 1 x intersect in


exactly one point, namely (x, y) = (1/3, 2/3).

Chapter 2: Introduction to MATLAB

Substituting x = 1/3 into the first (or second) equation gives

MAS162 Foundations of Discrete Maths

10

20

A common mathematical problem is to find simultaneous solutions to two


equations in two variables (unknowns), say x and y.

Dr Amy Glen (Murdoch University)

Plotting in two dimensions

0
x

10

(Sec2pt3eqns.m)

Lecture 7 S1 2016

Plotting in two dimensions

Dr Amy Glen (Murdoch University)

11

MAS162 Foundations of Discrete Maths

Chapter 2: Introduction to MATLAB

Lecture 7 S1 2016

Plotting in two dimensions

10
12

Example

Find the positive solution of the two equations


y = cos(x) and y = x 2 .

This is equivalent to finding the point of intersection of the graphs of the


functions cos(x) and x 2 in the positive quadrant of R2 (i.e., the positive
quadrant of the xy-plane).

For more complicated equations it is usually impossible to find the


solution(s) algebraically.
But we can still approximate the solution(s) graphically.

The MATLAB commands plot, zoom, and ginput are very useful for
this purpose.

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

11

The next program plots the graphs and allows the user to zoom in on the
intersection point to approximate the solution.
% Solve two equations using plot, zoom and ginput (Ex2pt12.m)
clear
x=0:0.01:1;
y1=cos(x);
y2=x.^2;
plot(x,y1,x,y2,--)
zoom on
pause % press Enter in command window to continue
zoom off
[xint yint]=ginput % (xint, yint) is the intersection point
Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

12

Chapter 2: Introduction to MATLAB

Plotting in two dimensions

13

Chapter 2: Introduction to MATLAB

Plotting in two dimensions

14

Graphs of y=cos(x) and y=x2


1
0.9
0.8

0.7

0.6
0.5

After zooming in on the intersection point (by repeatedly clicking),


pressing Enter, and then clicking on the intersection point with the
cursor (cross), we find that the estimated coordinates of the
intersection point (i.e., the approximate solution values) are:

0.4

x = 0.8241 and y = 0.6792.

0.3

0.2
0.1
0

0.1

0.2

0.3

0.4

0.5
x

0.6

0.7

0.8

0.9

If a certain accuracy is required, it is important to use a sufficiently


small increment (such as 0.0001) for the plots and to zoom in to the
intersection point sufficiently many times.

xint =
0.8241
yint =
0.6792
Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Chapter 2: Introduction to MATLAB

Lecture 7 S1 2016

Plotting in three dimensions

13
15

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Chapter 2: Introduction to MATLAB

Lecture 7 S1 2016

Plotting in three dimensions

14
16

Plotting in three dimensions


I

MATLAB has a number of specialised commands for plotting in three


dimensions.

The next MATLAB program plots the surface defined by the function
z = f (x, y) =

1
0.8

1 + x 2 + y2

0.6
0.4
0.2

% Plot a surface in three dimensions


clear
x = -2:0.1:2;
y = -1:0.1:2;
[xgrid, ygrid] = meshgrid(x,y); % defines grid matrices
z = 1./(1 + xgrid.^2 + ygrid.^2);
mesh(xgrid,ygrid,z) % generates a mesh plot
Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

0
2
2

1
1
0

0
1
1

15

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

16

Chapter 2: Introduction to MATLAB

Plotting in three dimensions

17

Chapter 2: Introduction to MATLAB

Plotting in three dimensions

18

Replace the last line by


surf(xgrid,ygrid,z)

0.8
0.6

% generates a shaded mesh plot

0.4

and note the difference in the plot . . .

0.2
0
2
2

1
1
0

0
1
1

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Chapter 2: Introduction to MATLAB

Lecture 7 S1 2016

Plotting in three dimensions

17
19

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Chapter 2: Introduction to MATLAB

Lecture 7 S1 2016

Plotting in three dimensions

18
20

Using contour(x,y,z)
2

1.5

We can also use each of the commands contour(x,y,z) and


meshc(xgrid,ygrid,z) to plot contours of the function.

0.5

0.5

1
2

1.5

0.5

0.5

1.5

Contour line: a curve along which z = f (x, y) has a constant value


i.e., a contour line joins points of equal height above the xy-plane
Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

19

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

20

Chapter 2: Introduction to MATLAB

Plotting in three dimensions

21

Chapter 2: Introduction to MATLAB

Plotting in three dimensions

22

Using meshc(xgrid,ygrid,z)
I

Note that you can specify the number n of contour lines by adding
this as an option:
contour(x,y,z,n)

0.6

To draw the contours corresponding to specific values of the function


z = f (x, y), use

0.4

contour(x,y,z,vec)

0.2

where vec is a vector containing the desired values.

0.8

0
2

vec=0.1:0.05:0.8;
contour(x,y,z,vec)

1
1
0

Lets try:
% vec=[0.10,0.15,0.20, ... ,0.75,0.80]

1
1

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Chapter 2: Introduction to MATLAB

Lecture 7 S1 2016

Plotting in three dimensions

21
23

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Chapter 2: Introduction to MATLAB

Lecture 7 S1 2016

Plotting in three dimensions

22
24

We can also use ginput with the contour plot to estimate the positions of
the local maximum and local minimum of the function z = f (x, y).

1.5

contour(x,y,z,500)
[xin,yin]=ginput
zmax=1/(1 + xin^2 + yin^2) % estimates local max if youve clicked
% on the corresponding contour line

0.5

Try the exercise on page 26 of the Unit Notes.


0.5

1
2

1.5

Dr Amy Glen (Murdoch University)

0.5

0.5

MAS162 Foundations of Discrete Maths

1.5

Lecture 7 S1 2016

23

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

24

Chapter 2: Introduction to MATLAB

for loop

25

for loop

for loop

26

for loop . . .

In computing it is often necessary to repeat the execution of a set of


statements, possibly to update some variable(s).

This can be achieved with a for loop.

More generally, the for loop in MATLAB has the form:

Simplest form in MATLAB:

for x=vec
... % some statement(s)
end

for n=p:q
... % some statement(s)
end

where the loop variable x takes the value of each of the elements of the
vector vec in order from the first to the last.

where n is the loop variable or index, which goes from p to q with


increment 1.
I

Chapter 2: Introduction to MATLAB

Note that n is not a vector here; it is a variable that takes on each of


the integer values from p up to q (inclusive) in turn.

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Chapter 2: Introduction to MATLAB

Lecture 7 S1 2016

for loop

25

Dr Amy Glen (Murdoch University)

27

Chapter 2: Introduction to MATLAB

An important use of a for loop is to compute a sequence of numbers that are


defined recursively.

Output

Example

t =

Write a MATLAB program to compute the first 10 terms of the sequence (tn )
defined by
t1 = 3; tn = tn1 + 2n for n > 2.

t =
t =
t =

% Compute a sequence
clear
t=3 % initialize variable as first term
for n=2:10
t=t+2*n % update variable: new value = old value + 2*n
end

t =
t =
t =
t =

The statement t=t+2*n in this program may look strange (because it is incorrect
as an equation).

t =

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

Lecture 7 S1 2016

for loop

26
28

3
7
13
21
31
43
57
73

91
t =
111

But such statements are common in computer programming and have a logical
meaning (as explained by the comment in the program above).
Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

27

Dr Amy Glen (Murdoch University)

MAS162 Foundations of Discrete Maths

Lecture 7 S1 2016

28

You might also like