% % In this example, we use the fixed-point iteration to solve % cos x = x % % Set the initial guess for x, TOL, N x = pi/3; TOL = 1e-8; N = 100; % Set the iteration index i = 1; % set the initial error err = 1; % start the iteration while ( i<=N & err>TOL ) i = i+1; x = cos(x); err = abs( cos(x)-x ); end % Print the result if ( err<TOL ) fprintf('Found the root %12.10f after %d iterations.\n', x, i-1); else fprintf('Method failed after %d iterations', i); end
Found the root 0.7390851272 after 45 iterations.