3
$\begingroup$

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}]

Mathematica graphics

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

$\endgroup$
4
  • 3
    $\begingroup$ Welcome to Mathematica.SE. Please include your expression as Mathematica code for easy handling by those who wish to answer. $\endgroup$
    – Mr.Wizard
    Commented Aug 8, 2014 at 7:08
  • $\begingroup$ Increase the WorkingPrecision: Plot[f[x], {x, 0, 10^4}, WorkingPrecision -> 20] gives this. $\endgroup$
    – Öskå
    Commented Aug 8, 2014 at 10:04
  • $\begingroup$ It does seem disappointing that the precision is not defined automatically based on the plot range and proportional value of a pixel. $\endgroup$
    – rhermans
    Commented Aug 8, 2014 at 11:16
  • 2
    $\begingroup$ 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. $\endgroup$
    – Michael E2
    Commented Aug 8, 2014 at 12:59

1 Answer 1

1
$\begingroup$

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}

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.