Professional Documents
Culture Documents
(continued)
LECTURE 7
Plotting in two and three dimensions in MATLAB (ctd.)
+ Intro to for loops
Dr Amy Glen (Murdoch University)
Lecture 7 S1 2016
Lecture 7 S1 2016
2
4
Example
I
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)
Lecture 7 S1 2016
Lecture 7 S1 2016
Example . . .
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.
f and g
0
2
4
Guess: 3x 2 .
8
10
10
(Ex2pt11ver2.m)
8
0
x
10
Lecture 7 S1 2016
Lecture 7 S1 2016
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
f and g
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.
0
x
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.
Lecture 7 S1 2016
15
10
y = 2x = 1 x.
Thus 3x = 1 and therefore x = 1/3.
10
y = 2 1/3 = 2/3.
15
20
10
10
20
0
x
10
(Sec2pt3eqns.m)
Lecture 7 S1 2016
11
Lecture 7 S1 2016
10
12
Example
The MATLAB commands plot, zoom, and ginput are very useful for
this purpose.
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)
Lecture 7 S1 2016
12
13
14
0.7
0.6
0.5
0.4
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
xint =
0.8241
yint =
0.6792
Dr Amy Glen (Murdoch University)
Lecture 7 S1 2016
13
15
Lecture 7 S1 2016
14
16
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
Lecture 7 S1 2016
0
2
2
1
1
0
0
1
1
15
Lecture 7 S1 2016
16
17
18
0.8
0.6
0.4
0.2
0
2
2
1
1
0
0
1
1
Lecture 7 S1 2016
17
19
Lecture 7 S1 2016
18
20
Using contour(x,y,z)
2
1.5
0.5
0.5
1
2
1.5
0.5
0.5
1.5
Lecture 7 S1 2016
19
Lecture 7 S1 2016
20
21
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
0.4
contour(x,y,z,vec)
0.2
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
Lecture 7 S1 2016
21
23
Lecture 7 S1 2016
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
1
2
1.5
0.5
0.5
1.5
Lecture 7 S1 2016
23
Lecture 7 S1 2016
24
for loop
25
for loop
for loop
26
for loop . . .
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.
Lecture 7 S1 2016
for loop
25
27
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 =
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)
27
Lecture 7 S1 2016
28