Python | funkce math.cos().
V Pythonu obsahuje matematický modul řadu matematických operací, které lze pomocí modulu snadno provádět. math.cos()> funkce vrací kosinus hodnoty předané jako argument. Hodnota předaná v této funkci by měla být v radiánech.
Syntax: math.cos(x)
Parametr:
x : hodnota, která má být předána do cos()Vrácení: Vrátí kosinus hodnoty předané jako argument
Kód #1:
# Python code to demonstrate the working of cos()> > # importing 'math' for mathematical operations> import> math> > a> => math.pi> /> 6> > # returning the value of cosine of pi / 6> print> (> 'The value of cosine of pi / 6 is : '> , end> => '')> print> (math.cos(a))> |
Výstup:
The value of cosine of pi/6 is : 0.8660254037844387
Kód #2:
# Python program showing> # Graphical representation of> # cos() function> import> math> import> numpy as np> import> matplotlib.pyplot as plt> > in_array> => np.linspace(> -> (> 2> *> np.pi),> 2> *> np.pi,> 20> )> > out_array> => []> > for> i> in> range> (> len> (in_array)):> > out_array.append(math.cos(in_array[i]))> > i> +> => 1> > > print> (> 'in_array : '> , in_array)> print> (> '
out_array : '> , out_array)> > # red for numpy.sin()> plt.plot(in_array, out_array, color> => 'red'> , marker> => 'o'> )> plt.title(> 'math.cos()'> )> plt.xlabel(> 'X'> )> plt.ylabel(> 'Y'> )> plt.show()> |
Výstup:
in_array : [-6,28318531 -5,62179738 -4,96040945 -4,29902153 -3,6376336 -2,97624567
-2,31485774 -1,65346982 -0,99208189 -0,33069396 0,33069396 0,99208189
1,65346982 2,31485774 2,97624567 3,6376336 4,29902153 4,96040945
5,62179738 6,28318531]Out_array: [1,0, 0,7891405093963934, 0,2454854871407988, -0,40169542465296987, -0,8794731, -0,98636130372223, -0,257281, -0,2572815, -0,2572815, -0,2572815, -0,257281571, -0,2072815, 0,08257934547233249, 0,5469481581224268, 0,9458172417006346, 0,9458172417006346, 0,5469481581224268, -0,0825793454547232316, -0,082579345, -0,082579345, 0,5469481581224268, -0,0823268, -0,0882579346, 0,546969515881224268, -0,08, 57405, -0,9863613034027223, -0,8794737512064893, -0,40169542465296987, 0,2454854871407988, 0,7891405093963934 , 1,0]