liky83672815 发表于 2008-3-14 17:29

关于S-函数的问题

function =PF_1(t,x,u,flag,Ts)
global A,B,C,D,G,H
A=;
B=;
C=;
D=;
G=exp(A*Ts);
H=quad(@fun,0,Ts);
switch flag,
    case 0,
      =mdlInitializeSizes;
    case 2,
      sys=mdlUpdate(t,x,u,Ts);
    case 3,
      sys=mdlOutputs(t,x,u,Ts);
    case 9,
      sys=[];
    otherwise,
      error=(['Unhandled flag=',num2str(flag)]);
end
function =mdlInitializeSizes
sizes=simsizes;
sizes.NumConStates=0;
sizes.NumDiscStates=1;
sizes.NumOutputs=1;
sizes.NumInputs=3;
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=1;
sys=simsizes(sizes);
x0=u(3);
str=[];
ts=;
function sys=mdlUpdates(t,x,u,Ts)
sys=G*x+H*u(2);
function sys=mdlOutPuts(t,x,u,Ts)
sys=C*G*G*x+C*G*H*u(2)+C*H*u(1);
function y=fun(t);
y=exp(A*Ts-A*t)*B;
以上是我自己编的程序,编辑S-函数模块时出现如下问题
??? Undefined function or variable "B".
Error in ==> PF_1 at 2
global A,B,C,D,G,H
高手帮我指点下怎么改谢谢了~~~~~

witty01 发表于 2008-3-19 15:17

function =PF_1(t,x,u,flag,Ts)
global A B C D G H
A=;
B=;
C=;
D=;
G=exp(A*Ts);
%H=quad(fun,0,Ts);
H=exp(A*Ts-A*t)*B;
switch flag,
    case 0,
      =mdlInitializeSizes;
    case 2,
      sys=mdlUpdate(x,u);
    case 3,
      sys=mdlOutput(x,u,C,G,H);
    case 9,
      sys=[];
    otherwise,
      error=(['Unhandled flag=',num2str(flag)]);
end
function =mdlInitializeSizes
sizes=simsizes;
sizes.NumConStates=0;
sizes.NumDiscStates=1;
sizes.NumOutputs=1;
sizes.NumInputs=3;
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=1;
sys=simsizes(sizes);
x0=u(3);
str=[];
ts=;
function sys=mdlUpdate(x,u)
sys=G*x+H*u(2);
function sys=mdlOutput(x,u,C,G,H)
sys=C*G*G*x+C*G*H*u(2)+C*H*u(1);

scdxhy 发表于 2008-3-19 18:10

我也问一个关于sfunction 的问题,请高手指点!

function =system1(t,x,u,flag)
switch flag,
    case 0,
      =mdlInitializeSizes;
    case 1,
      sys=mdlDerivatives(t,x,u);
    case 2,
      sys=mdlUpdate(t,x,u);
    case 3,
      sys=mdlOutputs(t,x,u);
    case 9,
      sys=mdlTerminate(t,x,u);
    otherwise
      error(['Unhandled flag=',num2str(flag)]);
end

function =mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates=3;
sizes.NumDiscStates=0;
sizes.NumOutputs=1;
sizes.NumInputs=1;
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=1;
sys=simsizes(sizes);
x0=[1;
    1;
    1];
str=[];
ts=;

function sys=mdlDerivatives(t,x,u)
dx(1)=-u*x(2)+x(3);
dx(2)=(u*u-1)*x(2)+u*x(3);
dx(3)=-x(3)+x(1)*x(1)+0.5;
sys=dx;

function sys=mdlUpdate(t,x,u)
sys=[];

function sys=mdlOutputs(t,x,u)
sys=x(1);
function sys=mdlTerminate(t,x,u)
sys=[];

命令窗口老是有Warning: Unable to reduce the step size without violating minimum step size of 0.01 at time 2.805106062496166.Continuing simulation with the step size restricted to 0.01 and using an effective relative error tolerance of 2.569641885883541e+040, which is greater than the specified relative error tolerance of 0.001.

还提示sfunction有问题,说是State derivatives returned by S-function 'system1' in 'fz/S-Function' during flag=1 call must be a real vector of length 3.
我实在找不出什么原因,请高手指教!

witty01 发表于 2008-3-20 08:53

这个看上去确实没什么问题,还是检查模型看看吧,

conquerv 发表于 2008-3-21 23:04

我也碰到这个问题了,请高手指点方向。谢谢!

State derivatives returned by S-function 'system1' in 'fz/S-Function' during flag=1 call must be a real vector of length 3.
页: [1]
查看完整版本: 关于S-函数的问题