Plot Mathematical Functions
Plot Mathematical Functions
Hello folks! In this tutorial, we are going to learn how we can plot mathematical
functions using Python. So let’s get started.
Prerequisites
For plotting different mathematical functions using Python, we require the following
two Python libraries:
1. NumPy
NumPy is a Python library that supports multi-dimensional arrays & matrices and
offers a wide range of mathematical functions to operate on the NumPy arrays &
matrices. It is one of the most fundamental libraries for scientific computation. We
can install NumPy on our local computer using the following command.
2. Matplotlib
Matplotlib is a Python library that is widely used for various types of plotting. Using
Matplotlib, We can plot static and interactive visualizations very easily. We can install
Matplotlib on our local computer using the following command.
import numpy as np
import matplotlib.pyplot as plt
For all the plottings, we will follow almost the same steps apart from using the
specific NumPy mathematical function in the respective plots.
x = np.arange(0, 11, 1)
y = x
print('Values of x: ', x)
print('Values of y: ', y)
plt.plot(x, y)
plt.title("Identity Function")
plt.xlabel("Values of x")
plt.ylabel("Values of y")
plt.show()
Output:
Values of x: [ 0 1 2 3 4 5 6 7 8 9 10]
Values of y: [ 0 1 2 3 4 5 6 7 8 9 10]
x = np.arange(-11, 11, 1)
a = 2
b = 9
c = 10
y = a*(x**2) + b*x + c
print('Values of x: ', x)
print('Values of y: ', y)
plt.plot(x, y)
plt.title("Quadratic Function")
plt.xlabel("Values of x")
plt.ylabel("Values of y")
plt.show()
Output:
x = np.arange(-11, 11, 1)
a = 2
b = 3
c = 4
d = 9
y = a*(x**3) + b*(x**2) + c*x + d
print('Values of x: ', x)
print('Values of y: ', y)
plt.plot(x, y)
plt.title("Cubic Function")
plt.xlabel("Values of x")
plt.ylabel("Values of y")
plt.show()
Output:
3 4 5 6 7 8 9 10]
-3 6 9 18 45 102 201 354 573 870 1257 1746 2349]
plt.plot(x, y)
Output:
plt.plot(x, y)
Output:
Values of x: [ 1. 1.001 1.002 ... 10.997 10.998 10.999]
Values of y: [0.00000000e+00 4.34077479e-04 8.67721531e-04 ... 1.04127423e
plt.plot(x, y)
Output:
plt.plot(x, y)
Output:
print('Values of x: ', x)
print('Values of y: ', y)
plt.plot(x, y)
plt.title("Signum Function")
plt.xlabel("Values of x")
plt.ylabel("Values of y)")
plt.show()
Output:
print('Values of x: ', x)
print('Values of y: ', y)
plt.plot(x, y)
plt.title("Sinusoidal Function")
plt.xlabel("Values of x")
plt.ylabel("Values of y")
plt.show()
Output:
print('Values of x: ', x)
print('Values of y: ', y)
plt.plot(x, y)
plt.title("Sinc function")
plt.xlabel("Values of x")
plt.ylabel("Values of y")
plt.show()
Output:
print('Values of x: ', x)
print('Values of y: ', y)
plt.plot(x, y)
plt.title("Hyperbolic Function")
plt.xlabel("Values of x")
plt.ylabel("Values of y")
plt.show()
Output:
Summing-up
In this tutorial, we have learned how to plot different types of mathematical
functions using Numpy and Matplotlib libraries. Hope you have understood the
plotting process of different mathematical functions and are ready to experiment on
your own. Thanks for reading! Stay tuned with us for amazing learning resources on
Python programming.
Search...
Recent Posts
What List of Criteria Do Designers and Developers Have to Ensure a Smooth-Running Site?
Want To Become A Computer Programmer? Here Are 15 Courses Worth Looking Into
Favorite Sites
GoLang Tutorials
VM-Help
Linux Tutorials
MySQL Tutorials
CodeForGeek
Mkyong