【DR_CAN-MPC学习笔记】3&4.详细的MPC建模例子和matlab代码

上一篇博客:【DR_CAN-MPC学习笔记】2.最优化数学建模推导

参照二次规划一般形式,详细推导了MPC的数学模型,即最小化代价函数的表达式,最终推导结果为:

gif.latex?J%3Dx_k%5ETGx_k+2x_k%5ETEU_k+U_k%5ETHU_k

DR_CAN的视频:

【MPC模型预测控制器】3_一个详细的建模例子

【MPC模型预测控制器】3

 【MPC模型预测控制器】4_完整案例讲解 - Octave代码

【MPC模型预测控制器】4

 


离散系统状态空间一般形式:

gif.latex?%5Cboldsymbol%20x%28k+1%29%3D%5Cboldsymbol%20A%20%5Cboldsymbol%20x%28k%29+%5Cboldsymbol%20B%20%5Cboldsymbol%20u%28k%29

其中 gif.latex?x%28k%29 为状态向量(n×1),gif.latex?u%28k%29 为输入向量(p×1),gif.latex?A 为系统状态矩阵(n×n),gif.latex?B 为系统输入矩阵(n×p)。

单输入二阶系统的例子:

gif.latex?%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%20x_%7B1%7D%28k+1%29%20%5C%5C%20x_%7B2%7D%28k+1%29%20%5Cend%7Barray%7D%5Cright%5D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcc%7D%201%20%26%200.1%20%5C%5C%200%20%26%202%20%5Cend%7Barray%7D%5Cright%5D%5Cleft%5B%5Cbegin%7Barray%7D%7Bl%7D%20x_%7B1%7D%28k%29%20%5C%5C%20x_%7B2%7D%28k%29%20%5Cend%7Barray%7D%5Cright%5D+%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%200%20%5C%5C%200.5%20%5Cend%7Barray%7D%5Cright%5D%20u%28k%29

上式中,gif.latex?%5Cboldsymbol%20A%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcc%7D%201%20%26%200.1%20%5C%5C%200%20%26%202%20%5Cend%7Barray%7D%5Cright%5Dgif.latex?%5Cboldsymbol%20B%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bl%7D%200%20%5C%5C%200.5%20%5Cend%7Barray%7D%5Cright%5D,n = 2,p = 1

系统状态向量和输入向量的关系:

gif.latex?%5Cboldsymbol%20X_k%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%20%5Cboldsymbol%20x%28k%20%5Cmid%20k%29%20%5C%5C%20%5Cboldsymbol%20x%28k+1%20%5Cmid%20k%29%20%5C%5C%20%5Cvdots%20%5C%5C%20%5Cboldsymbol%20x%28k+i%20%5Cmid%20k%29%20%5C%5C%20%5Cvdots%20%5C%5C%20%5Cboldsymbol%20x%28k+N%20%5Cmid%20k%29%20%5Cend%7Barray%7D%5Cright%5D%2C%5Cboldsymbol%20U_k%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%20%5Cboldsymbol%20u%28k%20%5Cmid%20k%29%20%5C%5C%20%5Cboldsymbol%20u%28k+1%20%5Cmid%20k%29%20%5C%5C%20%5Cvdots%20%5C%5C%20%5Cboldsymbol%20u%28k+i%20%5Cmid%20k%29%20%5C%5C%20%5Cvdots%20%5C%5C%20%5Cboldsymbol%20u%28k+N-1%20%5Cmid%20k%29%20%5Cend%7Barray%7D%5Cright%5D

  • gif.latex?x%28k+1%20%5Cmid%20k%29 表示在k时刻预测 k+1 时刻的系统状态。
  • 由于 gif.latex?x%28k%20+N%5Cmid%20k%29gif.latex?u%28k+N-1%20%5Cmid%20k%29 决定,因此不需要 gif.latex?u%28k+N%5Cmid%20k%29 ,所以 gif.latex?U_kgif.latex?X_k 少一个维度。
  • 因为初始值 gif.latex?x%28k%20%5Cmid%20k%29gif.latex?x%28k+i%20%5Cmid%20k%29 均为 n×1 向量,因此 gif.latex?X_k 为 (N+1)n×1 向量。同理可推出 gif.latex?U_k 为 Np×1 向量。

