0:01

In this video, we're going to simulate the physical component of

Â a thermostat that corresponds to temperature changing over time.

Â And we saw that in a video and this is the snapshot of the video,

Â where the temperature was capital T and its change is governed by

Â this differential equation where a is a decay rate of the temperature, is positive.

Â With a negative in front makes the temperature decay.

Â This Tr is a constant that corresponds to outside factors affecting the temperature.

Â And these T delta correspond to the heater capacities multiplied by u.

Â When u is equal to zero,

Â the heater is off.

Â When u is equal one,

Â the heater is on.

Â To model this, we will use the Hybrid Equation Toolbox.

Â And for that, we are going to essentially create

Â a Simulink block that executes that differential equation over time.

Â So, let's call this example TemperatureInARoom,

Â and this work inside this folder,

Â where we are going to start a Simulink model that we are going to call the same.

Â 2:46

And it's a good idea to put a terminator here for the X minus output,

Â so that we won't have any warning.

Â So with this, now we can navigate inside the hybrid block model in the plant.

Â I'm going to the flow map to implement the temperature equation.

Â Let's take this arrow here.

Â The temperature equation is given by this expression that you see right here.

Â T dot equal minus aT plus Tr plus T delta times u.

Â And in order to do that within this function,

Â I would like to define T as the state and then define its derivative to be

Â minus a times T plus this constant Tr plus this constant T delta times u.

Â And that will be the derivative of this state which will

Â be equal to xdot in the general form of these flow map.

Â The one thing we don't have

Â is this constant a, or this constant Tr, or this constant T delta.

Â So what we're going to do

Â is to define them from a vector that we're going to call parameters.

Â The first entry of a vector will be a,

Â the second entry with be Tr,

Â and the third entry will be T delta.

Â So essentially,

Â this function here will include not only x and u but also parameters.

Â We need to define parameters in an initialization file,

Â and we're going to do that in a minute. Let me just show you that.

Â As soon as we add it into the argument of x this vector parameters,

Â Simulink believed that that was an input to the block,

Â certainly not the case,

Â so we need to simulate that that is

Â a parameter for which we'll click on the model explorer,

Â which is that button right there.

Â Go to the flow map,

Â and then with this parameters input,

Â we're going to change that to Parameter. And that's it.

Â Now, x and u's just the two inputs to the the flow maps.

Â And we're ready to go with an initialization file.

Â So let's start that initialization file,

Â which we're going to call just initialization.

Â And we're going to having that is as usual, clear everything.

Â So we'll start from scratch,

Â but in this case, we're going to have parameters.

Â So the first parameter will be a, which we're going to pick to equal to 1,

Â Tr, let's say somewhere of 65,

Â and T delta to be 20.

Â With these, we can now define the vectors parameters to be a, Tr, T delta.

Â And they have the order that we already specify.

Â After the parameters, we need to define the initial conditions.

Â So the temperature initial will

Â be all we need to set up because the system has only one state.

Â And let's say we set it up to below Tr.

Â So we see some change regardless of the value of the input.

Â And let's say it's 55.

Â We also need to determine the simulation horizon.

Â Let's say, we're going this simulate this for 20 seconds,

Â and the number of events.

Â And let's say, we say this is 10 seconds,

Â and let's say the number of events is 20.

Â As we did in previous simulation,

Â we need to define the rule for jumps.

Â And let's say that the rule for jumps is equal to one

Â corresponding that we give priority to jumps of a system,

Â which we shouldn't have any jumps.

Â And then for solver tolerances,

Â let's say we set them up to what we have done in previous simulations as well.

Â 7:28

Okay, so that's our initialization file.

Â Before we forget, let's go back to the model and make sure that we don't have any jumps.

Â So the jump set is always returning zero,

Â that means there is no jump.

Â And the jump map returns always a value equal to zero, and that's fine.

Â Now, let's take a look,

Â the first set has some constraints depending on the inputs,

Â and we don't even need any of that.

Â So, we should say that v,

Â or the output of this map is always equal to one.

Â And only set this function in the template was used in different arguments.

Â Let's just remove these arguments from there so they don't generated any error.

Â With that, we are ready to simulate the system for which we say,

Â and then we run the initialization file.

Â It seems we have broad parameters. There is the same value,

Â the same variable to cover little T's used for the horizon.

Â So we need to be carefully when we expect the data.

Â Anyhow, we can simulate now.

Â 9:20

So, as I said before,

Â now T has changed because

Â the output is also capital T is not the simulation horizon anymore.

Â We just need to be careful about that.

Â It's not a good idea to do that,

Â but I was just trying to follow what we did in a previous video.

Â In any case, what we could probably do is to create

Â a post processing script to plot

Â the functions that we obtain from the simulation.

Â So the first plot will be temperature,

Â for which we can use the actually the plotarc going.

Â Just the simplest plant commander that we have in the toolbox.

Â In the x, we are going to have in this plot time, t, in seconds.

Â And the label of this will be temperature.

Â We can set the grid to be on and we can have a legend that corresponds to T.

Â What we can put now is to also plot

Â the input for the value that is supplied to u.

Â And we can also do plotarc and we will plot u to this,

Â if we were to generate it. So, we need to add that to our model.

Â 12:04

And what we see here is the following.

Â Remember that Tr was set to 65.

Â So when u is equal to zero,

Â essentially what we have is T dot is equal to minus aT plus Tr.

Â A equal to one, that means that T converges to Tr. Tr equals to 65.

Â This is making sense.

Â T converges to 65 exponentially.

Â Once we change the input from zero to one,

Â this is the profile applied by the signal generation.

Â The default values of a signal generator are between zero and one,

Â which is good enough for us.

Â What happens is that temperature start

Â growing to essentially a set point which is Tr plus T delta,

Â which in our case is 65 plus 20, which is 85.

Â So, this turns to 85.

Â But after two seconds,

Â the heater is cut off,

Â and then the temperature now converges to 65 as you see right here.

Â This is the simulation that we would expect.

Â