性能优化 - 最速下降法(固定学习率)

clear;
close all;
clc;
%F = x1.^2 + 25*x2.^2;
% 赫兹矩阵
% A = [1,0;0,25];
% 可求得最大的学习率为 1/25 = 0.04,
% 当学习率大于0.04时,系统发散,小于0.04时,系统收敛
%绘制3D图
xx1 = -1: 0.05:1;
xx2 = xx1;
[x1,x2] = meshgrid(xx1,xx2);
xxsize = size(x1);
subplot(1,4,4);
x1 = x1(:);
x2 = x2(:);
F = x1.^2 + 25*x2.^2;
x1 = reshape(x1,xxsize);
x2 = reshape(x2,xxsize);
F = reshape(F,xxsize);
mesh(x1,x2,F);
zlim([0,5]);
%绘制轮廓图
subplot(1,4,1);
hold on;
syms x1 x2;
F = x1^2 + 25*x2^2;
ezplot(F - 1);
ezplot(F - 2);
ezplot(F - 3);
ezplot(F - 4);
ezplot(F - 5);
xlim([-2,2]);
ylim([-2,2]);
title('学习率为0.039');
grid on;
N = 50;%迭代次数
%F = x1.^2 + 25*x2.^2;
alf = 0.039;% 学习率
x = [1.5 , 0.5]';
xy = zeros(2,N);
for i=1:1:N
    xy(1,i) = x(1);
    xy(2,i) = x(2);
    dF = [2,50]'.*x;
    x = x - alf*dF;
end
plot(xy(1,:),xy(2,:),'r-');
%绘制轮廓图
subplot(1,4,2);
hold on;
syms x1 x2;
F = x1^2 + 25*x2^2;
ezplot(F - 1);
ezplot(F - 2);
ezplot(F - 3);
ezplot(F - 4);
ezplot(F - 5);
xlim([-2,2]);
ylim([-2,2]);
title('学习率为0.041');
grid on;
N = 50;%迭代次数
%F = x1.^2 + 25*x2.^2;
alf = 0.041;% 学习率
x = [1.5 , 0.5]';
xy = zeros(2,N);
for i=1:1:N
    xy(1,i) = x(1);
    xy(2,i) = x(2);
    dF = [2,50]'.*x;
    x = x - alf*dF;
end
plot(xy(1,:),xy(2,:),'r-');

%绘制轮廓图
subplot(1,4,3);
hold on;
syms x1 x2;
F = x1^2 + 25*x2^2;
ezplot(F - 1);
ezplot(F - 2);
ezplot(F - 3);
ezplot(F - 4);
ezplot(F - 5);
xlim([-2,2]);
ylim([-2,2]);
title('学习率为0.01');
grid on;
N = 50;%迭代次数
%F = x1.^2 + 25*x2.^2;
alf = 0.01;% 学习率
x = [1.5 , 0.5]';
xy = zeros(2,N);
for i=1:1:N
    xy(1,i) = x(1);
    xy(2,i) = x(2);
    dF = [2,50]'.*x;
    x = x - alf*dF;
end
plot(xy(1,:),xy(2,:),'r-');

 

posted @ 2017-01-21 18:49  理舞  阅读(634)  评论(0编辑  收藏  举报