|
发表于 2004-4-28 16:34:48
|
显示全部楼层
<FONT color=#555555>%书上的一个程序,描述一只狗追一名慢跑者的运动轨迹 </FONT><>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]=ode23('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))); <P>clf;
hold on
axis([xmin xmax ymin ymax]);
axis equal
title('The jogger and the Dog') <P>for h=1:length(t)-1
plot([Y(h,1),Y(h+1,1)],[Y(h,2),Y(h+1,2)],'-',...
'color','r','EraseMode','none');
plot([J(h,1),J(h+1,1)],[J(h,2),J(h+1,2)],':',...
'color','green','EraseMode','none');
drawnow
pause(0.1);
end
hold off <P>%%%%%%%%%%%%%% <P>function s=jogger(t) <P>s=[8*t;0]; <P>%%%%%%%%%%%%%% <P>function [zs,isterminal,direction]=dog(t,z,flag); <P>global w % w=speed of the dog
X=jogger(t);
h=X-z;
nh=norm(h);
if nargin<3 | isempty(flag) % normal output
zs=(w/nh)*h;
else
switch(flag)
case 'events' % at norm(h)=0 there is a singularity
zs=nh-1e-3; % zero crossing at pos_dog=pos_jogger
isterminal=1;% this is a stopping event
direction=0; % don't care if decrease or increase
otherwise
error(['Unknown flag:' flag]);
end
end
|
|