This is the first tutorial for the series **NumPy Trigonometric Functions**. In this due course, we will be learning about Trigonometric Functions provided by the NumPy library with examples.

You must have calculated the **sine** of a particular angle and gotten the result in the range **[-1, 1]**. Well, the sine operation can be performed on the array as well. That’s where the **NumPy** library for Python plays an important role.

NumPy provides an extensive collection of high-level mathematical functions to operate on arrays.

In this tutorial, we will learn about how to use the NumPy sin function along with examples that will help you get a good understanding of it. We will also plot the graph of the sin function using the **Matplotlib Library**.

Let’s get started.

## What is NumPy Sin?

NumPy provides a lot of mathematical functions that can be performed on multi-dimensional arrays and `numpy.sin`

is one of the trigonometric functions provided by the NumPy library.

Note: `numpy.sin`

computes the trigonometric sine of a single number as well as a Numpy array of angles.

**Suggested Reading:** An Introduction to NumPy Arrays

## Pre-Requisites

- The latest version of python must be installed on your systems, it can be installed from https://www.python.org/downloads/
- Numpy must be installed on your systems, execute the following command to install the latest version of Numpy:

```
pip install numpy
```

- You can use any IDE of your choice. I would recommend using Jupyter Notebook, which can be installed by executing the following command:

```
pip install jupyter
```

## Setting up the environment

- Create a folder and open it in terminal

- Execute the following command to open Jupyter Notebook:

```
jupyter notebook
```

- Click on
`New`

button and select Python 3(ipykernel)

There you go a new untitled notebook in Jupyter is created in which we will be writing the code.

## Basic syntax of Numpy Sin function

The Numpy sin function takes an angle in **radians** as an argument. However, the angle in **degrees** can also be given as the argument.

where input can be a single number as well as a NumPy Array**Syntax: **numpy.sin(input)

## Calculating the Sine of an Angle Using Numpy Sin

Let’s now start playing with the numpy sin function to get a better understanding of how it works.

### Calculating Using the Numpy Sin Function

```
import numpy as np
print("Sine of 0 is :",np.sin(0))
print("Sine of pi/2 radians is :",np.sin(np.pi/2))
print("Sine of 3pi/2 radians is :",np.sin(3*np.pi/2))
```

Output

```
Sine of 0 is : 0.0
Sine of pi/2 radians is : 1.0
Sine of 3pi/2 radians is : -1.0
```

**Note:** After writing code in each cell in Jupyter Notebook hit `shift + enter`

to get the output.

In the above code snippet, the angle in radian is passed as an argument to np.sin().

Now, let’s see how can we pass angles in degrees as an argument to the np.sin() function.

### Combining Numpy Sin with deg2rad Function

To compute the sine of angles in which the argument for the sin function is in **degrees** a function `deg2rad`

is used.

```
import numpy as np
print("Sine of 45 degrees is :",np.sin(np.deg2rad(45)))
print("Sine of 60 degrees is :",np.sin(np.deg2rad(60)))
print("Sine of 90 degrees is :",np.sin(np.deg2rad(90)))
print("Sine of 270 degrees is :",np.sin(np.deg2rad(270)))
```

**Output**

```
Sine of 45 degrees is : 0.7071067811865476
Sine of 60 degrees is : 0.8660254037844386
Sine of 90 degrees is : 1.0
Sine of 270 degrees is : -1.0
```

**Note:** It’s really simple to convert angles(in degrees) to radians, just multiply `numpy.pi/180`

by the angle in degrees.

Now, let’s understand how can we perform the sine operation on an array of angles.

## Calculating the Sine of an Array of Angles

The sin function takes angles in radians as an argument so while using the sin function on the** NumPy array **we must make sure that the elements of the array are converted to radians.

### Calculating Sine Of A Numpy Array

In this snippet, a NumPy array is created using `numpy.array`

.

```
import numpy as np
a = np.array((0 , 30 , 45 , 60 , 90))
print("Sine Values :\n",np.sin(a*np.pi/180))
```

**Output**

```
Sine Values :
[0. , 0.5 , 0.70710678 , 0.8660254 , 1. ]
```

- We initialize the
- Then, each of the elements of the NumPy Array is multiplied by
`np.pi/180`

which is passed as an argument to`np.sin()`

. - The sine of each element is computed and stored in a NumPy Array, which we get as output.

### Calculating Sine of an evenly-spaced NumPy Array

In this code snippet, we will create a NumPy array of **10 evenly spaced** values using `NumPy.linspace`

.

```
import numpy as np
b = np.linspace(-np.pi,np.pi,10)
print("Printing the NumPy Array:",b)
print("\n")
print("Sine Values :",np.sin(b))
```

- We create a NumPy array having 10 evenly spaced values from
`-pi`

to`pi`

and pass it as an argument to`np.sin()`

. - The values in the NumPy Array are already in radians so the sine of each value is computed and stored in a NumPy Array.

Output

```
Printing the NumPy Array: [-3.14159265 -2.44346095 -1.74532925 -1.04719755 -0.34906585 0.34906585
1.04719755 1.74532925 2.44346095 3.14159265]
Sine Values : [-1.22464680e-16 -6.42787610e-01 -9.84807753e-01 -8.66025404e-01
-3.42020143e-01 3.42020143e-01 8.66025404e-01 9.84807753e-01
6.42787610e-01 1.22464680e-16]
```

- The output looks quite strange as all the computed sine values are expressed in scientific notations.
- For example, sin(pi) is equal to 0, and in the above output, the computed sine value of pi is a very large quantity having an exponent of 10
^{-16 }which is equal to 0.

So, that was all about using the NumPy Sin function with a single number as an argument and a NumPy array as an argument. I would recommend writing the code while going through this tutorial.

## Visualizing the Sin Function

The first step is to install the Matplotlib Library:

```
pip install matplotlib
```

After the installation gets completed we are ready to code so let’s get started.

```
import numpy as np
# Importing the Matplotlib Library
import matplotlib.pyplot as plt
# Creating a NumPy Array of 30 evenly-spaced elements
a = np.linspace((-2*np.pi),(2*np.pi),30)
# Storing the sine values in a NumPy Array
b = np.sin(a)
plt.plot(a, b, color = "red", marker = "o")
plt.title("numpy.sin()")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
```

**Output**

function is used to plot the Sine Function which takes four arguments.**plt.plot()**

- The
**first**argument is the**NumPy Array of angles**(created in Line No 7), plotted on the X-axis(Horizontal Axis). - The
**second**argument is the NumPy Array of computed sine values, plotted on the Y-axis(Vertical Axis). - The
**third**argument is the color of the plot. - The
**fourth**argument is the marker value which emphasizes each point with a specified marker. There are different types of markers that can be used to denote the points on the curve.

function is used to set the title of the plot.**plt.title()**

function is used to give the name to the horizontal axis on the plot. Similarly, **plt.xlabel()**

function is used to give the name to the vertical axis on the plot.**plt.ylabel()**

is used to display the curve plot.**plt.show()**

You have successfully plotted the sine curve.

## Summary

In this tutorial, we understood how to use the NumPy Sin function with examples. If you are using Jupyter Notebook then after writing each line of code in each cell press `shift+enter`

to get the output. **Your task** is to use the NumPy Sin function to compute the sine of more values of your choice.

In the next tutorial, we will learn about NumPy Cos Function. Till then keep learning and go through this article twice, practice the codes along with going through the tutorial.

## References

NumPy Documentation – NumPy Sin