Clear Filters
Clear Filters

Plotting temperature vs time using two equations and ode45 function

2 views (last 30 days)
I am trying to plot a temperature vs time profile for a simulation of the Marcet Boiler experiment. For those not familiar with it, the experiment is carried out by switching on a heater element that heats water in a chamber. Once the temperature has reached 200 degrees Celsius, the heater element is switched off and the apparatus is allowed to cool down.
The temperature while the heater is on is defined by:
dTdt = (Q_dot/mc) - ((A_plate * h)/mc) * T_diff;
where the left hand (Q_dot/mc) represents energy being added by the heater and the rest represents heat loss through a large metal disc fixed on the top of the apparatus. So when the heater is switched off, the temperature is defined by:
dTdt = - ((A_plate * h)/mc) * T_diff;
as the only heat transfer that is occurring in this stage is the heat loss through the metal disc. I am obtaining the differential using the ode45 function as follows:
tspan = [0 300]; %Period of experiment
T0 = 373.15; %Initial temperature in Kelvin
[t,T] = ode45('deltaT',tspan,T0);
So, what I am trying to do is switch between the two equations once the temperature has hit 200 degrees C (473.15 Kelvin). I tried a few different things, but couldn't get it to work properly.
The problem is that as the temperature drops, the program sees that the temperature has gone below 200 degrees C, so it switches back to the first equation, effectively turning the heater back on, and it gets stuck in this loop until it reaches the end of the tspan.
I would appreciate any help that anybody could offer, as I can imagine it will be a very simple solution that I just haven't thought of.
Thank you in advance.
Kind regards,
Craig

Answers (1)

Sarah Wait Zaranek
Sarah Wait Zaranek on 19 Mar 2011
You can use a persistent variable (e.g. switchVar and initially set it to zero) in your deltaT function
persistent switchVar
if isempty(switchVar)
switchVar = 0;
end
if switchVar == 1
% use equation 2
elseif T >= 200
% use equation 2
switchVar = 1
else
% use equation 1
end
Does that make sense? I think I worked the logic out correctly - but you can check me.
Here is the documentation for using persistent variables: http://www.mathworks.com/help/techdoc/ref/persistent.html

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!