saitodx 发表于 2008-12-29 08:15

使用s函数建立LMS算法的問題

刚学s函数, 想用s函数寫出一個如下圖的LMSBLOCK,可是结果总是不对
http://saitodx.myweb.hinet.net/LMS.JPG
其中output端口,主要需要的是error和wts所以只有設定2個output
輸入端口因為有關聯到y(n)所以我又多牽入一個input給他所以設定3個input
麻煩大家帮我看看是那里不对,多谢了


%S-function for LMS block
function = LMS(t,x,u,flag,T,w,mu)
switch flag,
case 0,
    =mdlInitializeSizes(T,w,mu);
case 1,
    sys=mdlDerivatives(t,x,u);
case 2,
    sys=mdlUpdate(t,x,u);
case 3,
    sys=mdlOutputs(t,x,u,w);
case 4,
    sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,
    sys=mdlTerminate(t,x,u);
otherwise
    error(['Unhandled flag = ',num2str(flag)]);
end
function =mdlInitializeSizes(T,w,mu)
sizes = simsizes;
sizes.NumContStates= 0;               
sizes.NumDiscStates= length(w)-1;
sizes.NumOutputs   = 2;               
sizes.NumInputs      = 3;               
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;               
sys = simsizes(sizes);
x0= zeros(length(w)-1,1);         
str = [];
ts= ;

function sys=mdlDerivatives(t,x,u)      
sys = [];
function sys=mdlUpdate(t,x,u,n)
sys(1) = ;
sys(2) = ;
sys(3) = ;
function sys=mdlOutputs(t,x,u,w)
e=-
sys(1) = w+mu**e;
sys(2)=e;

function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1;   
sys = t + sampleTime;
function sys=mdlTerminate(t,x,u)         
sys = [];
% end mdlTerminate

yufeng 发表于 2008-12-29 09:12

什么错误 另外n是什么

saitodx 发表于 2008-12-29 19:58

因為LMS的計算法是w(k+1) = w(k)+mu*x(k-1)*e(k)
其中e=error計算法是d(n)-y(n)
因為這樣會有兩個不同的迴圈,所以設定了兩個迴圈給k使用
所以n代表的是k的迴圈
所以我讓u1和u2依照n的迴圈計算,u1代表的是d(n),u2代表的是y(n),u3代表的是x(n)

用我寫的S-Function帶入程式以後會得到 :Trouble solving algebraic loop containing 'LMSF1/S-Function1' at time 0.Stopping simulation.There may be a singularity in the solution.If the model is correct, try reducing the step size (either by reducing the fixed step size or by tightening the error tolerances).
的錯誤訊息

yufeng 发表于 2008-12-30 09:06

可是你程序里面就没有n 呀 并且你这个计算方法和你的程序对不上 你应该看看s函数怎么写

jiaofirefly 发表于 2012-7-16 10:46

{:{44}:}

suyuntian 发表于 2012-12-17 16:13

haohaohahoahahoa
页: [1]
查看完整版本: 使用s函数建立LMS算法的問題