在matlab中如何编写多输入输出的s函数 30
我在matlab/simulink环境下搭建了一个框图,其中一部分输出为一个2*1的矩阵,然后我编写了一个s函数求解输出量,输入量分别为矩阵中的两个值,输出量为2*2的矩...
我在matlab/simulink环境下搭建了一个框图,其中一部分输出为一个2*1的矩阵,然后我编写了一个s函数求解输出量,输入量分别为矩阵中的两个值,输出量为2*2的矩阵,但是在运行时系统报错说我未定义输出量u1,u2。请大大帮忙看看我该如何定义这种输出量。贴出s函数的m文件。
function [sys,x0,str,ts,simStateCompliance] = sfun1(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,
sys=mdlTerminate(t,x,u);
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [0 0];
simStateCompliance = 'UnknownSimState';
function sys=mdlUpdata(t,x,u)
sys = []
function sys=mdlOutputs(t,x,u1,u2)
c=0.0361;
p=2.9763;
l=0.7;
y1=(sin(p*l)-sinh(p*l)-(sinh(p*l)+sin(p*l))/(cosh(p*l)+cos(p*l))*(cos(p*l)-cosh(p*l)))*c;
fun1(1,1)=-sin(u1)-y1*(u2).*cos(u1);
fun1(2,1)=-sin(u1);
fun1(1,2)=cos(u1)-y1*(u2).*sin(u1);
fun1(2,2)=cos(u1);
sfun1=fun1^-1;
sys = sfun1
function sys=mdlTerminate(t,x,u)
sys = []; 展开
function [sys,x0,str,ts,simStateCompliance] = sfun1(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,
sys=mdlTerminate(t,x,u);
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [0 0];
simStateCompliance = 'UnknownSimState';
function sys=mdlUpdata(t,x,u)
sys = []
function sys=mdlOutputs(t,x,u1,u2)
c=0.0361;
p=2.9763;
l=0.7;
y1=(sin(p*l)-sinh(p*l)-(sinh(p*l)+sin(p*l))/(cosh(p*l)+cos(p*l))*(cos(p*l)-cosh(p*l)))*c;
fun1(1,1)=-sin(u1)-y1*(u2).*cos(u1);
fun1(2,1)=-sin(u1);
fun1(1,2)=cos(u1)-y1*(u2).*sin(u1);
fun1(2,2)=cos(u1);
sfun1=fun1^-1;
sys = sfun1
function sys=mdlTerminate(t,x,u)
sys = []; 展开
1个回答
展开全部
问题太多,就不用一一指正了,以下程序已经经过调试成功。
function [sys,x0,str,ts] = test_0105(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 3,
sys=mdlOutputs(t,x,u);
case {2,4,9},
sys=[];
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 3;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 0;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [];
function sys=mdlOutputs(t,x,u)
Is=u(1);angle=u(2);
pos=rem(angle,2*pi);
if pos<pi/3
Ia=Is;Ib=-Is;Ic=0;
elseif pos<2*pi/3
Ia=Is;Ib=0;Ic=-Is;
elseif pos<pi
Ia=0;Ib=Is;Ic=-Is;
elseif pos<4*pi/3
Ia=-Is;Ib=Is;Ic=0;
elseif pos<5*pi/3
Ia=-Is;Ib=0;Ic=Is;
else
Ia=0;Ib=-Is;Ic=Is;
end
sys=[Ia Ib Ic];
问题回答:
1.是的
2.Is和angle已经通过mux模块集中传送给s-function,不需要通过s-function parameter赋值。在程序里面用u(1) u(2)就可以得到输入的值
3.你的程序都是从外部计算之后送给s-function的,不用定义参数。
function [sys,x0,str,ts] = test_0105(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 3,
sys=mdlOutputs(t,x,u);
case {2,4,9},
sys=[];
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 3;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 0;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [];
function sys=mdlOutputs(t,x,u)
Is=u(1);angle=u(2);
pos=rem(angle,2*pi);
if pos<pi/3
Ia=Is;Ib=-Is;Ic=0;
elseif pos<2*pi/3
Ia=Is;Ib=0;Ic=-Is;
elseif pos<pi
Ia=0;Ib=Is;Ic=-Is;
elseif pos<4*pi/3
Ia=-Is;Ib=Is;Ic=0;
elseif pos<5*pi/3
Ia=-Is;Ib=0;Ic=Is;
else
Ia=0;Ib=-Is;Ic=Is;
end
sys=[Ia Ib Ic];
问题回答:
1.是的
2.Is和angle已经通过mux模块集中传送给s-function,不需要通过s-function parameter赋值。在程序里面用u(1) u(2)就可以得到输入的值
3.你的程序都是从外部计算之后送给s-function的,不用定义参数。
追问
朋友,那篇问题我也看过,但是我们的问题就是不一样的,你直接粘贴过来也不适合啊...
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询