马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
我自己写了个最小二乘法辨识电机参数的程序,将s-function名称导入模块是出错,one or more output arguments not assigned,是真么原因,另外s-function中可以声明全局变量吗?下面是程序:
function [sys,x0,str,ts] = dsfunc(t,x,u,flag)
% An example M-file S-function for defining a discrete system.
% This S-function implements discrete equations in this form:
% x(n+1) = Ax(n) + Bu(n)
% y(n) = Cx(n) + Du(n)
%
% Generate a discrete linear system:
global y1;
global out Qn Pn Kn Q1;
out=[0;0;0;0;0];
Qn=[0 0 0 0 0 ];
Pn=10^5*eye(5);
switch flag,
case 0
sys = mdlInitializeSizes(); % Initialization
case 2
sys = mdlUpdate(t,x,u); % Update discrete states
case 3
sys = mdlOutputs(t,x,u); % Calculate outputs
case {1, 4, 9} % Unused flags
sys = [];
otherwise
error(['unhandled flag = ',num2str(flag)]); % Error handling
end
% End of dsfunc.
%==============================================================
% Initialization
%==============================================================
function [sys,x0,str,ts] = mdlInitializeSizes()
% Call simsizes for a sizes structure, fill it in, and convert it
% to a sizes array.
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 9;
sizes.NumOutputs = 5;
sizes.NumInputs = 9;
sizes.DirFeedthrough = 1; % Matrix D is nonempty.
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = ones(9,1); % Initialize the discrete states.
str = []; % Set str to an empty matrix.
ts = [0.01 0]; % sample time: [period, offset]
% End of mdlInitializeSizes.
%==============================================================
% Update the discrete states
%==============================================================
function sys = mdlUpdates(t,x,u)
y1=u(5)-u(3)*u(9);
Kn=Pn*Qn/(1+Qn'*Pn*Qn);
% End of mdlUpdate.
%==============================================================
% Calculate outputs
%==============================================================
function sys = mdlOutputs(t,x,u)
Q1=[u(3);u(6);u(1);u(8)+u(9)*u(7);u(9)*u(2)];
out=out+Kn(y1-Qn'*out);
Pn=Pn-(Pn*Qn*Qn'*Pn)/(1+Qn'*Pn*Qn);
Qn=[Qn' Q1]';
sys=out;
% End of mdlOutputs. |