3
$\begingroup$

I'm new to Mathematica and am not sure why I cannot show more decimal points in a variable. I am using Newton's Method to find the zero's of a function and I'm trying to print the x value of the zero.

f[x_] := Tan[x] - x/2;
fp[x_] := -1/2 + Sec[x]^2;
xn = 5.;
convergence = False;
\[Epsilon] = 1*^-6;
fx = f[xn];
While[convergence == False,
 fpx = fp[xn];
 d = fx/fpx;
 xn = xn - d;
 fx = f[xn];
 If[Abs[d] < \[Epsilon], convergence = True]]
N[xn, 10]
N[5, 10]

Which returns:

39.219
5.000000000

the zero is at 39.219, but I believe there are more decimals in this, and wanted to just test the N[] function with 5 to make sure that it will just print 0's after the number if there are not more numbers. Why is it only showing 5 numbers for xn?

$\endgroup$
  • $\begingroup$ This doesn't look right. According to the documentation it should work. I tried a workaround with SetPrecision[xn,10] and it printed 39.21895656 (Nice workaround yaay!) I'll have to look a bit into this before posting an answer. $\endgroup$ – Vahagn Tumanyan Sep 7 '16 at 14:31
  • $\begingroup$ N only works when the specified amount of digits can be found correctly. Using MachinePrecision numbers such as xn is like naively assuming 15.95 correct digits at any time, which doesn't provide the guarantee that N wants. $\endgroup$ – Coolwater Sep 7 '16 at 14:38
  • $\begingroup$ I believe this is a duplicate of either or both: (3736), (10624). Also related: (55292) $\endgroup$ – Mr.Wizard Sep 8 '16 at 6:14
4
$\begingroup$

Alright, even though intuitively this looks like something that should work, it doesn't. This is because of the way Mathematica handles precision. You might have noticed that you can output really large numbers like 10^10000 which are way beyond MachinePrecision of double. This is because Mathematica is a symbolic language. Each value gets it's precision which you can look at by calling Precision[a] Symbols, Integers and some other stuff has $\infty$ precision and some things have MachinePrecision. It is written in the documentation that

N[e] typically works by replacing numbers with machine numbers and computing the result.

and that:

The precision of the result is the same as that of the input:

This means if you have machine precision it will N will always return the same precision. In your sanity check if you wrote

N[5.,10] instead of N[5,10] you would get 5.

Now you can change the precision by using SetPrecision as I did in the comment and things will go smoother.

EDIT

Also N Will try to give you the result that can be Exactly computed without machine errors. So there's also that.

| improve this answer | |
$\endgroup$
2
$\begingroup$

This seems to me to be a tempest in a teapot. If you just want to see the what xn looks like in its full internal glory, evaluate

xn // FullForm

which gives

39.21895655961492`

| improve this answer | |
$\endgroup$
1
$\begingroup$

Long story short: instead

N[xn, 10]

39.219

type

SetAccuracy[xn, 10]

39.2189565596

| improve this answer | |
$\endgroup$
  • $\begingroup$ Can you elaborate more in your answer, what's the difference between SetAccuracy and and SetPrecision? $\endgroup$ – Vahagn Tumanyan Sep 7 '16 at 15:58
  • 1
    $\begingroup$ SetPrecision[xn, 10] gives 10 digits: 39.21895656; SetAccuracy[xn, 10] gives 10 digits after the decimal point:39.2189565596. $\endgroup$ – corey979 Sep 7 '16 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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