fork download
  1. format long;
  2. clc;
  3.  
  4. % Function definition
  5. f = @(x) x.^2 - 2;
  6.  
  7. % Initial guesses
  8. a = 1; % f(a) < 0
  9. b = 2; % f(b) > 0
  10.  
  11. % False Position Iteration
  12. for i = 1:10
  13. r(i) = (a*f(b) - b*f(a)) / (f(b) - f(a)); % False Position formula
  14.  
  15. % Store absolute error |b - a| at each step
  16. err(i) = abs(b - a);
  17.  
  18. % Update a or b
  19. if f(r(i)) < 0
  20. a = r(i);
  21. else
  22. b = r(i);
  23. end
  24. end
  25.  
  26. % Display root estimates
  27. disp('Root estimates at each iteration:');
  28. disp(r');
  29.  
  30. % Display absolute error at each step
  31. disp('Absolute error (|b - a|) at each iteration:');
  32. disp(err');
  33.  
Success #stdin #stdout 0.13s 46884KB
stdin
Standard input is empty
stdout
Root estimates at each iteration:
   1.333333333333333
   1.400000000000000
   1.411764705882353
   1.413793103448276
   1.414141414141414
   1.414201183431953
   1.414211438474870
   1.414213197969543
   1.414213499851323
   1.414213551646055
Absolute error (|b - a|) at each iteration:
   1.000000000000000e+00
   6.666666666666667e-01
   6.000000000000001e-01
   5.882352941176470e-01
   5.862068965517242e-01
   5.858585858585861e-01
   5.857988165680474e-01
   5.857885615251299e-01
   5.857868020304566e-01
   5.857865001486768e-01