4
$\begingroup$

I have implemented Newton's Method, and I am trying to find the exact value of the zero of a polynomial using Limit. The dream is that the following code works:

g[x_] := 21 - 3 x - 7 x^2 + x^3;
newtonNextGuess[f_, guess_] := guess - f[guess]/f'[guess];
Limit[Nest[newtonNextGuess[g, #] &, 1, n], n -> Infinity]

But this returns an error:

Nest: Non-negative machine-sized integer expected at position 3 in
    Nest[newtonNextGuess[g,#1]&,1,n].

The error persists if I make n an integer by replacing it with 1+Ceiling@Abs@n as talked about in this post. So the small question is: why is this error happening? But more importantly, is there way I can get Mathematica to tell me the limit of this sequence? The limit is $\sqrt{3}$, and it would be really nice to get Mathematica to display this explicitly (as opposed to numerically) for a presentation.

$\endgroup$
4
  • $\begingroup$ How about Roots[g[x] == 0, x]? $\endgroup$
    – bill s
    Commented Mar 3, 2017 at 2:30
  • $\begingroup$ @bills, well yeah I can get the roots with that. :) I'm doing this for a presentation on Mathematica though. I'm demonstrating how you could write a (hopefully) familiar algorithm in Mathematica. $\endgroup$ Commented Mar 3, 2017 at 2:35
  • $\begingroup$ Because you want the output in rootform, you may use RootApproximant[]. RootApproximant@FixedPoint[newtonNextGuess[g, #] &, 1.] $\endgroup$ Commented Mar 3, 2017 at 2:41
  • $\begingroup$ @AnjanKumar ah that works! Thank you. You should consider adding your approach as an answer. $\endgroup$ Commented Mar 3, 2017 at 2:48

3 Answers 3

4
$\begingroup$

To obtain the output in root form, RootApproximant[] can be used.

RootApproximant@FixedPoint[newtonNextGuess[g, #] &, 1.]
(*Sqrt[3]*)
$\endgroup$
4
$\begingroup$

For illustrative purposes:

You can get solution in radicals usingSolve:

g[x_] := 21 - 3 x - 7 x^2 + x^3;
Solve[g[x] == 0, x]

You can implement Newton's method with FixedPoint:

FixedPoint[# - g[#]/g'[#] &, 0.1]

You can illustrate method(with careful choice of starting point), e.g.

nm[x0_] := 
 Arrow /@ MapIndexed[{{#1[[1]], 10 #2[[1]]}, {#1[[2]], 10 #2[[1]]}} &,
    Partition[FixedPointList[# - g[#]/g'[#] &, x0], 2, 1]]
Manipulate[
 Plot[g[x], {x, -2, 10}, Epilog -> {Red, nm[p]}, 
  GridLines -> {x /. NSolve[g[x], x], None}], {p, 0, 5}]

enter image description here

$\endgroup$
3
$\begingroup$

I don't think you're going to be able to get the Newton iteration (using rationals or floats) to converge to an exact Sqrt[3]. You can demonstrate that it's headed there:

est = NestList[newtonNextGuess[g, #] &, 1, 6]
est - Sqrt[3] // N

{-0.732051, 0.125092, 0.00130337, 1.6763*10^-7, 2.8865*10^-15, 0., 2.2204*10^-16}

So you are effectively at "floating point zero" after about 5 iterations. Of course, the easy way is to use the Root function:

Roots[g[x] == 0, x]
x == Sqrt[3] || x == -Sqrt[3] || x == 7
$\endgroup$

Your Answer

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

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