21
$\begingroup$

Bug introduced in 7.0 or earlier and fixed in 11.0.0


(I'm using Mathematica 8.)

I have a Taylor series:

poly = Normal[Series[E^x, {x, 0, 10}]]

I want to produce a log-linear plot of the error. This is easy enough with the following code:

LogPlot[Abs[E^x - poly], {x, -1, 1}]

This produces

a log-linear plot

Now, I want to plot even smaller values of the error (in particular I want the plot to be sensible near zero), so I tell LogPlot to use high precision as follows:

LogPlot[Abs[E^x - poly], {x, -1, 1}, WorkingPrecision -> 30]

However this destroys the labeling on the y-axis:

bad behavior of LogPlot[]

Does anyone know what has gone wrong here? How do I fix it?

$\endgroup$
14
  • 2
    $\begingroup$ No solution, just some comments: WorkingPrecision -> MachinePrecision will use machine precision (default). WorkingPrecision -> $MachinePrecision will not use machine precision, but the built-in arbitrary precision with the same number of digits as machine precision. For high precision you would not want MachinePrecision, but set a higher number in WorkingPrecision (which as you noticed produces a plot with a wrong scale). $\endgroup$
    – Szabolcs
    Commented Dec 3, 2012 at 17:26
  • $\begingroup$ Setting WorkingPrecision, as in your example, crashes version 9. Can anyone reproduce this in 9? $\endgroup$
    – Szabolcs
    Commented Dec 3, 2012 at 17:27
  • 1
    $\begingroup$ @Szabolcs it crashes the kernel on macosX 10.7.5 +mma 9.0 $\endgroup$
    – chris
    Commented Dec 3, 2012 at 19:34
  • 1
    $\begingroup$ Apparently this bug still exists in MMA 9.0.1...Can't believe such a bug happens and remains unsolved for quite a long time. $\endgroup$
    – Leo Fang
    Commented Sep 5, 2013 at 13:26
  • 1
    $\begingroup$ @QuantumDot No, it's fixed in v11. It's one of the big fixes I'm glad about. $\endgroup$
    – Szabolcs
    Commented Aug 9, 2016 at 11:52

4 Answers 4

21
$\begingroup$

This is not simply a mislabeling of the axes. More than that is going on: the plot produced is not even logarithmic. Let's try to use the default (non-log-transformed tick marks):

First, with MachinePrecision (correct result):

Show[
 LogPlot[Abs[E^x - poly], {x, -1, 1}, WorkingPrecision -> MachinePrecision],
 Ticks -> Automatic
]

Mathematica graphics

Then with higher precision (incorrect result):

Show[
 LogPlot[Abs[E^x - poly], {x, -1, 1}, WorkingPrecision -> 30],
 Ticks -> Automatic
]

Mathematica graphics

I don't think it's worth digging into how LogPlot works, as at this point this clearly seems to be a bug.


You can work around it by using Plot instead of LogPlot:

Plot[Log@Abs[E^x - poly], {x, -1, 1}, WorkingPrecision -> 30]

Mathematica graphics

But then you have to do re-label the axes yourself (CustomTicks / LevelScheme are helpful packages). If you don't mind losing adaptive plotting, you can generate the points to be shown yourself and us ListLogPlot:

ListLogPlot[Table[Evaluate@Abs[E^x - poly], {x, -1, 1, 0.01`30}]]

Mathematica graphics

(You'd probably want Joined -> True here, but seeing where the points are helps you tune the plot, so I didn't include it now.)

$\endgroup$
2
  • $\begingroup$ Thanks If you could show how to use Plot with the CustomTicks package, that would be very useful $\endgroup$
    – AUK1939
    Commented Dec 4, 2012 at 15:14
  • 2
    $\begingroup$ Better yet: have Plot[] take care of the adaptive sampling, and pass the points thus generated to ListLogPlot[]. $\endgroup$ Commented May 4, 2013 at 5:36
9
$\begingroup$

Although Szabolcs warned "I don't think it's worth digging into how LogPlot works, as at this point this clearly seems to be a bug" I thought I would give a go. I found that:

System`LogPlot calls Graphics`LogPlotDump`scaledPlot which calls System`Plot which calls System`ProtoPlotDump`iPlot which calls Visualization`Core`Plot which is not readable.

We can demonstrate that the problem exists in this innermost function by making direct use of "MappingFunctions" which is what LogPlot ultimately becomes (among a few other settings):

poly = Normal[Series[E^x, {x, 0, 10}]];

Table[
 Visualization`Core`Plot[Abs[E^x - poly], {x, -1, 1}, WorkingPrecision -> wp, 
  Method -> {"MappingFunctions" -> {{#1, Log[#2]} &, {#1, #2} &}}],
 {wp, {MachinePrecision, 30}}
]

enter image description here

This bug therefore appears to be beyond our reach to fix. :-(

$\endgroup$
7
$\begingroup$

neat little work-around,

poly[x_] = Normal[Series[E^x, {x, 0, 10}]];
LogPlot[(Abs[E^# - poly[#]] &@N[Rationalize[x, 0], 20]), {x, -1, 1}]

enter image description here

$\endgroup$
6
$\begingroup$

EDIT: Problem persists with Mathematica version "10.3.1 for Mac OS X x86 (64-bit) (December 9, 2015)"

THIS IS NOT AN ANSWER but rather an extended comment providing an update for Mma v10.1.

In looking at the issue referenced in LogPlot does not show all points, I noticed that use of WorkingPrecision causes really odd results on my system (Mac OS 10.10.4) with Mathematica 10.1. A related issue with LogLogPlot and version 10 is documented in WorkingPrecision in LogLogPlot creates wrong plots.

This problem appears to have gotten worse since the frame ticks and labels are not displayed at all--although they appear in a saved file using Save Selection As...

$Version

"10.1.0 for Mac OS X x86 (64-bit) (March 24, 2015)"

LogPlot[1/x, {x, 10^-12, 1}, PlotRange -> {{-.1, 1}, All}, Frame -> True, 
 WorkingPrecision -> $MachinePrecision]

AS DISPLAYED:

enter image description here

AS SAVED using Save Selection As...:

enter image description here

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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