MATLAB Answers

How to compare solution of ODE for each time step?

3 views (last 30 days)
Heejun Lee
Heejun Lee on 2 Aug 2021
Answered: darova on 2 Aug 2021
This is the code I'm using now.
opts = odeset('MaxStep', 1);
[T,Y] = ode45(@hw0,[0 100],[0.01 10], opts);
plot(T,Y)
And hw0 is,
function dy = hw0(t,y)
D=0.02;
um=0.2;
Ks=0.05;
X0=0.01;
S0=0.1;
Sf=10;
dy = zeros(2,1);
dy(1) = -D*y(1) + (um*y(1)*y(2))./(Ks+y(2));
dy(2) = Sf * D - D*y(2) - (um*y(1)*y(2))./(0.4*(Ks+y(2)));
what I want to do is compare the y(i,1) and y(i+1,1) then change the ODE condition when y(i,1)-y(i+1,1)<=0.0001*y(i,1)
(change D as D+0.02 value then start ODE from this point, repeat this process until X becomes 0)
what I've tried is below, sadly it doesn't work as I expected.
function dy = hw3(t,y)
D=0.02;
um=0.2;
Ks=0.05;
X0=0.01;
S0=0.1;
Sf=10;
dy = zeros(2,1);
dy(1) = -D*y(1) + (um*y(1)*y(2))./(Ks+y(2));
dy(2) = Sf * D - D*y(2) - (um*y(1)*y(2))./(0.4*(Ks+y(2)));
n=numel(y(:,1));
for i=1:n-1
if abs(y(i,1)-y(i+1,1))<=0.0001*y(i,1)
ix=i;
D=D+0.02;
break
end
end
Then I'm planning ODE recursion from t=ix, recusion stops when Y(1)=0; anyway for now, finding ix is the main problem.
what should I do to find ix?

Answers (1)

darova
darova on 2 Aug 2021
Maybe you are looking for event option. Then just place ode45 insife while loop
t0 = 1;
while contdition
[t,x] = ode45(f,[0 t0],x0);
t0 = t(end);
end

Tags

Community Treasure Hunt

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

Start Hunting!