您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 金融资料 > S-Function-使用及应用举例
高范强@IEE版权所有1S-Function使用及应用举例【说明】近来在论坛中很多朋友问及Simulink下的S-函数的编写及使用方法,在西安交大BBS上,hii_yzf网友发表了有关S-函数使用的文章,举例比较详尽,相信对关心S-函数的网友会有很大帮助。发信人:hii_yzf(叶子),信区:MathTools标题:S-FUNCTIONS的书写之一发信站:交通大学思源BBS站(MonApr1616:35:092001),站内信件S-FUNCTIONS的书写之一s-function也就是system-function的缩写。说得简单,s-function就是用MATLAB所提供的模型不能完全满足用户,而提供给用户自己编写程序来满足自己要求模型的接口。要了解s-function,必须了解以下知识:(1)directfeedthrough(2)dynamicallysizedinputs(3)settingsampletimesandoffsets[由于我也不知道怎么把上面三句话精确的翻译成中文,因此在此不加翻译,大家自己理解,也许更好]由于上面三部分的重要性,在此详细进行分析。一.directfeedthroughdirectfeedthrough意思是说系统的输出或可变采样时间是否受到输入的控制。大家清楚有的系统是受到输入控制如:y=k*u(u是输入,k是放大因子,y是输出)而有的系统输出是不受到输入影响,如:输出:y=xdx=ux表示状态二.dynamicallysizedinputs主要是给出:输入连续状态数目(size.NumContStates),离散状态数目(size.NumDiscStates),输出数目(size.NumOutputs),输入数目(size.NumInputs),DirectFeedthrough(size.DirFeedthrough)。三.settingsampletimesandoffsetssettingsmapletimesandoffsets主要设置采样时间MATLAB为了用户方便,已经书写了S-FUNCTIONS模板函数sfuntmpl.m。为了更好的写S-FUNCTIONS,大家来看一下,该函数sfuntmpl.m内容如下:(我通过在该内容加注释来说明,以$开头)function[sys,x0,str,ts]=sfuntmpl(t,x,u,flag)$输出变量就此四个,大家必须注意它的顺序。$输入变量可以为t,x,u,flag,p1,...,pn等,但是前面的四个变量不能变,特此说明。%SFUNTMPLGeneralM-fileS-functiontemplate高范强@IEE版权所有2%WithM-fileS-functions,youcandefineyouownordinarydifferential%equations(ODEs),discretesystemequations,and/orjustabout%anytypeofalgorithmtobeusedwithinaSimulinkblockdiagram.%$上面是其功能%ThegeneralformofanM-FileS-functionsyntaxis:%[SYS,X0,STR,TS]=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.$具体怎样实现,大家参看后面的函数mdlInitializeSizes%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高范强@IEE版权所有3%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.%$需要说明的是sys的顺序不能乱%%X0=Initialstateconditionsor[]ifnostates.%%STR=Stateorderingstringswhichisgenerallyspecifiedas[].%%TS=Anm-by-2matrixcontainingthesampletime%(period,offset)information.Wherem=numberofsample%times.Theorderingofthesampletimesmustbe:%%TS=[00,:Continuoussampletime.%01,:Continuous,butfixedinminorstep%sampletime.%PERIODOFFSET,:Discretesampletimewhere%PERIOD0&OFFSET〈PERIOD.%-20];:Variablestepdiscretesampletime%whereFLAG=4isusedtogettimeof%nexthit.%%Therecanbemorethanonesampletimeproviding%theyareorderedsuchthattheyaremonotonically%increasing.Onlytheneededsampletimesshouldbe%specifiedinTS.Whenspecifyingthanone%sampletime,youmustcheckforsamplehitsexplicitlyby%seeingif%abs(round((T-OFFSET)/PERIOD)-(T-OFFSET)/PERIOD)%iswithinaspecifiedtolerance,generally1e-8.This%toleranceisdependentuponyourmodel'ssamplingtimes%andsimulationtime.%%YoucanalsospecifythatthesampletimeoftheS-function%isinheritedfromthedrivingblock.Forfunctionswhich%changeduringminorsteps,thisisdoneby%specifyingSYS(7)=1andTS=[-10].Forfunctionswhich高范强@IEE版权所有4%areheldduringminorsteps,thisisdonebyspecifying%SYS(7)=1andTS=[-1-1].%Copyright(c)1990-1998byTheMathWorks,Inc.AllRightsReserved.%$Revision:1.12$%%ThefollowingoutlinesthegeneralstructureofanS-function.%switchflag,%%%%%%%%%%%%%%%%%%%Initialization%%%%%%%%%%%%%%%%%%%case0,[sys,x0,str,ts]=mdlInitializeSizes;$大家是不是觉得此函数名太长,当然可以根据自己的爱好加以改变,不过后面的相应改。%%%%%%%%%%%%%%%%Derivatives%%%%%%%%%%%%%%%%case1,sys=mdlDerivatives(t,x,u);%%%%%%%%%%%Update%%%%%%%%%%%case2,sys=mdlUpdate(t,x,u);%%%%%%%%%%%%Outputs%%%%%%%%%%%%case3,sys=mdlOutputs(t,x,u);%%%%%%%%%%%%%%%%%%%%%%%%GetTimeOfNextVarHit%%%%%%%%%%%%%%%%%%%%%%%%case4,sys=mdlGetTimeOfNextVarHit(t,x,u);%%%%%%%%%%%%%%Terminate%%%%%%%%%%%%%%case9,sys=mdlTerminate(t,x,u);%%%%%%%%%%%%%%%%%%%%%Unexpectedflags%%%%%%%%%%%%%%%%%%%%%otherwise,error(['Unhandledflag=',num2str(flag)]);end%endsfuntmpl%%==============================================================高范强@IEE版权所有5===============%mdlInitializeSizes%Returnthesizes,initialconditions,andsampletimesfortheS-function.%=============================================================================%function[sys,x0,str,ts]=md
本文标题:S-Function-使用及应用举例
链接地址:https://www.777doc.com/doc-4464521 .html