fork download
  1. clc;
  2. clear all;
  3.  
  4. % Define the fixed-point function
  5. g = @(x) exp(-x);
  6.  
  7. % Initial guess
  8. a = 0;
  9.  
  10. % Fixed-Point Iteration
  11. for i = 1:10
  12. r(i) = g(a); % Compute next approximation
  13. err(i) = abs((a - r(i)) / r(i)) * 100; % Relative error (%)
  14. a = r(i); % Update guess
  15.  
  16. % Display results
  17. fprintf('Iter: %i APP_root: %.6f Error: %.5f%%\n', i, r(i), err(i));
  18.  
  19. % Stop if error is less than 0.01%
  20. if err(i) < 0.01
  21. break;
  22. end
  23. end
  24.  
Success #stdin #stdout 0.11s 46792KB
stdin
Standard input is empty
stdout
Iter: 1  APP_root: 1.000000  Error: 100.00000%
Iter: 2  APP_root: 0.367879  Error: 171.82818%
Iter: 3  APP_root: 0.692201  Error: 46.85364%
Iter: 4  APP_root: 0.500474  Error: 38.30915%
Iter: 5  APP_root: 0.606244  Error: 17.44679%
Iter: 6  APP_root: 0.545396  Error: 11.15662%
Iter: 7  APP_root: 0.579612  Error: 5.90335%
Iter: 8  APP_root: 0.560115  Error: 3.48087%
Iter: 9  APP_root: 0.571143  Error: 1.93080%
Iter: 10  APP_root: 0.564879  Error: 1.10887%