function [x_k,f_k,k,xx,yy,dd,dd1] = sd_ros()

% tol: stopping criterion on the norm of gradient
tol = 1e-5;

% maxit: maximum number of iterations
maxit=10000;

% x0: initial guess
% x0=[1.2;1.2];
x0=[-1.2;1];
x_k=x0;
id_n=diag([1,1]);

xx=0*(1:maxit);
yy=0*(1:maxit);
dd=0*(1:maxit);
dd1=dd;

% Initial guess for the Hessian is the identity matrix.
H_k=id_n;

for k=1:maxit
    
    % Calculation of the cost function.
    [f_k,g_k,~] = fun(x_k);
    p_k = -H_k*g_k;
    % p_k=-g_k/norm(g_k);

    % Optimum Step length, initial guess for Step Length is 1:
    a_k = lsa(x_k,p_k,1);
    x_k_new = x_k+a_k*p_k;
    
    s_k=x_k_new-x_k;
    
    [~,g_k_new,~] = fun(x_k_new);
    y_k=g_k_new-g_k;
    rho_k=1/dot(y_k,s_k);
    V_k=id_n-rho_k*y_k*(s_k');
    
    H_k_new=(V_k')*H_k*V_k+rho_k*(s_k*s_k');
    
    dd(k)=dot(y_k,s_k);
        
    x_k=x_k_new;
    H_k=H_k_new;
    
    ll=eig(H_k);
    dd1(k)=min(ll);
    
    xx(k)=x_k(1);
    yy(k)=x_k(2);
    

    
    
    if(mod(k,10)==0)
        display(strcat('Cost Function:',num2str(f_k)))
        display(strcat('a=',num2str(a_k)))
    end
    
    if norm(g_k) < tol
        display(strcat('Convergence Reached: |\nabla f|=',...
            num2str(norm(g_k))))
        break;
    end

end

end