实验十S函数的编写及应用一、实验目的1、学习S函数的编写方法。2、利用S函数在Simulink环境中实现复杂模块。二、实验内容1.编写S函数,实现下图死区非线性模块功能编写S函数如下:function[sys,x0,str,ts]=sqnl(t,x,u,flag,s1)%死区非线性%s1为死区环值switchflag,case0,[sys,x0,str,ts]=mdlInitializeSizes;case1sys=x;case3,sys=mdlOutputs(t,x,u,s1);case{1,2,4,9}sys=[];otherwiseerror(['Unhandledflag=',num2str(flag)]);endfunction[sys,x0,str,ts]=mdlInitializeSizessizes=simsizes;sizes.NumContStates=0;s-sh-hs+h-s-hxysizes.NumDiscStates=0;sizes.NumOutputs=1;sizes.NumInputs=1;sizes.DirFeedthrough=1;sizes.NumSampleTimes=1;%atleastonesampletimeisneededsys=simsizes(sizes);x0=[];str=[];ts=[-10];functionsys=mdlOutputs(t,x,u,s1)ifabs(u)=s1,ifu0,sys=u-s1;elsesys=u+s1;endelsesys=0;end按上面介绍的方法,建立Simulink模块,运行验证。2.编写S函数,实现积分分离PID控制功能系统框图如下图所示编写S函数如下:function[sys,x0,str,ts]=jfflpid(t,x,u,flag,Kp,Ti,Td,T,E)%积分分离PIDS-函数%T为采样周期,E为积分分离阈值globalumaxKiKduk_1ek_1ek_2B%umax为最大控制量,B为积分分离算子switchflag,case0,%Initialization初始化部分sizes=simsizes;sizes.NumContStates=0;sizes.NumDiscStates=0;sizes.NumOutputs=1;sizes.NumInputs=1;sizes.DirFeedthrough=1;sizes.NumSampleTimes=1;sys=simsizes(sizes);x0=[];str=[];ts=[T0];umax=50;uk_1=0;ek_1=0;ek_2=0;Ki=Kp*T/Ti;Kd=Kp*Td/T;%控制器参数初始化case3,%Outputs控制器输出计算ek=u;%获取误差ifabs(ek)=E,B=1;elseB=0;enduk=uk_1+Kp*(ek-ek_1)+B*Ki*ek+Kd*(ek-2*ek_1+ek_2);ifukumax,uk=umax;endifuk-umax,uk=-umax;enduk_1=uk;ek_2=ek_1;ek_1=ek;sys=[uk];case{1,2,4,9}sys=[];otherwiseerror(['无效的标志flag=',num2str(flag)]);end建立Simulink模块,在MATLAB命令窗口给变量赋值(注意大小写):Kp=2.9;Ti=0.65;Td=1.65;T=0.03;E=100(E为积分分离阈值,取一个很大值相当于没有积分分离作用),运行模块,注意响应曲线。改变E值为1(输入阶跃信号值),运行模块,观察响应曲线。第二个响应的稳态值为什么不是给定值?如果将采样周期改为0.3,又怎样?采取什么办法可避免上述现象?三、实验结果(1)function[sys,x0,str,ts]=sqnl(t,x,u,flag,s1)%死区非线性%s1为死区环值switchflag,case0,[sys,x0,str,ts]=mdlInitializeSizes;case1sys=x;case3,sys=mdlOutputs(t,x,u,s1);case{1,2,4,9}sys=[];otherwiseerror(['Unhandledflag=',num2str(flag)]);endfunction[sys,x0,str,ts]=mdlInitializeSizessizes=simsizes;sizes.NumContStates=0;sizes.NumDiscStates=0;sizes.NumOutputs=1;sizes.NumInputs=1;sizes.DirFeedthrough=1;sizes.NumSampleTimes=1;%atleastonesampletimeisneededsys=simsizes(sizes);x0=[];str=[];ts=[-10];functionsys=mdlOutputs(t,x,u,s1)ifabs(u)=s1,ifu0,sys=u-s1;elsesys=u+s1;endelsesys=0;endsys=sqnl(1,1,1,0,0.1)sys=0011011sys=sqnl(1,1,1,1,0.1)sys=1sys=sqnl(1,1,1,3,0.1)sys=0.9000sys=sqnl(1,1,1,10,0.1)???Errorusing==sqnlUnhandledflag=10sys=sqnl(1,1,1,9,0.1)sys=[]Flag=0flag=!0(2)function[sys,x0,str,ts]=jffpid(t,x,u,flag,Kp,Ti,Td,T,E)globalumaxKiKduk_1ek_1ek_2Bswitchflag,case0,sizes=simsizes;sizes.NumContStates=0;sizes.NumDiscStates=0;sizes.NumOutputs=1;sizes.NumInputs=1;sizes.DirFeedthrough=1;sizes.NumSampleTimes=1;sys=simsizes(sizes);x0=[];str=[];ts=[T0];umax=50;uk_1=0;ek_1=0;ek_2=0;Ki=Kp*T/Ti;Kd=Kp*Td/T;case3,ek=u;ifabs(ek)=E,B=1;elseB=0;enduk=uk_1+Kp*(ek-ek_1)+B*Ki*ek+Kd*(ek-2*ek_1+ek_2);ifukumax,uk=umax;endifuk-umax,uk=-umax;enduk_1=uk;ek_2=ek_1;ek_1=ek;sys=[uk];case{1,2,4,9}sys=[];otherwiseerror(['无效的标志flag=',num2str(flag)]);endsys=jffpid(1,1,1,0,2.9,0.65,1.65,0.03,100)sys=0011011sys=jffpid(1,1,1,3,2.9,0.65,1.65,0.03,100)sys=50K=3K=4.5sys=jffpid(1,1,1,3,2.9,0.65,1.65,0.03,1)sys=-50sys=jffpid(1,1,1,0,2.9,0.65,1.65,0.03,1)sys=0011011sys=jffpid(1,1,1,0,2.9,0.65,1.65,0.3,100)sys=0011011sys=jffpid(1,1,1,3,2.9,0.65,1.65,0.3,100)sys=20.1885k=4.5,T=0.3