%
% 收敛阶
%
clear all; 
close all;

f  = @(x) x^2 - 3;
g1 = @(x) x^2 - 3 + x;
g2 = @(x) x - (x^2 - 3)/4;
g3 = @(x) 0.5*(x + 3/x);

xt = sqrt(3); % 真解

n  = 8;  % 设置迭代步数
x0 = 2;  % 迭代初始值
x1 = x0; 
x2 = x0; 
x3 = x0;
 
err2_old = abs(x2 - xt);
err3_old = abs(x3 - xt);

for k = 1 : n
    x1 = g1(x1);
    x2 = g2(x2);
    x3 = g3(x3);
    err1 = abs(x1 - xt);
    err2 = abs(x2 - xt);
    err3 = abs(x3 - xt);
    fprintf('k = %d,\t err1 = %.4e \n', k, err1);
    fprintf('       \t err2 = %.4e, order = %.2f\n', err2, log(err2)/log(err2_old));
    fprintf('       \t err3 = %.4e, order = %.2f\n', err3, log(err3)/log(err2_old));
    err2_old = err2; err3_old = err3;
end

