I have no knowledge to solve your problem from a mathematical point of view, but from a programming point of view there is a point that I could notice.
Indeed, you are right. It takes too many iterations until it reaches the result:
Elapsed time is 6.487824 seconds. count = 292945
When you determine the step size to achieve the final result, the step length must be optimized. Otherwise, you either approach the answer too slowly, or go through the optimal answer several times and zigzag around it, because the length of your step is too long.
To optimize the step size, I first create a function according to your script (plus some minor modifications):
function [solution, count, T] = SDhilb(d, step) h = hilb(d); tic b = ones(d, 1); solution = zeros(d, 1); residual = h * solution - b; res2 = residual' * residual; tol = 10^(-6); count = 0; while res2 > tol; roe = res2/(residual' * h * residual); solution = solution - step * roe * residual; % here the step size appears residual = h * solution - b; count = count + 1; res2 = residual' * residual; % let calculate this once per iteration end T = toc;
Now, using this function for a range of steps (0.1: 0.1: 1.3) and a pair of Hilbert matrices (d = 2, 3, 4, 5), it is obvious that 1 not an effective step size:

Instead, step = 0.9 seems a lot more efficient. let's see how effective it is in the case of hilb(5) :
[result, count, T] = SDhilb(5, 0.9) result = 3.1894 -85.7689 481.4906 -894.8742 519.5830 count = 1633 T = 0.0204
This is more than two orders of magnitude faster.
Similarly, you can try different tol values ββto see how dramatic this can affect speed. In this case, there is no optimal value: the smaller the tolerance, the more time you need to wait.