Getting errors Power::infy and Infinity::indet while running a For-loop

My program gives the above-mentioned errors when I run the loop more than 3 times. It is a 4th order iterative method of Ostrowski family.

Plot[Exp[x] - 2, {x, -5, 5}]

fx = Exp[x] - 2;
f1x = Exp[x];
fy = Exp[y] - 2;
fxn1 = Exp[xn1] - 2;
x = 0.6'35;
xnv = {};
fxn = {fx};
For[i = 1, i <= 4,

y = x - (fx*f1x)/(f1x^2 + fx^2);

xn1 = x - 1/2*(fx*f1x)/(f1x^2 + fx^2)*(1 + fx/(fx - 2*fy));
AppendTo[xnv, xn1]

x = xn1;
AppendTo[fxn, fx]
i++
]
Print["value of x is equal to ", x]
coc = Log[Abs[fxn[[3]]/fxn[[2]]]]/Log[Abs[fxn[[2]]/fxn[[1]]]];
Print["coc is equal to ", coc]
Quit[]

• Instructions within the for look need to be separated by ;. A space indicates multiplication. Even after fixing that, though, the result is indeterminate. What are you trying to do? Commented Jun 22, 2017 at 15:13
• This is a fourth order iterative method and I have to find root of the equation with accuracy of 35 significant figures. Commented Jun 22, 2017 at 16:15
• the quantity fx/(fx - 2*fy) becomes indeterminate as both x and y numerically approach Log[2] (presumably the solution). You should probably fix your algorithm to avoid computing that quantity. Commented Jun 22, 2017 at 17:16
• side comment, stringing a bunch of zeros on the initial x doesnt give higher precision. You should do x=0.635. After that you do not need SetAccuracy, the extended precision will follow since the initial x is the only floating number in the problem. Commented Jun 22, 2017 at 17:18
• Ohhh....I'll try that. Thanks Commented Jun 23, 2017 at 15:28

You get those error messages because your algorithm converges to x == Log[2] so fast that you really don't want to iterate four times.

Let's look at your code in a cleaner form.

fx[x_] := Exp[x] - 2
f1x[x_] := Exp[x]
y[x_] := x - (fx[x]*f1x[x])/(f1x[x]^2 + fx[x]^2)
xn[x_] :=
x - 1/2*(fx[x]*f1x[x])/(f1x[x]^2 + fx[x]^2)*(1 + fx[x]/(fx[x] - 2*fx[y[x]]))

x0 = 0.636;
Module[{x, xv, fv},
xv = {x0};
fv = {fx[x0]};
For[i = 1, i <= 4, i++,
x = xn[xv[[i]]];
AppendTo[xv, x]; AppendTo[fv, fx[x]]];
Transpose @ {xv, fv}]


Infinity::indet: Indeterminate expression 0.*10^-34 ComplexInfinity encountered.
Power::infy: Infinite expression 1/0.*10^-34 encountered.

{{0.600000000000000000000000000000000000, -0.17788119960949102512463233183713549},
{0.69311715333638072739341638011885481, -0.00006005354550403350594508637314563},
{0.6931471805599453090446452647720226, -7.451737133723080*10^-19},
{0.6931471805599453094172321214581766, 0.*10^-34},
{Indeterminate, Indeterminate}}


You can see convergence was complete on the 3rd iteration, but your For-loop didn't stop there.

Here is some code that will solve your problem in Mathematica's functional style and which will automatically detect the convergence.

next[{x_, _}] := Module[{u = xn[x]}, {u, fx[u]}]
FixedPoint[next, {x0, fx[x0]}, SameTest -> (Abs[#2[[2]]] < 10^-30 &)] // First

0.6931471805599453094172321214581766


Also note that to carry out a high precision computation, all I had to do was define x0 as a high-precision, inexact number with x0 = 0.636.

Update

The modified For-loop version of the OP's code shown above only requires a test for convergence to be added to eliminate the errors messages. Here is the code with such a test installed.

x0 = 0.636;
ϵ = 10^-30;
Module[{x, f, xv, fv},
xv = {x0};
fv = {fx[x0]};
For[i = 1, i <= 4, i++,
x = xn[xv[[i]]];
f = fx[x];
AppendTo[xv, x]; AppendTo[fv, f];
If[Abs[f] < ϵ, Break[]]];
Transpose @ {xv, fv}]

{0.600000000000000000000000000000000000, -0.17788119960949102512463233183713549},
{0.69311715333638072739341638011885481, -0.00006005354550403350594508637314563},
{0.6931471805599453090446452647720226, -7.451737133723080*10^-19},
{0.6931471805599453094172321214581766, 0.*10^-34}}

• My problem isn't to about finding the solution of the equation but rather about how to program an iterative method, that is why I can't use FixedPoint. Thanks for the help though Commented Jun 23, 2017 at 15:27
• @user49702. The For-loop I wrote only needs to apply the convergence test I used in the FixedPoint example to get rid of the error messages. I show how to do install such a test in the update I just made. Commented Jun 23, 2017 at 20:42