我的代码是:
import numpy as np
import matplotlib as plt
polyCoeffiecients = [1,2,3,4,5]
plt.plot(PolyCoeffiecients)
plt.show()
结果是描述 1,2,3,4,5 中的点的直线以及它们之间的直线,而不是具有 1,2,3,4,5 的 5 次多项式作为它的系数 ( P(x) = 1 + 2x + 3x + 4x + 5x)
我该如何绘制仅包含其系数的多项式?
最佳答案
Eyzuky,看看这是不是你想要的:
import numpy as np
from matplotlib import pyplot as plt
def PolyCoefficients(x, coeffs):
""" Returns a polynomial for ``x`` values for the ``coeffs`` provided.
The coefficients must be in ascending order (``x**0`` to ``x**o``).
"""
o = len(coeffs)
print(f'# This is a polynomial of order {o}.')
y = 0
for i in range(o):
y += coeffs[i]*x**i
return y
x = np.linspace(0, 9, 10)
coeffs = [1, 2, 3, 4, 5]
plt.plot(x, PolyCoefficients(x, coeffs))
plt.show()
关于python - 使用 Matplotlib 和系数绘制多项式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37352098/