fork download
  1. f = @(x) exp(-x) - x;
  2. df = @(x) -exp(-x) - 1;
  3.  
  4. % Initial guess
  5. a = 0;
  6.  
  7. % Number of iterations
  8. n = 5;
  9.  
  10. % Newton-Raphson Iteration
  11. for i = 1:n
  12. r = a - (f(a) / df(a));
  13. tol(i) = abs(a - r);
  14.  
  15. % Display root at each step
  16. disp(['Iteration ', num2str(i), ': Root estimate = ', num2str(r)]);
  17.  
  18. a = r;
  19. end
  20.  
  21. disp(['Final root estimate after ', num2str(n), ' iterations:']);
  22. disp(r);
  23.  
  24. disp('Error (tolerance) at each iteration:');
  25. disp(tol);
  26. # your code goes here
Success #stdin #stdout 0.14s 47440KB
stdin
Standard input is empty
stdout
Iteration 1: Root estimate = 0.5
Iteration 2: Root estimate = 0.56631
Iteration 3: Root estimate = 0.56714
Iteration 4: Root estimate = 0.56714
Iteration 5: Root estimate = 0.56714
Final root estimate after 5 iterations:
 0.56714
Error (tolerance) at each iteration:
   5.0000e-01   6.6311e-02   8.3216e-04   1.2537e-07   2.8866e-15