% We compute numerically the order of convergence the sequence x_{n+1} =\cos \left(x_n \right).

format long
N = 20;
m = 10;
x = zeros(N+m,1); %initialize the first N+m terms of the sequence x_1, x_2,..., x_(m+N)
x(1) = 0; %initial guess
for k = 1:(N+m-1)
    x(k+1) = g(x(k));
end

% Define the errors \epsilon_n =|x_n -x_* |\approx |x_n -x_{n-1} |.

eps = zeros(N+m,1);
for k = 2:(N+m)
    eps(k) = abs(x(k) - x(k-1));
end

% Make vector X and vector Y:

X = log(eps(m:(m+N-1)));
Y = log(eps((m+1):(m+N)));
f = fit(X,Y,'poly1')
plot(f,X,Y,'*')

%
% Define functiong for the fixed point problem x=g\left(x\right):

function y = g(x)
    y = cos(x);
end