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

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

What is X, Y and Z? You want to modify T after each timestep? But T is your solution. And do you mean each step, or each successful step only - be aware that ODE45 has a step size control and the setps are not performed in chronological order necessarily.
Hello, X,Y and Z are just some arbitrary constants from elsewhere in the code...
What I mean is that after each successful time-step, I can manipulate the T array before the next time step is solved for. From what you say, I'm beginning to suspect that this is perhaps impossible?
Of course you can do everything in Matlab, as long as it is recursively computational. But ODE needs a sufficiently smooth curve for an integration and modifying T sounds like it introduces discontinuities, such that the stepsize control can cause to random results.
But I'm still not convinced, that mdofying the solution inside the integration is mathematically correct.
The big question is what are your goals here? I think Jan may be implying why are you using the ODE solver at all if you're simply changing the result when it's done?
I know this is easily doable in Simulink, which was a question on here not long ago, but I'm not sure how easy it is here.
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.
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

Asked:

on 20 Sep 2012

Community Treasure Hunt

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

Start Hunting!