Byali2016-5-3利用S-Function实现传递函数利用S-Function实现传递函数................................................................................................................1引言....................................................................................................................................................21、MATLAB函数命令介绍............................................................................................................21.1MATLABtf函数命令:......................................................................................................21.2MATLABss函数命令:.....................................................................................................21.3MATLABtf2ss函数命令:.................................................................................................32、实例仿真......................................................................................................................................32.1假设工业过程某传递函数:..............................................................................................32.2修改S-Function模板参数:..............................................................................................42.3仿真结果............................................................................................................................10Byali2016-5-3引言自然界大多数的过程都可以看做是一个系统,一般来说系统都会有输入和输出,而传递函数就是输入到输出的一个转换方式,比如,对于一个罐体来说,流量是输入,液位是输出,则从流量转换到液位的过程就是传递函数的本质,可以通过求解方程式,来确定传递函数的数学形式,也可以通过实验法来辨识这个数学模型。S-Function的功能就不多说了,总之,其功能之强大,属MATLAB/Simulink开发者必学之内容。这篇短文讲述的就是如何利用S-Function实现传递函数:我们知道S-Function有自己的模板,而我们要做的就是更改模板代码中的某些参数就行了,考虑到这些参数和状态方程的关系密切,因此,本文的思路就是首先将传递函数转为系统状态方程,然后再对参数进行修改。1、MATLAB函数命令介绍1.1MATLABtf函数命令:建立传递函数:例1:s=tf('s');H=s/(s^2+2*s+10);例2:h=tf([10],[1210]);1.2MATLABss函数命令:建立状态空间模型连续模型:sys=ss(a,b,c,d)createsastate-spacemodelobjectrepresentingthecontinuous-timestate-spacemodelForamodelwithNxstates,Nyoutputs,andNuinputs:aisanNx-by-Nxreal-orcomplex-valuedmatrix.bisanNx-by-Nureal-orcomplex-valuedmatrix.cisanNy-by-Nxreal-orcomplex-valuedmatrix.disanNy-by-Nureal-orcomplex-valuedmatrix.TosetD=0,setdtothescalar0(zero),regardlessofthedimension.离散模型:sys=ss(a,b,c,d,Ts)createsthediscrete-timemodelwithsampletimeTs(inseconds).SetTs=-1orTs=[]toleavethesampletimeunspecified.Byali2016-5-31.3MATLABtf2ss函数命令:将传递函数模型转化为状态空间模型,单输入单输出系统转换如下:利用该命令不仅可以转换SISO系统也可以转换MIMO系统,如下MISO系统也可以:den=[023;121];num=[10.41];[A,B,C,D]=tf2ss(den,num)结果:A=-0.4000-1.00001.00000B=10C=2.00003.00001.60000D=012、实例仿真2.1假设工业过程某传递函数:den=[1.5];num=[151];[A,B,C,D]=tf2ss(den,num)结果:a=x1x2Byali2016-5-3x1-5-1x210b=u1x11x20c=x1x2y101.5d=u1y10所以有:221215.1010115xyuxxxx2.2修改S-Function模板参数:从结果可以看出,系统有两个状态,没有离散状态,输入1个,输出1个,无直通反馈,一种采用时间,打开模板,复制代码。function[sys,x0,str,ts,simStateCompliance]=sfuntmpl(t,x,u,flag)【一个主函数,需修改原函数名:sfuntmpl】%SFUNTMPLGeneralMATLABS-FunctionTemplate%WithMATLABS-functions,youcandefineyouownordinarydifferential%equations(ODEs),discretesystemequations,and/orjustabout%anytypeofalgorithmtobeusedwithinaSimulinkblockdiagram.%%ThegeneralformofanMATLABS-functionsyntaxis:%[SYS,X0,STR,TS,SIMSTATECOMPLIANCE]=SFUNC(T,X,U,FLAG,P1,...,Pn)%%WhatisreturnedbySFUNCatagivenpointintime,T,dependsonthe%valueoftheFLAG,thecurrentstatevector,X,andthecurrent%inputvector,U.%%FLAGRESULTDESCRIPTION%-------------------------------------------------------%0[SIZES,X0,STR,TS]Initialization,returnsystemsizesinSYS,%initialstateinX0,stateorderingstrings%inSTR,andsampletimesinTS.Byali2016-5-3%1DXReturncontinuousstatederivativesinSYS.%2DSUpdatediscretestatesSYS=X(n+1)%3YReturnoutputsinSYS.%4TNEXTReturnnexttimehitforvariablestepsample%timeinSYS.%5Reservedforfuture(rootfinding).%9[]Termination,performanycleanupSYS=[].%%%Thestatevectors,XandX0consistsofcontinuousstatesfollowed%bydiscretestates.%%Optionalparameters,P1,...,PncanbeprovidedtotheS-functionand%usedduringanyFLAGoperation.%%WhenSFUNCiscalledwithFLAG=0,thefollowinginformation%shouldbereturned:%%SYS(1)=Numberofcontinuousstates.%SYS(2)=Numberofdiscretestates.%SYS(3)=Numberofoutputs.%SYS(4)=Numberofinputs.%AnyofthefirstfourelementsinSYScanbespecified%as-1indicatingthattheyaredynamicallysized.The%actuallengthforallotherflagswillbeequaltothe%lengthoftheinput,U.%SYS(5)=Reservedforrootfinding.Mustbezero.%SYS(6)=Directfeedthroughflag(1=yes,0=no).Thes-function%hasdirectfeedthroughifUisusedduringtheFLAG=3%call.Settingthisto0isakintomakingapromisethat%UwillnotbeusedduringFLAG=3.Ifyoubreakthepromise%thenunpredictableresultswilloccur.%SYS(7)=Numberofsampletimes.ThisisthenumberofrowsinTS.%%%X0=Initialstateconditionsor[]ifnostates.%%STR=Stateorderingstringswhichisgenerallyspecifiedas[].%%TS=Anm-by-2matrixcontainingthesampletime%(period,offset)information.Wherem=numberofsample%times.Theorderingofthesampletimesmustbe:%%TS=[00,:Continuoussampletime.%01,:Continuous,butfixedinminorstepByali2016-5-3%sampletime.%PERIODOFFSET,:Discretesampletimewhere%PERIOD0&OFFSETPERIOD.%-20];:Variablestepdiscretesampletime%whereFLAG=4isusedtogettimeof%nexthit.%%Therecanbemorethanonesampletimepro