E. Trofimov
E. Trofimov

Reputation: 92

Python plotting trigonometrical func

I have a function 2*x*arcctg(x) - 1, and i try to plot it in Python:

import os
import matplotlib.pyplot as plt 
import numpy as np 

x = np.linspace(0, np.pi / 2)
y = 2 * x * np.cos(2 * x) / np.sin(2 * x)
plt.plot(x, y)
plt.axis('tight')
plt.show()

but it's plot smthg like that: Wrong pic in python and when i plot it in wolfram it looks:

Right plot in Wolfram What am i doing wrong?

Upvotes: 0

Views: 1676

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477824

The function should be:

2*x*arcctg(x) - 1

But arcctg(x) is not cos(2x)/sin(2x) (the expression you describe in your code). A ctg is the co-tangens, so cos(x)/sin(x). So that means that arcctg(x) is arctan(1/x).

So you can use:

import matplotlib.pyplot as plt 
import numpy as np 

x = np.linspace(0, np.pi / 2)
y = 2 * x * np.arctan(1/x) - 1
plt.plot(x, y)
plt.axis('tight')
plt.show()

This produces the following plot:

The plot of 2x arcctg(x) - 1

Which matches with the plot in the question.

In case you want to make the plot look more than the one in Wolfram Alpha, you can like @MSeifert says, set the range from -pi/2 to pi/2, like:

import matplotlib.pyplot as plt 
import numpy as np 

x = np.linspace(-np.pi / 2, np.pi / 2, 1000)
y = 2 * x * np.arctan(1/x) - 1
plt.plot(x, y)
plt.axis('tight')
plt.show()

this then produces:

plot with different bounds

Upvotes: 3

Related Questions