-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchebyshev_nodes.py
43 lines (41 loc) · 1.25 KB
/
chebyshev_nodes.py
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
42
43
import math
"""
Chebyshev's Polynomial
f(x) = 1/(1+25x^2)
The standard interval is [-1, 1]
Transforming it to our interval, we get x = z using x = 1/2[(b-a)z + (b+a)]
"""
def chebyshev_coefficients(f, n):
nodes = [math.cos(((2*i-1)/(2*n))*math.pi) for i in range(1,n+1)]
y = [f(i) for i in nodes]
print('i\t\tNode\t\t\tf(x)')
for i in range(n):
print(f'{i}\t\t{nodes[i]:.5f}\t\t{y[i]:.5f}')
diff = 1
final = []
for i in range(len(nodes)):
final.append(y[0])
newy = []
for j in range(1, len(y)):
newy.append((y[j]-y[j-1])/(nodes[i+j]-nodes[i+j-diff]))
y = newy
diff += 1
print("#"*50)
print("Chebyshev's Coefficient:")
for i in range(len(final)):
print(f'a{i} = {final[i]:0.5f}')
func = f'{final[0]}'
x_coeff = ''
nodes[len(nodes)//2] = 0
print("#"*50)
for i in range(1, len(final)):
if nodes[i-1] != 0:
x_coeff += f"(x{' - ' if nodes[i-1] > 0 else ' + '}{abs(nodes[i-1])})"
else:
x_coeff += "(x)"
func += f"{' + ' if final[i] > 0 else ' - '}{abs(final[i])}{x_coeff}"
print("P(x) = %s" % func)
if __name__ == "__main__":
f = lambda x: 1/(1+25*x**2)
n = 11
chebyshev_coefficients(f, n)