# Dealing with square roots [duplicate]

Consider following expression: $$\sqrt{-\left(2 \sqrt{x^{-8/3}-x^{-4/3}+1}\, x^{4/3}+5\right) x^{4/3}+4 \sqrt{x^{-8/3}-x^{-4/3}+1}\, x^{4/3}+2 x^{8/3}+5}$$ In code:

f[x_] := Sqrt[
-(2 Sqrt[x^(-8/3) - x^(-4/3) + 1] x^(4/3) + 5) x^(4/3)
+ 4 Sqrt[x^(-8/3) - x^(-4/3) + 1] x^(4/3) + 2 x^(8/3) + 5
]


Why in Mathematica one can see non smoothness at $x\sim10^4$?

Plot[f[x], {x, 0, 10^4}]


I guess, it is a numerical issue, but then how one can deal with it?

• Welcome to Mathematica.SE. Please include your expression as Mathematica code for easy handling by those who wish to answer. – Mr.Wizard Aug 8 '14 at 7:08
• Increase the WorkingPrecision: Plot[f[x], {x, 0, 10^4}, WorkingPrecision -> 20] gives this. – Öskå Aug 8 '14 at 10:04
• It does seem disappointing that the precision is not defined automatically based on the plot range and proportional value of a pixel. – rhermans Aug 8 '14 at 11:16
• Similar: 3152, 7109, 18126, 38769. N.B. The solution WorkingPrecision -> Infinity from an answer to [7019] does not work on this example; but PPlot[N[f[x], 8], {x, 0, 10^4}, Exclusions -> None, WorkingPrecision -> Infinity] does. I'll propose [3152] as a duplicate. – Michael E2 Aug 8 '14 at 12:59

In addition to using WorkingPrecision as pointed out in the comments, it is often more efficient and reduces numerical noise to Simplify (or in some cases FullSimplify) the function's definition.

Original definition

f1[x_] := Sqrt[-(2 Sqrt[x^(-8/3) - x^(-4/3) + 1] x^(4/3) + 5) x^(4/3) +
4 Sqrt[x^(-8/3) - x^(-4/3) + 1] x^(4/3) + 2 x^(8/3) + 5]


Simplify once by using Set rather than SetDelayed.

f2[x_] = Sqrt[-(2 Sqrt[x^(-8/3) - x^(-4/3) + 1] x^(4/3) + 5) x^(4/3) +
4 Sqrt[x^(-8/3) - x^(-4/3) + 1] x^(4/3) + 2 x^(8/3) + 5] // Simplify;


Or use Evaluate with SetDelayed.

f3[x_] := Evaluate[
Sqrt[-(2 Sqrt[x^(-8/3) - x^(-4/3) + 1] x^(4/3) + 5) x^(4/3) +
4 Sqrt[x^(-8/3) - x^(-4/3) + 1] x^(4/3) + 2 x^(8/3) + 5] // Simplify]

Timing[Plot[#[x], {x, 0, 10^4}, WorkingPrecision -> 15];][[1]] & /@ {f1, f2,
f3}


{1.472757, 0.066547, 0.066083}