mathworks社区中的这个资料还是值得一说的。

1 openExample('mpc/mpccustomqp')

我们从几个角度来解析两者关系,简单的说就是MPC是带了约束的LQR.

在陈虹模型预测控制一书中P20中,提到在目标函数中求得极值的过过程中,相当于对输出量以及状态量相当于加的软约束

而模型预测控制与LQR中其中不同的一点,就是MPC中可以加入硬约束进行对状态量以及输出量的硬性约束

形如:S.T.表示的硬性约束,在LQR中没有这一项

下面我们从代码的角度解析这个问题:
1, 定义被控系统:

1 A = [1.1 2; 0 0.95];
2 B = [0; 0.0787];
3 C = [-1 1];
4 D = 0;
5 Ts = 1;
6 sys = ss(A,B,C,D,Ts);
7 x0 = [0.5;-0.5]; % initial states at [0.5 -0.5]

2,设计无约束LQR:


1 Qy = 1;
2 R = 0.01;
3 K_lqr = lqry(sys,Qy,R);

3, 运行仿真闭环结果:

1 t_unconstrained = 0:1:10;
2 u_unconstrained = zeros(size(t_unconstrained));
3 Unconstrained_LQR = tf([-1 1])*feedback(ss(A,B,eye(2),0,Ts),K_lqr);
4 lsim(Unconstrained_LQR,'-',u_unconstrained,t_unconstrained,x0);
5 hold on;

4,设计MPC控制器:

 1 %%
