LMS学习函数MATLAB代码

clear,clc
close all
P=-5:5;
d=3*P-7;
randn('state',2);
d=d+randn(1,length(d))*1.5
P=[ones(1,length(P));P]
lp.lr=0.01;
MAX=150;
ep1=0.1;
ep2=0.0001;
w=[0,0];
for i=1:MAX
    fprintf('第%d次迭代:\n',i)
    e=d-purelin(w*P);
    ms(i)=mse(e);
    ms(i)
    if(ms(i)<ep1)
        fprintf('均方差小于指定数而终止\n');
        break;
    end
    dW=learnwh([],P,[],[],[],[],e,[],[],[],lp,[]);
    if(norm(dW)<ep2)
        fprintf('权值变化小于指定数而终止\n');
        break;
    end
    w=w+dW
end
fprintf('算法收敛于:\nw=(%f,%f),MSE: %f\n',w(1),w(2),ms(i));
figure;
subplot(2,1,1);
plot(P(2,:),d,'o');title('散点与直线拟合结果');
xlabel('x');ylabel('y');
axis([-6,6,min(d)-1,max(d)+1]);
x1=-5:.2:5;
y1=w(1)+w(2)*x1;
hold on;
plot(x1,y1);
subplot(212)
semilogy(1:i,ms,'-o');
xlabel('迭代次数');ylabel('MSE');title('均方差下降曲线');

猜你喜欢

转载自blog.csdn.net/qq_24163555/article/details/83991823
今日推荐