0% found this document useful (0 votes)
6 views

Plot Mathematical Functions

This tutorial provides a comprehensive guide on how to plot various mathematical functions using Python libraries NumPy and Matplotlib. It covers the installation of these libraries and demonstrates the plotting of functions such as identity, quadratic, cubic, logarithmic, exponential, sinusoidal, and hyperbolic functions. The document includes code snippets and expected outputs for each function, encouraging readers to experiment with plotting on their own.

Uploaded by

mahi.fatimazohra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Plot Mathematical Functions

This tutorial provides a comprehensive guide on how to plot various mathematical functions using Python libraries NumPy and Matplotlib. It covers the installation of these libraries and demonstrates the plotting of functions such as identity, quadratic, cubic, logarithmic, exponential, sinusoidal, and hyperbolic functions. The document includes code snippets and expected outputs for each function, encouraging readers to experiment with plotting on their own.

Uploaded by

mahi.fatimazohra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Plot Mathematical Functions – How to

Plot Math Functions in Python?


By Sanjay Kumar / August 28, 2021

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.

> python -m pip install numpy

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.

> python -m pip install matplotlib

Steps to Plot Mathematical Functions


First import the numpy and matplotlib.pyplot module in the main Python program
(.py) or Jupyter Notebook (.ipynb) using the following Python commands.

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.

1. Plot (y = x) Identity function

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]

Identity Function Plot


2 2
2. Plot (y = a.x + b.x + c) Quadratic function

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:

Values of x: [-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3


Values of y: [153 120 91 66 45 28 15 6 1 0 3 10 21 36 55

Quadratic Function Plot


3. Plot (y = a.x3 + b.x2 + c.x + d) Cubic function

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]

Cubic Function Plot


4. Plot (y = ln(x) or loge(x)) Natural logarithm function

x = np.arange(1, 11, 0.001)


y = np.log(x)
print('Values of x: ', x)
print('Values of y: ', y)

plt.plot(x, y)

plt.title("Natural logarithm Function")


plt.xlabel("Values of x")
plt.ylabel("Values of y")
plt.show()

Output:

Values of x: [ 1. 1.001 1.002 ... 10.997 10.998 10.999]


Values of y: [0.00000000e+00 9.99500333e-04 1.99800266e-03 ... 2.39762251e

Natural Logarithm Function Plot


5. Plot (y = log10x) Common/Decimal logarithm function

x = np.arange(1, 11, 0.001)


y = np.log10(x)
print('Values of x: ', x)
print('Values of y: ', y)

plt.plot(x, y)

plt.title("Common logarithm Function")


plt.xlabel("Values of x")
plt.ylabel("Values of y")
plt.show()

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

Common Logarithm Function Plot


x
6. Plot (y = e ) Natural Exponential function

x = np.arange(-11, 11, 0.01)


y = np.exp(x)
print('Values of x: ', x)
print('Values of y: ', y)

plt.plot(x, y)

plt.title("Natural exponential Function")


plt.xlabel("Values of x")
plt.ylabel("Values of y")
plt.show()

Output:

Values of x: [-11. -10.99 -10.98 ... 10.97 10.98 10.99]


Values of y: [1.67017008e-05 1.68695557e-05 1.70390975e-05 ... 5.81045934e
Natural Exponential Function Plot
x
7. Plot (y = a ) General Exponential function

x = np.arange(-11, 11, 0.01)


a = 8
y = a**x
print('Values of x: ', x)
print('Values of y: ', y)

plt.plot(x, y)

plt.title("General exponential Function")


plt.xlabel("Values of x")
plt.ylabel("Values of y")
plt.show()

Output:

Values of x: [-11. -10.99 -10.98 ... 10.97 10.98 10.99]


Values of y: [1.16415322e-10 1.18861455e-10 1.21358987e-10 ... 8.07043896e
General Exponential Function Plot
8. Plot (y = sign(x)) Signum function

x = np.arange(-11, 11, 0.001)


y = np.sign(x)

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:

Values of x: [-11. -10.999 -10.998 ... 10.997 10.998 10.999]


Values of y: [-1. -1. -1. ... 1. 1. 1.]
Signum Function Plot
9. Plot (y = a.sin(b.x + c)) Sinusoidal function in Python

x = np.arange(-11, 11, 0.001)


a = 5
b = 3
c = 2
y = a*np.sin(b*x + c)

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:

[-11. -10.999 -10.998 ... 10.997 10.998 10.999]


[ 2.02018823 2.03390025 2.04759397 ... -2.10016104 -2.11376421 -2.12734835]
Sinusoidal Function Plot
10. Plot (y = sinc(x)) Sinc function

x = np.arange(-11, 11, 0.01)


y = np.sinc(x)

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:

Values of x: [-11. -10.99 -10.98 ... 10.97 10.98 10.99]


Values of y: [1.41787526e-16 9.09768439e-04 1.82029537e-03 ... 2.73068428e
1.82029537e-03 9.09768439e-04]
Sinc Function Plot
11. Plot (y = cosh(x)) Hyperbolic function

x = np.arange(-11, 11, 0.001)


y = np.cosh(x)

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:

Values of x: [-11. -10.999 -10.998 ... 10.997 10.998 10.999]


Values of y: [29937.07086595 29907.14875865 29877.2565585 ... 29847.394235
Hyperbolic Cosine Function Plot

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.

← Previous Post Next Post →

Search... 

Recent Posts

The Power Of Python In Online Gambling Data Analysis

How Python Upgrades Everyday Randomness in Games

Step-By-Step Guide to Building a Text Summarizer Using Langchain and Ollama

A Quick Guide to Run LLMs Locally on PCs

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

How to Build a Crypto Wallet Using Python: A Step-by-Step Guide

Top Methods to Get Text from Images in 2024


Leveraging Python for Advanced Web Development Projects

7 Things You Might Not Know You Can Do With Python

Favorite Sites

GoLang Tutorials

VM-Help

Linux Tutorials

MySQL Tutorials

CodeForGeek

Mkyong

Copyright © 2024 AskPython · All Rights Reserved


Privacy Policy · Terms and Conditions · Contact · About · Team
AskPython is part of Diyansh IT Services Private Limited

You might also like