forked from agezerlis/NumericalMethodsPhysicsWithPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarycentric.py
More file actions
41 lines (35 loc) · 970 Bytes
/
barycentric.py
File metadata and controls
41 lines (35 loc) · 970 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
33
34
35
36
37
38
39
40
41
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
import numpy as np
def f(x):
return 1/(1 + 25*x**2)
def generatedata(n,f,nodes="cheb"):
if nodes=="cheb":
dataxs = -np.cos(np.linspace(0,np.pi,n))
else:
dataxs = np.linspace(-1,1,n)
datays = f(dataxs)
return dataxs, datays
def weights(dataxs):
n = dataxs.size
ws = np.ones(n)
for k in range(n):
for j in range(n):
if j == k:
continue
ws[k] *= (dataxs[k]-dataxs[j])
return 1/ws
def bary(dataxs,datays,ws,x):
k = np.where(x == dataxs)[0]
if k.size == 0:
nume = np.sum(ws*datays/(x-dataxs))
denom = np.sum(ws/(x-dataxs))
val = nume/denom
else:
val = datays[k[0]]
return val
if __name__ == '__main__':
dataxs, datays = generatedata(15, f)
ws = weights(dataxs)
x = 0.3; pofx = bary(dataxs, datays, ws, x)
print(x, pofx, f(x))