请教S函数的问题
请教一下,S函数能实现多数入多输出的功能吗?我毕设做的是模糊自适应,有个功能快要用到s函数,输入e(连续),We,ke,(离散),输出E(离散,
如果可以几个关键参数该如何设置,请指点
本人QQ271281808 看这个帖子http://forum.vibunion.com/forum/thread-43106-1-1.html
下面有个连接
希望大家问问题时候先搜索下,这个S函数很多帖子得,希望能得到你要得。 先谢谢了,我了解matlab比较少,理解的比较肤浅最近急需 所以比较急,以后一定加倍努力,还希望你继续指点一下
你给的帖子我看过了,我实现的功能比较简单但和那位的不同
我有几个问题问下
1.我能用在s函数中用到我说的那几个参数吗?我遍的时候提示变量未定义
2.如果我用u1,u2,u3分别代替e,We,ke 希望实现ke(k)=ke(k)+u2*u3*ke0;sys=ke*u1;
具体如下
function =area_sel(t,x,u,flag)
switch flag,
case 0,
=mdlInitializeSizes;
case 3,
sys=mdlOutputs(t,x,u);
case 9,
sys=[];
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates= 0;
sizes.NumDiscStates= 0;
sizes.NumOutputs = 1;
sizes.NumInputs = 3;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0= [];
str = [];
ts = ;
ke0=1;
function sys=mdlOutputs(t,x,u)
ke(k)=ke(k-1)+u3*u2*ke0;
sys=ke*u1;
还是提示错误,请帮忙纠正一下啊,谢谢 输入e(连续),We,ke,(离散),
三个都是输入?没有要求别的?
ke(k)=ke(k)+u2*u3*ke0;sys=ke*u1;你的k要变化?没有用到状态变量吧.
你的ke0是什么意思?是不是ke(0)?就是ke(k)的初始值你想? 输入是e(连续),We,还有M,不是ke,这两个都是离散的,ke0是可ke(k)的初始值,
我以为ke只是一个中间变量,要用它来求输出的,如果将它理解为状态
如何实现呢? :@L 继续补充下,我最开始表述错了,输入就是e,We,M,就是要实现ke(k-1)=ke(k)+We*M*ke0,sys=ke(K)*e;
[ 本帖最后由 insectking 于 2007-5-17 22:20 编辑 ] 用状态变量来写
mdlUpdate里面写这个式子
ke(k-1)=ke(k)+We*M*ke0
mdlOutputs写这个式子
sys=ke(K)*e;
当然具体的要相应去改。
因为你这里涉及到了连续变量和离散变量2种
看下该例子
function = dsfunc(t,x,u,flag)
%DSFUNC An example M-file S-function for defining a discrete system.
% Example M-file S-function implementing discrete equations:
% x(n+1) = Ax(n) + Bu(n)
% y(n) = Cx(n) + Du(n)
%
% See sfuntmpl.m for a general S-function template.
%
% See also SFUNTMPL. % Copyright (c) 1990-1998 by The MathWorks, Inc. All Rights Reserved.
% $Revision: 1.13 $
% Generate a discrete linear system:
A=[-1.3839 -0.5097 1.0000 0]; B=[-2.5559 0 0 4.2382];
C=[ 0 2.0761 0 7.7891]; D=[ -0.8141 -2.9334 1.2426 0];
switch flag,
% Initialization
case 0, = mdlInitializeSizes(A,B,C,D);
% Update % %%%%%%%%%%
case 2, sys = mdlUpdate(t,x,u,A,B,C,D); %%%%%%%%%%
% Output % %%%%%%%%%%
case 3, sys = mdlOutputs(t,x,u,A,C,D); %%%%%%%%%%%%%
% Terminate % %%%%%%%%%%%%%
case 9, sys = []; % do nothing %%%%%%%%%%%%%%%%%%%%
% Unexpected flags % %%%%%%%%%%%%%%%%%%%%
otherwise, error(['unhandled flag = ',num2str(flag)]);
end %end dsfunc
%
%=======================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=======================================================================
%
function = mdlInitializeSizes(A,B,C,D)
sizes = simsizes;
sizes.NumContStates = 0; sizes.NumDiscStates = size(A,1); sizes.NumOutputs = size(D,1);
sizes.NumInputs = size(D,2); sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 1;
sys = simsizes(sizes); x0 = ones(sizes.NumDiscStates,1); str = []; ts = ;
% end mdlInitializeSizes
%
%=======================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=======================================================================
% function sys = mdlUpdate(t,x,u,A,B,C,D)
sys = A*x+B*u; %end mdlUpdate
%
%=======================================================================
% mdlOutputs
% Return Return the output vector for the S-function
%=======================================================================
%
function sys = mdlOutputs(t,x,u,A,C,D)
sys = C*x+D*u; %end mdlUpdate
S-FUNCTIONS的书写之四(离散和连续的混合型)
function = mixedm(t,x,u,flag)
%MIXEDM An example integrator followed by unit delay M-file S-function
% Example M-file S-function implementing a hybrid system consisting
% of a continuous integrator (1/s) in series with a unit delay (1/z).
% Sampling period and offset for unit delay.
dperiod = 1; doffset = 0;
switch flag
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0, =mdlInitializeSizes(dperiod,doffset);
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1, sys=mdlDerivatives(t,x,u);
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 2, sys=mdlUpdate(t,x,u,dperiod,doffset);
%%%%%%%%%%
% Output %
%%%%%%%%%%
case 3, sys=mdlOutputs(t,x,u,doffset,dperiod);
%%%%%%%%%%%%%
% Terminate %
%%%%%%%%%%%%%
case 9, sys = [];
% do nothing
otherwise, error(['unhandled flag = ',num2str(flag)]);
end % end mixedm %
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function =mdlInitializeSizes(dperiod,doffset)
sizes = simsizes; sizes.NumContStates = 1; sizes.NumDiscStates = 1;
sizes.NumOutputs = 1; sizes.NumInputs = 1; sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 2; sys = simsizes(sizes); x0 = ones(2,1); str = [];
ts = [0 0; % sample time
dperiod doffset];
% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives % Compute derivatives for continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u)
sys = u; % end mdlDerivatives
%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step % requirements.
%=============================================================================
%
function sys=mdlUpdate(t,x,u,dperiod,doffset)
% next discrete state is output of the integrator
if abs(round((t - doffset)/dperiod) - (t - doffset)/dperiod) < 1e-8, sys = x(1);
else sys = []; end % end mdlUpdate
%
%=============================================================================
% mdlOutputs % Return the output vector for the S-function
%=============================================================================
%
function sys=mdlOutputs(t,x,u,doffset,dperiod)
% Return output of the unit delay if we have a
% sample hit within a tolerance of 1e-8. If we
% don't have a sample hit then return [] indicating
% that the output shouldn't change.
if abs(round((t - doffset)/dperiod) - (t - doffset)/dperiod) < 1e-8, sys = x(2); else sys = []; end % end mdlOutputs 关于这些程序的说明,我将在后面讲解。
S-Function书写之五
在本帖中我对前面列举的关于系统是连续,离散,连续与离散混合的三个例子加以说明,以至于大家在看下面的例子时能更好的理解。
一.函数mdlInitializeSizes
对于描述连续型的函数csfunc.m
sizes.NumContStates = 2; sizes.NumDiscStates = 0;
$表明本函数是描述连续型的
sizes.NumOutputs = 2; sizes.NumInputs = 2;
sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 1;
对于描述离散型的函数dsfunc.m
sizes.NumContStates = 0; sizes.NumDiscStates = size(A,1);
$表明本函数是描述离散型的
sizes.NumOutputs = size(D,1); sizes.NumInputs = size(D,2);
sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 1;
对于描述混合型的函数mixedm.m
sizes.NumContStates = 1; sizes.NumDiscStates = 1;
$表明本函数是描述混合型的
sizes.NumOutputs = 1; sizes.NumInputs = 1;
sizes.DirFeedthrough = 0; sizes.NumSampleTimes = 2;
关于变量sizes.numoutputs和sizes.numinputs则由描述的系统的输出,输入而定。
关于变量sizes.DirFeedthrough,则可以查看矩阵D,如果D是非空,则为1,否则为0
关于变量sizes.NumSampleTimes,即是ts这个矩阵的行数。
二.函数mdlDerivatives
仅仅出现在含有连续型的系统中;这部分应该根据系统状态来决定。
三.函数mdlUpdate 仅仅出现在含有离散型的系统中;该函数应该根据系统状态来决定
四.函数mdlOutputs 该函数在各种类型中都有,应该根据描述系统的输出来决定; 今天弄了一天,经过多次调试,现在程序如下
function =area_sel(t,x,u,flag,ke)
switch flag
case 0,
=mdlInitializeSizes;
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case 9,
sys=[];
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates= 0;
sizes.NumDiscStates= 0;
sizes.NumOutputs = 3;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 =[];
str = [];
ts= [-1 0];
ke0=1;
function sys=mdlUpdate(t,x,u)
ke(k)=ke(k-1)+m*We*ke0;
function sys=mdlOutputs(t,x,u)
sys=ke(k)*e;
这样系统没有提示错误,但是我还有几点不清楚
sizes.NumContStates= 0;
sizes.NumDiscStates= 0;是不是说明了此系统并没有用到状态变量?但是ke应该算是吧,这样如何理解状态量的选取
还有一个问题是关于if 语句嵌套的,同http://forum.vibunion.com/thread-42705-1-1.html遇到的问题类似,也是提示
This statement is incomplete.我个人认为S函数里面对这个句型有严格要求的,END不能和function语句结束冲突,我是按照提示错误一步一步更正最后通过的
在这里先谢谢你了,接下来通过SIMULINK仿真如果还有问题还希望继续指教 可能诸如% x(n+1) = Ax(n) + Bu(n)
% y(n) = Cx(n) + Du(n) 此类的才叫状态变量
而且你想啊,如果ke(k)=ke(k-1)+m*We*ke0;叫状态变量,那么是不是没有状态变量的就不能update呢?明显是错的,呵呵
sizes.NumDiscStates= 0;说明了此系统并没有用到状态变量,无论离散的还是连续的,ke只能是离散系统的一个update吧。
至于end与function的冲突倒不是特明显,只是觉得习惯不好,至于究竟有无冲突,还没研究过,当时也觉得是那里的问题,但实践证明他的问题是sizes.NumOutputs和sizes.NumInputs的问题。
S函数还是博大精深的,虽然有固定的格式。
PS:不用客气的,大家相互学习,通过你这个帖子,我也学到了一些东西。也谢谢你的回来分析。
页:
[1]