Integrate Legendre polynomials in SymPy and use these integrals as coefficients
NickName:Oscar Reula Ask DateTime:2018-03-13T23:24:35

Integrate Legendre polynomials in SymPy and use these integrals as coefficients

I am trying to make a simple example in SymPy to compute some coefficients and then use them in a sum of legendre polynomials. Finally plot it. Very simple but can not make it work. I want to use it for my electromagnetism course. I get errors in both the attempts below:

%matplotlib inline
from sympy import *
x, y, z = symbols('x y z')
k, m, n = symbols('k m n', integer=True)
f, step, potential = symbols('f step potential', cls=Function)
var('n x')
A=SeqFormula(2*(2*m+1)*Integral(legendre(2*m+1,x),(x,0,1)).doit(),(m,0,oo)).doit()
Sum(A.coeff(m).doit()*legendre(2*m+1,x),(m,0,10)).doit()
B=Tuple.fromiter(2*(2*n+1)*Integral(legendre(2*n+1,x),(x,0,1)).doit() for n in range(50))
Sum(B[m]*legendre(2*m+1,x),(m,0,10)).doit()

Here is a part of an script in Mathematica of what I would like to replicate:

Nn = 50; 
Array[A, Nn] 
For[i = 0, i <= Nn, i++, A[i + 1] = Integrate[LegendreP[2*i + 1, x]*(2*(2*i + 1) + 1), {x, 0, 1}]]; 
Step = Sum[A[n + 1]*LegendreP[2*n + 1, #], {n, 0, Nn}] & Plot[Step[x], {x, -1, 1}]

Copyright Notice:Content Author:「Oscar Reula」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49259905/integrate-legendre-polynomials-in-sympy-and-use-these-integrals-as-coefficients

More about “Integrate Legendre polynomials in SymPy and use these integrals as coefficients” related questions

Integrate Legendre polynomials in SymPy and use these integrals as coefficients

I am trying to make a simple example in SymPy to compute some coefficients and then use them in a sum of legendre polynomials. Finally plot it. Very simple but can not make it work. I want to use i...

Show Detail

Generating Legendre Polynomials in Sympy

I have a project where I would like to use a set of associated legendre polynomials. I had the idea to generate the code for a set of degrees and orders by using sympy. I can get sympy to calculate

Show Detail

Sympy polynomials with `mpfr` coefficients?

I want to use Sympy's polynomials, but I also want to use higher-precision coefficients. Just Doing It seems to give me polynomials with sympy.core.numbers.float coefficients. import sympy from s...

Show Detail

Sympy step by step solution of integrals

In doc of sympy http://docs.sympy.org/latest/modules/integrals/integrals.html we can read: The manualintegrate module has functions that return the steps used (see the module docstring for more

Show Detail

Sympy step by step solution of integrals

In doc of sympy http://docs.sympy.org/latest/modules/integrals/integrals.html we can read: The manualintegrate module has functions that return the steps used (see the module docstring for more

Show Detail

Numerical integration Legendre Polynomials MATLAB

The Legendre polynomials are implemented in MATLAB as vectors, where you also get all the associated Legendre polynomials evaluated at a particular point x. Thus, I don't know how I can use these

Show Detail

Python and associated Legendre polynomials

I have been searching for a python implementation of the associated Legendre polynomials quite a long time and have found nothing satisfying me. There is an implementation in scipy.special, but it ...

Show Detail

Generalized associated Legendre Polynomials

Is there a simple way to calculate the generalized associated Legendre polynomials in Python (article)? I know that you can get the associated Legendre Polynomials using SciPy or pyshtools (articl...

Show Detail

Legendre polynomials derivative

I use this code to create legendre polynomials, from 1st to the 7th order. 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

Show Detail

Legendre Polynomials Python

I am trying to calculate the 720th Legendre polynomial with scipy: &gt;&gt;&gt;from scipy.special import legendre &gt;&gt;&gt;print(legendre(720)) it prints the powers correctly but returns NAN f...

Show Detail