This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
polynom.py
90 lines (64 loc) · 2.21 KB
/
polynom.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
Documentation: https://en.wikipedia.org/wiki/Newton_polynomial
"""
import matplotlib.pyplot as plt
import numpy as np
def divided_diff(x: list, y: list) -> list:
"""
Compute the divided differences table
and return the coefficients of the interpolating polynomial.
Args:
x (list): The list of x values.
y (list): The list of y values.
Returns:
list: The list of coefficients.
Doctests:
>>> divided_diff([0, 1, 2], [1, 2, 3])
[1.0, 1.0, 0.0]
Documentation:
https://en.wikipedia.org/wiki/Divided_differences
"""
n = len(y)
coef = np.zeros([n, n])
coef[:, 0] = y
for j in range(1, n):
for i in range(n - j):
coef[i][j] = (coef[i + 1][j - 1] - coef[i][j - 1]) / (x[i + j] - x[i])
return list(coef[0])
def newton_poly(coef, x_data, x):
"""
Compute the value of the Newton's polynomial at the point x.
Args:
coef (list): The list of coefficients.
x_data (list): The list of x values.
x (float): The point at which the value of the polynomial is computed.
Returns:
float: The value of the polynomial at the point x.
Doctests:
>>> newton_poly([1, 1, 0], [0, 1, 2], 0.5)
1.5
Documentation:
https://en.wikipedia.org/wiki/Newton_polynomial
"""
n = len(x_data) - 1
p = coef[n]
for k in range(1, n + 1):
p = coef[n - k] + (x - x_data[n - k]) * p
return p
if __name__ == "__main__":
print("Find the coefficients of the Newton's polynom and plot it.")
print("x_k = -5 + k * 10 / n, y_k = 1 / (1 + x_k^2), k = 0, 1, 2, 3, ..., n, n = 4, 5, ..., 15")
print()
plt.figure(figsize=(10, 10), dpi=200)
plt.title("Newton's polynom")
for n in range(4, 16, 1):
x = [-5 + k * 10 / n for k in range(n + 1)]
y = [1 / (1 + x_k**2) for x_k in x]
coefficients = divided_diff(x, y)
print(f"n = {n}, coef = {coefficients}")
plt.subplot(4, 3, n - 3)
plt.ylim(-0.5, 1.5)
plt.grid()
plt.scatter(x, y, label=f"n = {n}")
plt.plot(np.linspace(-5, 5, 100), newton_poly(coefficients, x, np.linspace(-5, 5, 100)))
plt.savefig("lesson_5/polynom.png")