Unexpected behaviour of LogLogPlot function
I think this is a bug. This is just a long comment on what I think is happening here.
First of all, LogPlot
, LogLogPlot
, etc. have indeed changed significantly in version 11. This long standing bug is fixed now. Plot
now has the ScalingFunctions
option, and Plot[..., ScalingFunctions -> {"Log", "Log"}]
appears to behave exactly the same as LogLogPlot
, including this bug.
What is happening?
Normally Plot[expr, {x,...}]
behaves by temporarily setting a value to x
(like Block
), and then evaluating expr
. It seems that this is not true anymore in version 11. Now we have a mix of setting a value to x
(Block
) and replacing x
(Replace
).
When running
u = x^2;
LogLogPlot[u, {x, 1, 10}]
the replacement step fails as u
does not explicitly contain x
. However, when u
is later evaluated, then the temporarily set OwnValue
of x
is picked up.
What evidence do I have for this?
We can use EvaluationMonitor
to test what is happening. But we must do so carefully because it turns out that the replacement step is done on the contents of the EvaluationMonitor
option as well.
Test 1:
LogLogPlot[u, {x, 1, 10}, EvaluationMonitor :> Print[Hold[x]],
MaxRecursion -> 0]
This prints Hold[Exp[x]]
. This is evidence for the replacement.
Test 2:
f[] := OwnValues[x]
LogLogPlot[u, {x, 1, 10}, EvaluationMonitor :> Print[f[]],
MaxRecursion -> 0]
This prints from {HoldPattern[x]:>4.69915*10^-8}
to {HoldPattern[x]:>2.30259}
, which are Log[1]
to Log[10]
. This is evidence for setting a temporary OwnValue
for x
. I used f[]
to prevent the replacement.
Test 3:
We can see the combined effect of these like so:
LogLogPlot[u, {x, 1, 10}, EvaluationMonitor :> Print[x],
MaxRecursion -> 0]
This prints values from 1
to 10
. This is achieved by first replacing x
by Exp[x]
, then evaluating it with values from x=1
to x=10
.
Test 4:
Thus
u = x^2;
LogLogPlot[u, {x, 1, 10}]
is really equivalent to
LogLogPlot[Log[x]^2, {x, 1, 10}]
since x
is really set values not from 1
to 10
but from Log[1]
to Log[10]
.
Evaluating these two commands gives the same plot.
Is this a bug?
My personal opinion is that this is confusing enough that I would call it a bug. However, I do expect that some will disagree. Generally, masking the variable of an expression in Plot
can lead to strange effect in various ways.
The following is a clearer and less error prone way to do the same thing:
Clear[u]
u[x_] := x^2
LogLogPlot[u[x], {x, 1, 10}]
This bug has been fixed in the just released Mathematica 11.0.1.