f(x) having fixed significant digits in Table
f(x) having fixed significant digits in Table
Define function f
Clear[f, x];
f[x_] := 2^x;
N[ f[arg], #sig. digits]
data = Table[
N[Pi, n],
N[ f[N[Pi, n]], 10 ]
, n, 1, 8
];
Text gridding...
Text@Grid[Prepend[data, "x", "f(x)"],
Alignment -> Left,
Dividers -> Center, 2 -> True
]
beginarrayl
textx & textf(x) \
hline
3. & 0. \
3.1 & 9. \
3.14 & 8.8 \
3.142 & 8.82 \
3.1416 & 8.825 \
3.14159 & 8.8250 \
3.141593 & 8.82498 \
3.1415927 & 8.824978 \
endarray
How do I show the following instead?
beginarrayl
textx & textf(x) \
hline
3. & 8.000000 \
3.1 & 8.574188 \
3.14 & 8.815241 \
3.142 & 8.821353 \
3.1416 & 8.824411 \
3.14159 & 8.824962 \
3.141593 & 8.824974 \
3.1415927 & 8.824978 \
endarray
2 Answers
2
Using arbitrary precision arithmetic produces the 1st result you show because Mathematica normally does not show digits with no precision since they are just noise. However, if you are trying to show how having better and better rational approximations to π improves the approximation of $2^pi$, I suggest the following approach.
data =
With[x = π, n = 8,
Table[
With[u = Round[x, 10^-i], u, Round[f[u], 10^-i]] // N,
i, 0, n]];
TableForm[Map[NumberForm[#, 10, 8] &, data, 2],
TableHeadings -> None, x, f[x]]
It might take some additional formatting to get exactly what you're after, but this is a start. I acknowledge some things may be able to be made simpler.
Clear[f, x];
f[x_] := 2^x;
data = N@Table[
Table[10^(-j + 1), j, 1, i].RealDigits[N@Pi, 10, i][[1]], i,
8] /. x_?NumericQ :> NumberForm[x, 8], f[x];
Text@Grid[Prepend[data, "x", "f(x)"], Alignment -> Left,
Dividers -> Center, 2 -> True]
Thanks for contributing an answer to Mathematica Stack Exchange!
But avoid …
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.