Help with logic inside of a solver

1 次查看(过去 30 天)
I am using ode45 to solve a function describing the interaction between tanks in series. I have the variables C_IN and C_OUT specifing if carbon is flowing into or out of a tank at the specific time. If the mass of carbon in tank 1 drops below a lower limit, carbon must flow into tank 1 from tank 2 until a upper limit is reached. A portion of my code is below. The problem is that C_IN and C_out are reverted back to 0 for each iteration of the solver. Therefore instead of increasing to the concentration to the upper limit, C_IN switches back to 0 as soon as the carbon raises above the lower limit. How can I change my logic so that I can either keep my variables the same from the previous iteration of the solver, or otherwise restructure my code to achieve the same reslut?
C_IN = zeros(11, 1);
C_OUT = zeros(11, 1);
if x(11+i) < 20
C_IN(i/11) = 1;
C_OUT(i/11 + 1) = 1;
end
if x(11+i) > 30
C_IN(i/11) = 0;
C_OUT(i/11 + 1) = 0;
end
Dx(11+i) = C_IN(i/11)*(CF*x(11+i+ci)/T.volume*(1-j6) + j6*4) - C_OUT(i/11)*CF*x(11+i)/T.volume;

采纳的回答

Jan
Jan 2021-5-25
编辑:Jan 2021-5-25
The expression "previous iteration of the solver" is not meaningful. Remember, that the integrator can reject steps and evaluates the function several times for each successful step.
You need events to trigger the change of the model. See odeset and search for "EventFcn". Let the even stop the integration and start with the different parameter using the final value of the variables as initial values of the new integration.

更多回答(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