Comment calculer et tracer une fonction tangente en python ?


Exemples de comment calculer et tracer une fonction tangente en python

Calculer une tangente pour un angle donné

Pour obtenir la tangente d'un angle donné une solution consiste à utiliser le module math, par exemple

import math

angle = 0

math.tan(angle)

donne

0

Note: la fonction tan suppose que l'angle est en radian. Pour convertir du degré en radian en python, une solution consiste à utiliser math.radians():

angle = 45 # angle in degree
angle = math.radians(angle) # angle in radian

AUtre exemple

angle = math.pi / 6

math.tan(angle)

donne

0.5773502691896256

Note: pour arrondir un float en python, une solution consiste à utiliser round():

round( math.tan(angle), 2 )

donne

0.58

Tracer une fonction tangente

import matplotlib.pyplot as plt
import numpy as np

angle_min = - 2.0 * math.pi
angle_max = 2.0 * math.pi
res = 0.01

angle_list = [a for a in np.arange(angle_min,angle_max,res)]
angle_tan_list = [math.tan(a) for a in angle_list]

plt.plot(angle_list, angle_tan_list)

plt.title("How to calculate a tangent for a given angle in python ?",fontsize=12)

plt.ylim(-10,10)

plt.savefig("tangent_function_01.png", bbox_inches='tight', dpi=100)

plt.show()

Comment calculer et tracer une fonction tangente en python ?
Comment calculer et tracer une fonction tangente en python ?

Références

Image

of