资源描述:
《PID算法Matlab仿真程序和C程序.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、增量式PID控制算法Matlab仿真程序设一被控对象G(s)=50/(0.125s^2+7s),用增量式PID控制算法编写仿真程序(输入分别为单位阶跃、正弦信号,采样时间为1ms,控制器输出限幅:[-5,5],仿真曲线包括系统输出及误差曲线,并加上注释、图例)。程序如下clearall;closeall;ts=0.001;sys=tf(50,[0.125,7,0]);dsys=c2d(sys,ts,'z');[num,den]=tfdata(dsys,'v');u_1=0.0;u_2=0.0;y_1=0.0;y_2=0.0;x=[0,0,0]';error_1=0;
2、error_2=0;fork=1:1:1000time(k)=k*ts;S=2;ifS==1kp=10;ki=0.1;kd=15;rin(k)=1;%StepSignalelseifS==2kp=10;ki=0.1;kd=15;%SineSignalrin(k)=0.5*sin(2*pi*k*ts);enddu(k)=kp*x(1)+kd*x(2)+ki*x(3);%PIDControlleru(k)=u_1+du(k);%Restrictingtheoutputofcontrollerifu(k)>=5u(k)=5;endifu(k)<=-5u(k)=-5;end
3、%Linearmodelyout(k)=-den(2)*y_1-den(3)*y_2+num(2)*u_1+num(3)*u_2;error(k)=rin(k)-yout(k);%Returnofparametersu_2=u_1;u_1=u(k);y_2=y_1;y_1=yout(k);x(1)=error(k)-error_1;%CalculatingPx(2)=error(k)-2*error_1+error_2;%CalculatingDx(3)=error(k);%CalculatingIerror_2=error_1;error_1=error(k);e
4、ndfigure(1);plot(time,rin,'b',time,yout,'r');xlabel('time(s)'),ylabel('rin,yout');figure(2);plot(time,error,'r')xlabel('time(s)');ylabel('error');微分先行PID算法Matlab仿真程序%PIDControlerwithdifferentialinadvanceclearall;closeall;ts=20;sys=tf([1],[60,1],'inputdelay',80);dsys=c2d(sys,ts,'zoh');[
5、num,den]=tfdata(dsys,'v');u_1=0;u_2=0;u_3=0;u_4=0;u_5=0;ud_1=0;y_1=0;y_2=0;y_3=0;error_1=0;error_2=0;ei=0;fork=1:1:400time(k)=k*ts;6%Linearmodelyout(k)=-den(2)*y_1+num(2)*u_5;kp=0.36;kd=14;ki=0.0021;rin(k)=1.0*sign(sin(0.00025*2*pi*k*ts));error(k)=rin(k)-yout(k);ei=ei+error(k)*ts;gama=
6、0.50;Td=kd/kp;Ti=0.5;c1=gama*Td/(gama*Td+ts);c2=(Td+ts)/(gama*Td+ts);c3=Td/(gama*Td+ts);M=1;ifM==1%PIDControlwithdifferentialinadvanceud(k)=c1*ud_1+c2*yout(k)-c3*y_1;u(k)=kp*error(k)+ud(k)+ki*ei;elseifM==2%SimplePIDControlu(k)=kp*error(k)+kd*(error(k)-error_1)/ts+ki*ei;endifu(k)>=110u(
7、k)=110;endifu(k)<=-110u(k)=-110;end%Updateparametersu_5=u_4;u_4=u_3;u_3=u_2;u_2=u_1;u_1=u(k);y_3=y_2;y_2=y_1;y_1=yout(k);error_2=error_1;error_1=error(k);endfigure(1);plot(time,rin,'r',time,yout,'b');xlabel('time(s)');ylabel('rin,yout');figure(2);plot(time,u,'r');xlabel('time(s)');yl