xspeedis5.0whileabs(vr(k))>=5.0rin(k)=rand;vr(k)=abs((rin(k)-r_1)/ts);endendu(k)=kp*x(1)+kd*x(2)+ki*x(3);%PIDController%Restrictingtheoutputofcontrollerifu(k)>=10u(k)=10;endifu(k)<=-10u(k)=-10;end%Linearmodelyout(k)=-den(2)*y_1-den(3)*y_2-den(4)*y_3+num(2)*u_1+num(3)*u_2+num(4)*u_3;error(k)=rin(k)-yout(k);r_1=rin(k);u_3=u_2;u_2=u_1;u_1=u(k);y_3=y_2;y_2=y_1;y_1=yout(k);x(1)=error(k);%CalculatingPx(2)=(error(k)-error_1)/ts;%CalculatingDx(3)=x(3)+error(k)*ts;%CalculatingIxi(k)=x(3);error_1=error(k);ifD==1%DynamicSimulationDisplayplot(time,rin,'b',time,yout,'r');pause(0.000001);endendplot(time,rin,'r',time,yout,'b');xlabel('time(s)');ylabel('rin,yout');Ex7采用Simulink实现PID控制器的设计,如图2-2所示,其中离散PID控制的子系统如图2-3所示,其封装界面如图2-4所示。仿真程序:ex7.mdl图2-2离散PID控制的Simulink主程序图2-3离散PID控制的Simulink控制器程序图2-4离散PID控制的封装界面