function update_history(obj, indx, theta_proj, f, df_proj)
% Update history of position differences & gradient differences for subfunction indx.;

    if obj.eval_count(indx) > 1 % if there exist at least one earlier measurement from this;
    % subfunction, then use it to compute position & gradient differences.;
        ddf = df_proj - obj.last_df(:,indx);       % difference in gradient;
        ddt = theta_proj - obj.last_theta(:,indx); % difference in position;
        lddf = sqrt(sum(ddf.^2)); % length of gradient change vector (l_2 norm);
        lddt = sqrt(sum(ddt.^2)); % length of position change vector (l_2 norm);

        corr_ddf_ddt = ddf.' * ddt / (lddt*lddf);
        % Update the corresponding history according to the following cases:
        if obj.display > 3 && corr_ddf_ddt < 0
            fprintf('Negative inner product of dgradient and dtheta >> add it anyway.');
        end
        if lddt > obj.eps || lddf > obj.eps || abs(corr_ddf_ddt) > obj.eps %% change in theta, gradient and inner product isn't very small
            obj.hist_deltatheta(:,2:end,indx) = obj.hist_deltatheta(:,1:end-1,indx); % shift the history by one timestep;
            obj.hist_deltatheta(:,1,indx) = ddt; % store the difference in theta since the subfunction was last evaluated;
            obj.hist_deltadf(:,2:end,indx) = obj.hist_deltadf(:,1:end-1,indx); % shift the history by one timestep;
            obj.hist_deltadf(:,1,indx) = ddf; % store the change in gradient since the subfunction was last evaluated;
        end
    end

    obj.last_theta(:,indx) = theta_proj;
    obj.last_df(:,indx) = df_proj;
    obj.hist_f(indx,2:end) = obj.hist_f(indx,1:end-1);
    obj.hist_f(indx,1) = f;
end
