自动化专业综合设计报告
设计题目:利用matla b编写S函数求解微分方程
所在实验室:自动化系统仿真实验室
指导教师:***
学生姓名律迪迪
班级文自0921学号************
成绩评定:
一、设计目的
了解使用sim ulink的扩展工具——S-函数,s函数可以利用m atlab的丰富资源,而不仅仅局限于si muli nk提供的模块,而用c或c++等语言写的s函数还可以实现对硬件端口的操作,还可以操作wi ndowsA PI等的,它的魅力在于完美结合了si m ulink框图简洁明快的特点和编程灵活方便的优点,提供了增强和扩展sinul ink能力的强大机制,同时也是使用R TW实现实时仿真的关键。
二、设计要求
求解解微分方程
y’=y-2x/y
y(0)=1
要求利用mat lab编写S函数求解
三、设计内容(可加附页)
【步骤1】获取状态空间表达式。
在matlab中输入
dsolve(‘Dy=y-2*x/y’,’y(0)=1’,’x’)
得到
y=(2*x+1).^(1/2);
【步骤2】建立s函数的m文件。
利用21·用S函数模板文件。
以下是修改之后的模板文件s funtmpl.m的内容。
functio n [sys,x0,str,ts] = sfuntmp l(t,x,u,flag)
%SFUNTMP L General M-file S-functio n templat e
% With M-file S-functio ns, you can defineyou own ordinar y differe ntial
% equatio ns (ODEs), discret e systemequatio ns, and/or just about
% any type of algorit hm to be used withina Simulin k block diagram.
%
% The general form of an M-File S-functio n syntaxis:
% [SYS,X0,STR,TS] = SFUNC(T,X,U,FLAG,P1,...,Pn)
%
% What is returne d by SFUNC at a given point in time, T, depends on the
% value of the FLAG, the current state vector, X, and the current
% input vector, U.
%
% FLAG RESULTD ESCRIP TION
% ----- ------ --------------------------------------------
% 0 [SIZES,X0,STR,TS] Initial izatio n, returnsystemsizes in SYS,
% initial state in X0, state orderin g strings
% in STR, and sampletimes in TS.
% 1 D X Returncontinu ous state derivat ives in SYS.
% 2 D S Updatediscret e statesSYS = X(n+1)
% 3 Y Returnoutputs in SYS.
% 4 T NEXT Returnnext time hit for variabl e step sample
% time in SYS.
% 5 Reserve d for future(root finding).
% 9 [] Termina tion, perform any cleanup SYS=[].
%
%
% The state vectors, X and X0 consist s of continu ous statesfollowe d
% by discret e states.
%
% Optiona l paramet ers, P1,...,Pn can be provide d to the S-functio n and
% used duringany FLAG operati on.
%
% When SFUNC is calledwith FLAG = 0, the followi ng informa tion
% shouldbe returne d:
%
% S YS(1) = Numberof continu ous states.
% S YS(2) = Numberof discret e states.
% S YS(3) = Numberof outputs.
% S YS(4) = Numberof inputs.
% Any of the first four element s in SYS can be specifi ed
% as -1 indicat ing that they are dynamic ally sized. The
% actuallengthfor all other flags will be equal to the
% lengthof the input, U.
% S YS(5) = Reserve d for root finding. Must be zero.
% S YS(6) = Directfeedthr ough flag (1=yes, 0=no). The s-functio n
% has directfeedthr ough if U is used duringthe FLAG=3
% call. Setting this to 0 is akin to makinga promise that
% U will not be used duringFLAG=3. If you break the promise
% then unpredi ctable results will occur.
% S YS(7) = Numberof sampletimes. This is the numberof rows in TS. %
%
% X0 = Initial state conditi ons or [] if no states.
%
matlab定义函数表达式% S TR = State orderin g strings which is general ly specifi ed as [].
%
% T S = An m-by-2 matrixcontain ing the sampletime
% (period, offset) informa tion. Where m = numberof sample
% times. The orderin g of the sampletimes must be:
%
% TS = [0 0, : Continu ous sampletime.
% 0 1, : Continu ous, but fixed in minor step
sampletime.
%
% PERIODOFFSET, : Discret e sampletime where
% PERIOD> 0 & OFFSET< PERIOD.
% -2 0]; : Variabl e step discret e sampletime
% where FLAG=4 is used to get time of % next hit.
%
% There can be more than one sampletime providi ng
% they are ordered such that they are monoton ically
% increas ing. Only the neededsampletimes shouldbe
% specifi ed in TS. When specify ing than one
% sampletime, you must check for samplehits explici tly by
% seeingif
% abs(round((T-OFFSET)/PERIOD) - (T-OFFSET)/PERIOD)
% is withina specifi ed toleran ce, general ly 1e-8. This
% toleran ce is depende nt upon your model's samplin g times
% and simulat ion time.
%
% You can also specify that the sampletime of the S-functio n
% is inherit ed from the driving block. For functio ns which
% changeduringminor steps, this is done by
% specify ing SYS(7) = 1 and TS = [-1 0]. For functio ns which
% are held duringminor steps, this is done by specify ing
% SYS(7) = 1 and TS = [-1 1].
% Copyrig ht 1990-2002 The MathWor ks, Inc.
% $Revisio n: 1.18 $
%
% The followi ng outline s the general structu re of an S-functio n.
%
switchflag,
%%%%%%%%%%%%%%%%%%
% Initial izatio n %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts]=mdlInit ialize Sizes;
%%%%%%%%%%%%%%%
% Derivat ives %
%%%%%%%%%%%%%%%
case 1,
sys=mdlDeri vative s(t,x,u);
%%%%%%%%%%
% Update%
%%%%%%%%%%
case {2,3,9},
sys=[];
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
% GetTime OfNext VarHit %
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
% Termina te %
%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
% Unexpec ted flags %
%%%%%%%%%%%%%%%%%%%%
otherwi se
error(['Unhandl ed flag = ',num2str(flag)]);
end
% end sfuntmp l
%
%==================================================================== =========
% mdlInit ialize Sizes
% Returnthe sizes, initial conditi ons, and sampletimes for the S-functio n.
%==================================================================== =========
%
functio n [sys,x0,str,ts]=mdlInit ialize Sizes
%
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论