I have written a function to compute the fixed point iteration of $f(x) = x^2 - 3x + 2 = 0$ using $g(x)=\sqrt{3x-2}$. I know that it has linear convergence with constant 0.75, however in my code I am not able to get a similar result for the convergence. I compute my convergence rate as follows: $p = \log{\left( \frac{e_{i+1}}{e_i} \right)} \Big/ \log{\left( \frac{e_{i}}{e_{i-1}} \right)}$
def convergence_rate(x_k,x_0,x_1): #using 2 because that is the root I am finding
return math.log(abs(x_k-2.0)/abs(x_0-2.0))/math.log(abs(x_0-2.0)/abs(x_1-2.0))
def g2(initial,eps=10**10, iterations=100):
x_k = initial
x_k_1 = math.sqrt((3.0*x_k)-2.0)
i = 0
x_0, x_1, x_2 = (0,0,0)
while f(x_k) != 0 and i < iterations:
x_0, x_1, x_2 = (x_k, x_0, x_1)
x_k_1 = math.sqrt((3.0*x_k)-2.0)
x_k = x_k_1
i+=1
print(convergence_rate(x_k,x_0,x_1)) #gives 1.0065524232348098
return x_k #gives 2.0000000000001177
Your result seems to be right, you get $1$ as order of convergence, which is linear convergence. To get the basis of the geometric series of the error, you need to print also the fractions $e_{i-1}/e_i$.
returning in 20th step
returning the expected factor close to $0.75$.