Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. function [root,Rt,itr,p] = nrm(fun,x0,acc)
  2.  
  3. % Start guess x
  4. x = x0;
  5. % Step, h, for derivate
  6. h = 0.05;
  7.  
  8. err = inf;
  9. errPrev = 0;
  10. konv = 0;
  11. itr = 0;
  12.  
  13. % While error is bigger than desired error
  14. while(err > acc)
  15.  
  16. % Count iterations
  17. itr = itr+1;
  18.  
  19. % y = function value of x
  20. y = fun(x);
  21. % f'(x)
  22. der = (fun(x+h)-fun(x))/h;
  23. % Next x value to test
  24. xnext = x - (y/der);
  25. % Method independent error estimate
  26. err = (abs(y)/abs(der));
  27. konv = konv + errPrev/err;
  28. errPrev = err;
  29. % Next x value to test
  30. x = xnext;
  31.  
  32. end
  33.  
  34. % Done with iterations
  35. root = x;
  36. Rt = err;
  37. p = log2(konv/(itr-1));
  38.  
  39. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement