使用 Matplotlib 和系数绘制多项式

2024-05-07

我的代码是:

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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Matplotlib 和系数绘制多项式 的相关文章

随机推荐