The world’s leading publication for data science, AI, and ML professionals.

Trigonometric Functions in Python: Complete Guide

In this article we will explore trigonometric functions in Python

Image by Author
Image by Author

Table of contents

  • Introduction
  • Sine function
  • Inverse sine function
  • Cosine function
  • Inverse cosine function
  • Tangent function
  • Inverse tangent function
  • Conclusion

Introduction

Since this tutorial is focused on trigonometric functions, we will need to have a few important definitions to better understand each function.

Trigonometry

Trigonometry is a branch of mathematics that studies relationships between angles and lengths of sides in triangles.

The three main trigonometric functions used in trigonometry are: sine, cosine, and tangent, which are based on the right triangle.

Right Triangle

What is a right triangle? It’s a triangle with a right angle (or 90°) angle).

See example below:

Image by Author
Image by Author

Here, angle C is the right angle (angle C = 90°), which now forms a right triangle ABC.

In addition, we should learn the names of the sides of the right triangle with respect to some chosen angle θ.

See below:

Image by Author
Image by Author
  • Adjacent – the side always adjacent to the angle θ and also forms the right angle (90°).
  • Opposite – the side opposite of the angle θ.
  • Hypotenuse – the side opposite of the right angle (90°) and also is the longest side in the right triangle.

Radian

Another important concept in Geometry is radian. Radian is a unit of angle in the center of a circle which is created by the radius of the circle connected to an arc of an equal length with radius.

The formal definition sounds very complicated and challenging to follow, so it’s much easier to work through this definition visually.

See below:

Image by Author
Image by Author

Step by step explanation:

  1. Start at point O and draw a horizontal line to point P, which creates the segment OP with a length of a radius (r).
  2. From point P create a segment PT which is perpendicular to segment OP (angle = 90°), and is of the same length as segment OP which is equal to radius (r). You should have equal lengths: OP=PT.
  3. Next, imagine you were to start bending the segment TP to the left until it fits the circumference. One you do it, you have a projection of segment TP, which is an arc VP. You should have equal lengths: TP=VP.

After these steps, we have created an angle (angle VOP) which is what we refer to as 1 radian:

Image by Author
Image by Author

This angle is exactly 1 radian, or can also be calculated in degrees as:

Image by Author
Image by Author

Alternatively, we can calculate degrees in radians:

Image by Author
Image by Author

Sine function

Sine function is one of the main trigonometric functions.

Sine function explained

The sine function of angle θ is the ratio of the side of the triangle opposite the angle θ and the hypotenuse:

Image by Author
Image by Author

It is also often referred to as SOH: Sine is Opposite over Hypotenuse.

Image by Author
Image by Author

There is also a table that provides values of sine values for standard angles:

Image by Author
Image by Author

Sine function calculation example

We have a right triangle ABC, where AB = 20cm, angle A = 30°, and angle C = 90° as shown in the figure below. We need to find the length of BC.

Image by Author
Image by Author

From the table in the previous section we know that sin 30 = 1/2, and we can use this to solve the question in example:

Image by Author
Image by Author

In this example, using the sin function we were able to find the length of side BC which is equal to 10 cm.

Sine function sin() in Python

In this section we will try to solve the above example using Python.

In order use the sine function sin() in Python we will need to import it from math library (which is built-in).

Let’s begin with importing the required functions:

Next, convert degrees into radians:

And calculate the ratio (note that you should round it to 2 decimals):

And you should get:

0.5

Which is exactly the same ratio of sin 30 as we saw in the previous section. We found the same result by using trigonometric functions in Python.

The rest of the calculations would be the same and you will get the answer equal to 10 cm.


Inverse sine function

Inverse sine function is one of the inverse trigonometric functions often referred to as arc sine.

Inverse sine function explained

The inverse sine function of the ratio of the side of the triangle opposite the angle θ and the hypotenuse is angle θ.

It doesn’t sound as intuitive as it actually looks.

Recall that the sine function takes angle θ and gives the ratio of {Opposite}/{Hypotenuse}.

Now, the inverse sine function takes the ratio of {Opposite}/{Hypotenuse} and gives angle θ:

Image by Author
Image by Author
Image by Author
Image by Author

Inverse sine function calculation example

We have a right triangle ABC, where AB = 20cm, BC = 10cm, and angle C = 90° as shown in the figure below. We need to find angle A.

Image by Author
Image by Author

First, we will find the ratio of {Opposite}/{Hypotenuse}:

Image by Author
Image by Author

When we know the ratio, we can easily find the angle θ using any calculator and the arcsin function.

You should get:

Image by Author
Image by Author

Inverse sine function asin() in Python

In this section we will try to solve the above example using Python.

In order use the inverse sine asin() function in Python we will need to import it from math library (which is built-in).

Let’s begin with importing the required functions:

Next, find the radian measure of angle of a ratio equal to 1/2:

And you should get:

0.5235987755982989

Then finally convert the radian measure to degrees (and round it):

And you should get:

30

We found that the inverse sine of a 1/2 ratio is angle equal to 30° by using trigonometric functions in Python.


Cosine function

Sine function is the second most popular trigonometric function.

Cosine function explained

The cosine function of angle θ is the ratio of the side of the triangle adjacent to the angle θ and the hypotenuse:

Image by Author
Image by Author

It is also often referred to as CAH: Cosine is Adjacent over Hypotenuse.

Image by Author
Image by Author

There is also a table that provides values of cosine values for standard angles:

Image by Author
Image by Author

Cosine function calculation example