gif.latex?%5Cboldsymbol%7BM%7D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%20%5Cboldsymbol%7BI%7D_%7Bn%20%5Ctimes%20n%7D%20%5C%5C%20%5Cboldsymbol%7BA%7D_%7Bn%20%5Ctimes%20n%7D%20%5C%5C%20%5Cboldsymbol%7BA%7D_%7Bn%20%5Ctimes%20n%7D%5E%7B2%7D%20%5C%5C%20%5Cvdots%20%5C%5C%20%5Cboldsymbol%7BA%7D%5E%7BN%7D%20%5Cend%7Barray%7D%5Cright%5D%2C%20%5Cboldsymbol%7BC%7D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcccc%7D%200%20%26%200%20%26%20%5Cldots%20%26%200%20%5C%5C%20%5Cvdots%20%26%20%5Cvdots%20%26%20%5Cldots%20%26%20%5Cvdots%20%5C%5C%200%20%26%200%20%26%20%26%200%20%5C%5C%20%5Cboldsymbol%7BB%7D%20%26%200%20%26%20%5Cldots%20%26%200%20%5C%5C%20%5Cboldsymbol%7BA%20B%7D%20%26%20%5Cboldsymbol%7BB%7D%20%26%20%5Cldots%20%26%200%20%5C%5C%20%5Cvdots%20%26%20%5Cvdots%20%26%20%5Cddots%20%26%200%20%5C%5C%20%5Cboldsymbol%7BA%7D%5E%7BN-1%7D%20%5Cboldsymbol%7BB%7D%20%26%20%5Cboldsymbol%7BA%7D%5E%7BN-2%7D%20%5Cboldsymbol%7BB%7D%20%26%20%5Cldots%20%26%20%5Cboldsymbol%7BB%7D%20%5Cend%7Barray%7D%5Cright%5D

  • gif.latex?M 为 (N+1)n×n 矩阵。
  • gif.latex?C 矩阵上面所有的 0 与初始状态 gif.latex?x%28k%20%5Cmid%20k%29 有关(n×1矩阵),gif.latex?B%2CAB...A%5E%7BN-1%7DB 均为 n×p 矩阵,因此 gif.latex?C 为 (N+1)n×Np 矩阵。

具体可参考上一篇博客的推导(【DR_CAN-MPC学习笔记】2.最优化数学建模推导):

【DR_CAN-MPC学习笔记】3&4.详细的MPC建模例子和matlab代码_第1张图片

分析过程:

回到单输入二阶系统的例子,gif.latex?%5Cboldsymbol%20A%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcc%7D%201%20%26%200.1%20%5C%5C%200%20%26%202%20%5Cend%7Barray%7D%5Cright%5Dgif.latex?%5Cboldsymbol%20B%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bl%7D%200%20%5C%5C%200.5%20%5Cend%7Barray%7D%5Cright%5D,n = 2,p = 1,假设预测区间 N=3 ,

整理一下维度:

gif.latex?X_k = gif.latex?M gif.latex?x_k + gif.latex?C gif.latex?U_k
(N+1)n×1 = (N+1)n×n n×1 + (N+1)n×Np Np×1
8×1 = 8×2 2×1 + 8×3 3×1

gif.latex?%5Cboldsymbol%7BX%7D%28k%29%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%20%5Cboldsymbol%20x%28k%20%5Cmid%20k%29%20%5C%5C%20%5Cboldsymbol%20x%28k+1%20%5Cmid%20k%29%20%5C%5C%20%5Cboldsymbol%20x%28k+2%20%5Cmid%20k%29%20%5C%5C%20%5Cboldsymbol%20x%28k+3%20%5Cmid%20k%29%20%5Cend%7Barray%7D%5Cright%5D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%20x_%7B1%7D%28k%20%5Cmid%20k%29%20%5C%5C%20x_%7B2%7D%28k%20%5Cmid%20k%29%20%5C%5C%20x_%7B1%7D%28k+1%20%5Cmid%20k%29%20%5C%5C%20x_%7B2%7D%28k+1%20%5Cmid%20k%29%20%5C%5C%20x_%7B1%7D%28k+2%20%5Cmid%20k%29%20%5C%5C%20x_%7B2%7D%28k+2%20%5Cmid%20k%29%20%5C%5C%20x_%7B1%7D%28k+3%20%5Cmid%20k%29%20%5C%5C%20x_%7B2%7D%28k+3%20%5Cmid%20k%29%20%5Cend%7Barray%7D%5Cright%5D

gif.latex?%5Cboldsymbol%7BU%7D%28k%29%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%20%5Cboldsymbol%20u%28k%20%5Cmid%20k%29%20%5C%5C%20%5Cboldsymbol%20u%28k+1%20%5Cmid%20k%29%20%5C%5C%20%5Cboldsymbol%20u%28k+2%20%5Cmid%20k%29%20%5C%5C%20%5Cend%7Barray%7D%5Cright%5D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%20u%28k%20%5Cmid%20k%29%20%5C%5C%20u%28k+1%20%5Cmid%20k%29%20%5C%5C%20u%28k+2%20%5Cmid%20k%29%20%5Cend%7Barray%7D%5Cright%5D

