各位朋友,大家好!在本教程中,我们将学习如何用Python绘制数学函数。所以让我们开始吧。
先决条件
为了使用Python绘制不同的数学函数,我们需要以下两个Python库。
1.NumPy
NumPy 是一个支持多维数组和矩阵的Python库,并提供了大量的数学函数来操作NumPy数组和矩阵。它是科学计算的最基本库之一。我们可以使用以下命令在本地计算机上安装NumPy。
> python -m pip install numpy
2.2.Matplotlib
Matplotlib 是一个Python库,广泛用于各种类型的绘图。使用Matplotlib,我们可以非常容易地绘制静态和交互式可视化。我们可以使用以下命令在本地计算机上安装Matplotlib。
> python -m pip install matplotlib
绘制数学函数的步骤
首先在Python主程序(.py)或Jupyter笔记本(.ipynb)中使用以下Python命令导入numpy 和matplotlib.pyplot 模块。
import numpy as np
import matplotlib.pyplot as plt
对于所有的绘图,除了在各自的绘图中使用特定的NumPy数学函数之外,我们将遵循几乎相同的步骤。
1.绘制(y = x) 身份函数
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()
输出
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]
身份函数图
2.绘制(y=a.x2+b.x2+c) 二次函数
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()
输出
Values of x: [-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10]
Values of y: [153 120 91 66 45 28 15 6 1 0 3 10 21 36 55 78 105 136 171 210 253 300]
二次函数图
3.绘制(y = a.x3 + b.x2 + c.x + d)立体函数图
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()
输出
Values of x: [-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10]
Values of y: [-2334 -1731 -1242 -855 -558 -339 -186 -87 -30 -3 6 9 18 45 102 201 354 573 870 1257 1746 2349]
立体函数图
4.绘制(y = ln(x) 或 loge(x)):自然对数函数
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()
输出
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+00 2.39771344e+00 2.39780436e+00]
自然对数函数图
5.绘制(y = log10x)普通/小数对数函数图
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()
输出
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+00 1.04131372e+00 1.04135320e+00]
常用对数函数图
6.绘制(y=ex)自然指数函数图
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()
输出
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+04 5.86885543e+04 5.92783841e+04]
自然指数函数图
7.绘制(y =ax)一般指数函数图
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()
输出
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+09 8.24001604e+09 8.41315629e+09]
一般指数函数图
8.绘制(y = sign(x))Signum函数
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()
输出
Values of x: [-11. -10.999 -10.998 ... 10.997 10.998 10.999]
Values of y: [-1. -1. -1. ... 1. 1. 1.]
Signum函数图
9.绘制(y = a.sin(b.x + c)) 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()
输出
Values of x: [-11. -10.999 -10.998 ... 10.997 10.998 10.999]
Values of y: [ 2.02018823 2.03390025 2.04759397 ... -2.10016104 -2.11376421 -2.12734835]
正弦波函数图
10.绘制 (y = sinc(x))正弦函数
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()
输出
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-03
1.82029537e-03 9.09768439e-04]
正弦函数图
11.绘制(y = cosh(x))双曲函数
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()
输出
Values of x: [-11. -10.999 -10.998 ... 10.997 10.998 10.999]
Values of y: [29937.07086595 29907.14875865 29877.2565585 ... 29847.39423524 29877.25655813 29907.14875828]
双曲余弦函数图
归纳总结
在本教程中,我们已经学会了如何使用Numpy和Matplotlib库绘制不同类型的数学函数。希望你已经了解了不同数学函数的绘制过程,并准备好自己的实验。谢谢你的阅读!请继续关注我们,了解有关Python编程的精彩学习资源。