Python. Module math. Trigonometric functions




Module math. Trigonometric functions


Contents


Search other websites:

1. Features of the use of trigonometric functions. Convert radians to degrees and vice versa

To use trigonometric functions in a program, you need to include the math module

import math

All trigonometric functions operate with radians. The relationship between radians and degrees is determined by the formula:

1 radian = 180°/π = 57.2958°

If the angle in degrees is known, then for the trigonometric functions to work correctly, this angle must be converted to radians.

For example. Given an angle having n degrees. Calculate the arccosine of this angle. In this case, the formula for calculating the result will be as follows:

...
n_rad = n*3.1415/180 # get the angle in radians
ac = math.acos(n_rad) # calculate arccosine
...

To get a more accurate value of the result, you can use the constant math.pi in the program, which determines the number π. In this case, the program text will be as follows

n_rad = n*math.pi/180 # get the angle in radians
ac = math.acos(n_rad) # calculate arccosine

 

2. Python tools for converting from degrees to radians and vice versa. Functions math.degrees(x) and math.radians(x)

The math module has conversion functions from degrees to radians and, conversely, from radians to degrees.
The math.degrees(x) function converts the value of the parameter x from radians to degrees.
The math.radians(x) function converts the value of the parameter x from degrees to radians.

Example.

# Function math.degrees(x)
import math

x = 1 # x - angle in radians
y = math.degrees(x) # y = 57.29577951308232 - angle in degrees

x = math.pi # x = 3.1415...
y = math.degrees(x) # y = 180.0

# Function math.radians(x)
x = 180.0/math.pi
y = math.radians(x) # y = 1.0

x = 45 # x - angle in degrees
y = math.radians(x) # y = 0.7853981633974483

 

3. Restrictions on the use of trigonometric functions

When using trigonometric functions, the corresponding restrictions that follow from the very essence of these functions should be taken into account. For example, there is no arcsine from a number that is greater than 1.
If an incorrect argument is specified when calling a function, the interpreter will display the corresponding error message

ValueError: math domain error

 



4. Function math.acos(x). Arc cosine of angle

The acos(x) function returns the arc cosine of the angle x. The argument x is given in radians and can be either an integer or a real number.

Example.

# Function math.acos(x)
import math

n = float(input('n = ')) # input n

n_rad = n*math.pi/180 # get the angle in radians
ac = math.acos(n_rad) # calculate arccosine

print('n_rad = ', n_rad)
print('ac = ', ac)

The result of the program

n = 35
n_rad = 0.6108652381980153
ac = 0.913643357298706

 

5. Function math.asin(x). Arcsine

The math.asin(x) function computes the arcsine of the angle of argument x. The value of the argument x is given in radians.

Example.

# Function math.asin(x)
import math

n = 10 # n - angle in degrees

# convert from degrees to radians
n_rad = n*math.pi/180 # n_rad = 0.17453292519943295

# calculate arcsine
asn = math.asin(n_rad) # asn = 0.17543139267904395

 

6. Function math.atan(x). Arctangent

The math.atan(x) function returns the arc tangent of argument x, whose value is specified in radians. When using the function, it is important to remember the permissible x values that can be set when calculating the arctangent.

Example.

# Function math.atan(x)
import math

n = 60 # n - angle in degrees

# convert from degrees to radians
n_rad = n*math.pi/180 # n_rad = 1.0471975511965976

# calculate arctangent
atn = math.atan(n_rad) # atn = 0.808448792630022

 

7. Function math.atan2(x, y). The arctangent of x/y

The math.atan2(x, y) function calculates the arc tangent of the angle from dividing x by y. The function returns the result from –π to π. Arguments x, y determine the coordinates of the point through which the segment from the origin passes. Unlike the atan(x) function, this function correctly calculates the quadrant that affects the sign of the result.

Example.

# Function math.atan2(x,y)
import math

x = -2
y = -1

res = math.atan2(x, y) # res = -2.0344439357957027

 

8. Function math.cos(x). Cosine of angle

The math.cos(x) function computes the cosine of the angle for argument x. The value of the argument x is given in radians.

Example.

# Function math.cos(x)
import math

x = 0
y = math.cos(x) # y = 1.0

x = math.pi
y = math.cos(x) # y = -1.0

x = 2 # 2 radians
y = math.cos(x) # y = -0.4161468365471424

 

9. Function math.sin(x). Sine of the angle

The math.sin(x) function returns the sine of the angle from the argument x specified in radians.

Example.

# Function math.sin(x)
import math

x = math.pi
y = math.sin(x) # y = 1.2246467991473532e-16

x = 0
y = math.sin(x) # y = 0.0

x = 2 # 2 radians
y = math.sin(x)

 

10. Function math.hypot(x, y). Euclidean norm

The function returns the Euclidean norm, which is equal to the length of the vector from the origin to the point x, y and is determined by the formula

Python. Euclidean norm. Formula

Example.

# Function math.hypot(x, y)
import math

x = 1.0
y = 1.0
z = math.hypot(x, y) # z = 1.4142135623730951

x = 3.0
y = 4.0
z = math.hypot(x, y) # z = 5.0

 

11. Function math.tan(x). The tangent of the angle x

The math.tan(x) function returns the tangent of the argument x. The argument x is given in radians.

Example.

# Function math.tan(x, y)
import math

x = 1.0
y = math.tan(x) # y = 1.5574077246549023

x = 0.0
y = math.tan(x) # y = 0.0

 


Related topics