Why does $\tan\left(\frac{1}{x}\right)$ look different to what I expect it should look like on WolframAlpha as x approaches 0?

107 Views Asked by At

https://www.wolframalpha.com/input/?i=tan%281%2Fx%29%2C+-0.01+%3C+x+%3C+0.01

enter image description here

$$$$

https://www.wolframalpha.com/input/?i=tan%281%2Fx%29%2C+-0.001+%3C+x+%3C+0.001

enter image description here

$$$$

For larger intervals of x, such as $ -0.1< x< 0.1,\ $ the graph looks how I would expect it to look.

My suspicion is that the graphs that WolframAlpha is plotting are wrong/misleading as $ x $ gets close to 0.

Unless I'm wrong and the graph on WolframAlpha is correct, which would be totally fascinating.

What I think the function should look like is basically cot(x) but getting more and more squished in the x-direction as $ x {\to} 0$.

To be more precise, the qualitative properties I expect tan(1/x) to have for each interval $ \frac{1}{(k + \frac{1}{2})\pi} < x < \frac{1}{(k - \frac{1}{2})\pi}, k \in \mathbb{Z} $ are:

  1. $\tan(1/x)$ is coming from $+ \infty$ when x is just greater than $\frac{1}{(k + \frac{1}{2})\pi}$

  2. $\tan(1/x)$ is approaching $0$ from above for x just less than $\frac{1}{k\pi}$

  3. $\tan(1/x)$ is approaching $0$ from below for x just greater than $\frac{1}{k\pi}$

  4. $\tan(1/x)$ goes down to $- \infty$ when x is just less than $\frac{1}{(k - \frac{1}{2})\pi}$

1

There are 1 best solutions below

4
On BEST ANSWER

Mathematica (11.3) replicates your plot. I've also instrumented it to see how many times it evaluates the function to make the plot.

counts = 0;
Plot[Tan[1/x], {x, -1/100, 1/100}, EvaluationMonitor :> counts++]
counts

Mathematica graphics

4288

So the function was (only) evaluated 4288 times to get the plot you see. In the part near $x = 0$, picking a random $x$ and evaluating it essentially gives a random height, so we should expect to see something unstructured there. What if we force more points to be more evenly sampled?

counts = 0
Plot[Tan[1/x], {x, -1/100, 1/100}, PlotPoints -> 100000, EvaluationMonitor -> counts++]
counts

Mathematica graphics

1946107

and perhaps what you see is closer to what you expected, although the plot code evaluated the function nearly 2 million times to do so.

Do we have to be so aggressive with sampling? Apparently not, if we let the plotting functions sample more finely where the function seems more wildly behaved.

counts = 0;
Plot[Tan[1/x], {x, -1/100, 1/100}, PlotPoints -> 100, MaxRecursion -> 15, EvaluationMonitor :> counts++]
counts

Mathematica graphics

(I cheated. There's no visible difference between the two plots, so I reused the same uploaded image.)

507263

So it can be done with about one quarter as many samples.


Where are these sample points?

samples = Reap[
    Plot[Tan[1/x], {x, -1/100, 1/100}, EvaluationMonitor :> Sow[{x,Tan[1/x]}]]
];
Length[samples[[2, 1]] ]
Show[samples[[1]] , ListPlot[samples[[2, 1]], PlotStyle -> Red ]]


4288

Mathematica graphics

Where the features are wide, the sampling seems to do fine. There's a little bit of wagon wheel effect near $x = \pm 0.005$, where the samples are nearly horizontal nearly linear bands. The samples corresponding to the tops and bottoms of the filled in parts around those same $x$s are easily visible. At a guess, they seem to correspond to incorrectly classified turning points of the function. Not sure why the algorithm "ignores" the outliers farther away from the $x$-axis than these samples. Closer to zero, around $[-0.002, 0.002]$ the points seem to be vaguely randomly distributed with a distribution that is peaked near the $x$-axis (as we would expect from the function we are plotting).

If we increase the number of starting samples,

samples = Reap[
    Plot[Tan[1/x], {x, -1/100, 1/100}, PlotPoints -> 500, EvaluationMonitor :> Sow[{x, Tan[1/x]}]]
];
Length[samples[[2, 1]] ]
Show[samples[[1]] , ListPlot[samples[[2, 1]], PlotStyle -> Red ]]

27360

Mathematica graphics

We see broadly similar behaviour.