필터 지우기
필터 지우기

how to solve ODE with variable coefficients?

조회 수: 15 (최근 30일)
Daniel Niu
Daniel Niu 2022년 11월 14일
댓글: John D'Errico 2022년 11월 14일
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