1

I need help with the Events option in ode45. I have a pursuit problems which involves a dog chasing a rabbit. I have already solved the preliminary questions which involved plotting the trajectory of both the dog and rabbit.

This is the function / ode. THIS CODE IS CORRECT

function f = dograbbit(t,Y)
k=1.5;
r1 = (1+log(1+t))*cos(t);
r2 = (1+log(1+t))*sin(t);

dr1 = (-sin(t)-log(1+t)*sin(t)+(cos(t))/(t+1));
dr2 = (cos(t)+log(1+t)*cos(t)+(sin(t))/(t+1));

s = (k*sqrt(dr1.^2 + dr2.^2))/(sqrt((r1-Y(1)).^2 + (r2-Y(2)).^2));

f(1,1) = s*[r1-Y(1)];
f(2,1) = s*[r2-Y(2)];

end

I want to stop the simulation when the dog has caught the rabbit. This is when the distance between the two becomes smaller than a threshold value (10^-4)

Here is my events function (not sure if this 100% correct?):

function [value,isterminal,direction] = eventchase(t,Y)
r1 = (1+log(1+t))*cos(t);
r2 = (1+log(1+t))*sin(t);
distance = sqrt((Y(end,1) - r1)^2 + (Y(end,2) - r2)^2); 

if distance < 10^(-4)
    value = Y[end, 1];
end 

isterminal = 1;
direction = -1;
end

And this is the script where I call ode45 (getting errors):

options = odeset('Events',@eventchase); 
tspan = [0 10];
[t,Y,te,ye,ie] = ode45(@dograbbit, tspan,[4 0],options);


plot(Yv(:,1),Yv(:,2), 'r');

r1 = (1+log(1+tv)).*cos(tv);
r2 = (1+log(1+tv)).*sin(tv);
hold on 
plot(r1, r2, 'b');
legend('dog','rabbit');
scatter(4,0, 'r');
scatter(r1(1), r2(1), 'b');

Thank you

4

0 回答 0