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. low-code

    low-code特点: 1. 一个创造软件的开发环境,类似vs-code: 2. 通过可视化拖拽和参数配置高效开发. 1. 背景 1.1 目标 需求的交付质量和交付效率一直是中后台项目开发中非常关注的 ...

  2. TypeError

    1.only size-1 arrays can be converted to Python scalars 问题来源:需要把一个float数组A转为int,于是直接在代码中写了 B=int(A), ...

  3. broken pipe 报错分析和解决办法

    参考资料: 1.博客1:https://blog.csdn.net/qq_37535749/article/details/113781338 2.博客2:https://blog.csdn.net/ ...

  4. c/c++(c++和网络编程)日常积累(二)

    Linux下C编程通过宏定义打开和关闭调试信息 https://www.cnblogs.com/robinsons/p/3667032.html https://blog.csdn.net/u0134 ...

  5. Pycharm新建Python项目

    关于新建项目时配置项目环境(最好是每个项目单独的虚拟Python环境): Python为什么要使用虚拟环境-Python虚拟环境的安装和配置-virtualenv Pycharm创建Python项目 ...

  6. MySQL-5.7.29解压版安装教程【全网最新】

    作者:北顾箫 博客园地址:https://www.cnblogs.com/Aarom 1.下载解压 下载地址:https://downloads.mysql.com/archives/communit ...

  7. quartz框架(七)-JobStore

    JobStore 在之前的博文中,博主已经写了关于Job的相关内容.本篇博文,博主将介绍JobStore相关的内容. JobStore是存放Job和Trigger的地方.当我们调用Scheduler对 ...

  8. HarmonyOS小白入门设备开发的“芯”路历程

    HarmonyOS Connect设备开发,相信不少刚入门的开发者都被这些问题所困扰,面对五花八门的开发板不知道该怎么选取?芯片.模组.开发板傻傻分不清?如何使用代码控制开发板? 本期,我们将一一为你 ...

  9. ob-页面静态化(1)

    $page = $_GET['page'] ?? 1; $filename = 'list_' . $page . '.html'; ////判断有没有静态页面,有的话直接读取静态页面,没有的话,连接 ...

  10. find: `./folder': No such file or directory 错误处理

    这是我正在处理的目录的内容: misha@hp-laptop:~/work/c/5$ ls -l total 8 -rw-rw-r-- 1 misha misha 219 May 20 15:37 d ...