It would be nice to simultaneously show the motions of both the child and the toy or the dog and the jogger instead of just plotting statically their orbits. This is possible using handle graphics commands in MATLAB. The main program for the child and her toy now looks as follows:
% main3.m
y0 = [0 20]';
options= odeset('RelTol',1e-10);
[t y] = ode45 ('f', [0 40], y0, options);
[X, Xs, Y, Ys] = child (t);
xmin = min (min (X), min (y (:, 1)));
xmax = max (max (X), max (y (:, 1)));
ymin = min (min (Y), min (y (:, 2)));
ymax = max (max (Y), max (y (:, 2)));
clf; hold on;
axis ([xmin xmax ymin ymax]);
% axis('equal');
title ('The Child and the Toy.');
stickhandle = line ('Color', 'blue', 'EraseMode', 'xor', ...
'LineStyle', '-', 'XData', [], 'YData', []);
for k = 1:length(t)-1,
plot ([X(k), X(k+1)], [Y(k), Y(k+1)], '-', ...
'Color', 'red', 'EraseMode', 'none');
plot ([y(k,1), y(k+1,1)], [y(k,2), y(k+1,2)], '-', ...
'Color', 'green', 'EraseMode', 'none');
set (stickhandle, 'XData', [X(k+1), y(k+1,1)], ...
'YData', [Y(k+1), y(k+1,2)]);
drawnow;
end;
hold off;
We define the variable stickhandle as a handle to a graphical
object of type line associated with the stick. In the loop, we
draw new segments of the child and toy orbits and move the position of
the stick. The drawnow command forces these objects to be plotted
instantaneously. Therefore, we can watch the two orbits and the stick
being plotted simultaneously.
In the case of the jogger and the dog we do not even have to define a handle. All we have to do is to draw the segments of the two orbits in the proper sequence:
% main4.m
global w;
y0 = [60; 70]; % initial conditions, starting point of the dog
w = 10; % w speed of the dog
options= odeset('RelTol',1e-5,'Events','on');
[t,Y] = ode45 ('dog', [0 20], y0, options);
J=[];
for h= 1:length(t),
w = jogger(t(h));
J = [J; w'];
end
xmin = min (min (Y (:, 1)), min (J (:, 1)));
xmax = max (max (Y (:, 1)), max (J (:, 1)));
ymin = min (min (Y (:, 2)), min (J (:, 2)));
ymax = max (max (Y (:, 2)), max (J (:, 2)));
clf; hold on;
axis ([xmin xmax ymin ymax]);
% axis ('equal');
title ('The Jogger and the Dog.');
for h=1:length(t)-1,
plot ([Y(h,1), Y(h+1,1)] , [Y(h,2), Y(h+1,2)], '-', ...
'Color', 'red', 'EraseMode','none');
plot ([J(h,1), J(h+1,1)] , [J(h,2), J(h+1,2)], '*', ...
'Color', 'blue', 'EraseMode','none');
drawnow;
pause(1);
end
hold off;