The last exercise is to produce a cobweb plot like that on page 9
of the notes.
To test this we will return to the problem
. Create
a new file called iteration2.m using the cp command (or the
editor):
cp iteration.m iteration2.m
Change the plotting commands in iteration2.m
so that both
and
are displayed. Run iteration2.m,
using an interval of
to ensure that this part is right.
To give a better visual test of the cobweb plot,
start with
. We now need to interpret the hand algorithm in MATLAB.
The first iterate
is
; to locate this point graphically
move horizontally across the plot until the line
is encountered.
Thus our starting point for the first vertical line is
(x(1),x(1))
.
The end point of the line has the corresponding
-value
cos(x(1))
or
g(x(1))
i.e.
x(2)
. Thus our first (vertical) line runs from
(x(1),x(1))
to
(x(1),x(2))
. The first (horizontal) line then
runs from
(x(1),x(2))
to
(x(2),x(2))
. So we need code like
plot( [x(n) x(n)], [x(n) x(n+1)], 'k') % vertical line
plot( [x(n) x(n+1)], [x(n+1) x(n+1)], 'k--') % dashed horizontal line
pause % requires user to hit enter to continue to the next iteration
Your code will also require a
clf
command right at the
beginning, to clear the figure at the start of each run, and a
hold on
command after the first plot command, to ensure that
all plots are on the figure at one time.
Additional: Write a short MATLAB function that will put a small arrow
head at the end of each of the vertical and horizontal lines, as in the figure.
You will need to pass the angle each line makes to the horizontal to this function;
the MATLAB function
atan2
may be useful here.