简体   繁体   English

勒让德多项式导数

[英]Legendre polynomials derivative

I use this code to create legendre polynomials, from 1st to the 7th order.我使用此代码创建从一阶到七阶的legendre多项式。

N = 4000
xvals = np.linspace(-1, 1, N)

def Legendre(x, n):
    leg = legendre(n)
    P_n = leg(x)
    return P_n

for i in range(1, 8):
    func = Legendre(xvals, i)
    plt.plot(xvals, func, '--', label="n =" + str(i))

It works well, but right now I am struggling with making derivatives from those functions.它运作良好,但现在我正在努力从这些函数中制作衍生物。 I decided to switch to numpy to do this, but it is quite poorly described and I stuck with this problem.我决定改用 numpy 来做到这一点,但它的描述很差,我一直在解决这个问题。

Here is the code:这是代码:

for i in range(1, 8):
    func = np.polynomial.legendre.Legendre.deriv(i)
    print func

UPDATE: Thanks to @James C.更新:感谢@James C。

I used his suggestion:我使用了他的建议:

le = np.polynomial.legendre.Legendre((1,2,3,4,5,6,7))

for i in range(1,8):
    print le.deriv(i)

And what I get something like this:我得到的是这样的:

leg([12. 45. 50. 84. 54. 77.])
leg([206. 312. 805. 378. 693.])
leg([ 690. 4494. 1890. 4851.])
leg([ 9345.  5670. 24255.])
leg([ 5670. 72765.])
leg([72765.])
leg([0.])

Unfortunately I am no mathematican, but is it the correct result?不幸的是,我不是数学家,但这是正确的结果吗? I need those derivatives to the equation and at this point I have no idea, how can I put those arrays into.我需要方程的这些导数,此时我不知道如何将这些数组放入。

Libraries: https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.polynomial.legendre.Legendre.deriv.html#numpy.polynomial.legendre.Legendre.deriv图书馆: https : //docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.polynomial.legendre.Legendre.deriv.html#numpy.polynomial.legendre.Legendre.deriv

It looks similar to the following:它看起来类似于以下内容:

Python Unbound Method TypeError Python 未绑定方法类型错误

You want an instance of the class first:你首先需要一个类的实例:

le = np.polynomial.legendre.Legendre((1,2,3,4,5))
le.deriv(4)

Just tested:刚刚测试:

import numpy as np


for i in range(1, 8):
    le = np.polynomial.legendre.Legendre((1,2,3,4,5,6,7,8,9))
    print le.deriv(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM