1. Home
  2. Explore
  3. Engineering
  4. MPC Controller Design in MATLAB

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

MPC Controller Design in MATLAB

Transcript

Transcriber’s Name: Crescendo Transcription Pvt Ltd Process Control-Design, Analysis and Assessment Doctoral Research Scholar Manikandan S Department of Chemical Engineering Indian Institute of Technology, Madras MATLAB Tutorial-Controller Design - Part 3 Welcome everyone to MATLAB tutorial for process control: analysis, design and assessment. In this tutorial, we look at MATLAB implementation of model predictive controller. Just as an introduction, I will give the formulation of MPC and I will directly go to the code section of MATLAB and then show you how how to code implement model predictive controller and how we can uhh during the model predictive controller in order to get the performance we require and so on. So the model predictive controller is a controller which solves an optimisation problem online. So the optimisation problem is formulated such that Uhh the model predictive controller

uses model and uhhet can be easily solved. Most of the time, we solve a quadratic program optimisation. It computes optimal input moves which which is optimal in the sense of the objective function we have chosen. And since this is an optimisation problem, Uhh it can handle constraints easily. Uhh And it is easily extendable to MIMO systems. The example I am going to show here is for SISO system. Uhh But the implementation is not restricted to see SISO system. It can

be extended to MIMO. Only that we will have to look at how the structure of each term in the objective function and prediction oriented terms are in MIMO systems. And MPC is also computationally time-consuming Uhh but the examples which I am going to show here will be fast enough. But if we have more inputs and more more outputs for example like 25 cross 20 system where we have 25 out and we have 20 inputs then such systems will require more computational

time in order to get the optimal in order to calculate the optimal solution for the optimisation. Now this is the formulation where we minimise the objective function where Y sat pine minus Y hat of K plus whole square and there is a minimum move Uhh objective added to the deviation from setpoint. Now the Y hat of K plus I is computed as an impulse response that finite table’s response model with gamma coefficients. And we have the constraint

that U of K plus I equals to U of K plus M minus 1 where I is M to P. So what we are essentially saying is though we have gamma elements of U to Uhh optimise, we choose to optimise only M elements. And on top of it, we have limit constraints on U for all time instants which is from 1 to M. Notice that I have not specified what P is and what M is and neither have I specified the limits. So these will depend on individual Uhh Uhh these will depend on individual applications

and we may have to modify them to suit to get a better performance. Look at how we can compute and make the computation easier for us to implement it in MATLAB. So we are computing summation I equals to 1 to P y of k plus i reference minus y hat of k plus i whole square. So we need to compute y of k plus I y hat of k plus i as a function of h1 to h gamma. So we have h1 u of k plus h2 u of k minus 1 all the way up to h gamma

u of k minus gamma plus 1. Now in this optimisation function, we have minimized u. This u is constructed such that it has uk, uk plus 1 all the way up to uk plus m minus 1. So these are the unknowns which we which we have to optimise. So this uk is unknown, whereas the rest of the terms are known. So this essentially evaluates to a constant. And the next term y of k plus 2 will be h1 times u of k plus 1 plus h2 times

u of k plus all the way up to h gamma times u of k minus gamma plus 2. This will continue up to y hat of k plus m minus 1 which is h1 times u of k plus m minus 2 all the way up to h gamma times u of k minus gamma plus and minus 1. And y hat of k minus 1 k k plus m is h1 times u of k plus m minus 1 all the way. Similarly they will have y hat of k plus m plus 1. Here is where because the constraint that u of k plus m equals to u of k plus m minus 1. Because of that we will have h1 plus

h2 times u of k plus m minus 1 plus all the way. So finally we will have y hat of k plus p which is h1 to h into u of k plus m minus 1 and etc. So we can notice the general structure where we have h1, h2 h1, h3 h2 h1, like this times u plus some known matrix which we can formulate. We will look at MPC implementation in MATLAB. Shown here is the code which implements an MPC for a SISO system. First I have declared the system here as a transfer function with

the 3S plus 1 divided by 2S square plus 5S plus 1 as the transfer function. And I have discretised the system using a sampling time of 1 1 second. So TS equal to 1 implies that I have sampled the system once, for one second and I have computed the impulse coefficient using the inbuilt function, Impulse. Now in the next section, I have declared the model. So as a start we will start with the same system as the model and see how the performance is. Then we will look at introducing plant

