Strange Sin[x] graph in Mathematica
I randomly plotted a Sin[x] function in Mathematica 7 and this is what it shows:
Note the visible defect at approximately x = -1开发者_如何学Go00
.
Here is a zoom of the defect part, clearly showing that Mathematica for some reason uses a much lower resolution between the points there:
Anybody know why this happens and why only at x = -100
?
Note: same happens in Wolfram Alpha, by the way.
Short answer: default plotting accuracy is not sufficient for that function, so increase it as follows
Plot[Sin[x], {x, -42 Pi, 42 Pi}, PlotPoints -> 100]
Long answer: Plot
works by evaluating the function at a finite set of points, and connecting those points by straight lines. You can see the points used by Plot
using the following command
Plot[Sin[x], {x, -42 Pi, 42 Pi}, Mesh -> All, PlotStyle -> None,
MeshStyle -> Black]
You can see that for your function, the points where the function was evaluated "missed the peak" and introduced a large approximation error. The algorithm used to pick locations of points is very simple and this situation might happen when two peaks are spaced more closely together than PlotRange/PlotPoints.
Plot
starts with 50 equally spaced points and then inserts extra points in up to MaxRecursion
stages. You can see how this "hole" appears if you plot the region for various settings of MaxRecursion
.
plot1 = Plot[Sin[x], {x, -42 Pi, 42 Pi}, PlotPoints -> 100,
PlotStyle -> LightGray];
Table[plot2 =
Plot[Sin[x], {x, -42 Pi, 42 Pi}, Mesh -> All, MeshStyle -> Thick,
PlotStyle -> Red, MaxRecursion -> k];
Show[plot1, plot2, PlotRange -> {{-110, -90}, {-1, 1}},
PlotLabel -> ("MaxRecursion " <> ToString[k])], {k, 0,
5}] // GraphicsColumn
According to Stan Wagon's Mathematica book, Plot
decides whether to add an extra point halfway between two consecutive points if the angle between two new line segments would be more than 5 degrees. In this case, plot got unlucky with initial point positioning and subdivision does not meet that criterion. You can see that inserting a single evaluation point in the center of the hole will produce almost identically looking plot.
The way to increase the angle used to decide when to subdivide by using Refinement
option (I got it from the book, but it doesn't seem to be documented in product)
plot1 = Plot[Sin[x], {x, -42 Pi, 42 Pi}, PlotPoints -> 100,
PlotStyle -> LightGray];
Show[plot1,
Plot[Sin[x], {x, -42 Pi, 42 Pi}, Mesh -> All, MeshStyle -> Thick,
PlotStyle -> Red, MaxRecursion -> 3,
Method -> {Refinement -> {ControlValue -> 4 \[Degree]}}],
PlotRange -> {{-110, -90}, {-1, 1}}]
Here you can see that increasing it by 1 degree from default 5 fixes the hole.
精彩评论