利用matlab编写S函数求解微分方程Word格式.docx
- 文档编号:13266119
- 上传时间:2022-10-09
- 格式:DOCX
- 页数:15
- 大小:23.84KB
利用matlab编写S函数求解微分方程Word格式.docx
《利用matlab编写S函数求解微分方程Word格式.docx》由会员分享,可在线阅读,更多相关《利用matlab编写S函数求解微分方程Word格式.docx(15页珍藏版)》请在冰豆网上搜索。
y'
=y-2x/y
y(0)=1
要求利用matlab编写S函数求解
三、设计内容(可加附页)
【步骤1】获取状态空间表达式。
在matlab中输入
dsolve(‘Dy=y-2*x/y'
'
y(0)=1'
,'
x'
)
得到
y=(2*x+1).^(1/2);
【步骤2】建立s函数的m文件。
利用21·
用S函数模板文件。
以下是修改之后的模板文件sfuntmpl.m的内容。
function[sys,x0,str,ts]=sfuntmpl(t,x,u,flag)
%SFUNTMPLS-function
M-fileGeneral
template
defineyoucanWith%M-fileS-functions,
youownordinarydifferential
systemequations(ODEs),discrete%
equations,and/orjustabout
anytypeofalgorithmtobeusedwithina%
Simulinkblockdiagram.
%
%ThegeneralformofanM-FileS-function
syntaxis:
%[SYS,X0,STR,TS]=
SFUNC(T,X,U,FLAG,P1,...,Pn)
givenis%WhatreturnedbySFUNCata
pointintime,T,dependsonthe
valueoftheFLAG,thecurrentstatevector,%
X,andthecurrent
inputvector,U.%
%FLAGRESULT
DESCRIPTION
-----
------%
--------------------------------------------
[SIZES,X0,STR,TS]%0
Initialization,returnsystemsizesinSYS,
stateinitial%
inX0,stateorderingstrings
inSTR,and%
sampletimesinTS.
Return
%1DX
continuousstatederivativesinSYS.
%2DSUpdate
discretestatesSYS=X(n+1)
%3YReturn
outputsinSYS.
%4TNEXTReturn
nexttimehitforvariablestepsample
%timein
SYS.
%5Reserved
forfuture(rootfinding).
%9[]Termination,
performanycleanupSYS=[].
%Thestatevectors,XandX0consistsof
continuousstatesfollowed
%bydiscretestates.
%Optionalparameters,P1,...,Pncanbe
providedtotheS-functionand
%usedduringanyFLAGoperation.
%WhenSFUNCiscalledwithFLAG=0,
thefollowinginformation
%shouldbereturned:
%SYS
(1)=Numberofcontinuousstates.
%SYS
(2)=Numberofdiscretestates.
%SYS(3)=Numberofoutputs.
%SYS(4)=Numberofinputs.
%Anyofthefirstfour
elementsinSYScanbespecified
%as-1indicatingthattheyare
dynamicallysized.The
%actuallengthforallother
flagswillbeequaltothe
%lengthoftheinput,U.
%SYS(5)=Reservedforrootfinding.
Mustbezero.
%SYS(6)=Directfeedthroughflag
(1=yes,0=no).Thes-function
%hasdirectfeedthroughifU
isusedduringtheFLAG=3
%call.Settingthisto0isakin
tomakingapromisethat
%Uwillnotbeusedduring
FLAG=3.Ifyoubreakthepromise
%thenunpredictableresults
willoccur.
%SYS(7)=Numberofsampletimes.
ThisisthenumberofrowsinTS.
%X0=Initialstateconditionsor[]
ifnostates.
%STR=Stateorderingstrings
whichisgenerallyspecifiedas[].
%TS=Anm-by-2matrix
containingthesampletime
%(period,offset)information.
Wherem=numberofsample
%times.Theorderingofthe
sampletimesmustbe:
%TS=[00,:
Continuoussampletime.
%01,:
Continuous,butfixedinminorstep
sampletime.
%PERIODOFFSET,:
Discretesampletimewhere
PERIOD>
0&
OFFSET<
PERIOD.
%-20];
:
Variablestepdiscretesampletime
whereFLAG=4isusedtogettimeof
nexthit.
%Therecanbemorethanone
sampletimeproviding
%theyareorderedsuchthat
theyaremonotonically
%increasing.Onlytheneeded
sampletimesshouldbe
%specifiedinTS.When
specifyingthanone
%sampletime,youmust
checkforsamplehitsexplicitlyby
%seeingif
abs(round((T-OFFSET)/PERIOD)-
(T-OFFSET)/PERIOD)
specifiedaiswithin%
tolerance,generally1e-8.This
toleranceisdependentupon%
yourmodel'
ssamplingtimes
andsimulationtime.%
Youcanalsospecifythatthe%
sampletimeoftheS-function
isinheritedfromthedriving%
block.Forfunctionswhich
steps,minorchange%during
thisisdoneby
%specifyingSYS(7)=1and
TS=[-10].Forfunctionswhich
%areheldduringminorsteps,
thisisdonebyspecifying
%SYS(7)=1andTS=[-11].
%Copyright1990-2002TheMathWorks,
Inc.
%$Revision:
1.18$
%Thefollowingoutlinesthegeneralstructure
ofanS-function.
switchflag,
%%%%%%%%%%%%%%%%%%
%Initialization%
case0,
[sys,x0,str,ts]=mdlInitializeSizes;
%%%%%%%%%%%%%%%
%Derivatives%
case1,
sys=mdlDerivatives(t
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 利用 matlab 编写 函数 求解 微分方程