⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pid_mpc.m

📁 PID 和 DMC 实现的matlab控制仿真
💻 M
字号:
clc
clear all
close all
%采样时间
ts=20
%PID的控制变量 1 0.005 0.3
kp=1;
ki=0.05;
kd=0.3;
%原系统离散化
sysc=tf([1],[60 1],'inputdelay',80);
sysd=c2d(sysc,ts,'zoh');
%得到需要的离散系统参数
[num,den]=tfdata(sysd,'v');
%PID仿真参数设定
u1=0;u2=0;u3=0;u4=0;u5=0;
y1=0;y2=0;y3=0;
e1=0;e2=0;
es=0; 
%开始PID仿真
for k=1:500
    t(k)=k*ts;
    y(k)=-den(2)*y1+num(2)*u5;
    r(k)=1;
    e(k)=r(k)-y(k);
    es=es+e(k)*ts;
    %PID Control
    u(k)=kp*e(k)+kd*(e(k)-e1)/ts+ki*es;
    %Control V Lim
    if u(k)>5
        u(k)=5;
    elseif u(k)<-5
        u(k)=-5;
    end
    u5=u4;
    u4=u3;
    u3=u2;
    u2=u1;
    u1=u(k); 
    
    e2=e1;
    e1=e(k);
end
%DMC仿真
%建立离散的系统
gmpc=poly2tfd([1],[60 1],0,80);
%系统阶跃响应的时间
tend=20000;
mymodel=tfd2step(tend,ts,1,gmpc);
%MPC参数设定
P=5;
M=3;
ywt=[];
uwt=[1];
rmpc=[1];
%系统的仿真时间设定
tend=10000;
%建立MPC的系数矩阵
mpcM=mpccon(mymodel,ywt,uwt,M,P);
%MPC仿真
[ympc,umpc,ymmpc]=mpcsim(mymodel,mymodel,mpcM,tend,rmpc);
%绘图
%PID控制绘图
figure(1)
subplot(2,1,1);
title('PID System Output');
plot(t,r,'b',t,y,'r');
grid on
xlabel('Time(s)');
ylabel('System Input(V) & System Output');
subplot(2,1,2);
title('PID Control Output');
plot(t,u);
grid on
xlabel('Time(s)');
ylabel('Control Output')
%MPC控制绘图
figure(2)
subplot(2,1,1);
title('MPC System Output');
plot(t,r,'b',t,ympc(1:500),'r');
grid on
xlabel('Time(s)');
ylabel('System Input(V) & System Output');
subplot(2,1,2);
title('MPC Control Output');
plot(t,umpc(1:500));
grid on
xlabel('Time(s)');
ylabel('Control Output')
%PID控制与MPC控制比较
figure(3)
subplot(2,1,1);
title('PID System Output(blue) Vs MPC System Output(red)');
plot(t,y,'b',t,ympc(1:500),'r');
grid on;
xlabel('Time(s)');
ylabel('System Output');
subplot(2,1,2);
title('PID Control Output(blue) Vs MPC Control Output(red)');
plot(t,u,'b',t,umpc(1:500),'r');
grid on;
xlabel('Time(s)');
ylabel('Control Output');



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -