Numpy Sin Implementation : Generate Sine Wave and Plot it

Numpy Sin Implementation

Numpy is the best python module that can perform mathematical calculations efficiently. There are many methods to perform it and NumPy sin is one of them. It allows you to calculate trigonometric sin for all input array elements (in radians). In this entire tutorial, you will know how to generate a NumPy sin wave and also how to plot it.

But first going to the coding demonstration part lets know the method.

numpy.sin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

The explanation of the major parameters is below.

x: It is your input array. Its element will be in radians.

out: If you want to store your output in another array then use it. 

Examples on Implementation of Numpy Sin

Example 1: Simple Use of Numpy Sin

In this example rather than putting NumPy array as an argument, I will put a single element. Let’s use np.p1/2.

Copy, paste the code and execute it.

import numpy as np
import matplotlib.pyplot as plt

print(np.sin(np.pi/2))

Output

Simple Use of Numpy Sin
Simple Use of Numpy Sin

You can verify the output using your mathematical skills. In trigonometry sin(pi/2) is 1.

Example 2: Generate Sin wave using  the array of elements

The above example was using a single value. But in this example, I will pass the array of elements in radian. After executing the following lines of code you will get the sin output for each of the elements.

import numpy as np
import matplotlib.pyplot as plt
array = np.array([0,np.pi/2,np.pi/3,np.pi/4,np.pi/5,np.pi])
print(np.sin(array))

Output

Generate Sin wave using  the array of elements
Generate Sin wave using  the array of elements

You can verify the results using your calculation or scientific calculator. In the next example, You will plot the sin wave using matplotlib.

Example 3: Generate and Plot the Numpy Sin wave

Firstly, I will create a NumPy array with elements in radians. Then after that, I will Plot the results using Matplotlib. Copy, Paste and execute the code.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 10)
y = np.sin(x)
plt.plot(x, y)
plt.xlabel('Angle in radians')
plt.ylabel('sin(x)')
plt.axis('tight')
plt.show()

Explanation of  the Code

Here I am taking input as x variable, which will use the np.linspace() to generate 10 values.  The variable y will be the results of sin(x). After that, I will plot the graph using the plot(x,y). Lastly, to make the graph more readable, I will label the graph using xlabel() and ylabel().

When you run the above code you will get the output as below.

Output

Generate and Plot the Sin wave
Generate and Plot the Sin wave

Conclusion

There are many methods in NumPy for doing trigonometry calculation. The numpy.sin() is one of them. Hope you have understood how to generate NumPy sin wave after implementing the above examples. Even if you have any doubts then you can contact us for more help.

Source:

Numpy Sin Offical Documentation

 

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner