4
$\begingroup$

I'm here with a rather interesting problem which I have no clue how to fix.

I am trying to plot a function:

Plot[x && 0 <= x <= (1/8), {x,-9,9}]

However the graph does not seem to appear at all.

The interesting thing though is if I change $\frac{1}{8}$ to $\frac{185}{1000}$ the plot works normally. Change it to $\frac{184}{1000}$ and it stops working again. So why does nothing plot where the second value is below $\frac{184}{1000}$?

Regarding to why I don't use the {x,-9,9} to set the domain. I am plotting other functions on the same graph and omitted them for this post. The error is still present when it is on its own.

Also if you were wondering. Here are some other things I did to troubleshoot the error to save some time. Firstly, changing the first value of 0 to $\frac{1}{100}$ retrieves the same problem and the second value can not go below $\frac{184}{1000}$ but can still go above $\frac{185}{1000}$ so it doesnt seem like the issues relates to the difference between the minimum and maximum. Changing the function to $2x$ doesnt change this either and has the same possible values. Same with $2x^2$ and $\sqrt{x}$. So from this I can see this problem occurs with every single type of function.

There is one parameter which changes this however. If we change {x,-9,9} to {x,-8,8} then $\frac{184}{1000}$ becomes possible. As a result of changing this the new possible values for the domain are $\frac{164}{1000}$ and above (instead of $\frac{185}{1000}$ and above). Anything lower and it doesnt work although I haven't gone to any larger denominators I'm sure its evident to see there is some issue here.

Also, if this is just purely a Mathematica error. Any alternatives to set a domain for an individual function without using Show[plot1,plot2 etc.] would be awesome. If it helps I am on Mathematica 9.0. Thanks

EDIT: Just clarifying here after a response. I wish for the final plot to still show from -9 to 9 because there are other functions being plotted on this graph and this is the domain I wish for the entire graph to go over.

$\endgroup$
2
  • $\begingroup$ Use a large value for PlotPoints, say PlotPoints->100? $\endgroup$
    – kglr
    Aug 23, 2014 at 6:28
  • $\begingroup$ PlotPoints->100 has fixed it. Thanks for your help $\endgroup$
    – VikeStep
    Aug 23, 2014 at 6:35

3 Answers 3

6
$\begingroup$

The problem is your range is too large for the plot. But as suggested by @kguler you can increase PlotPoints

 Plot[x && 0 <= x <= (1/8), {x, -9, 9}, PlotPoints -> 100]

Mathematica graphics

$\endgroup$
0
2
$\begingroup$

The problem is, given the large domain specified for the expression you want to plot, Plot is not sampling the function with a fine enough mesh in the interval 0 <= x <= 1/8 to see that the expression has some numerical values in that interval.

Looking at the plot mesh is a useful debugging tool for this kind of plot problem. Even asking for an initial sampling of 79 points is not sufficient.

Plot[x && 0 <= x <= 1/8, {x, -9, 9},
  Mesh -> All,
  MeshStyle -> Directive[PointSize[Medium]],
  PlotPoints -> 79,
  PlotStyle -> {Red, Thick}]

bad-plot

The initial sampling of mesh points found only one point with a numerical value. So Plot decided there was no curve to plot.

However, increasing the initial sample to 80 points allows Plot to detect some variation in numerical values, so it refines the mesh in the interval showing variation and makes a plot.

Plot[x && 0 <= x <= 1/8, {x, -9, 9},
  Mesh -> All,
  MeshStyle -> Directive[PointSize[Medium]],
  PlotPoints -> 80,
  PlotStyle -> {Red, Thick}]

good-plot

As you increase the width of the interval over which your expression returns numerical values, it eventually becomes wide enough for Plot's default sampling to see some variation in the interval. Then it will refine its sampling inside the interval and produce a visible plot. This is why the changes to the interval you made had the effect you described.

$\endgroup$
1
$\begingroup$

The important thing in generating a good plot is to get at least one, and maybe a few, initial sample points in every interval of interest.

By default, Plot generates a nearly uniform sample grid of 50 points, whose successive differences vary from 1/49 of the length of the plot interval by up to about 10%. We can see which points are generated with the following:

Cases[
 Plot[x, {x, -9, 9.}, MaxRecursion -> 0], 
 Line[p_] :> p[[All, 1]], Infinity]
(*
  {{-9., -8.64668, -8.26365, -7.906, -7.55536, -7.17501, -6.82004, 
    -6.43535, -6.05767, -5.70538, -5.32337, -4.96674, -4.61713, -4.2378, 
    -3.88385, -3.50018, -3.12353, -2.77226, -2.39127, -2.03567, -1.65035, 
    -1.27204, -0.919114, -0.53647, -0.17921, 0.171035, 0.550999, 0.905579,
     1.28988, 1.66716, 2.01906, 2.40068, 2.75692, 3.10614, 3.48508, 
     3.83864, 4.22192, 4.57981, 4.93069, 5.31128, 5.6665, 6.05143, 
     6.42934, 6.78188, 7.16413, 7.521, 7.87085, 8.25043, 8.60462, 9.}}
*)

By inspection, we see that none between 0 and 1/8 happen to be used. The function is undefined at all these points and Plot gives up. Increasing PlotPoints makes it more likely that a sample point will land in the interval. If we make it big enough, we can guarantee that at least one point will lie in between 0 and 1/8 using the observation above about the differences between sample points. Below sample represents a list of points between which it is necessary to sample. From the smallest interval between them, an appropriate minimum number of plot points is computed.

sample = {0, 1/8};
{x1, x2} = {-9, 9};
pp = 1 + Ceiling[1.1 (x2 - x1) / Min[DeleteCases[Differences[Sort[sample]], x_ /; Chop[x] == 0]]]
Plot[x && 0 <= x <= (1/8), {x, -9, 9}, PlotPoints -> pp]

(* 160 *)

Mathematica graphics

(The use of DeleteCases and Chop is just in case the list sample is generated by code. It is possible that the same point might be calculated twice and differ slightly due to round-off error.)

Once a point is found, recursive subdivision will begin and the interval will be filled out. There is a limit to how much it will be filled out. The limit is controlled by the MaxRecursion option. But it might be better to start with more points, by setting PlotPoints -> 2 pp or higher.

By the way, it just happens that PlotPoints -> 5 results in one initial sample point landing in the interval between 0 and 1/8. The following will produce a graph like the one above:

Plot[x && 0 <= x <= (1/8), {x, -9, 9}, PlotPoints -> 5, MaxRecursion -> 5]
$\endgroup$

Your Answer

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

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