利用matlab编写S函数求解微分方程自动化专业综合设计报告自动化专业综合设计报告函数求解微S编写设计题目:利用matlab分方程自动化系统仿真实验室所在实验室:郭卫平指导教师:律迪迪学生姓名200990519114 班级文自0921 学号成绩评定:自动化专业综合设计报告一、设计目的了解使用simulink的扩展工具——S-函数,s函数可以利用matlab的丰富资源,而不仅仅局限于simulink提供的模块,而用c或c++等语言写的s函数还可以实现对硬件端口的操作,还可以操作windows API 等的,它的魅力在于完美结合了simulink 框图简洁明快的特点和编程灵活方便的优点,提供了增强和扩展sinulink能力的强大机制,同时也是使用RTW实现实时仿真的关键。
二、设计要求求解解微分方程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)%SFUNTMPL S-functionM-file Generaltemplatedefine you can With % M-fileS-functions,you own ordinary differential system equations (ODEs),discrete %equations, and/or just about any type of algorithm to be used within a %Simulink block diagram.%自动化专业综合设计报告% The general form of an M-File S-functionsyntax is:% [SYS,X0,STR,TS] =SFUNC(T,X,U,FLAG,P1,...,Pn)%given is % What returned by SFUNC at apoint in time, T, depends on the value of the FLAG, the current state vector, %X, and the currentinput vector, U. %%% FLAG RESULT DESCRIPTION----------- %--------------------------------------------[SIZES,X0,STR,TS] % 0 Initialization, return systemsizes in SYS,state initial % in X0, state ordering stringsin STR,and %sample times in TS.Return% 1 DX自动化专业综合设计报告continuous state derivatives inSYS.% 2 DSUpdatediscrete states SYS = X(n+1)% 3 YReturnoutputs in SYS.% 4 TNEXTReturnnext time hit for variable step sample% timeinSYS.% 5 Reservedfor future (root finding).% 9 [] Termination,perform any cleanup SYS=[].%%% The state vectors, X and X0 consists ofcontinuous states followed% by discrete states.%% Optional parameters,P1,...,Pn can beprovided to the S-function and% used during any FLAG operation.自动化专业综合设计报告%% When SFUNC is called with FLAG = 0,the following information% should be returned:%% SYS(1) = Number of continuous states.% SYS(2) = Number of discrete states.% SYS(3) = Number of outputs. % SYS(4) = Number of inputs. % Any of the first fourelements in SYS can be specified % as -1 indicating that they aredynamically sized. The% actual length forall otherflags will be equal to the% length of the input, U.% SYS(5) = Reserved for root finding.Must be zero.% SYS(6) = Direct feedthrough flag(1=yes, 0=no). The s-function% has direct feedthrough if Uis used during the FLAG=3自动化专业综合设计报告% call. Setting this to 0 is akinto making a promise that% U will not be used duringFLAG=3. If you break the promise % then unpredictable resultswill occur.% SYS(7) = Number of sample times.This is the number of rows in TS. %%% X0 = Initial state conditions or []if no states.%% STR = State ordering stringswhich is generally specified as [].%% TS = An m-by-2 matrix containing the sample time% (period, offset) information.Where m = number of sample% times. The ordering of thesample times must be:自动化专业综合设计报告%% TS = [00, :Continuous sample time.% 01, :Continuous, but fixed in minor step% sample time.% PERIOD OFFSET, :Discrete sample time where% PERIOD > 0 & OFFSET < PERIOD.% -20]; :Variable step discrete sample time%where FLAG=4 is used to get time of% next hit.%% There can be more than onesample time providing% they are ordered such thatthey are monotonically自动化专业综合设计报告% increasing. Onlythe neededsample times should be% specified in TS. Whenspecifying than one% sample time, you mustcheck for sample hits explicitly by% seeing if%abs(round((T-OFFSET)/PERIOD) -(T-OFFSET)/PERIOD)specified a is within % tolerance, generally 1e-8. This tolerance is dependentupon %your model's sampling timesand simulation time. % %You can also specify thatthe %sample time of the S-functionis inherited from thedriving %block. For functions whichsteps, minor change % duringthis is done by自动化专业综合设计报告% specifying SYS(7) =1 andTS = [-1 0]. For functions which % are held during minor steps,this is done by specifying% SYS(7) = 1 and TS = [-1 1].% Copyright 1990-2002 The MathWorks,Inc.% $Revision: 1.18 $%% The following outlines the general structureof an S-function.%switch flag,%%%%%%%%%%%%%%%%%%% Initialization %%%%%%%%%%%%%%%%%%%case 0,[sys,x0,str,ts]=mdlInitializeSi zes;自动化专业综合设计报告%%%%%%%%%%%%%%%% Derivatives %%%%%%%%%%%%%%%%case 1,sys=mdlDerivatives(t,x,u);%%%%%%%%%%% Update %%%%%%%%%%%case {2,3,9},sys=[];%%%%%%%%%%%% Outputs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % GetTimeOfNextVarHit % %%%%%%%%%%%%%%%%%%%%%%%自动化专业综合设计报告%%%%%%%%%%%%%% Terminate %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Unexpected flags %%%%%%%%%%%%%%%%%%%%%otherwiseerror(['Unhandled flag = ',num2str(flag)]);end% end sfuntmpl%%=============================================================================自动化专业综合设计报告% mdlInitializeSizes% Return the sizes, initial conditions, andsample times for the S-function. %=============================================================================%function[sys,x0,str,ts]=mdlInitializeSi zes%% call simsizes for a sizes structure, fill it in andconvert it to a% sizes array.%% Note that in this example, the values are hardcoded. This is not a% recommended practice as the characteristicsof the block are typically% defined by the S-function parameters.%sizes = simsizes;自动化专业综合设计报告sizes.NumContStates = 1; sizes.NumDiscStates = 0; sizes.NumOutputs = 0;sizes.NumInputs = 0;sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 1; % at least onesample time is neededsys = simsizes(sizes);%% initialize the initial conditions%= [0 1.0000]; x0%% str is always an empty matrix %str = [];%% initialize the array of sample times自动化专业综合设计报告%ts = [0 0];% end mdlInitializeSizes%%============================== =============================== ================% mdlDerivatives% Return the derivatives for the continuousstates.%============================== =============================== ================%functionsys=mdlDerivatives(t,x,u)sys(1)=(u-2*x/u)*t+u;【步骤3】利用matlab调用函数得到结果。