%% Animiate simple algorithms on the Rosenbrock function 

%% Compute the iterations with steepest descent
[x,f,g,hist,histx] = gradient_descent_1(...
    @rosenbrock,[0;0],'maxiter',5); % does not converge

%% Plot them
clf; cla; % clear the plot
h = ezcontour(@(x,y) (1-x)^2 + 100*(y - x^2)^2); hold on;
plot(histx(1,:), histx(2,:),'.-');
hold off;
set(h,'LevelList',logspace(-2,4,25));

%% Compute the iterations with normalized steepest descent
[x,f,g,hist,histx] = gradient_descent_2(...
    @rosenbrock,[0;0],'maxiter',100); % does not converge

%% Plot them
clf; cla; % clear the plot
h = ezcontour(@(x,y) (1-x)^2 + 100*(y - x^2)^2); hold on;
plot(histx(1,:), histx(2,:),'.-');
hold off;
set(h,'LevelList',logspace(-2,4,25));

%% Compute the iterations with exact steepest descent
[x,f,g,hist,histx] = exact_gradient_descent(...
    @rosenbrock,[0;0],'maxiter',500); % does not converge
%% Plot them
clf; cla; % clear the plot
h = ezcontour(@(x,y) (1-x)^2 + 100*(y - x^2)^2); hold on;
plot(histx(1,:), histx(2,:),'r.-','LineWidth',2);
hold off;
set(h,'LevelList',logspace(-2,4,25));


%% Compute the iterations with Newton's method
[x,f,g,H,hist,histx] = newtons_method_1(...
    @rosenbrock,[0;0],'maxiter',25); % does not converge

%% Plot them
clf; cla; % clear the plot
h = ezcontour(@(x,y) (1-x).^2 + 100.*(y - x.^2).^2); hold on;
plot(histx(1,:), histx(2,:),'r.-','MarkerSize',12,'LineWidth',2);
hold off;
set(h,'LevelList',logspace(1,4,25));

%% Compute the iterations with Newton's method
[x,f,g,H,hist,histx] = newtons_method_1(...
    @rosenbrock,5*randn(2,1),'maxiter',25); % does not converge

%% Plot them
clf; cla; % clear the plot
h = ezcontour(@(x,y) (1-x).^2 + 100.*(y - x.^2).^2); hold on;
plot(histx(1,:), histx(2,:),'r.-','MarkerSize',12,'LineWidth',2);
hold off;
set(h,'LevelList',logspace(1,4,25));
