事件选项ode45 MATLAB

时间:2016-12-02 00:39:13

标签: matlab events ode45

我需要ode45中的Events选项的帮助。我有一个追逐问题,涉及一只狗追逐一只兔子。我已经解决了初步问题,其中包括绘制狗和兔子的轨迹。

这是函数/ ode。这个代码是正确的

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

我想在狗抓到兔子时停止模拟。这是当两者之间的距离变得小于阈值(10 ^ -4)

这是我的事件功能(不确定这100%是否正确?):

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

这是我调用ode45(获取错误)的脚本:

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');

谢谢

0 个答案:

没有答案