Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [root,Rt,itr,p] = nrm(fun,x0,acc)
- % Start guess x
- x = x0;
- % Step, h, for derivate
- h = 0.05;
- err = inf;
- errPrev = 0;
- konv = 0;
- itr = 0;
- % While error is bigger than desired error
- while(err > acc)
- % Count iterations
- itr = itr+1;
- % y = function value of x
- y = fun(x);
- % f'(x)
- der = (fun(x+h)-fun(x))/h;
- % Next x value to test
- xnext = x - (y/der);
- % Method independent error estimate
- err = (abs(y)/abs(der));
- konv = konv + errPrev/err;
- errPrev = err;
- % Next x value to test
- x = xnext;
- end
- % Done with iterations
- root = x;
- Rt = err;
- p = log2(konv/(itr-1));
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement