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. We are going to use the Hybrid Equation Toolbox or resistor Simulink here. We're going to use the block that corresponds to the plant, which is right here. The input to this plant will be given by a signal that will be the zero one, that will be assigned in u. Let's use this Signal Builder. And the state of the system will be just the temperature. As we did in previous simulations, let's send it to the Workspace in the form of an array. And we can also do output the flow time and the jump time. 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. 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. The Simulink MATLAB will compile, generate the maps, code, and after that, the simulation will start. 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. y label ('u') and then grid on. Then we are going to save it as postprocessing. And let's see, do we have u? No, we don't have u because we didn't put it there. So let's create u, which will be just this signal here. Now we can resimulate for which we should re-initialize, otherwise T will complain. And there we go. So now we have to make into the postprocessing. 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.