Help with logic inside of a solver

1 view (last 30 days)
Sebastian Schultz
Sebastian Schultz on 25 May 2021
Edited: Jan on 25 May 2021
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;

Accepted Answer

Jan
Jan on 25 May 2021
Edited: Jan on 25 May 2021
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.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!