How to overcome reaching NaN in trying to write Secant Method?
Show older comments
Hello,
My friend and I are trying to write a code that will represent Secant Method for finding roots for functions.
After receiving x0, x1 (initial approximations), the number of iterations and the function from the user, this is how we wrote the code:
while i <= iter
x = x1-y1*(x1-x0)/(y1-y0);
i = i+1;
if isnan(x)
x = x1;
break
end
x0=x1; x1=x;
y0=f(x0); y1=f(x1);
end
We put an "if isnan(x)" because if not x turns out to be NaN with many iterations. The problem is that by doing so, we are actually limiting the number of iterations and cannot reach a more approximate value for the root.
We think this happens because as iterations proceed, x1 and x0 (as well as y1 and y0) become very close, to the point of 0/0.
Do you have any idea about how can we execute the function without limiting the number of iterations, and without getting to NaN?
Thanks in advance.
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!