2 % The MPC objective function is |J(k) = sum(x(k)'*Q*x(k) + u(k)'*R*u(k) +
3 % x(k+N)'*Q_bar*x(k+N))|. To ensure that the MPC objective function has the
4 % same quadratic cost as the infinite horizon quadratic cost used by LQR,
5 % terminal weight |Q_bar| is obtained by solving the following Lyapunov
6 % equation:
7 Q = C'*C;
8 Q_bar = dlyap((A-B*K_lqr)', Q+K_lqr'*R*K_lqr);
9
10 %%
11 % Convert the MPC problem into a standard QP problem, which has the
12 % objective function |J(k) = U(k)'*H*U(k) + 2*x(k)'*F'*U(k)|.
13 Q_hat = blkdiag(Q,Q,Q,Q_bar);
14 R_hat = blkdiag(R,R,R,R);
15 H = CONV'*Q_hat*CONV + R_hat;
16 F = CONV'*Q_hat*M;
17
18 %%
19 % When there are no constraints, the optimal predicted input sequence U(k)
20 % generated by MPC controller is |-K*x|, where |K = inv(H)*F|.
21 K = H\F;
22
23 %%
24 % In practice, only the first control move |u(k) = -K_mpc*x(k)| is applied
25 % to the plant (receding horizon control).
26 K_mpc = K(1,:);
27
28 %%
29 % Run a simulation with initial states at [0.5 -0.5]. The closed-loop
30 % response is stable.
31 Unconstrained_MPC = tf([-1 1])*feedback(ss(A,B,eye(2),0,Ts),K_mpc);
32 lsim(Unconstrained_MPC,'*',u_unconstrained,t_unconstrained,x0)
33 legend show

到这里,完全可以说明,在无约束前提下,两种方法是一致的:

1 K_lqr =
2
3 4.3608 18.7401
4
5
6 K_mpc =
7
8 4.3608 18.7401

5,对LQR施加约束:

 1 x = x0;
2 t_constrained = 0:40;
3 for ct = t_constrained
4 uLQR(ct+1) = -K_lqrx;
5 uLQR(ct+1) = max(-1,min(1,uLQR(ct+1)));
6 x = Ax+BuLQR(ct+1);
7 yLQR(ct+1) = Cx;
8 end
9 figure
10 subplot(2,1,1)
11 plot(t_constrained,uLQR)
12 xlabel(‘time’)
13 ylabel(‘u’)
14 subplot(2,1,2)
15 plot(t_constrained,yLQR)
16 xlabel(‘time’)
17 ylabel(‘y’)
18 legend(‘Constrained LQR’)


6,对MPC施加约束:

 1 %% MPC Controller Solves QP Problem Online When Applying Constraints
2 % One of the major benefits of using MPC controller is that it handles
3 % input and output constraints explicitly by solving an optimization
4 % problem at each control interval.
5 %
6 % Use the built-in KWIK QP solver, |mpcqpsolver|, to implement the custom
7 % MPC controller designed above. The constraint matrices are defined as
8 % Ac*x>=b0.
9 Ac = -[1 0 0 0;...
10 -1 0 0 0;...
11 0 1 0 0;...
12 0 -1 0 0;...
13 0 0 1 0;...
14 0 0 -1 0;...
15 0 0 0 1;...
16 0 0 0 -1];
17 b0 = -[1;1;1;1;1;1;1;1];
18
19 %%
20 % The |mpcqpsolver| function requires the first input to be the inverse of
21 % the lower-triangular Cholesky decomposition of the Hessian matrix H.
22 L = chol(H,'lower');
23 Linv = L\eye(size(H,1));
24
25 %%
26 % Run a simulation by calling |mpcqpsolver| at each simulation step.
27 % Initially all the inequalities are inactive (cold start).
28 x = x0;
29 iA = false(size(b0));
30 opt = mpcqpsolverOptions;
31 opt.IntegrityChecks = false;
32 for ct = t_constrained
33 [u, status, iA] = mpcqpsolver(Linv,F*x,Ac,b0,[],zeros(0,1),iA,opt);
34 uMPC(ct+1) = u(1);
35 x = A*x+B*uMPC(ct+1);
36 yMPC(ct+1) = C*x;
37 end
38 figure
39 subplot(2,1,1)
40 plot(t_constrained,uMPC)
41 xlabel('time')
42 ylabel('u')
43 subplot(2,1,2)
44 plot(t_constrained,yMPC)
45 xlabel('time')
46 ylabel('y')
47 legend('Constrained MPC')

转载:https://blog.csdn.net/gophae/article/details/104546805/

Matlab解析LQR与MPC的关系的更多相关文章

  1. MATLAB模型预测控制(MPC,Model Predictive Control)

    模型预测控制是一种基于模型的闭环优化控制策略. 预测控制算法的三要素:内部(预测)模型.参考轨迹.控制算法.现在一般则更清楚地表述为内部(预测)模型.滚动优化.反馈控制. 大量的预测控制权威性文献都无 ...

  2. Autofac官方文档翻译--二、解析服务--2隐式关系类型

    Autofac 隐式关系类型 Autofac 支持自动解析特定类型,隐式支持组件与服务间的特殊关系.要充分利用这些关系,只需正常注册你的组件,但是在使用服务的组件或调用Resolve()进行类型解析时 ...

  3. MATLAB解析PFM格式图像

    http://www.p-chao.com/ja/2016-09-27/matlab%E8%A7%A3%E6%9E%90pfm%E6%A0%BC%E5%BC%8F%E5%9B%BE%E5%83%8F/ ...

  4. wordpress源码解析-目录结构-文件调用关系(1)

    学习开源代码,是一种很快的提升自己的学习方法.Wordpress作为一个开源的博客系统,非常优秀,应用广泛,使用起来简单方便,具有丰富的主题和插件,可以按照自己的需求来任意的进行修改.所以就从word ...

  5. 黄聪:wordpress源码解析-目录结构-文件调用关系(转)

    Wordpress是一个单入口的文件,所有的前端处理都必须经过index.php,这是通过修改web服务器的rewrite规则来实现的.这种做法的好处是显而易见的,这样URL更好看,不必为每一个url ...

  6. Apollo代码学习(七)—MPC与LQR比较

    前言 Apollo中用到了PID.MPC和LQR三种控制器,其中,MPC和LQR控制器在状态方程的形式.状态变量的形式.目标函数的形式等有诸多相似之处,因此结合自己目前了解到的信息,将两者进行一定的比 ...

  7. 开发者说 | Apollo控制算法之汽车动力学模型和LQR控制

    参考:https://mp.weixin.qq.com/s?__biz=MzI1NjkxOTMyNQ==&mid=2247486444&idx=1&sn=6538bf1fa74 ...

  8. 以神经网络使用为例的Matlab和Android混合编程

    由于需要在一个Android项目中使用神经网络,而经过测试发现几个Github上开源项目的训练效果就是不如Matlab的工具箱好,所以就想在Android上使用Matlab神经网络代码(可是...) ...

  9. Matlab与数学建模

    一.学习目标. (1)了解Matlab与数学建模竞赛的关系. (2)掌握Matlab数学建模的第一个小实例—评估股票价值与风险. (3)掌握Matlab数学建模的回归算法. 二.实例演练. 1.谈谈你 ...

随机推荐

  1. 针对python输入要求

    针对python输入要求 类型: 1.输入行数不确定,并且每一行输入一个数据. a=[] b=input() while b!='-1': //指随意使用一个值作为一个标志,来进行控制输入的行数.(在 ...

  2. 平平无奇的项目「GitHub 热点速览 v.22.10」

    不知道大家对高星项目什么印象?提到这个词第一个想到哪个项目呢?本周有几个项目看着普普通通,却完成了一周 2k+ star 的事迹.比如 SingleFile,它是个浏览器扩展,点击图标之后即可保存一个 ...

  3. P1424

    #include <stdio.h> int main(){ int s = 250; int x, n, distance = 0; scanf("%d %d",&a ...

  4. LeetCode-033-搜索旋转排序数组

    搜索旋转排序数组 题目描述:整数数组 nums 按升序排列,数组中的值 互不相同 . 在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行 ...

  5. DPLL 算法(求解k-SAT问题)详解(C++实现)

    \(\text{By}\ \mathsf{Chesium}\) DPLL 算法,全称为 Davis-Putnam-Logemann-Loveland(戴维斯-普特南-洛吉曼-洛夫兰德)算法,是一种完备 ...

  6. Spring Cloud Gateway actuator组建对外暴露RCE问题漏洞分析

    Spring Cloud gateway是什么? Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关.网关作为流量的,在微服务系统中有着非常作 ...

  7. Docker安装与基本命令使用

    1. 卸载旧版本 Docker在CentOS上的安装 官方文档:https://docs.docker.com/engine/install/centos/ sudo yum remove docke ...

  8. vue结合antV/g6 实现网络拓扑图

    最近很多业务场景都会需要用到拓扑图,翻找了很多资料,最后选择了antV/g6,主要原因有以下几点: 1.阿里出品,所以框架的成熟性有保障 2.业务场景契合(1.规则拓扑图:2.动画流向:每个节点会有流 ...

  9. rodert教你学FFmpeg实战这一篇就够了

    rodert教你学FFmpeg实战这一篇就够了 建议收藏,以备查阅 pdf阅读版: 链接:https://pan.baidu.com/s/11kIaq5V6A_pFX3yVoTUvzA 提取码:jav ...

  10. python 之 matplotlib 练习

    import numpy as npimport matplotlib.pyplot as plt x = np.linspace(0,10,1000)# 自变量y = np.sin(x) + 1 # ...