I have this sacent method function. it works fine while testing it.
function [root, ea, iter] = secant(f, xi, xs, es, maxit) iter=0; while(true) root = xi - (f(xi) * (xi-xs))/(f(xi)-f(xs)); xs = xi; xi=root; iter=iter+1; if root~=0 ea= abs((xi-xs)/xi)*100; end if iter >=maxit || ea<=es break; end endend
But when i call it like following it only returns 1st iteration values.
[root, ea, iter]= secant(@(x)x^2-9,2,5,0,.01,5)
Output:
root = 2.7143ea = 26.316iter = 1
But original output:
root = 3.0000ea = 0.0010005iter = 5
What am i doing wrong?
Best Answer