0
Follow
0
View

# mpc model code in matlab

daybreak30 注册会员
2023-02-26 19:25

It C fills a matrix C(rows-n, 1:end-p) by concatenating two other matrices tmp*B and.

This code uses a loop to iterate over the index N of i from 1 to where N is some positive integer. For each i, it defines a row index vector rows that starts at i n+1 and includes consecutive integers after n(that is, i n+1 to i*n+n).

the bank use two series filling matrix by the index of the matrix C(rows, :) = [TMP < em > B, C(rows - n, 1: end - p)]; 's line. The first matrix is the product of sums. The second matrix is a submatrix that starts the row earlier than the current row(). This syntax means to include all the columns of the submatrix except the last column. Together, the two matrices are joined horizontally and assigned to rows indexed by in. rowsCtmpBtmp BCnC(rows-n, 1:end-p)1:end-pprowsC

duyanfeng2012 注册会员
2023-02-26 19:25

For reference to GPT and your own ideas, this section of code is part of the MPC_Matrices function for constructing the constraint matrix C that controls the problem. These two sentences are explained below:

rows = in+(1:n); % to define the current line number, starting in, a total of n lines < br / > C(rows, :) = [TMP * B, C(rows - n, 1: end - p)]; % Fill the C matrix

First, since the prediction interval is N, the dimensions of the state and control input vectors need to be extended. Each row of the C matrix corresponds to a state vector and its corresponding control input vector. Therefore, the dimension of the C matrix should be(N+1)n x Np.

For each time step i, we need to add the corresponding state vector and control input vector to the C matrix. In this process, we use A temporary variable tmp to store the power of the state matrix A. Specifically, we multiply tmp by A in iteration i and take the result as tmp for the next iteration. At the same time, we multiply tmp by B and add the result to the C matrix, while adding the corresponding row from the previous iteration's C matrix to the current row. This process can be expressed using the following formula:

C(rows,:)=[tmp*B,C(rows-n, 1:end-p)];

Here, the rows variable represents the index of the current row, and since each state vector and control input vector contains n and p elements, their total dimension is n+p. Therefore, we use C(rows,:) to select the current row and then concatenate the two vectors horizontally. tmpB represents a matrix of np obtained by multiplying tmp with the B matrix. And C(rows-n,1:end-p) represents the corresponding rows in the last iteration of the C matrix, from column 1 to column(end-p). This procedure ensures that each row in the C matrix contains all state and control inputs prior to the current time step.

djf0711 注册会员
2023-02-26 19:25

``````for i=1:N % 循环,i从1到N
rows =i*n+(1:n); %定义当前行数,从i*n开始，共n行
C(rows,:)=[tmp*B,C(rows-n, 1:end-p)]; %将c矩阵填满
tmp= A*tmp; %每一次将tmp左乘一次A
M(rows,:)=tmp; %将M矩阵写满

``````

This piece of code is the for loop in the MPC_Matrices function for updating the control matrix C and the state matrix M. Specifically, the code in the loop means something like this:

1. For the ith loop, calculate the rows from the th row to the rows+n-1 row in the C matrix, i.e. rows = i*n+(1:n). Where, i represents the current number of cycles, and n is the number of rows in the state matrix A, that is, the number of state variables. The function of this line of code is to combine the input matrix from time i to time i+N in the control matrix C with the previous state matrix to get a control matrix of Np lines, where p is the number of columns of the input matrix B, that is, the number of control inputs.

2. Then, multiply the current tmp matrix(the initial value is the identity matrix) by the state matrix A to obtain a new tmp matrix. The function of this line of code is to update the influence of the state variable at the current moment on the future moment under the action of the state matrix A.

3. Finally, the updated tmp matrix is written to rows row in M matrix to rows+n-1, that is, M(rows,:) = tmp. The function of this line of code is to write the updated state matrix into the M matrix to obtain a state matrix for line N+1, where the first act is the state matrix of the current moment, and the second to the N+1 act is the state matrix of the future moment.

Mr_Sun73 注册会员
2023-02-26 19:25

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > this is a MPC controller based on state space model of MATLAB code. Wherein, E and H matrices are calculated by defining state space matrix A, input matrix B, weight matrix Q, object matrix F and constraint matrix R, and using MPC_Matrices function. Then the state Prediction is made by prediction function, and the predicted value is taken as the input of optimization problem, and the output value of controller is calculated. Finally, the state variables and input changes of each time step are calculated through the loop iteration, and the results are drawn as graphs.

The MPC controller can be applied to a variety of system control. By adjusting input matrix B, weight matrix Q and constraint matrix R, the control requirements of different systems can be realized.

wh5212 注册会员

Publish Time
2023-02-26 19:24
Update Time
2023-02-26 19:24