% Solve the nonlinear equation
%        2x - tan(x) = 0
% using different fixed point iterations
% first does not converge, second converges linearly, third converges
% quadratically (it's Newton written as fixed point method)

xx = 0:0.01:1.3;
ff = 2*xx - tan(xx);
plot(xx,ff,'Linewidth', 3);
axis tight

x = 1.1;
for i=1:10
    % different fixed point iterations
    x = 0.5 * tan(x);
    %x = atan(2*x);
    %x = x-(2*x-tan(x))/(1-tan(x)^2);
    fprintf('Iteration %4d: %2.14f\n', i, x);
 %   if (abs(2*x-tan(x))<1e-10)
 %       fprintf('Converged after %d iterations.\n', i)
 %       break;
 %   end
end


% plotting of fixed point functions
plot(xx,xx,'k--');  hold on
plot(xx,0.5*tan(xx),'Linewidth', 2);
plot(xx,atan(2*xx),'Linewidth', 2);
plot(xx,xx-(2*xx-tan(xx))./(1-tan(xx).^2),'Linewidth', 2);
legend('x=y', 'Method 1', 'Method 2', 'Method 3', 'Location', 'South')
axis([0,1.3,0,1.3])