function a=single_PI()
clc
clear all
%%%%%%%%PID控制%%%%%%%%%%%%%
de=0;ie=0; e=0; u=0;
Kc=28;Kp=25;Kd=0.82; %PID控制参数
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n=1;h=0.01; x10=[0;0;]; u=0; t=0; Tmax=2; %计算程序用得初始值
while t<=Tmax
u=Kp*e+Kc*ie+Kd*de; %控制规律
%%%%%%%%%%%%%%%%%%%%%四阶龙格计算%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[u,dX]=derives(t,x10,u);
m1=h*dX; t=t+0.5*h;X=x10+0.5*m1; %更新导数
[u,dX]=derives(t,X,u);
m2=h*dX; X=x10+0.5*m2; %更新导数
[u,dX]=derives(t,X,u);
m3=h*dX; t=t+0.5*h; X=x10+m3; %更新导数
[u,dX]=derives(t,X,u);
m4=h*dX;
x10=x10+(1/6)*(m1+2*m2+2*m3+m4);
C=[0 149.2537]; %振动函数输出
y=C*x10;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rink=0; %跟踪对象,或者比较对象
e1=e; e=rink-y; de=(e-e1)/h; ie=ie+e; %比列积分和微分积分多注意
XX(n)=y;RR(n)=rink; Time(n)=t; %用于保存
n=n+1;
end
plot(Time,XX,'b',Time,RR,'r');grid on;
end
%求导数
function [u,dX]=derives(t,X,u)
fi=10*exp(-0.5*t).*cos(3*t); %振动系统对象
A=[-14.6254 0;1 0]; B=[1;0]; f=u+fi;
dX=A*X+B*f;
end
|