Changing array variables whilst they are being solved within ODE45 routine at each timestep

1 view (last 30 days)
Hello,
I have a problem which takes the following form:
[t T] = ode45(@dTdt,tspan,ICs)
end
Rates = dTdt(t,T,X,Y,Z)
dTdt = (X+Y-Z)*T -2*t;
end
So I am numerically solving for an array of temperatures, T over time t.
The problem is, I need to modify the temperatures after they have been solved over each time step. For example, after each timestep I might have to do the following:
for k = 2:NumberTemps;
if T(k-1)>T(k);
T(k) = 0;
end
end
The problem is that I don't know how to access the array of variables, T whilst the ODE45 solver is running.
How should I go about doing this?
Do I need to modify the ODE45 routine that is called and include the numerical step inside it?
Or is there a way to come out of ODE45 after each time step and execute an arbitrary procedure on the array that it is solving?
Best Regards
Pete
  6 Comments
Star Strider
Star Strider on 20 Sep 2012
If you want to stop the integration when the temperature T(t) begins to decrease (as in your example), I suggest you explore the options of the ODE Events Property ‘Events’ in odeset. Experiment with it until you get the result you want.
Peter
Peter on 20 Sep 2012
Hello and thanks for the comments so far.
The reason I need to stop the solver at each time step is that I am dealing with a thermal stratification problem in a liquid. Without going into the details, the procedure is essentially to numerical solve for a heat balance and then determine whether one nodal temperature is greater than another, then, depending on buoyancy and height, the nodes are mixed so that the temperature distribution is consistent with what you'd expect in a stratifying liquid. I'm confident that the procedure is correct for this application as it is an approach that I've read about.
As I say, the issue is that I need to perform the above procedure at each time step. I'll give the 'Events' route a shot as Star Strider suggests.
Again, very much appreciate your feedback on this.

Sign in to comment.

Answers (0)

Categories

Find more on Programming 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!