how to solve ODE with variable coefficients?

15 次查看(过去 30 天)
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

采纳的回答

John D'Errico
John D'Errico 2022-11-14
编辑:John D'Errico 2022-11-14
You cannot use a numerical ODE solver, if you don't provide all of the coefficients. Numerical solvers like ODE45 work ONLY with numbers.
You CAN use tools like dsolve, if an analytical solution exists for the system.
  2 个评论
Daniel Niu
Daniel Niu 2022-11-14
Thank you for your answers, John.
But the lecturer said the prefered way is to use ode45.
How to solve the problem if there is no analytical solution exist?
thank you
John D'Errico
John D'Errico 2022-11-14
Then maybe you need to talk to your lecturer. Let me repeat a fact: ODE45 CANNOT solve a problem with a symbolic parameter in there.
Perhaps what your lecturer wanted you to do is to write a code that will solve the problem for some specific value of that parameter, where you can pass that parameter in. You might be doing an optimization of some sort perhaps, or you might be trying to use this to solve for a boundary value as a shooting method. So an optimizer might be varying a parameter of interest, then passing that parameter into the odesolver. At that point, the parameter is known, so ODE45 can be used.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by