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.6`36;
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.6`36
.
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.6`36;
ϵ = 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}}
;
. A space indicates multiplication. Even after fixing that, though, the result is indeterminate. What are you trying to do? $\endgroup$fx/(fx - 2*fy)
becomes indeterminate as bothx
andy
numerically approachLog[2]
(presumably the solution). You should probably fix your algorithm to avoid computing that quantity. $\endgroup$x
doesnt give higher precision. You should dox=0.6`35
. After that you do not needSetAccuracy
, the extended precision will follow since the initialx
is the only floating number in the problem. $\endgroup$