gif.latex?%5Cboldsymbol%20M%3D%5Cleft%5B%20%5Cbegin%7Barray%7D%7Bcccc%7D%20%5Cboldsymbol%20I_%7B2%5Ctimes%202%7D%20%5C%5C%20%5Cboldsymbol%20A_%7B2%5Ctimes%202%7D%20%5C%5C%20%5Cboldsymbol%20A_%7B2%5Ctimes%202%7D%5E2%20%5C%5C%20%5Cboldsymbol%20A_%7B2%5Ctimes%202%7D%5E3%20%5C%5C%20%5Cend%7Barray%7D%20%5Cright%5D%3D%5Cleft%5B%20%5Cbegin%7Barray%7D%7Bcccccccc%7D%201%261%20%5C%5C%201%261%20%5C%5C%201%260.1%20%5C%5C0%262%20%5C%5C%201%260.3%20%5C%5C0%264%20%5C%5C%201%260.7%20%5C%5C0%268%20%5C%5C%5Cend%7Barray%7D%20%5Cright%5D

gif.latex?%5Cboldsymbol%7BC%7D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcccc%7D%200%20%26%200%20%26%20%5Cldots%20%26%200%20%5C%5C%20%5Cvdots%20%26%20%5Cvdots%20%26%20%5Cldots%20%26%20%5Cvdots%20%5C%5C%200%20%26%200%20%26%20%26%200%20%5C%5C%20%5Cboldsymbol%7BB%7D%20%26%200%20%26%20%5Cldots%20%26%200%20%5C%5C%20%5Cboldsymbol%7BAB%7D%20%26%20%5Cboldsymbol%7BB%7D%20%26%20%5Cldots%20%26%200%20%5C%5C%20%5Cvdots%20%26%20%5Cvdots%20%26%20%5Cddots%20%26%200%20%5C%5C%20%5Cboldsymbol%7BA%7D%5E%7BN-1%7D%20%5Cboldsymbol%7BB%7D%20%26%20%5Cboldsymbol%7BA%7D%5E%7BN-2%7D%20%5Cboldsymbol%7BB%7D%20%26%20%5Cldots%20%26%20%5Cboldsymbol%7BB%7D%20%5Cend%7Barray%7D%5Cright%5D%20%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcccccccc%7D%200%20%26%200%20%26%200%20%5C%5C%200%20%26%200%20%26%200%20%5C%5C%20%5Cboldsymbol%20B%20%26%200%20%26%200%20%5C%5C%20%5Cboldsymbol%7BAB%7D%20%26%20%5Cboldsymbol%20B%20%26%200%20%5C%5C%20%5Cboldsymbol%7BA%5E2B%7D%20%26%20%5Cboldsymbol%7BAB%7D%20%26%20%5Cboldsymbol%20B%20%5Cend%7Barray%7D%5Cright%5D%20%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcccccccc%7D%200%20%26%200%20%26%200%20%5C%5C%200%20%26%200%20%26%200%20%5C%5C%200%20%26%200%20%26%200%20%5C%5C%200.5%20%26%200%20%26%200%20%5C%5C%200.05%20%26%200%20%26%200%20%5C%5C%201%20%26%200.5%20%26%200%20%5C%5C%200.15%20%26%200.05%20%26%200%20%5C%5C%202%20%26%201%20%26%200.5%20%5Cend%7Barray%7D%5Cright%5D

对于系统输出方程: gif.latex?y%3Dx ,参考值 gif.latex?R%27%3D0 ,误差 gif.latex?E%27%3Dy-R%27%3Dx-0%3Dx ,代价函数为:

gif.latex?%5Cboldsymbol%20J%3D%5Csum_%7Bi%3D0%7D%5E%7BN-1%7D%5Cleft%28%5Cboldsymbol%20x%28k+i%20%5Cmid%20k%29%5E%7BT%7D%20%5Cboldsymbol%20Q%20%5Cboldsymbol%20x%28k+i%20%5Cmid%20k%29+%5Cboldsymbol%20u%28k+i%20%5Cmid%20k%29%5E%7BT%7D%20%5Cboldsymbol%20R%20%5Cboldsymbol%20u%28k+i%20%5Cmid%20k%29+%5Cboldsymbol%20x%28k+N%20%5Cmid%20k%29%5E%7BT%7D%20%5Cboldsymbol%20F%20%5Cboldsymbol%20x%28k+N%20%5Cmid%20k%29%5Cright%29

代价函数 = 误差加权和 + gif.latex?u%28k+i%20%5Cmid%20k%29%5E%7BT%7D%20R%20u%28k+i%20%5Cmid%20k%29 输入加权和 + gif.latex?x%28k+N%20%5Cmid%20k%29%5E%7BT%7D%20F%20x%28k+N%20%5Cmid%20k%29 终端误差,其中 gif.latex?Qgif.latex?R 为权重系数矩阵且均为对角矩阵。

