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?
SetPrecision[xn,10]
and it printed39.21895656
(Nice workaround yaay!) I'll have to look a bit into this before posting an answer. $\endgroup$N
only works when the specified amount of digits can be found correctly. UsingMachinePrecision
numbers such asxn
is like naively assuming 15.95 correct digits at any time, which doesn't provide the guarantee thatN
wants. $\endgroup$