Bug introduced in 3.0 and persisting through 13.2 (reported as CASE:3208982)
I'm trying to plot MathieuC[-3,0.3,I x]
for $x\in[0,10]$, and here's what I get even with arbitrary precision arithmetic (here I use ListPlot
and Table
instead of Plot
to make computation faster and use a regular grid):
$MaxExtraPrecision=200;
ListPlot[Table[{z,N[Re@MathieuC[-3,3/10,I z],50]},{z,0,7,1/100}],
PlotRange->{-0.6,0.6}, Joined->True]
So, starting from about z==3.3
, the results are unreliable at all. Comparing machine precision and arbitrary precision computations with N[]
, I get this:
N[Re@MathieuC[-3, 3/10, 5 I]]
N[Re@MathieuC[-3, 3/10, 5 I], 50]
-1.26013246174486*10^28
-1.2601324617438073657004964476674284869363635740962*10^28
So, it seems not even lack of working precision — looks like the algorithm is broken. To make sure I'm not misunderstanding the supposed behavior of Mathieu functions, I've also checked it by solving Mathieu equation numerically:
With[{a = -3, q = 0.3},
sol = NDSolveValue[{
-w''[z] - 2 q Cosh[2 z] w[z] == -a w[z],
w[0] == Re@MathieuC[a, q, 0],
w'[0] == 0
}, w, {z, 0, 7}, MaxSteps -> 10^6]
];
$MaxExtraPrecision=200;
Show[
Plot[sol[z],{z,0,7}, PlotStyle->Darker@Green, PlotPoints->300],
ListPlot[Table[{z,N[Re@MathieuC[-3,3/10,I z],50]},{z,0,7,1/100}],Joined->True],
PlotRange->{-0.6,0.6}]
This shows that the function indeed should look nicer, but apparently MathieuC
can't calculate it for even moderately large imaginary arguments. It also appears that Mathematica versions 5 to 12 give exactly the same results (sometimes with differences in several least significant digits).
Is it a bug, or is it documented somewhere? Are there any workarounds, allowing me to evaluate Mathieu functions not as slowly as via NDSolve
, and still not reimplementing them like e.g. here?
N
(or just supply numbers with higher precision, not necessarily exact). In any case, I've given an example of exact arguments andN
in the OP, with the same wrong results. $\endgroup$Plot[Re@MathieuC[-3, 3/10, I z], {z, 0, 10}, PlotRange -> {-0.6, 0.6}, WorkingPrecision -> 50]
as the comparison, rather than the one calculated at fixed precision. $\endgroup$MathieuC
andMathieuS
), it appeared to fail in exactly the same way. So I suspect Wolfram and Maplesoft used algorithms from the same paper, which appear broken as published, and no one had noticed this. $\endgroup$