I want to integrate a piecewise defined function that is multiplied by Legendre polynomials. Unfortunately, I cannot find how to use the nth Legendre polynomial x in the documentation . I want to integrate every Legendre polynomial from x when n = 1,..., 50, so I set n = np.arange(1, 51, 1).
import numpy as np
import pylab
from scipy import integrate
n = np.arange(1, 51, 1)
def f(x):
if 0 <= x <= 1:
return 1
if -1 <= x <= 0:
return -1
I suppose I need to define another function, say u(x).
c = []
def u(x):
c.append((2. * n + 1) / 2. * integrate.quad(f(x) * insert Legendre polynomials here, -1., 1.))
return sum(c * Legendre poly, for nn in range(1, 51))
So, I would then return some u(x)with the first 50 members extending my piecewise function to Legendre polynomials.
Change 1:
If this is not done, I could use the Rodrigas Formula to calculate the nth Legendre polynomial. However, I could not find anything useful when I was looking for calculations of the nth derivative in Python.
P_n(x) = \frac{1}{2^n n!}\frac{d^n}{dx^n}(x^2 - 1)^n
, , - , Python.
2:
, :
import numpy as np
from scipy.integrate import quad
def f(x, coef):
global p
p = np.polynomial.legendre.Legendre(coef=coef)
if 0 <= x <= 1:
return 1*p(x)
if -1 <= x <= 0:
return -1*p(x)
c = []
for n in range(1, 51):
c.append((2. * n + 1.) / 2. * quad(f, -1, 1, args=range(1,n+1))[0])
def g(x)
return sum(c * p(x) for n in range(1, 51))
, c, . 1.5, 0, -7/8, 0, ...
, g, x = np.linspace(-1, 1, 500000), , c - 50. ?