经过简化后消去变量 gif.latex?X_k (简化过程参考【DR_CAN-MPC学习笔记】2.最优化数学建模推导):

gif.latex?%5Cboldsymbol%20J%3D%5Cboldsymbol%20x_k%5ET%20%5Cboldsymbol%20G%20%5Cboldsymbol%20x_k+2%20%5Cboldsymbol%20x_k%5ET%20%5Cboldsymbol%20E%20%5Cboldsymbol%20U_k+%20%5Cboldsymbol%20U_k%5ET%20%5Cboldsymbol%20H%20%5Cboldsymbol%20U_k

由上式可见,gif.latex?J 只包含了初始状态项 gif.latex?x_k 和输入项 gif.latex?U_k ,对 gif.latex?J 进行最优化可以得到输入项 gif.latex?U_k

矩阵 gif.latex?G%2CE%2CH 与 gif.latex?M%2CC 有关:

gif.latex?%5Cbegin%7Baligned%7D%20%5Cboldsymbol%7BG%7D%20%26%3D%5Cboldsymbol%7BM%7D%5E%7BT%7D%20%5Cbar%7B%20%5Cboldsymbol%20Q%7D%20%5Cboldsymbol%20M%20%5C%5C%20%5Cboldsymbol%20E%20%26%3D%5Cboldsymbol%20M%5E%7BT%7D%20%5Cbar%7B%20%5Cboldsymbol%20Q%7D%20%5Cboldsymbol%20C%5C%5C%20%5Cboldsymbol%20H%20%26%3D%20%5Cboldsymbol%20C%5E%7BT%7D%20%5Cbar%7B%20%5Cboldsymbol%20Q%7D%20%5Cboldsymbol%20C+%5Cbar%7B%20%5Cboldsymbol%20R%7D%20%5Cend%7Baligned%7D

 其中 gif.latex?%5Cbar%20Qgif.latex?%5Cbar%20R 是原来两个权重矩阵 gif.latex?Qgif.latex?R 的增广形式:

gif.latex?%5Cbar%7BQ%7D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bccccc%7D%20Q%20%26%200%20%26%20%5Ccdots%20%26%200%20%5C%5C%200%20%26%20Q%20%26%20%5Ccdots%20%26%200%5C%5C%20%5Cvdots%20%26%20%26%20%5Cddots%20%26%20%5Cvdots%5C%5C%200%20%26%200%20%26%20%5Ccdots%20%26%20F%20%5Cend%7Barray%7D%5Cright%5D%2C%5Cbar%7BR%7D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bccccc%7D%20R%20%26%200%20%26%20%5Ccdots%20%26%200%20%5C%5C%200%20%26%20R%20%26%20%5Ccdots%20%26%200%5C%5C%20%5Cvdots%20%26%20%26%20%5Cddots%20%26%20%5Cvdots%5C%5C%200%20%26%200%20%26%20%5Ccdots%20%26%20R%20%5Cend%7Barray%7D%5Cright%5D

矩阵计算较为复杂,可用编程求解。

例子代码:

gif.latex?%5Cboldsymbol%20x%28k+1%29%3D%5Cboldsymbol%20A%20%5Cboldsymbol%20x%28k%29+%5Cboldsymbol%20B%20%5Cboldsymbol%20u%28k%29

控制目标:设计合适的 gif.latex?u%28k%29 使得 gif.latex?x%28k+1%29 随着 gif.latex?k 的增加,趋近于0。引入误差 gif.latex?e

gif.latex?e%3Dx_d-x_%7Bk+1%7D

gif.latex?x_d 为目标值,控制目标为令误差接近0。

状态矩阵(n×k):

gif.latex?%5Cboldsymbol%20X_K%3D%5B%5Cboldsymbol%20x%280%29%2C%5Cboldsymbol%20x%281%29%2C%5Cboldsymbol%20x%282%29...%5Cboldsymbol%20x%28k%29%5D

gif.latex?%5Cboldsymbol%20x%281%29%3Ax_1%281%29%2Cx_2%281%29...x_n%281%29

...

gif.latex?%5Cboldsymbol%20x%28k%29%3Ax_1%28k%29%2Cx_2%28k%29...x_n%28k%29

输入矩阵(p×k):

gif.latex?%5Cboldsymbol%20U_K%3D%5B%5Cboldsymbol%20u%280%29%2C%5Cboldsymbol%20u%281%29%2C%5Cboldsymbol%20u%282%29...%5Cboldsymbol%20u%28k%29%5D

gif.latex?%5Cboldsymbol%20u%281%29%3Au_1%281%29%2Cu_2%281%29...u_p%281%29

...

gif.latex?%5Cboldsymbol%20u%28k%29%3Au_1%28k%29%2Cu_2%28k%29...u_p%28k%29

状态方程:

gif.latex?%5Cboldsymbol%20X_K%28%3A%2Ck+1%29%3D%28%5Cboldsymbol%20A*%5Cboldsymbol%20X_K%28%3A%2Ck%29+%5Cboldsymbol%20B%20*%20%5Cboldsymbol%20U_K%28%3A%2Ck%29%29%3B

例如:k=1 时,gif.latex?X_K%28%3A%2C2%29 表示第2列 gif.latex?x_1%282%29%2Cx_2%282%29...x_n%282%29

回到单输入二阶系统的例子:

gif.latex?%5Cbegin%7Barray%7D%7Bc%7D%20%7B%5Cleft%5B%5Cbegin%7Barray%7D%7Bl%7D%20x_%7B1%7D%28k+1%29%20%5C%5C%20x_%7B2%7D%28k+1%29%20%5Cend%7Barray%7D%5Cright%5D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcc%7D%201%20%26%200.1%20%5C%5C%200%20%26%202%20%5Cend%7Barray%7D%5Cright%5D%5Cleft%5B%5Cbegin%7Barray%7D%7Bl%7D%20x_%7B1%7D%28k%29%20%5C%5C%20x_%7B2%7D%28k%29%20%5Cend%7Barray%7D%5Cright%5D+%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%200%20%5C%5C%200.5%20%5Cend%7Barray%7D%5Cright%5D%20u%28k%29%7D%20%5Cend%7Barray%7D

gif.latex?%5Cboldsymbol%7BQ%7D%3D%5Cleft%28%5Cbegin%7Barray%7D%7Bll%7D%201%20%26%200%20%5C%5C%200%20%26%201%20%5Cend%7Barray%7D%5Cright%29%20%5Cquad%20%5Cboldsymbol%7BF%7D%3D%5Cleft%28%5Cbegin%7Barray%7D%7Bll%7D%201%20%26%200%20%5C%5C%200%20%26%201%20%5Cend%7Barray%7D%5Cright%29%20%5Cquad%20%5Cboldsymbol%7BR%7D%3D0.1

  gif.latex?Q 为系统状态变量权重矩阵, gif.latex?R 为系统输入变量权重矩阵,gif.latex?F 为终端权重矩阵。

DR_CAN给出了Octave代码,在Matlab中也可以运行。下面的代码是我在此基础上修改后的单输入例子的代码,后面也有介绍如何修改为多输入系统。

传送门:(二输入系统)【MPC模型预测控制器】4_Octave代码

代码一共由三个部分组成,分别为主程序:MPC_Test.m,以及两个函数:MPC_Matrices.m和Prediction.m

MPC_Test.m

设置初始参数:

 gif.latex?%5Cboldsymbol%20A%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcc%7D%201%20%26%200.1%20%5C%5C%200%20%26%202%20%5Cend%7Barray%7D%5Cright%5Dgif.latex?%5Cboldsymbol%20B%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bl%7D%200%20%5C%5C%200.5%20%5Cend%7Barray%7D%5Cright%5D...

%% 清屏
clear; 
close all; 
clc;

%% 加载 optim package,若使用matlab,则注释掉此行
pkg load optim;

%% 第一步,定义状态空间矩阵
%% 定义状态矩阵 A, n x n 矩阵
A = [1 0.1; 0 2];
n= size (A,1); % 计算矩阵第一个维度的长度

%% 定义输入矩阵 B, n x p 矩阵
B = [0; 0.5];
p = size(B,2); % 计算矩阵第二个维度的长度

%% 定义Q矩阵,n x n 矩阵
Q=[1 0; 0 1];

%% 定义F矩阵,n x n 矩阵
F=[1 0; 0 1];

%% 定义R矩阵,p x p 矩阵
R=[0.1];

%% 定义step数量k
k_steps=100; 

%% 定义矩阵 X_K, n x k 矩 阵
X_K=zeros(n,k_steps);

%% 初始状态变量值, n x 1 向量
X_K(:,1)=[20;-20]; % 初始状态不为0,控制目标为0

%% 定义输入矩阵 U_K, p x k 矩阵
U_K=zeros(p,k_steps);

%% 定义预测区间K
N=5;

%% Call MPC_Matrices 函数 求得 E,H矩阵 
[E,H]=MPC_Matrices(A,B,Q,R,F,N);

%% 计算每一步的状态变量的值
for k = 1 : k_steps 
%% 求得U_K(:,k)
U_K(:,k) = Prediction(X_K(:,k),E,H,N,p);
%% 计算第k+1步时状态变量的值
X_K(:,k+1)=(A*X_K(:,k)+B*U_K(:,k));
end

%% 绘制状态变量和输入的变化
subplot(2, 1, 1);
hold;
for i =1 :size (X_K,1)
plot(X_K(i,:));
end
legend("x1","x2")
hold off;
subplot(2, 1, 2);
hold;
for i =1 : size(U_K,1)
plot(U_K(i,:));
end
legend("u1") 

%% 作者:DR_CAN https://www.bilibili.com/read/cv16891782 出处:bilibili

分析:

注释掉以下行,即输入设为0,可单独运行:

N=5;
[E,H]=MPC_Matrices(A,B,Q,R,F,N);
U_K(:,k) = Prediction(X_K(:,k),E,H,N,p);

得到:

【DR_CAN-MPC学习笔记】3&4.详细的MPC建模例子和matlab代码_第2张图片

由上图可见,gif.latex?x_1%2Cx_2 均趋于无穷,因为初始状态不为0且输入为0.

接下来设置合适的输入使得状态值趋于0.

代价函数:

gif.latex?%5Cboldsymbol%20J%3D%5Cboldsymbol%20x_k%5ET%20%5Cboldsymbol%20G%20%5Cboldsymbol%20x_k+2%20%5Cboldsymbol%20x_k%5ET%20%5Cboldsymbol%20E%20%5Cboldsymbol%20U_k+%20%5Cboldsymbol%20U_k%5ET%20%5Cboldsymbol%20H%20%5Cboldsymbol%20U_k

gif.latex?%5Cboldsymbol%20x_k%5ET%20%5Cboldsymbol%20G%20%5Cboldsymbol%20x 与初始状态有关,不影响代价函数,因此控制目标为最小化 gif.latex?2%20%5Cboldsymbol%20x_k%5ET%20%5Cboldsymbol%20E%20%5Cboldsymbol%20U_k+%20%5Cboldsymbol%20U_k%5ET%20%5Cboldsymbol%20H%20%5Cboldsymbol%20U_k .

gif.latex?%5Cboldsymbol%20U_k%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%20%5Cboldsymbol%20u%28k%20%5Cmid%20k%29%20%5C%5C%20%5Cboldsymbol%20u%28k+1%20%5Cmid%20k%29%20%5C%5C%20%5Cvdots%20%5C%5C%20%5Cboldsymbol%20u%28k+i%20%5Cmid%20k%29%20%5C%5C%20%5Cvdots%20%5C%5C%20%5Cboldsymbol%20u%28k+N-1%20%5Cmid%20k%29%20%5Cend%7Barray%7D%5Cright%5D

预测了N项,但结果只取第一项 gif.latex?%5Cboldsymbol%20u%28k%20%5Cmid%20k%29 .

gif.latex?%5Cbegin%7Baligned%7D%20%5Cboldsymbol%20E%20%26%3D%5Cboldsymbol%20M%5E%7BT%7D%20%5Cbar%7B%20%5Cboldsymbol%20Q%7D%20%5Cboldsymbol%20C%2C%20%5Cboldsymbol%20H%20%26%3D%20%5Cboldsymbol%20C%5E%7BT%7D%20%5Cbar%7B%20%5Cboldsymbol%20Q%7D%20%5Cboldsymbol%20C+%5Cbar%7B%20%5Cboldsymbol%20R%7D%20%5Cend%7Baligned%7D

矩阵 gif.latex?C%2CM%2CQ%2CR 在之前已经分析过了:

gif.latex?%5Cboldsymbol%20M%3D%5Cleft%5B%20%5Cbegin%7Barray%7D%7Bcccc%7D%20%5Cboldsymbol%20I_%7B2%5Ctimes%202%7D%20%5C%5C%20%5Cboldsymbol%20A_%7B2%5Ctimes%202%7D%20%5C%5C%20%5Cboldsymbol%20A_%7B2%5Ctimes%202%7D%5E2%20%5C%5C%20%5Cboldsymbol%20A_%7B2%5Ctimes%202%7D%5E3%20%5C%5C%20%5Cend%7Barray%7D%20%5Cright%5D%3D%5Cleft%5B%20%5Cbegin%7Barray%7D%7Bcccccccc%7D%201%261%20%5C%5C%201%261%20%5C%5C%201%260.1%20%5C%5C0%262%20%5C%5C%201%260.3%20%5C%5C0%264%20%5C%5C%201%260.7%20%5C%5C0%268%20%5C%5C%5Cend%7Barray%7D%20%5Cright%5D

gif.latex?%5Cbar%7BQ%7D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bccccc%7D%20Q%20%26%200%20%26%20%5Ccdots%20%26%200%20%5C%5C%200%20%26%20Q%20%26%20%5Ccdots%20%26%200%5C%5C%20%5Cvdots%20%26%20%26%20%5Cddots%20%26%20%5Cvdots%5C%5C%200%20%26%200%20%26%20%5Ccdots%20%26%20F%20%5Cend%7Barray%7D%5Cright%5D%2C%5Cbar%7BR%7D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bccccc%7D%20R%20%26%200%20%26%20%5Ccdots%20%26%200%20%5C%5C%200%20%26%20R%20%26%20%5Ccdots%20%26%200%5C%5C%20%5Cvdots%20%26%20%26%20%5Cddots%20%26%20%5Cvdots%5C%5C%200%20%26%200%20%26%20%5Ccdots%20%26%20R%20%5Cend%7Barray%7D%5Cright%5D

gif.latex?%5Cboldsymbol%7BC%7D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcccc%7D%200%20%26%200%20%26%20%5Cldots%20%26%200%20%5C%5C%20%5Cvdots%20%26%20%5Cvdots%20%26%20%5Cldots%20%26%20%5Cvdots%20%5C%5C%200%20%26%200%20%26%20%26%200%20%5C%5C%20%5Cboldsymbol%7BB%7D%20%26%200%20%26%20%5Cldots%20%26%200%20%5C%5C%20%5Cboldsymbol%7BAB%7D%20%26%20%5Cboldsymbol%7BB%7D%20%26%20%5Cldots%20%26%200%20%5C%5C%20%5Cvdots%20%26%20%5Cvdots%20%26%20%5Cddots%20%26%200%20%5C%5C%20%5Cboldsymbol%7BA%7D%5E%7BN-1%7D%20%5Cboldsymbol%7BB%7D%20%26%20%5Cboldsymbol%7BA%7D%5E%7BN-2%7D%20%5Cboldsymbol%7BB%7D%20%26%20%5Cldots%20%26%20%5Cboldsymbol%7BB%7D%20%5Cend%7Barray%7D%5Cright%5D%20%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcccccccc%7D%200%20%26%200%20%26%200%20%5C%5C%200%20%26%200%20%26%200%20%5C%5C%20%5Cboldsymbol%20B%20%26%200%20%26%200%20%5C%5C%20%5Cboldsymbol%7BAB%7D%20%26%20%5Cboldsymbol%20B%20%26%200%20%5C%5C%20%5Cboldsymbol%7BA%5E2B%7D%20%26%20%5Cboldsymbol%7BAB%7D%20%26%20%5Cboldsymbol%20B%20%5Cend%7Barray%7D%5Cright%5D%20%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcccccccc%7D%200%20%26%200%20%26%200%20%5C%5C%200%20%26%200%20%26%200%20%5C%5C%200%20%26%200%20%26%200%20%5C%5C%200.5%20%26%200%20%26%200%20%5C%5C%200.05%20%26%200%20%26%200%20%5C%5C%201%20%26%200.5%20%26%200%20%5C%5C%200.15%20%26%200.05%20%26%200%20%5C%5C%202%20%26%201%20%26%200.5%20%5Cend%7Barray%7D%5Cright%5D

矩阵的计算用 MPC_Matrices 函数解决,即代码:

[E,H]=MPC_Matrices(A,B,Q,R,F,N);

功能:输入矩阵 A,B,Q,R,F 和预测区间 N ,输出矩阵 E,H。具体过程参考下面的 MPC_Matrices.m 文件。接下来进行预测:

U_K(:,k) = Prediction(X_K(:,k),E,H,N,p);

MPC_Matrices.m

function  [E , H]=MPC_Matrices(A,B,Q,R,F,N)

n=size(A,1);   % A 是 n x n 矩阵, 得到 n
p=size(B,2);   % B 是 n x p 矩阵, 得到 p

M=[eye(n);zeros(N*n,n)]; % 初始化 M 矩阵. M 矩阵是 (N+1)n x n的, 
                         % 它上面是 n x n 个 "I", 这一步先把下半部
                         % 分写成 0 
C=zeros((N+1)*n,N*p); % 初始化 C 矩阵, 这一步令它有 (N+1)n x NP 个 0

% 定义M 和 C 
tmp=eye(n);  %定义一个n x n 的 I 矩阵

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

% 定义Q_bar和R_bar
Q_bar = kron(eye(N),Q);
Q_bar = blkdiag(Q_bar,F);
R_bar = kron(eye(N),R); 

% 计算G, E, H
G=M'*Q_bar*M; % G: n x n
E=C'*Q_bar*M; % E: NP x n
H=C'*Q_bar*C+R_bar; % NP x NP 
end 

%%作者:DR_CAN https://www.bilibili.com/read/cv16891782 出处:bilibili

Prediction.m

function u_k= Prediction(x_k,E,H,N,p)

U_k = zeros(N*p,1); % NP x 1
U_k = quadprog(H,E*x_k); % 求出代价函数最小时U_k的数值
u_k = U_k(1:p,1); % 取第一个结果

