声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2411|回复: 5

[控制系统类] 使用s函数建立LMS算法的問題

[复制链接]
发表于 2008-12-29 08:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
刚学s函数, 想用s函数寫出一個如下圖的LMSBLOCK,可是结果总是不对

                               
登录/注册后可看大图

其中output端口,主要需要的是error和wts所以只有設定2個output
輸入端口因為有關聯到y(n)所以我又多牽入一個input給他所以設定3個input
麻煩大家帮我看看是那里不对,多谢了


%S-function for LMS block
function [sys,x0,str,ts] = LMS(t,x,u,flag,T,w,mu)
switch flag,
  case 0,
    [sys,x0,str,ts]=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 [sys,x0,str,ts]=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  = [T 0];

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

function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1;   
sys = t + sampleTime;
function sys=mdlTerminate(t,x,u)         
sys = [];
% end mdlTerminate
回复
分享到:

使用道具 举报

发表于 2008-12-29 09:12 | 显示全部楼层
什么错误 另外n是什么
 楼主| 发表于 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).
的錯誤訊息
发表于 2008-12-30 09:06 | 显示全部楼层
可是你程序里面就没有n 呀 并且你这个计算方法和你的程序对不上 你应该看看s函数怎么写
发表于 2012-7-16 10:46 | 显示全部楼层
发表于 2012-12-17 16:13 | 显示全部楼层
haohaohahoahahoa
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-16 04:37 , Processed in 0.050775 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表