-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegroots.py
More file actions
32 lines (26 loc) · 714 Bytes
/
legroots.py
File metadata and controls
32 lines (26 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
from legendre import legendre
import numpy as np
def legnewton(n,xold,kmax=200,tol=1.e-8):
for k in range(1,kmax):
val, dval = legendre(n,xold)
xnew = xold - val/dval
xdiff = xnew - xold
if abs(xdiff/xnew) < tol:
break
xold = xnew
else:
xnew = None
return xnew
def legroots(n):
roots = np.zeros(n)
npos = n//2
for i in range(npos):
xold = np.cos(np.pi*(4*i+3)/(4*n+2))
root = legnewton(n,xold)
roots[i] = -root
roots[-1-i] = root
return roots
if __name__ == '__main__':
roots = legroots(9); print(roots)