function [best_x,best_E,x_vec,E_vec]=mySA0()

% Function for computing the global minimum of:
%
% y=sin(x)/(x^2+10);

% Initialize x:
x=5;

maxit_outer = 250;   % Maximum Number of Iterations
maxit_inner = 15;    % Maximum Number of Sub-iterations

T0 = 1;         % Initial Temperature
alpha = 0.95;       % Temperature Reduction Rate

% Initialize temperature:
T=T0;

% Initial energy:
E=my_cost_fn(x);

best_E=E;
best_x=x;

ctr=1;
x_vec=0*(1:maxit_outer*maxit_inner);
E_vec=0*(1:maxit_outer*maxit_inner);

for k_outer=1:maxit_outer

    for k_inner = 1:maxit_inner

        % Generate new guess using a normal distribution:
        x_new=normrnd(x, 2*T, 1);
        E_new=my_cost_fn(x_new);

        delta_E=E_new-E;

        % If delta_E<=0, then accept the new guess with probability 1.
        if(delta_E<=E)
            x=x_new;
            E=E_new;
        else
            % If delta_E>0, then accept the new guess with a probability
            % exp(-delta_E/T).
            
            prob = exp(-delta_E/T);
            if(rand <= prob)
                x = x_new;
                E = E_new;
            end
        end

        if(E<best_E)
            best_E=E;
            best_x=x;
        end

        x_vec(ctr)=x;
        E_vec(ctr)=E;
        ctr=ctr+1;

    end

    % Update the temperature:
    T = alpha*T;
    % T=5/(log(k_outer)+1);

    display(strcat('outer iteration=',num2str(k_outer),'; best value=',num2str(best_E),'; best x=',num2str(best_x)));

end

end

% *************************************************************************

function y=my_cost_fn(x)
    % y=(x-1)^2
    y=sin(x)/(x^2+10);
end