9

I have a bit of trouble with Mathematica and it's plotting stuff. I tried to plot Jacobi amplitude function:

JacobiAmplitude[Sqrt[0.1]*x, 10], {x, 0, 50}]

but the result leaves gaps where the roots of the function should be (particularly for this function around x = 3, 6, 10, ...). I've done a bit of googling and found that either Exclusions set to None or raising PlotPoints should help. Well, it didn't. I even tried to set MaxRecursion to 8, PlotPoints to one million, waited several hours for the plot and it didn't change. Funny thing is, that when I added Mesh -> None, I've found out, that mathematica added plot points even at the gaps, but it didn't join them with lines! This is something like bug, or how should I deal with that? Any help'd be appreciated.

P.S.: I'm using Mathematica 9.0

kglr
  • 394,356
  • 18
  • 477
  • 896
user16320
  • 2,396
  • 15
  • 25

3 Answers3

12

There's a small but nontrivial complex part to the value being plotted that arises from using MachinePrecision numbers for plotting. Use arbitrary precision numbers by setting the WorkingPrecision to a suitable value.

Plot[JacobiAmplitude[Sqrt[0.1]*x, 10], {x, 0, 50}, WorkingPrecision -> 10]

Mathematica graphics

Or, Szbolcs' points out, one can apply Chop orRe to your function, which will be faster, if you are certain your function is real-valued.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • Or use Chop or use Re (knowing that it's real anyway). With WorkingPrecision, the tradeoff is just performance, so WorkingPrecision is probably the best (except for logarithmic plots, where it's broken). – Szabolcs Sep 05 '14 at 17:06
  • @Szabolcs Thanks! I did use Chop at first, but I like to check it with arbitrary precision. If the imaginary part persists with arbitrary precision, then I like to look into things further. (I don't know much about JacobiAmplitude, so I was cautious.) – Michael E2 Sep 05 '14 at 17:08
5
Graphics[{Blue, 
          Line[Sort @@  Cases[Plot[JacobiAmplitude[Sqrt[0.1]*x, 10], {x, 0, 50}, Mesh -> All],
                              GraphicsComplex[a_, ___] :> a]]}, 
 AspectRatio -> 1/GoldenRatio, Axes -> True]

Mathematica graphics

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
  • Szabolcs: Thank you for the explanation why does this occur...really weird bug, now I'll be aware of that and it's cause. – user16320 Sep 05 '14 at 17:08
  • belisarius: now while this is definitely working, I have no clue why...thank you very much, could you explain that code to me please? :) – user16320 Sep 05 '14 at 17:09
  • 1
    @user16320 We are just joining the mesh points that Plot[] misses to recognize as joined – Dr. belisarius Sep 05 '14 at 17:13
  • 4
    @user16320 There's no bug here really. The computation uses complex numbers (often necessary even if the result is strictly real), and due to roundoff errors the imaginary part comes out as tiny but non-zero. If you set WorkingPrecision, this still happens. However, in this case Mathematica also does precision tracking (a rather unique feature of the system) and it is able to automatically realize that the imaginary part is closer to zero than the numerical errors (and thus discardable). – Szabolcs Sep 05 '14 at 17:35
  • @Szabolcs I didn't want to mean it's a bug. And yes, you're right. – Dr. belisarius Sep 05 '14 at 17:37
  • @Szabolcs I understand, it's a backfire of numerical approach. Thank you for the help :) – user16320 Sep 05 '14 at 22:46
0

Nevertheless, plotting pendulum solutions shows discontinuities, change the example given in the helppages from over-the-top to oscillations yields a discontinuity before the first period ends (and then more)

\[Phi][t_] = With[{k = 1 - 10^-3}, 2 JacobiAmplitude[k t, k^-2]];

Plot[[Phi][t], {t, 0, 36}, WorkingPrecision -> 50]

no help from WorkingPrecision, and the answers are real-valued.

creidhne
  • 5,055
  • 4
  • 20
  • 28
markoh
  • 41
  • 2