马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
我的M文件是这样的
function [sys,x0,str,ts] = kalman(t,x,u,flag)
switch flag,
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
case 2
sys=mdlUpdates(t,x,u);
case 3
sys=mdlOutputs(t,x,u);
case {1,4,9}
sys=[];
otherwise
error(['Unhandled flag =',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes(FEstmtx,Hmtx,Cmtx);
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 5;
sizes.NumOutputs = 2;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 =zeros(5,1);
str = [];
ts = [1 0];
function sys=mdlUpdates(t,x,u,FEstmtx,Hmtx,Cmtx)
x(1)=Ialfa;x(2)=Ibeta;x(3)=Phira;x(4)=Phirb;x(5)=wr
STEP=1;%步长值
koff=12000;%开始采样
ts=0.001;%仿真输入采样周期
tsest=STEP*ts;%估计采样周期
N=1000;
%MAXSAMPLES=floor(max(size(wr)-koff)/STEP):%最大采样值
Rs=0.7384:Ls=0.127145:Lm=0.1241:Rr=0.7403:Lr=0.127145;%电机定值设定
%PMTXINIT=10:%初始状态估算误差协方差值
%QMTXINIT=1;%过程噪声协方差阵
%RMTXINIT=0.001:%测量噪声协方差阵
%计算矩阵
U=zeros(2,1);Y=U;A=zeros(5,5):C=[1 0 0 0 0;0 1 0 0 0]
%Xestmtx=[0;0;0 ;0]
PP=[10,0,0,0,0;
0,10,0,0,0;
0,0,10,0,0;
0,0,0,10,0;
0,0,0,0,10];
Q=[1,0,0,0,0;
0,1,0,0,0;
0,0,1,0,0;
0,0,0,1,0;
0,0,0,0,200];
RR=[0.001 0;0 0.001];K=zeros(5,2)
%开始参数估计循环
for k=1:N;
sample=k*STEP+koff%采样时间
U(1)=Va(sample);U(2)=Vb(sample);Y(1)=Ia(sample);Y(2)=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
%根据扩展卡尔曼滤波算法计算
P=G*PP*G'+RR;
K=P*H'*inv(H*P*H'+RR);
x=x+K*(Y-C*x);
PP=P+K*H*P;
%end
%保存变量
Ialfa(k+1)=x(1);
Ibeta(k+1)=x(2);
phira(k+1)=x(3);
phirb(k+1)=x(4);
wr(k+1)=x(5);
end
function sys=mdlOutputs(t,x,u,A,B,C)
sys=x(5);
S-FUNCTION是这样的
提示错误信息Error evaluating parameter 'FunctionName' in block 'resistor1/S-Function': Undefined variable 'myekf' or class 'myekf.m'
[ 本帖最后由 tyler 于 2007-9-9 10:35 编辑 ] |