I have some function $f(x)$ I wish to evaluate, which is yielding divide-by-zero errors for sufficiently large inputs. How do I increase the precision with which this function is evaluated in order to prevent this? Can I do this while also plotting the function?
1 Answer
Straight from the Mathematica documentation of SetPrecision
bit = Log[10., 2.];
f[x_] := Module[{p = Precision[x], lx},
lx = Block[{$MaxPrecision = p, $MinPrecision = p},4*x*(1 - x)];
SetPrecision[lx, p - bit]]
then testing
x0 = N[1/3, 20];
fl = NestList[f, x0, 20]
As mentioned in the comment of your question, those related questions has got tons of tricks about precision management in Mathematica. I should be a bit more explanatory with my answer here.
Problem:
Lets evaluate this code.
{Sin[Exp[200.]], a + 2 b + 3 c, Sin[Exp[200]] > 0}
{-0.89766,a+2 b+3 c,Sin[E^200]>0}
the above result comes with a error as the logical positivity test fails.
N::meprec: "Internal precision limit $MaxExtraPrecision = 50.` reached while evaluating -Sin[E^200]."
Now an easy solution here will be to extend Mathematica's precision using the key $MaxExtraPrecision
. This can be done in a Block[]
construct to not have a global effect on the kernel.
Block[{$MaxExtraPrecision = 150},
{SetPrecision[#, 30] & /@ {N[Sin[Exp[200]], {Infinity, 30}],
a + 2 b + 3 c},
Sin[Exp[200]] > 0}]
{{-0.815597574752651427792149914303,a+2.00000000000000000000000000000b+3.00000000000000000000000000000 c},False}
One needs to be clear with the expression N[Sin[Exp[200]], {Infinity, 30}]
where Infinity
is the Precision
and 30
is accuracy requested for the expression Sin[Exp[200]]
.
You can use the block construct as above to define your function that require extra precision.
-
$\begingroup$ @Mr.Wizard I added a bit from my limited understanding ;) Thx for the suggestion. Anybody please feel free to edit the answer to make it more useful and informative. $\endgroup$ Commented Jan 14, 2013 at 15:24