1
$\begingroup$

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[]
$\endgroup$
5
  • $\begingroup$ 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? $\endgroup$
    – MarcoB
    Jun 22, 2017 at 15:13
  • $\begingroup$ This is a fourth order iterative method and I have to find root of the equation with accuracy of 35 significant figures. $\endgroup$
    – user49702
    Jun 22, 2017 at 16:15
  • $\begingroup$ 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. $\endgroup$
    – george2079
    Jun 22, 2017 at 17:16
  • $\begingroup$ side comment, stringing a bunch of zeros on the initial x doesnt give higher precision. You should do x=0.6`35. After that you do not need SetAccuracy, the extended precision will follow since the initial x is the only floating number in the problem. $\endgroup$
    – george2079
    Jun 22, 2017 at 17:18
  • $\begingroup$ Ohhh....I'll try that. Thanks $\endgroup$
    – user49702
    Jun 23, 2017 at 15:28

1 Answer 1

1
$\begingroup$

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}}
$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$
    – user49702
    Jun 23, 2017 at 15:27
  • $\begingroup$ @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. $\endgroup$
    – m_goldberg
    Jun 23, 2017 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.