%
% Steepest Descent Method
%

clear all;
close all;

tol = 1e-6; 
IterMax = 20;

A = [2 1; 1 3];    % 等高线比较扁
xt = [1; 1];
b = A*xt; 
 
% initial guess
x0 = [-3; 0.5]; 
% x0 = [1;1] + [-2.552; 1.577]; % x0 = xt + q, 其中 q 是近似特征向量 

% SD iteration 
n = length(b);
x = x0; 
xx = x; % store the iterate solution

r = b - A*x; 
r0norm = norm(r);
if abs(r0norm)<1e-16, return, end

q = A * r;
for k = 1 : IterMax
    alpha = r'*r / (r'*q);
    x = x + alpha * r;
    xx = [xx,x];
    r = r - alpha*q;
    relres = norm(r) / r0norm;
    fprintf('k=%d, x=[%.4f,%.4f], relres=%.2e\n', k,x(1),x(2),relres);
    if relres < tol
        break
    end
    q = A * r;
end

figure
% f = @(x,y) 1/2*[x y] * A * [x; y] - [x y]*b;
f = @(x,y) x.*x + x.*y + 3/2*y.*y - 3*x - 4*y;

hold on 
set(gcf,'position',[50,50,900,900]);
fc = fcontour(f,[-5,5,-5,5],'b-','linewidth',1);
fc.LevelList = [-3.4 -2.3 0 3 8 15 25 50];
plot([-5,5],[0,0],'r-', [0,0],[-5,5],'r-')
% plot(1,1,'sb','markersize',10,'linewidth',2)
% text(-0.4,-0.2,'\it O','fontsize',20)
text([-4:-1]-0.2,[zeros(4,1)]-0.2, num2cell(-4:-1),'fontsize',20)
text([1:4]-0.1,[zeros(4,1)]-0.2, num2cell(1:4),'fontsize',20)
text([zeros(4,1)]-0.3,[-4:-1], num2cell([-4:-1]),'fontsize',20)
text([zeros(4,1)]-0.2,[1:4], num2cell([1:4]),'fontsize',20)
text(-0.2,-0.2, {0}, 'fontsize', 20)
set(gca,'fontsize',20)
grid on
axis equal
xticks([-5:5]); yticks([-5:5]);
set(gca,'YTickLabel',[],'XTickLabel',[]);

plot(xx(1,:),xx(2,:),'*--r','linewidth',1,'markersize',10)
hold off
