您好,欢迎访问三七文档
当前位置:首页 > 财经/贸易 > 资产评估/会计 > S函数设计初学者实例
利用matlab中s函数模板设计一个连续系统的s-function。给定控制系统的传递函数为1()1Gss。试利用仿真集成环境simulink中的s函数,绘制此控制系统的阶跃响应曲线。【步骤1】获取状态空间表达式。根据传递函数,写出该控制系统的运动方程,如下:()1()()()()1YsytytutUss选取状态变量x=y,则系统的状态空间方程表示为:xxuyx【步骤2】建立s函数的m文件。复制matlab安装文件夹下的toolbox\simulink\blocks子目录下的sfuntmpl.m文件,并将其改名为sfuction_example.m再根据状态方程修改程序中的代码。(1)重新命名函数;function[sys,x0,str,ts]=sfunction_example(t,x,u,flag,x_initial)%SFUNTMPLGeneralM-fileS-functiontemplate%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.%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,butfixedinminorstep%sampletime.%PERIODOFFSET,:Discretesampletimewhere%PERIOD0&OFFSETPERIOD.%-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%areheldduringminorsteps,thisisdonebyspecifying%SYS(7)=1andTS=[-11].%Copyright1990-2002TheMathWorks,Inc.%$Revision:1.18$%%ThefollowingoutlinesthegeneralstructureofanS-function.%switchflag,%%%%%%%%%%%%%%%%%%%Initialization%%%%%%%%%%%%%%%%%%%case0,[sys,x0,str,ts]=mdlInitializeSizes(x_initial);%%%%%%%%%%%%%%%%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%%%%%%%%%%%%%%%%%%%%%otherwiseerror(['Unhandledflag=',num2str(flag)]);end(2)修改“初始化”子函数部分的代码;function[sys,x0,str,ts]=mdlInitializeSizes(x_initial)%%callsimsizesforasizesstructure,fillitinandconvertittoa%sizesarray.%%Notethatinthisexample,thevaluesarehardcoded.Thisisnota%recommendedpracticeasthecharacteristicsoftheblockaretypically%definedbytheS-functionparameters.%sizes=simsizes;sizes.NumContStates=1;sizes.NumDiscStates=0;sizes.NumOutputs=1;sizes.NumInputs=1;sizes.DirFeedthrough=0;sizes.NumSampleTimes=1;%atleastonesampletimeisneededsys=simsizes(sizes);%%initializetheinitialconditions%x0=x_initial;%%strisalwaysanemptymatrix%str=[];%%initializethearrayofsampletimes%ts=[00];(3)修改“计算模块导数”子函数部分的代码;functionsys=mdlDerivatives(t,x,u)dx=-x+usys=dx;(4)修改“更新模块离散状态”子函数部分代码;functionsys=mdlUpdate(t,x,u)sys=[];(5)修改“计算模块输出”子函数部分代码;functionsys=mdlOutputs(t,x,u)sys=x;(6)修改“计算下一个采样时间点”子函数部分的代码;functionsys=mdlGetTimeOfNextVarHit(t,x,u)sampleTime=1;%Example,setthenexthittobeonesecondlater.sys=t+sampleTime;(7)修改“仿真结束”子函数部分的代码。functionsys=mdlTerminate(t,x,u)sys=[];%endmdlTerminate【步骤3】将sfunction_example创建成s-function模块。打开simulink,在simulink中新建一个空白的模型窗口,拖动“User-DefinedFunction”库中的S-Function模块到其中,并相应放置输入的阶跃信号发生器和监测输出波形的示波器,如下图:【步骤4】给状态变量
本文标题:S函数设计初学者实例
链接地址:https://www.777doc.com/doc-4628613 .html