这是我用导纳增量法编写的S函数,老是运行不成功。希望大家帮我找出来。这里的u(1)、u(2)分别为电流和电压。x(3)为参考电压并设它的初始值为0.45
function [sys,x0,str,ts] =incCond(t,x,u,flag,d)
golbal x(3);
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 2,
sys=mdlUpdate(t,x,u,x(3),d);
case 3,
sys=mdlOutputs(t,x,u,x(3),d);
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,
sys=mdlTerminate(t,x,u);
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 3;
sizes.NumOutputs = 1;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [0;0;0.45];
str = [];
ts = [0.2 0];
function sys=mdlDerivatives(t,x,u)
sys = [];
function sys=mdlUpdate(t,x,u)
sys = [u(1);u(2);x(3)];
function sys=mdlOutputs(t,x,u,d)
di=u(1)-x(1);
dv=u(2)-x(2);
if dv==0
if di==0
dd=0;
else
if di>0
dd=d;
else
dd=-d;
end
end
else
s=u(1)+u(2)*(u(1)-x(1))/(u(2)-x(2));
if s==0
dd=0;
else if s<0
dd=-d;
else
dd=d;
end
end
end
x(3)=x(3)+dd;
sys =x(3);
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1;
sys = t + sampleTime;
function sys=mdlTerminate(t,x,u)
sys = [];