I'd like to consecutively plot the zeros of several Jacobi polynomials. The parameters and degree of the Jacobi polynomials depend on a certain size variable n. I want to compute the roots of each polynomial by first extracting the coefficients of the polynomial into an array. However, this cannot be done with the coeffs()
command since the Jacobi polynomial is of class sympy.core.add.Add
, which does not have coeffs()
as an attribute. Any suggestions on how to overcome this problem?
import matplotlib.pyplot as plt
init_printing()
x = Symbol("x")
def show_roots(n,a,b,c):
for k in range (1,n+1):
p = jacobi(a*k,-(a+b)*k,(b+c)*k,x)
coeff = p.coeffs(x)
roots = numpy.roots(coeff)
plt.plot(roots)
plt.show()
plt.pause(3)
The error I am shown when I try out show_roots with specific values:
AttributeError: 'Add' object has no attribute 'coeffs'
You need to convert Add
to Poly
first then call coeffs()
on the result.
import matplotlib.pyplot as plt
import sympy
import numpy
x = sympy.symbols('x')
def show_roots(n,a,b,c):
for k in range (1,n+1):
p = sympy.jacobi(a*k,-(a+b)*k,(b+c)*k,x)
coeff = sympy.poly(p).coeffs()
roots = numpy.roots(coeff)
print(roots)
show_roots(3,1,2,3)
gives
[2.]
[2.+0.65465367j 2.-0.65465367j]
[2.24801968+0.j 1.87599016+0.92968658j 1.87599016-0.92968658j]
You can now do the plotting stuff.