how to solve ODE with variable coefficients?
Show older comments
Dear friend,
How to solve ODE with variable coefficients like this?

where the speed s_r and s_f depend on the distance they travelled. s_r=sf0*exp(-df(t)) and s_f=sf0*exp(-dr(t))
df and dr are the distance they have travelled.
I only can solve the problem when s_r and s_f are constant. I don't know how to solve the equation when the speed depends on the solution of
the ODE.
Your help would be highly appreciated.
s_r = 13;
s_f = 19;
z0 = [-250 -550];
x_burrow=[-600 600];
mindist = 0.01;
ts=[0 norm(z0)/(s_f-s_r)];
options = odeset ('Events',@(t,z)foxrab1(t,z,s_r, mindist,x_burrow));
[t,z,te,ze,zi] = ode45(@(t,z)foxode2(t,z,s_r,s_f),ts,z0,options);
plot(z(:,1),z(:,2),-(s_r * t)/sqrt(2),(s_r * t)/sqrt(2))
axis([-600 0 -550 600])
function [value , isterminal , direction]=foxrab1(t,z,s_r,mindist,x_burrow)
r = [-(s_r * t)/sqrt(2) (s_r * t)/sqrt(2)];
value(1) = sqrt((r(1)-z(1))^2+(r(2)-z(2))^2) - mindist;
isterminal (1) = 1;
direction (1) = -1;
value(2) = x_burrow(1)-r(1);
isterminal (2) = 1;
direction (2) = 1;
end
function dzdt = foxode2(t,z,s_r,s_f) % the definition of the ODE
r = [-(s_r * t)/sqrt(2) (s_r * t)/sqrt(2)]; % the position of the rabbit
dist = sqrt((r(1)-z(1))^2+(r(2)-z(2))^2);
dzdt = zeros(2,1);% make sure the output is a column vector
dzdt(1) = s_f*(r(1)-z(1))/dist; % horizontal velocity
dzdt(2) = s_f*(r(2)-z(2))/dist; % vertical velocity
end
Accepted Answer
More Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!