forked from agezerlis/NumericalMethodsPhysicsWithPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgauleg.py
More file actions
26 lines (22 loc) · 614 Bytes
/
gauleg.py
File metadata and controls
26 lines (22 loc) · 614 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
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
from legendre import legendre
from legroots import legroots
from newtoncotes import f
import numpy as np
def gauleg_params(n):
xs = legroots(n)
cs = 2/((1-xs**2)*legendre(n,xs)[1]**2)
return xs, cs
def gauleg(f,a,b,n):
xs, cs = gauleg_params(n)
coeffp = 0.5*(b+a)
coeffm = 0.5*(b-a)
ts = coeffp + coeffm*xs
contribs = cs*f(ts)
return coeffm*np.sum(contribs)
if __name__ == '__main__':
ans = np.log(1 + np.sqrt(2))
print(ans)
for n in range(2,10):
print(n, gauleg(f,0.,1,n))