end 

%%作者:DR_CAN https://www.bilibili.com/read/cv16891782 出处:bilibili

分析:

quadprog:matlab自带的最优化函数

【DR_CAN-MPC学习笔记】3&4.详细的MPC建模例子和matlab代码_第3张图片

运行结果:

7056c855bf9e487894e6a12bf8f214dc.png

由上图所示,状态值 gif.latex?x_1%2Cx_2 趋于0.

以上为单输入系统的例子。

二输入例子:

代码修改一下,也可以用于多输入,比如:

gif.latex?%5Cleft%5B%5Cbegin%7Barray%7D%7Bl%7D%20x_%7B1%7D%28k+1%29%20%5C%5C%20x_%7B2%7D%28k+1%29%20%5Cend%7Barray%7D%5Cright%5D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bcc%7D%201%20%26%200.1%20%5C%5C%20-1%20%26%202%20%5Cend%7Barray%7D%5Cright%5D%5Cleft%5B%5Cbegin%7Barray%7D%7Bl%7D%20x_%7B1%7D%28k%29%20%5C%5C%20x_%7B2%7D%28k%29%20%5Cend%7Barray%7D%5Cright%5D+%5Cleft%5B%5Cbegin%7Barray%7D%7Bll%7D%200.2%20%26%201%20%5C%5C%200.5%20%26%202%20%5Cend%7Barray%7D%5Cright%5D%5Cleft%5B%5Cbegin%7Barray%7D%7Bl%7D%20u_%7B1%7D%28k%29%20%5C%5C%20u_%7B2%7D%28k%29%20%5Cend%7Barray%7D%5Cright%5D

修改矩阵 A,B,R,Q,F,R ,使得 gif.latex?x_1 变化得更快(通过对矩阵Q的设置),且降低能耗减小 gif.latex?u_1 初始值(系统的输入一般是耗能的部分,通过对矩阵R的设置)

修改后的 MPC_Test.m:(其余不变)

%% 清屏
clear; 
close all; 
clc;

%% 加载 optim package,若使用matlab,则注释掉此行
% pkg load optim;

%% 第一步,定义状态空间矩阵
%% 定义状态矩阵 A, n x n 矩阵
% A = [1 0.1; 0 2];
A = [1 0.1; -1 2];
n= size (A,1); % 计算矩阵维度

%% 定义输入矩阵 B, n x p 矩阵
% B = [0; 0.5];
B=[0.2 1; 0.5 2];
p = size(B,2);

%% 定义Q矩阵,n x n 矩阵
% Q=[1 0; 0 1];
Q=[100 0; 0 1]; % 更加看重x_1的变化

%% 定义F矩阵,n x n 矩阵
% F=[1 0; 0 1];
F=[100 0; 0 1];

%% 定义R矩阵,p x p 矩阵
% R=[0.1];
R=[1 0; 0 0.1]; % 减小能耗,减小输入u_1

%% 定义step数量k
k_steps=100; 

%% 定义矩阵 X_K, n x k 矩 阵
X_K = zeros(n,k_steps);

%% 初始状态变量值, n x 1 向量
X_K(:,1) =[20;-20];

%% 定义输入矩阵 U_K, p x k 矩阵
U_K=zeros(p,k_steps);

%% 定义预测区间K
N=5;

%% Call MPC_Matrices 函数 求得 E,H矩阵 
[E,H]=MPC_Matrices(A,B,Q,R,F,N);

%% 计算每一步的状态变量的值
for k = 1 : k_steps 
%% 求得U_K(:,k)
U_K(:,k) = Prediction(X_K(:,k),E,H,N,p);
%% 计算第k+1步时状态变量的值
X_K(:,k+1)=(A*X_K(:,k)+B*U_K(:,k));
end

%% 绘制状态变量和输入的变化
subplot(2, 1, 1);
hold;
for i =1 :size (X_K,1)
plot(X_K(i,:));
end
legend("x1","x2")
hold off;
subplot(2, 1, 2);
hold;
for i =1 : size (U_K,1)
plot(U_K(i,:));
end
legend("u1","u2") 

%% 作者:DR_CAN https://www.bilibili.com/read/cv16891782 出处:bilibili

95357d359de9448998812bd926199b2e.png

由上图所示,gif.latex?x_1 迅速趋近于0. 相比于 R=[0.1 0; 0 0.1] 时,R=[1 0; 0 0.1] 时的输入 gif.latex?u_1 减小,但 gif.latex?x_1 趋近于0的速度变缓。由此也可以看出MPC的最优化的决策结果不是绝对的。

你可能感兴趣的:(最优化,学习,MPC,模型预测控制,DR_CAN,控制)