本文介绍了使用 Matplotlib 和系数绘制多项式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码是:
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)
The result for this is straight lines that describe the points in 1,2,3,4,5 and the straight lines between them, instead of the polynomial of degree 5 that has 1,2,3,4,5 as its coeffiecients ( P(x) = 1 + 2x + 3x + 4x + 5x)
我应该如何绘制仅包含系数的多项式?
How am i suppose to plot a polynomial with just its coefficients?
推荐答案
Eyzuky,看看这是否是您想要的:
Eyzuky, see if this is what you want:
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 {ord}.')
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()
这篇关于使用 Matplotlib 和系数绘制多项式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!