# How to store and retrieve variables during ode45 or ode23 call.

1 view (last 30 days)
Telema Harry on 26 Aug 2021
Commented: Jan on 30 Aug 2021
Please I need suggesstion on how to solve this problem.
My model is a set of coupled nonlinear and linear ODE and I am running the simulation using ODE23.
I want to open a vent 10 mins (600 secs) after the state variable Z = 10000, at that point the upward_velocity will be theoretically = 0, but in simulation the value of upward_velocity at the targeted Z will vary between (- 0.1 to 0.1).
Usually, the simulation will run for more than 6000 seconds before it attains the point when Z >= 10000. However, the time it runs before it attains this height alto depends on other factors. So the time is not fix.
First, I want to record the time instant (t_z) when Z = 10000 or just greater than 10000. The reason for this condition (Z >= 10000) is that the Z == 10000 may never be achieved.
Secondly, I want to open the vent after 600 seconds.
persistent t_z
tolerance = 100;
if Z >= 10000 && Z <= (10000 + tolerance)
t_z = t ; % when Z >= 10000 && Z <= 10000 + tolerance
end
if exist('t','var') ~= 1 % This code is not workinging because I declared t_z as persistent variable
if t >= (t_z + 600) && upward_velocity >= -5
venting = - A_ * C1 * sqrt(2*DP * rho);
% Opening the vent will cause a decrease of the upward_velocity.
% I want the vent to close when upward_velocity falls to a maximum
% of - 5 m/s
elseif t < (t_z + 600)
dM_venting = 0
end
else
dM_venting = 0
end
% A representative of my model is shown below
dZ = upward_velocity;
dM_valve = - rho*A_valve * valve_flow ;
dM = venting + valve;
I want to determine if a value has been assigned to t_z or if the variable exist. before implementing the second conditional statement (if t >= (t_z + 600) && upward_velocity >= -5).
So the question is;
1. What is the best way to record t_z variable just ones.
2. How can I check to see if the variable t_z exist.
I have struggled with this for a couple of days and I will greatly appreciate any help.
Thank you.
Jan on 30 Aug 2021
I do not understand what you are asking for.
Remember that Matlab's ODE integrators handle smooth functions only. Changing a parameter internally will confuse the stepsize controlling. This let the integration stop with an error, if you are lucky. With less luck, Matlab reduces the stepsize until the rounding errors are larger than the jump. This means: Do not use this approach. Use an event function to restart the integration, if themodel changes.

### Categories

Find more on Programming in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!