‫Welcome back.
‫And now let's continue with our inner loop now we're here in MPAC simplification and we're going to
‫enter this NPC simplification function.
‫So this function is here, support that NPC simplification.
‫It takes in your discrete ABC and the matrices and your horizon period, and it gives you these huge
‫matrices each double bar, then double bar transposed, then C double bar and then A with two hats.
‫So this NPC simplification function, I literally copied it from my previous course.
‫I did not change anything in this function compared to the previous course, because in the previous
‫course, I built it in a scalable way.
‫So if I have bigger matrices, then this function will take that into account automatically.
‫Remember that the point of the NPC simplification was to go from your discrete A, B, C and D matrices.
‫To first of all, these matrices, que double bar te double bar are double bar.
‫So you can see them here.
‫Cue double bar, T double bar, double bar.
‫And that's the process for that.
‫So first of all, you take your disk with a b, c d matrices, then you augment them.
‫And remember that this is the augmentation process.
‫So you see, I create these eight will then be till the C deal, then detailed the like this and I'm
‫doing the same thing here in the code.
‫Then these ones, here they are.
‫My MBC weight matrices, QSR.
‫And then I also need these terms here.
‫C tilde transpose times, queue times, C2 and then also see till there, transpose times s time still
‫there and then also queue times Ctrl there and then also s time see tilde and all that is created here
‫in this block.
‫Then queue, double bar, double bar and double bar.
‫That's this one here.
‫Well, actually here I simply create the big zero matrices.
‫And then here in this loop, I will start filling in these zero matrices.
‫So all these elements in these big matrices, these diagonal elements, they will enter into these Q
‫double bar and T double bar matrices here in this loop in your RW bar matrix is then created here,
‫which is inside this loop as well.
‫And then I also need my C double bar matrix and then a with two hats.
‫So these are these matrices here, and they are created here.
‫So for example, if you take a look at this C double bar matrix, then it becomes pretty long.
‫But these are just positions, so it's c double bar matrix.
‫And this would be the role for this matrix.
‫And then this would be the column for this matrix.
‫And then that element in the matrix equals this, which is some kind of element like, for example,
‫b tilde or maybe a deal, the squared times B tilde.
‫And then once they have those matrices, then I also need to get my h double bar and f double bar transposed.
‫And so I get them here.
‫Once I have my other matrices, I can construct my h double bar and then f double bar transposed, then
‫happens all here.
‫So, for example, h double bar equals this c double bar transpose times Q Double Bar Times C Double
‫Bar, plus R double bar.
‫And with these two operations, I will get that.
‫So first of all, I multiply the CW bar transpose with Q Double Bar and then whatever I get here, I
‫multiply by C Double Bar and then I add this double bar.
‫And well, these four lines here they are here to create this f double bar transposed matrix.
‫And then you give all these matrices to the main file and your main file receives each double bar,
‫double bar transpose C double bar and A with two hats.
‫And then if you take this row vector and you multiply it by f double bar transpose.
‫Then you will get your small f transposed.
‫So it's this one here.
‫So this role, that's this vector times this matrix.
‫And then to get your delta use, you had to take the gradient of your cost function and then you had
‫to take the inverse of your h double bar matrix.
‫And then you also had the minus sign here.
‫And then this f double bar times this vector that will be the transpose of this small F.T.
‫And so this formula here that you see in the code.
‫The formula is this one.
‫So you see I take the inverse of the double bar matrix.
‫I also take the transpose of my F.T.
‫And then I multiply them together and I put the minus sign here as well.
‫And then I get my dues.
‫Remember that here.
‫My due is not a scalar.
‫It's a vector.
‫And not only I have four time instances here, not only I have their K and then keep one and then K
‫plus two and then K plus three.
‫But for each time period, I have three different control input increments.
‫So for example, four K plus two I have Delta You two, Delta U three and Delta U four.
‫And that's true for K K plus one, K plus two and K plus three.
‫That means that this vector will have twelve elements.
‫But of course, for the plant, I need the original control inputs.
‫So I take my previous you to Q3 and Q4.
‫And then out of this deal vector, I take the first element for each control input.
‫So essentially, I'm doing this thing here.
‫You see, I only want to take this vector.
‫I have this big vector and then I have the small sub vectors.
‫And here you have an example four K plus two.
‫That's how this vector looks like.
‫And I only want to take this first vector.
‫And in the cold, I do it here and then here in this Utah little matrix, I keep track of all my control
‫inputs in order to be able to plot them later.
‫So you see, whenever I have my new, you want you to use three and you four.
‫They get concatenated to this you total matrix.