model mismatch and see how the MPC performs. So I will just comment the this line also. Then I move on to control specification where I specify the length of the simulation as 100 seconds and we dif we initialise the output parameters y and u. Then I move on to tuning parameters which we can change based on the requirement. So the tuned at P is the prediction horizon, tuned at N is the control horizon. TS is again sapling time. Bias and biasc are

bias correction parameters. Uhh we will start with dim off now and we will see how they affect the controller performance later. Lambda is the mult multiplier used for waiting the objectives which is deviation from setpoint square plus Uhh minimum move. So the subjective option helps us decide which objective to be optimised in the MPC formulation. Now the then I have specified the lower and upper limits for input which are specified

as minus 0.5 to 10. Then I declare reference trajectory which is the setpoint as of now as 2.5. And I declare such some internal variables. Now because we are using linear time invariant models, the matrices multiplying the decision variables uk to uk plus m minus 1 and the constant matrix are time invariant. Because of that, I precompute those matrices called T and S in in this section and I store it in the structure tune.

These are done apriory so that the come computation time is reduced. Now runMPC is the functional which runs the optimisation for one iteration M provides solution from uk plus uk to uk plus m minus 1 and those values are stored in the variable u of which we will just select the first few Uhh first input variable, first-time instant input variables and apply it to the system. Notice that I have computed y predicted as well as y actual using model and system

coefficients. This is to generalise the case where we plant model mismatch. So in in case of plant model mismatch, predicted y and the actual y will be different. So we will have to correct for that bias in a in real-time fashion. So that has to be updated in the MPC formulation. So in order to do do that, I have computed both predicted y and the actual y. The final section of the code consists of plotting the results and correspondingly I have named the

trajectories and given labels for y, x and titles. Let us look at how runMPC is configured so that we are sure about the coding. So here, you can see the runMPC function. It takes reference y, the model parameter as model, tuned structure as tuned, OP is the previous iteration previous time instant input variable. These are used to compute the constant which is part of MPC formulation and the u not is the initial condi initial condition or initial guess for the optimisation. In the first 2

lines, I have created a matrix containing the input lower limit and upper limits repeated m times. In order to do that, I have used MATLAB function, repmat. And I have also given a provision for specifying terminal constraint which is u of k plus p equals to y setpoint which we will do in the in a later case. So now we will be running this case where we have no constraints other than limits. Now this MPC objective function computes the MPC objective which is y minus y setpoint

whole square times whole square summation y minus y setpoint whole square for I equals to k plus 1 to k plus p. And similarly we have summation u of k plus i whole square where i runs from 0 to m minus 1. So that is calculated here. The In order to compute yp, we use that Uhh the the matrices which we computed as T and S here. T times u. u is the decision variables containing u of k to u of k plus m minus 1 ad OP is the variable containing previous iteration u values.

So we will have YP as a vector of P output variables and then we update the bias here by constructing a vector of P values repeated either as 0s or using the constant which is computed in the previous iteration. Then we have 2 cases where in one case, we do not have this summation u u of k plus i whole square and in in the other case where we have summation u of k plus i whole square, the effect of this will be easily seen later.

Let us move on and run the code. So this is the function that has to be studied. This is the script we have to run in order to see the performance of MPC. Let us run it. So now you can see that the system reaches the setpoint fairly quickly, within see 10 to 15 seconds. We can zoom in and see. So around 15 second it reaches to 0.5 and stays there. Notice the input variation. It starts from 3.3, goes down to 2.5. So it has 0.8 as the variation.

Now without closing this we window, I am going to just change the type of objective which we have used. So this, the objective we have used did not have the summation of u square. So if if we just change the objective, how will the system perform is what we are going to look at now. Now you can see that initially the whole variation was around 3.3 to 2.8 which was 0.5 and here also it is similar but the output since the initial Uhh initial

input was 3.3 at that time, the output was able to reach 2.5. But since we have minimal variation in the input as a objective, there is a bias in the performance. So we can tune that by tuning this lambda. We will put minimal weight to the 2nd term which is summation u square. Let us see how that works. Now you can see the response actually reach the setpoint and the variation is like 3.3 to 2.5 which was the previous variation also.

Now let us look at in introducing model mis plant model mismatch and how the system performs. I will just change the 2nd parameter to say 0.1323. So what we are saying is all the parameters other than model 2nd term in the model is estimated properly. This particular coefficient is estimated in a wrong way. So if if we do that without the bias correction, how will the performance be? Now you can see, we have oscillatory performance. This oscillation

