python - 如何打印高达 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()
解决方案
推荐阅读
- php - Codeigniter 3 博客错误:分页仅显示第一页
- javascript - 多种选择形式由一个主选择控制
- r - R 错误“Selenium 消息:无法创建新服务:ChromeDriverService”
- c++ - C++ No Matching function call to Errors
- c# - docker/linux aspnet core 2.03 截断发布数据。这怎么可能?
- javascript - 函数内部的重叠循环
- javascript - 为什么 window.location.href.split("?") 数组本身正在改变它的值
- php - 从字符串中提取票号
- java - 如何遍历具有不同类型列表的单个活动并单击返回按钮应返回上一个列表
- php - 无法使用 jQuery 和 PHP 将动态数据正确加载到数据表中