首页 > 解决方案 > 如何打印高达 Pn+1(x) 的所有多项式

问题描述

这是一个练习,您使用 numpy 中的多项式例程递归地生成勒让德多项式。除了打印直到 Pn+1(x) 的所有多项式外,我已经完成了所有部分。我怎样才能添加一条线来实现这个目标。有人,请帮助!

import numpy as np
import matplotlib.pyplot as plt

from scipy import integrate


def P(n, x):
    if(n == 0):
        return 1 # P0 = 1
    elif(n == 1):
        return x # P1 = x
    else:
        return N_f(n) * x * P(n - 1, x) + alpha_f(n)*P(n-2, x)


def norm_f(x, n):
    return P(n-1, x) * P(n-1, x)


def overlap_f(x, n):
    return x*P(n, x) * P(n-1, x)


def alpha_f(n):
    return -(n-1)/float(n)


def N_f(n):
    return ((2 * n)-1)/float(n)


def norm_n(n):
    return integrate.quad(norm_f(n), -1, 1)


def overlap_n(n):
    return integrate.quad(overlap_f(n), -1, 1)


max_n = int(input('input the max n: '))

# Creating an array of x values
X = np.linspace(-1, 1, 200)

x = np.linspace(-1, 1, 200)

for i in range(1, 7):
    plt.plot(x, P(i, x), label="P" + str(i))


plt.legend(loc="best")
plt.xlabel("X")
plt.ylabel("Pn")
plt.savefig('plot_legend.png')
plt.show()

    

标签: python

解决方案


推荐阅读