highpost
highpost

Reputation: 1323

problems plotting tan() with SymPy

I'm trying to plot various trig functions with sympy. This works fine with sine, cosine, etc., but tan() displays wierd results. Furthermore, if I rerun just the plot() function, then I get a different result each time.

from sympy import symbols
from sympy.functions.elementary.trigonometric import tan
from sympy.plotting.plot import plot

x = symbols('x')
eqn = tan(x)
plot(eqn, (x, -10, 10))

Upvotes: 0

Views: 602

Answers (1)

user6655984
user6655984

Reputation:

I understand that this is not how one usually visualizes the tangent function:

tan1

but it's not wrong. SymPy found that there are extremely large y-values, and chose the vertical scale accordingly. On this scale, all values are practically zero except in close proximity to the singularities. And the unequal height of spikes near singularities is because the evaluation points are not going to hit any pi/2 + pi*n exactly, and how close they come depends on what n is.

All that said, you need a sane vertical scale, enforced with ylim:

plot(eqn, (x, -10, 10), ylim=(-20, 20))

tan2

Upvotes: 3

Related Questions