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;
x = x1;
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.