📄 电机s函数.txt
字号:
function[sys,x0,str,ts]=model_sfunc(t,x,u,flag)
% MODEL_SFUNC A M-file S-function for defining a system of inductor motor
model_parameter; % model_paramerer.m
A=K*[-Rs*Lr,w*Lm^2,Rr*Lm,w*Lm*Lr
-w*Lm^2,-Rs*Lr,-w*Lm*Lr,Rr*Lm
Rs*Lm,-w*Lm*Ls,-Rr*Ls,-w*Lr*Ls
w*Lm*Ls,Rs*Lm,w*Lr*Ls,-Rr*Ls];
B=K*[Lr,0;0,Lr;-Lm,0;0,-Lm];
C=[1,0,0,0;0,1,0,0];
D=zeros(2,2);
%
% dispatch the flag
%
switch flag,
case 0
[sys,x0,str,ts]=mdlInitializeSizes; % Initialization
case 1
sys=mdlDerivatives(t,x,u); % Calculate derivatives
case 3
sys=mdlOutputs(t,x,u); % Calculate outputs
case{2,4,9}
sys=[ ]; % Unused flags
otherwise
error(['Unhandled flag=',num2str(flag)]); % Error handling
end % end model_sfunc
%
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%
function [sys,x0,str,ts]=mdlInitializeSizes
sizes=simsizes;
sizes.NumContStates=5;
Sizes.NumDiscStates=0;
Sizes.NumOutputs=6;
Sizes.NumInputs=3;
Sizes.DirFeedthrough=1; % Matrix D is nonempty
Sizes.NumSampleTimes=1;
sys=simsizes(sizes);
x0=[ 1.800,6.530,-2.350,-0.970,600 ]; % initialize the initial conditions
str=[] ; % str is an empty matrix.
Ts=[0,0] % Initialize the array of sample times
% end mdlInitializeSizes
%
% mdlDerivatives Return the derivatives for the continuous ststes.
%
function sys=mdlDerivatives(t,x,u)
Sys(1:4)=A*X(1:4)+B*u;
Te=np*Lm*(X(2)*X(3)-X(4)*X(1));
w=X(5);
Sys(5)=((Te-u(3))*np-D*X(5))/J;
% end mdlDerivatives.
%
% mdlOutputs Return the block outputs
%
Function sys=mdlOutputs(t,x,u)
sys(1:2)=X(1:2);
sys(3)=Ls*X(1)+Lm*X(3);
sys(4)=Ls*X(2)+Lm*X(4);
sys(5)=X(5);
sys(6)=np*Lm*(X(2)*X(3)-X(4)*X(1));
% end mdlOutputs.
%交流异步电动机参数M文件:
[model_parameter.m]
Rs=0.687; % stator resistance(Ω)
Rr=0.642; % rotor resistance(Ω)
Ls=0.084; % stator inductance(H)
Lr=0.852; % rotor inductance(H)
Lm=0.813; % mutual inductance(h)
W=1400; % real speed(rpm)
Np=2; % pole number
J=0.3; % moment of inertia(kgmm)t
D=0.01; % torque damp coefficien
K=1/(Ls*Lr-Lm*Lm);
% end
function [sys,x0,str,ts]=skdj(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
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 = 7;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1;
sizes.NumInputs = 7;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = zeros(7,1);
str = [];
ts = [0 0];
function sys=mdlDerivatives(t,x,u)
Pa=2;
La=0.1561;
Ra=2.650;
Lar=0.15557;
Rar=2.521;
Lma=01451;
Pb=3;
Lb=0.1383;
Rb=3.400;
Lbr=0.1380;
Rbr=2.9200;
Lmb=0.1274;
J=0.2;
Tm=0;
Rc=0.05;
A=[La 0 0 0 Lma 0 0;0 La 0 0 0 Lma 0;0 0 Lb 0 -Lmb 0 0;0 0 0 Lb 0 Lmb 0;Lma 0 -Lmb 0 Lar+Lbr 0 0;0 Lma 0 Lmb 0 Lar+Lbr 0;0 0 0 0 0 0 1];
B=[1 0 0 0 0 0;0 1 0 0 0 0;0 0 1 0 0 0;0 0 0 1 0 0;0 0 0 0 0 0;0 0 0 0 0 0;0 0 0 0 0 0];
C=[-Ra Pa*La*x(7) 0 0 0 Pa*Lma*x(7) 0;-Pa*La*x(7) -Ra 0 0 -Pa*Lma*x(7) 0 0;0 0 -Rb Pb*Lb*x(7) 0 Pb*Lmb*x(7) 0;0 0 -Pb*x(7)*Lb -Rb Pb*x(7)*Lmb 0 0;0 0 0 0 -Rar-Rbr 0 0;0 0 0 0 0 -Rar-Rbr 0;0 0 0 0 0 0 -Rc/J];
x=[x(1);x(2);x(3);x(4);x(5);x(6);x(7)];
u=[u(1);u(2);u(3);u(4);0;0];
sys=inv(A)*C*x+inv(A)*B*u;
function sys=mdlOutputs(t,x,u)
Pa=2;
La=0.1561;
Ra=2.650;
Lar=0.15557;
Rar=2.521;
Lma=01451;
Pb=3;
Lb=0.1383;
Rb=3.400;
Lbr=0.1380;
Rbr=2.9200;
Lmb=0.1274;
J=0.2;
Tm=0;
Rc=0.05;
Te=Pa*Lma*(x(2)*x(5)-x(6)*x(1))+Pb*Lmb*(-x(4)*x(5)-x(6)*x(3));
sys=(Te-Tm-Rc*x(7))/J;
磁链模型
function [sys,x0,str,ts] = sflux(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case {2,4,9}
sys=[];
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% end sfuntmpl
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2;
sizes.NumInputs = 4;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [0 0];
% end mdlInitializeSizes
% mdlDerivatives
% Return the derivatives for the continuous states.
%=======================================
%输入:u(1)=Uαs,u(2)=Uβs,u(3)=Iαs,u(4)=Iβs
%输出:sys(1)=vαs,sys(2)ψβ
%状态变量:x(1)=ψαs,x(2)=ψβs
% 电机参数:Rs,Pn
function sys=mdlDerivatives(t,x,u)
Rs=0.1165;
sys(1)=u(1)-Rs*u(3);
sys(2)=u(2)-Rs*u(4);
% end mdlDerivatives
%=============================
% mdlOutputs
% Return the block outputs.
%=============================
function sys=mdlOutputs(t,x,u)
sys(1)=x(1);
sys(2)=x(2);
% end mdlOutputs
首先:
sizes.NumContStates = 0;
如果连续状态的要改为2
其次
sizes.DirFeedthrough = 1要改为0
因为你的输出没有直馈
再次
x0 = [];是不是要改为[0,0]
最后
检查输入是不是四个,输出是不是两个
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -