simulink自定义模块的S函数运行时出现“变量未定义”错误
simulink中自定义一个模块,模块的功能是通过两个输入变量来计算得到一个输出。运行时报错“Errorin'bldcpwm/S-Function'whileexecut...
simulink 中自定义一个模块,模块的功能是通过两个输入变量来计算得到一个输出。运行时报错“
Error in 'bldcpwm/S-Function' while executing M-File S-function 'BEMF', flag = 3 (output), at time 0. MATLAB error message:
Undefined function or variable 'w' ”
Simulink 连接图如下:
程序如下:
function [sys,x0,str,ts] = BEMF(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u,w,Pos);
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,
sys=mdlTerminate(t,x,u);
% Unexpected flags %
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% end BEMF
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 = 1;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [0 0];
function sys=mdlDerivatives(t,x,u)
sys = [];
function sys=mdlUpdate(t,x,u)
sys = [];
function [Ea,Eb,Ec]=mdlOutputs(t,x,u,w,Pos)
if pos<pi/3
Ea=k*w;
Eb=-k*w;
Ec=k*w*((-Pos)/(pi/6)+1);
elseif pos<2*pi/3
Ea=k*w;
Eb=k*w*((Pos-pi/3)/(pi/6)-1);
Ec=-k*w;
elseif pos<pi
Ea=k*w*((pi*2/3-Pos)/(pi/6)+1);
Eb=k*w;
Ec=-k*w;
elseif pos<4*pi/3
Ea=-k*w;
Eb=k*w;
Ec=k*w*((Pos-pi)/(pi/6)-1);
elseif pos<5*pi/3
Ea=-k*w;
Eb=k*w*((4*pi/3-Pos)/(pi/6)+1);
Ec=k*w;
else
Ea=k*w*((Pos-5*pi/3)/(pi/6)-1);
Eb=-k*w;
Ec=k*w;
end
sys = [Ea Eb Ec];
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1;
sys = t + sampleTime;
function sys=mdlTerminate(t,x,u)
sys = []; 展开
Error in 'bldcpwm/S-Function' while executing M-File S-function 'BEMF', flag = 3 (output), at time 0. MATLAB error message:
Undefined function or variable 'w' ”
Simulink 连接图如下:
程序如下:
function [sys,x0,str,ts] = BEMF(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u,w,Pos);
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,
sys=mdlTerminate(t,x,u);
% Unexpected flags %
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% end BEMF
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 = 1;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [0 0];
function sys=mdlDerivatives(t,x,u)
sys = [];
function sys=mdlUpdate(t,x,u)
sys = [];
function [Ea,Eb,Ec]=mdlOutputs(t,x,u,w,Pos)
if pos<pi/3
Ea=k*w;
Eb=-k*w;
Ec=k*w*((-Pos)/(pi/6)+1);
elseif pos<2*pi/3
Ea=k*w;
Eb=k*w*((Pos-pi/3)/(pi/6)-1);
Ec=-k*w;
elseif pos<pi
Ea=k*w*((pi*2/3-Pos)/(pi/6)+1);
Eb=k*w;
Ec=-k*w;
elseif pos<4*pi/3
Ea=-k*w;
Eb=k*w;
Ec=k*w*((Pos-pi)/(pi/6)-1);
elseif pos<5*pi/3
Ea=-k*w;
Eb=k*w*((4*pi/3-Pos)/(pi/6)+1);
Ec=k*w;
else
Ea=k*w*((Pos-5*pi/3)/(pi/6)-1);
Eb=-k*w;
Ec=k*w;
end
sys = [Ea Eb Ec];
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1;
sys = t + sampleTime;
function sys=mdlTerminate(t,x,u)
sys = []; 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询