Sometimes we need higher numerical precision to deal with large number cancellation in an equation. But if this cancellation happens only in a small (and known) parameter space, would it be possible to only use high precision calculation in that small part of the parameter space? For example, something like
NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30},
WorkingPrecision -> If[x < 5, $MachinePrecision, 50],
PrecisionGoal -> If[x < 5, $MachinePrecision, 50]]
This code doesn't work, because Mathematica doesn't relate the x
in the two If
expressions to the x
in the ODE. Is there any way to make the above idea work?
$MachinePrecision
is different fromMachinePrecision
.WorkingPrecision -> $MachinePrecision
uses arbitrary precision with$MachinePrecision
digits of precision, which may be what you wanted. One can useWorkingPrecision -> MachinePrecision
, which is the default, for machine precision calculation, when desired. $\endgroup$