MPC模型预测控制器——控制器建模+MATLAB编程
MPC模型预测控制器——控制器建模+MATLAB编程
参考如下链接:
建模例子详解
对于以下控制模型:
x ( k + 1 ) = A x ( k ) + B u ( k ) x(k+1)=A x(k)+B u(k) x(k+1)=Ax(k)+Bu(k)
-
注:需要注意对应的输入矩阵和状态矩阵每个矩阵的维度
x矩阵:n*1
A矩阵:n*n
B矩阵:n*p
u矩阵:p*1
写成矩阵格式:
[ x 1 ( k + 1 ) x 2 ( k + 1 ) ] = [ 1 0.1 0 2 ] [ x 1 ( k ) x 2 ( k ) ] + [ 0 0.5 ] u ( k ) \left[\begin{array}{l}x_{1}(k+1) \\x_{2}(k+1)\end{array}\right]=\left[\begin{array}{cc}1 & 0.1 \\0 & 2\end{array}\right]\left[\begin{array}{l}x_{1}(k) \\x_{2}(k)\end{array}\right]+\left[\begin{array}{c}0 \\0.5\end{array}\right] u(k) [x1(k+1)x2(k+1)]=[100.12][x1(k)x2(k)]+[00.5]u(k)
此处的 x 1 x_{1} x1和 x 2 x_{2} x2可以理解为误差项
X ( k ) = [ x ( k ∣ k ) x ( k + 1 ∣ k ) ⋮ x ( k + j ∣ k ) ⋮ x ( k + N ∣ k ) ] X(k)=\left[\begin{array}{c}x(k \mid k) \\x(k+1 \mid k) \\\vdots\\ x(k+j \mid k)\\\vdots \\x(k+N \mid k)\end{array}\right] X(k)=⎣⎢⎢⎢⎢⎢⎢⎢⎢⎡x(k∣k)x(k+1∣k)⋮x(k+j∣k)⋮x(k+N∣k)⎦⎥⎥⎥⎥⎥⎥⎥⎥⎤ U ( k ) = [ u ( k ∣ k ) u ( k + 1 ∣ k ) ⋮ u ( k + i ∣ k ) ⋮ u ( k + N − 1 ∣ k ) ] U(k)=\left[\begin{array}{c}u(k \mid k) \\u(k+1 \mid k) \\\vdots\\ u(k+i \mid k)\\\vdots \\u(k+N-1 \mid k)\end{array}\right] U(k)=⎣⎢⎢⎢⎢⎢⎢⎢⎢⎡u(k∣k)u(k+1∣k)⋮u(k+i∣k)⋮u(k+N−1∣k)⎦⎥⎥⎥⎥⎥⎥⎥⎥⎤
-
矩阵的维度
U(k) → NP1
X(k) → (N+1)n*1
对于MPC矩阵:
x k = M x k + C u k x_{k}=M x_{k}+C u_{k} xk=Mxk+Cuk
M = [ I n × n A n × n A n 2 ⋮ A N ] M=\left[\begin{array}{c}I_{n \times n} \\A_{n \times n} \\A_{n}^{2} \\\vdots \\A^{N}\end{array}\right] M=⎣⎢⎢⎢⎢⎢⎡In×nAn×nAn2⋮AN⎦⎥⎥⎥⎥⎥⎤ C = [ 0 0 … 0 ⋮ … ⋮ ⋮ 0 0 0 0 … 0 B n × p 0 … 0 A B n × p B ⋮ 0 ⋮ ⋮ B ] C=\left[\begin{array}{cccc} 0 & 0 & \ldots & 0 \\ & \vdots & \ldots & \vdots \\ \vdots & 0 & & 0 \\ 0 & & 0 & \ldots & 0 \\ B_{n \times p} & 0 & \ldots & 0 \\ A B_{n \times p} & B & \vdots & 0 \\ \vdots & \vdots & & B \end{array}\right] C=⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡0⋮0Bn×pABn×p⋮0⋮00B⋮……0…⋮0⋮0…00B0⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤
-
矩阵的维度
M → (N+1)nn
X(k) → (N+1)nN*P
X ( k ) = [ x ( k ∣ k ) x ( k + 1 ∣ k ) x ( k + 2 ∣ k ) x ( k + 3 ∣ k ) ] = [ x 1 ( k ∣ k ) x 2 ( k ∣ k ) x 1 ( k + 1 ∣ k ) x 2 ( k + 1 ∣ k ) x 1 ( k + 2 ∣ k ) x 2 ( k + 2 ∣ k ) x 1 ( k + 3 ∣ k ) x 2 ( k + 3 ∣ k ) ] X(k)=\left[\begin{array}{c}x(k \mid k) \\x(k+1 \mid k) \\x(k+2 \mid k) \\x(k+3 \mid k)\end{array}\right]=\left[\begin{array}{c}x_{1}(k \mid k) \\x_{2}(k \mid k) \\x_{1}(k+1 \mid k) \\x_{2}(k+1 \mid k) \\x_{1}(k+2 \mid k) \\x_{2}(k+2 \mid k) \\x_{1}(k+3 \mid k) \\x_{2}(k+3 \mid k)\end{array}\right] X(k)=⎣⎢⎢⎡x(k∣k)x(k+1∣k)x(k+2∣k)x(k+3∣k)⎦⎥⎥⎤=⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡x1(k∣k)x2(k∣k)x1(k+1∣k)x2(k+1∣k)x1(k+2∣k)x2(k+2∣k)x1(k+3∣k)x2(k+3∣k)⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤
U ( k ) = [ u ( k ∣ k ) u ( k + 1 ∣ k ) u ( k + 2 ∣ k ) ] = [ u ( k ∣ k ) u ( k + 1 ∣ k ) u ( k + 2 ∣ k ) ] U(k)=\left[\begin{array}{c}u(k \mid k) \\u(k+1 \mid k) \\u(k+2 \mid k)\end{array}\right]=\left[\begin{array}{c}\frac{u(k \mid k)}{u(k+1 \mid k)} \\u(k+2 \mid k)\end{array}\right] U(k)=⎣⎡u(k∣k)u(k+1∣k)u(k+2∣k)⎦⎤=[u(k+1∣k)u(k∣k)u(k+2∣k)]
计算出M矩阵和C矩阵(此处省略计算过程)
之后构建二次规划模式(cost function)
J = ∑ k = 0 N − 1 x ( k + i ∣ k ) T Q x ( k + i ∣ k ) + u ( k + i ∣ k ) T R u ( k + i ∣ k ) + x ( k + N ∣ k ) T F x ( k + i ∣ k ) J=\sum_{k=0}^{N-1} x(k+i \mid k)^{T} Q x(k+i \mid k)+u(k+i \mid k)^{T} R u(k+i \mid k)+x(k+N \mid k)^{T} F x(k+i \mid k) J=k=0∑N−1x(k+i∣k)TQx(k+i∣k)+u(k+i∣k)TRu(k+i∣k)+x(k+N∣k)TFx(k+i∣k)
Q、R、F表示对应输入输出的权重系数矩阵
-
矩阵的维度
Q → n*n
R → 1*1
F → n*n
Q矩阵影响x变量的相关性,R矩阵跟输入相关,F矩阵跟终值相关
x ( k + N ∣ k ) T F x ( k + i ∣ k ) x(k+N \mid k)^{T} F x(k+i \mid k) x(k+N∣k)TFx(k+i∣k)与系统对应的终值状态相关
简化上面的 cost function 矩阵
J = x ( k ) T G x ( k ) + U ( k ) T H U ( k ) + 2 x ( k ) T E U ( k ) J=\boldsymbol{x}(k)^{T} \boldsymbol{G} \boldsymbol{x}(k)+\boldsymbol{U}(k)^{T} \boldsymbol{H} \boldsymbol{U}(k)+2 \boldsymbol{x}(k)^{T} \boldsymbol{E} \boldsymbol{U}(k) J=x(k)TGx(k)+U(k)THU(k)+2x(k)TEU(k)
该矩阵只包含了输入项和已知的初始输入项x(k)
G = M T Q ˉ M E = C T Q ˉ M H = C T Q ˉ C + R ˉ \begin{aligned}&G=M^{T} \bar{Q} M \\&E=C^{T} \bar{Q} M \\&H=C^{T} \bar{Q} C+\bar{R}\end{aligned} G=MTQˉME=CTQˉMH=CTQˉC+Rˉ
Q ˉ \bar{Q} Qˉ和 R ˉ \bar{R} Rˉ表示上面式子中的Q和R矩阵的增广形式
G、H、E矩阵跟上面的M和C矩阵相关
Q ‾ = [ Q ⋯ ⋮ Q ⋮ ⋯ F ] R ‾ = [ R ⋯ ⋮ ⋱ ⋮ ⋯ R ] \overline{\boldsymbol{Q}}=\left[\begin{array}{ccc}\boldsymbol{Q} & \cdots & \\\vdots & \boldsymbol{Q} & \vdots \\& \cdots & \boldsymbol{F}\end{array}\right] \quad \overline{\boldsymbol{R}}=\left[\begin{array}{ccc}\boldsymbol{R} & \cdots & \\\vdots & \ddots & \vdots \\& \cdots & R\end{array}\right] Q=⎣⎢⎡Q⋮⋯Q⋯⋮F⎦⎥⎤R=⎣⎢⎡R⋮⋯⋱⋯⋮R⎦⎥⎤
MATLAB代码详解
函数体
main.m
% 矩阵输入部分
A = input('Input Matrix (row n; col n) A=');
B = input('Input Matrix (row n; col p) B=');
N = input('Input Prediction Section N=');
x_k = input('Input Initial State Matrix (row n; col 1) x_k=');
Q = input('Input Matrix (row n; col n) Q=');
R = input('Input Matrix (row 1; col 1) R=');
F = input('Input Matrix (row n; col n) F=');
MPC_Zero_Ref(A,B,N,x_k,Q,R,F);
MPC_Zero_Ref.m
function [M,C,Q_bar,R_bar,G,E,H,U_k] = MPC_Zero_Ref(A,B,N,x_k,Q,R,F)
n = size(A,1); % A是n*n矩阵,得到n
p = size(B,2); % B是n*p矩阵,得到p
% 初始化M矩阵,M矩阵是(N+1)*n*n的
% M矩阵上面是一个n*n的I矩阵,这一步先把矩阵下半部分初始化为0
M = [eye(n); zeros(N*n, n)];
% 初始化C矩阵,这一步令它有(N+1)*N*P个0
C = zeros((N+1)*n, N*p);
tmp = eye(n); % 定义一个n*n的I矩阵
for i = 1:N % 循环N次
rows = i*n+(1:n); % 定义当前行数,从i*n开始,共n行
C(rows, :) = [tmp*B, C(rows-n, 1:end-p)]; % 将C矩阵填满
tmp = A*tmp; % 每一次将tmp左乘一次
M(rows, :) = tmp; % 将M矩阵填满
end
% 定义Q_bar
S_q = size(Q, 1); % 输出Q的维度
S_r = size(R, 1); % 输出R的维度
Q_bar = zeros((N+1)*S_q, (N+1)*S_r); % 初始化Q_bar为全0矩阵
for i = 0:N % 循环N+1次
% 将Q写到Q_bar对角线上
Q_bar(i*S_q+1:(i+1)*S_q, i*S_q+1:(i+1)*S_q) = Q;
end
Q_bar(N*S_q+1:(N+1)*S_q, N*S_q+1:(N+1)*S_q) = F;
% 定义R_bar
R_bar = zeros(N*S_r, N*S_r);
for i = 0:N-1
R_bar(i*S_r+1:(i+1)*S_r, i*S_r+1:(i+1)*S_r) = R;
end
% 求解G、E、H
G = M'*Q_bar*M;
E = C'*Q_bar*M;
H = C'*Q_bar*C+R;
% 最优化
f = (x_k'*E')'; % 定义f矩阵
U_k = quadprog(H, f); % 求解最优化U_k
end
输入举例
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)