We have a right triangle ABC, where AB = 20cm, angle B = 60°, and angle C = 90° as shown in the figure below. We need to find the length of BC.

Image by Author
Image by Author

From the table in the previous section we know that cos 60 = 1/2, and we can use this to solve the question in example:

Image by Author
Image by Author

In this example, using the cos function we were able to find the length of side BC which is equal to 10 cm.

Cosine function cos() in Python

In this section we will try to solve the above example using Python.

In order use the cosine function cos() in Python we will need to import it from math library (which is built-in).

Let’s begin with importing the required functions:

Next, convert degrees into radians:

And calculate the ratio (note that you should round it to 2 decimals):

And you should get:

0.5

Which is exactly the same ratio of cos 60 as we saw in the previous section. We found the same result by using trigonometric functions in Python.

The rest of the calculations would be the same and you will get the answer equal to 10 cm.


Inverse cosine function

Inverse cosine function is one of the inverse trigonometric functions often referred to as arc cosine.

Inverse cosine function explained

The inverse cosine function of the ratio of the side of the triangle adjacent to the angle θ and the hypotenuse is angle θ.

Recall that the cosine function takes angle θ and gives the ratio of {Adjacent}/{Hypotenuse}.

Now, the inverse cosine function takes the ratio of {Adjacent}{Hypotenuse} and gives angle θ:

Image by Author
Image by Author
Image by Author
Image by Author

Inverse cosine function calculation example

We have a right triangle ABC, where AB = 20cm, BC = 10cm, and angle C = 90° as shown in the figure below. We need to find angle B.

Image by Author
Image by Author

First, we will find the ratio of {Adjacent}/{Hypotenuse}:

Image by Author
Image by Author

When we know the ratio, we can easily find the angle θ using any calculator and the arccos function.

You should get:

Image by Author
Image by Author

Inverse cosine function acos() in Python

In this section we will try to solve the above example using Python.

In order use the inverse sine acos() function in Python we will need to import it from math library (which is built-in).

Let’s begin with importing the required functions:

Next, find the radian measure of angle of a ratio equal to 1/2:

And you should get:

1.0471975511965979

Then finally convert the radian measure to degrees (and round it):

And you should get:

60.0

We found that the inverse cosine of a 1/2 ratio is angle equal to 60° by using trigonometric functions in Python.


Tangent function

Tangent function is the third popular trigonometric function.

Tangent function explained

The tangent function of angle θ is the ratio of the side of the triangle opposite to the angle θ and the side of the triangle adjacent to the angle θ:

Image by Author
Image by Author

It is also often referred to as TOA: Tangent is Opposite over Adjacent.

Image by Author
Image by Author

There is also a table that provides values of tangent values for standard angles:

Image by Author
Image by Author

Tangent function calculation example

We have a right triangle ABC, where BC = 10cm, angle A = 30°, and angle C = 90° as shown in the figure below. We need to find the length of AC.

Image by Author
Image by Author

From the table in the previous section we know that tan 30 = {1}/{sqrt{3}}, and we can use this to solve the question in example:

Image by Author
Image by Author

In this example, using the tan function we were able to find the length of side BC which is equal to 17.32 cm.

Tangent function tan() in Python

In this section we will try to solve the above example using Python.

In order use the tangent function tan() in Python we will need to import it from math library (which is built-in).

Let’s begin with importing the required functions:

Next, convert degrees into radians:

And calculate the ratio (note that you should round it to 2 decimals):

And you should get:

0.58

In this case, the ratio is 0.58 which is approximately equal to {1}/{sqrt{3}}, which is exactly the same ratio of tan 30 as we saw in the previous section. We found the same result by using trigonometric functions in Python.

The rest of the calculations would be the same and you will get the answer equal to 17.24 cm (whereas the calculated without rounding would be 17.32 cm).


Inverse tangent function

Inverse tangent function is one of the inverse trigonometric functions often referred to as arc tangent.

Inverse tangent function explained

The inverse tangent function of the ratio of the side of the triangle opposite to the angle θ and the side of the triangle adjacent to the angle θ.

Recall that the tangent function takes angle θ and gives the ratio of {Opposite}/{Adjacent}.

Now, the inverse tangent function takes the ratio of {Opposite}/{Adjacent} and gives angle θ:

Image by Author
Image by Author
Image by Author
Image by Author

Inverse tangent function calculation example

We have a right triangle ABC, where BC = 10 cm, AC = 17.32 cm, and angle C = 90° as shown in the figure below. We need to find angle B.

Image by Author
Image by Author

First, we will find the ratio of {Opposite}/{Adjacent}:

Image by Author
Image by Author

When we know the ratio, we can easily find the angle θ using any calculator and the arctan function.

You should get:

Image by Author
Image by Author

Inverse tangent function atan() in Python

In this section we will try to solve the above example using Python.

In order use the inverse sine atan() function in Python we will need to import it from math library (which is built-in).

Let’s begin with importing the required functions:

Next, find the radian measure of angle of a ratio equal to 0.58:

And you should get:

0.5255837935516101

Then finally convert the radian measure to degrees (and round it):

And you should get:

30.11

We found that the inverse cosine of a 0.58 ratio is angle equal to 30.11° by using trigonometric functions in Python, which is approximately equal to 30° if we didn’t have the length of side AC rounded to 17.32 cm.


Conclusion

In this article we will focused on a complete walkthrough of trigonometric functions in Python using functions from math library. It includes sine, cosine, tangent, inverse sine, inverse cosine, and inverse tangent.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Statistics articles.


Originally published at https://pyshark.com on December 29, 2021.


Related Articles