function [x_k,f,k,xx,yy] = sd_ros()

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

% maxit: maximum number of iterations
maxit=10000;

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

% BT LS parameters:
c1=0.1;
rho=0.8;

% x0: initial guess for the SD method:
% x0=[1.2;1.2];
x0=[-1.2;1];
x_k=x0;

k=1;
while 1
    
    xx(k)=x_k(1);
    yy(k)=x_k(2);
    % Calculation of the cost function.  Here, fun is the cost 
    % function, this is defined in a separate Matlab routine and is 
    % called here.  The Hessian is known for this problem, that is 
    % why it is returned here.

    [f_k,g_k,Hessian] = fun(x_k);
    
    % Descent Direction:  The Hessian is known for this problem 
    % so I can use the full Newton method:
    
    p_k = -(Hessian^(-1))*g_k;
    % p_k=-g_k/norm(g_k);
    
    % Optimum Step length, initial guess for Step Length is 1:
    f=f_k;
    a_k=1;
    while(f>f_k+c1*a_k*dot(p_k,g_k))
        a_k=a_k*rho;
        f=fun(x_k+a_k*p_k);
    end
    
    x_k_new=x_k+a_k*p_k;
    x_k=x_k_new;
    
    if(mod(k,10)==0)
        display(strcat('Cost Function:',num2str(f)))
        display(strcat('a=',num2str(a_k)))
    end
    
    if norm(g_k) < tol
        display(strcat('Convergence Reached: |\nabla f|=',...
            num2str(norm(g_k))))
        break;
    end
    
    if(k == maxit)
        disp('Maximum number of iteration reached');
        break;
    end
    
    k=k+1;
end

end