How to stop ODE fuction in a certain value
Show older comments
This is a basically batch bioreactor but I want a fed-batch reactor. In the code, I want to stop the ODE solver when my y(2) fuction values equal 20 or less than 20. Then, I want to continoue to solve ODE with again setting y(2) as its inial point y2o(=31). So basically I want to go back to inital data when ever y(2)=<20
Is there anyway to add break if else or for loop to ode45
function [tsoln, ysoln] = odemultiple
kd=0.076
Bmax=15.658
ib=0.616
K1=171.492
Umaxg=0.730
Ksg=1.293
Ksx=4.469
Umaxx=0.615
Yxsg=0.523
Yxsx=0.058
Ybsg=1.196
Ybsx=0.232
Ybxg=Ybsg/Yxsg
Ybxx=Ybsx/Yxsx
to = 0; tn = 100;
y1o = 0.9; y2o = 31; y3o = 32; y4o=0;
[tsoln,ysoln] = ode45(@odefun,[to tn],[y1o y2o y3o y4o]);
plot(tsoln,ysoln)
legend('y1','y2','y3','y4');
function dy = odefun(t,y)
Usg=(Umaxg*y(2))/((Ksg+y(2))*((1+y(3))/Ksx))
Usx=(Umaxx*y(3))/((Ksx+y(3))*((1+y(2))/Ksg))
Ug=(Usg+Usx)*(K1/(K1+y(2)+y(3)))*(1-(y(4)/Bmax))^(ib)
Unet=Ug-kd
dy = zeros(4,1);
dy(1) = Unet*y(1);
dy(2) = -Usg*((1/Yxsg)+(1/Ybsg))*y(1);
dy(3) = -Usx*((1/Yxsx)+(1/Ybsx))*y(1);
dy(4)=(Usg*Ybxg+Usx*Ybxx)*y(1);
end
end
Accepted Answer
More Answers (1)
Better you use the Event facility of the ODE integrators.
Note that your results become complex-valued because y(4) becomes greater than Bmax. So y(4) has also be adapted for the integration beginning where y(2) <= 20.
odesolve()
function odesolve
kd=0.076;
Bmax=15.658;
ib=0.616;
K1=171.492;
Umaxg=0.730;
Ksg=1.293;
Ksx=4.469;
Umaxx=0.615;
Yxsg=0.523;
Yxsx=0.058;
Ybsg=1.196;
Ybsx=0.232;
Ybxg=Ybsg/Yxsg;
Ybxx=Ybsx/Yxsx;
to = 0; tn = 100;
y1o = 0.9; y2o = 31; y3o = 32; y4o=0;
options = odeset('Events',@restart);
[tsoln1,ysoln1,te,ye,ie] = ode45(@odefun,[to tn],[y1o y2o y3o y4o],options);
[tsoln2,ysoln2] = ode45(@odefun,[te tn],[ye(1) y2o ye(3) ye(4)]);
plot([tsoln1;tsoln2],[ysoln1;ysoln2])
legend('y1','y2','y3','y4')
function dy = odefun(t,y)
Usg=(Umaxg*y(2))/((Ksg+y(2))*((1+y(3))/Ksx));
Usx=(Umaxx*y(3))/((Ksx+y(3))*((1+y(2))/Ksg));
Ug=(Usg+Usx)*(K1/(K1+y(2)+y(3)))*(1-(y(4)/Bmax))^(ib);
Unet=Ug-kd;
dy = zeros(4,1);
dy(1) = Unet*y(1);
dy(2) = -Usg*((1/Yxsg)+(1/Ybsg))*y(1);
dy(3) = -Usx*((1/Yxsx)+(1/Ybsx))*y(1);
dy(4)=(Usg*Ybxg+Usx*Ybxx)*y(1);
end
function [value,isterminal,direction] = restart(t,y)
value = y(2)-20.0;
isterminal = 1;
direction = 0;
end
end
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!