is because of the constraint which is being Uhh forced in the object optimisation function. You can see that it becomes minus 0.5 for 2 seconds and then it goes back. So this is the reason why the response is oscillatory. If you are The actual model function of the 2nd constant has the value of 0.6 sorry, 0.6323. So when the model is changed, the 2nd coefficient is changed by 0.5, we have oscillatory response.

We can change this to say 0.3323 where with we will have smaller variation than the previous case. We will have fairly stable response. You can see that this green curve goes to stab stable and it reaches the setpoint but if we zoom in, we can sorry. If we zoom in, so you can see that the green curve has a deviation from 2.5. Let us close this and we will turn on the bias correction and see how this works. So we have like 0.1 sorry

0.015 as the bias. It is still not reaching the setpoint. So we will have to tune depending on how different the model is. The other way of eliminating this plant model mismatch is to add filters like Kalman filters which which estimates the states online using measurements. We can tune the filter to have optimal performance and then we can couple the filter implementation along with MPC so that we will have better control.

Next next let us at when the reference trajectory is changing how the system will respond? So we will change the reference trajectory as a first order response, first order response with sometime constant as 5 5 seconds and we will use the same model as the system and see how the system responds. So the idea is we we are not restricted to using a single set point. We can give a reference trajectory which is a function of time and we can still

control the system fairly, easily. So you can see that the control reference trajectory is a first order response and the system was able to reach the trajectory. We can try changing m which I have put it as 1. Let us put it as 3 so that we will have more room for optimisation. So in that case, the variation will become smoother and easier compared to the case where it is 1. So the orange curve is the new cover which uses one

setpoint and the corresponding curve is here. So you can see that it is kind of smooth. We can increase the puff prediction horizon together with control horizon to get a better performance. So in that case, we will sorry let us see here. So in that case we will get easier better approximations and hence we will get better control. Now we have looked at MPC implementation in MATLAB. We can do other customisations like we can add weights to different elements present

in this as well as this. So that will advise the optimiser to put more weightage towards that prediction or less weightage towards that prediction. So we already have a tuning parameter lambda multiplying this. So based on which we we advise the optimiser to weight this the 2nd term such that the first term is either more weight, given more weightage given less weightage. We can also add other constraints like y of k plus i lying between y of upper limit to

y of lower limit, sorry y of lower limit to upper limit. Now this constraint maybe these constraints may not be achievable because we assume the system to be linear and finite impulse response system. So there are plant model mismatches. Because of that, we may not exactly match this constraint but we can give some room for the constraint to be relaxed so that we can still get a feasible solution. So adding these constraint which is on output limits, we will may lead to visibility problems.

And we can add other constraints like Delta u being within some Delta u limits and other constraints called coincidence point which will help us reach the set point faster by means of specifying y hat of k plus i to be equal to some y reference of k plus i. But remember, this is also an output constraint. Because of that we may have problem of feasibility. The terminal constraint which is y hat of k plus p equal to y ref of k plus p is easy

to implement and it also stabilises the controller. So we may, we will not have oscillatory response with this cont constraint present. So MPC formulation is not just restricted to SISO systems, we can always use or extend this to MIMO system. In that case the h1 matrix h1 will become a matrix. So we will have h11 h12 all the way up to h1m and hn1 all the way up to hnn. So similarly we will have for each coefficient like this. So it can be easily extended for MIMO systems. We can as well

use nonlinear model to predict y y hat of k plus i. But the problem is if we are to use on nonlinear model, the optimisation becomes nonlinear optimisation. So we may end up with local optimum rather than global optimum we which is what we get in sa by solving this optimisation function. So like the MPC eformulation by itself does not specify which type of model to use. Other than that, if we were to if we have to solve optimisation problem in accorded a programming

framework or using linear programming principles, the model has to be linear. Other than that, there is no specification of how, which type of model to use for prediction. So we can use any type of model for prediction. We can add constraint on both us, ys, Delta us and other Uhh other such constraints which may occur and we can as well customised the objective function based on Uhh Uhh minimizing u square or minimising Delta u square, minimising time, et cetera.

So the MPC framework by itself is flexible enough but it has to be customised and each constraint we add may Uhh each equality constraint we we add and its output constraint we add may make the optimisation infeasible. So the stability and feasibility of this MPC framework has to be theoretically proved. So like this, we can implement MPC in MATLAB. There is also a toolbox called MPC Toolbox in MATLAB which is very easy to learn. At the same time, it

will be Uhh it will have more diagnostic facilities and other options. So I have shown here the traditional way of coding the MPC by hand and implementing it for any system. So with this, I finish MPC tutorial.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent