Skip to content

Commit 27b6d0b

Browse files
author
pmrukot
committed
Add approximation assignment
1 parent 0057751 commit 27b6d0b

File tree

6 files changed

+178
-0
lines changed

6 files changed

+178
-0
lines changed

Diff for: Approximation/errors.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import numpy as np
2+
3+
4+
def calculate_mean_square_error(n, function_y, interpolated_y):
5+
error = 0
6+
for i in range(0, n):
7+
error += (function_y[i] - interpolated_y[i])**2
8+
error /= n
9+
return error
10+
11+
12+
def calculate_max_error(n, function_y, interpolated_y):
13+
max_error = 0
14+
for i in range(0, n):
15+
max_error = max(np.fabs(function_y[i] - interpolated_y[i]), max_error)
16+
return max_error
17+
18+
19+
def print_error(N, x_len, y_values, y_interpolated, label):
20+
print('{0},{1},{2},{3}'.format(label, N,
21+
calculate_mean_square_error(x_len, y_values, y_interpolated),
22+
calculate_max_error(x_len, y_values, y_interpolated)
23+
))

Diff for: Approximation/function.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import numpy as np
2+
3+
4+
def fun(x):
5+
return np.exp(3*np.cos(2*x))
6+
7+
8+
def fun_der(x):
9+
return -6.*np.sin(2*x)*np.exp(3*np.cos(2*x))

Diff for: Approximation/main.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
from errors import print_error
5+
from function import fun
6+
from polynomial_approx import polynomial_approximate
7+
from points import get_regular_interpolation_nodes, get_czebyshev_interpolation_nodes
8+
from trigonometric_approx import trigonometric_approximate
9+
10+
if __name__ == '__main__':
11+
start = -2.*np.pi
12+
end = 1.*np.pi
13+
N = 7
14+
step = 0.05
15+
function = fun
16+
17+
x = np.arange(start, end, step)
18+
y = list(map(function, x))
19+
20+
x_len = len(x)
21+
22+
#points = get_regular_interpolation_nodes(N, function, start, end)
23+
points = get_czebyshev_interpolation_nodes(N, function, start, end)
24+
25+
x_nodes, y_nodes = zip(*points)
26+
27+
polynomial = polynomial_approximate(points, N)
28+
trigonometric = trigonometric_approximate(points, N)
29+
30+
polynomial_y = list(map(polynomial, x))
31+
trigonometric_y = list(map(trigonometric, x))
32+
33+
print('label,N,mean_sqaure_error,max_error')
34+
print_error(N, x_len, y, polynomial_y, 'polynomial')
35+
print_error(N, x_len, y, trigonometric_y, 'trigonometric')
36+
37+
plt.plot(x, y, "b--", linewidth=1, label='function')
38+
plt.plot(x, polynomial_y, "r--", linewidth=1, label='polynomial')
39+
plt.plot(x, trigonometric_y, "g--", linewidth=1, label='trigonometric')
40+
plt.plot(x_nodes, y_nodes, 'yo')
41+
plt.legend(loc='upper left', prop={'size': 8})
42+
plt.grid(True)
43+
plt.show()

Diff for: Approximation/points.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import numpy as np
2+
3+
4+
def get_regular_interpolation_nodes(n, fun, start, end):
5+
return [(x, fun(x)) for x in np.linspace(start, end, n)]
6+
7+
8+
def get_czebyshev_interpolation_nodes(n, fun, start, end):
9+
points = []
10+
half_length = (end - start) / 2
11+
middle = start + half_length
12+
for i in range(0, n):
13+
x = middle + np.cos(((2 * i + 1) * np.pi) / (2 * n)) * half_length
14+
y = fun(x)
15+
points.append((x, y))
16+
return points

Diff for: Approximation/polynomial_approx.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import numpy as np
2+
3+
4+
def get_s_matrix(x, n):
5+
m = n + 1
6+
S = np.empty((m, m))
7+
8+
for i in range(m):
9+
for j in range(m):
10+
S[i][j] = (sum([x**(i+j) for x in x]))
11+
12+
return S
13+
14+
15+
def get_t_vector(points, n):
16+
m = n + 1
17+
t = []
18+
19+
for i in range(m):
20+
t.append(sum([y*(x**i) for x, y in points]))
21+
22+
return t
23+
24+
25+
def polynomial_approximate(points, n):
26+
x, y = zip(*points)
27+
28+
S = get_s_matrix(x, n)
29+
t = get_t_vector(points, n)
30+
31+
A = np.linalg.solve(S, t)
32+
33+
def function(x_point):
34+
result = 0
35+
for i in range(0, n+1):
36+
result += (x_point**i) * A[i]
37+
return result
38+
39+
return function

Diff for: Approximation/trigonometric_approx.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import numpy as np
2+
3+
4+
def polynomial(x, i):
5+
return np.cos(x * i / 2) if i % 2 == 0 else np.sin(x * (i + 1) / 2)
6+
7+
8+
def get_s_matrix(x, n):
9+
m = n + 1
10+
S = np.empty((m, m))
11+
indxs = range(m)
12+
13+
for i in indxs:
14+
for j in indxs:
15+
S[i][j] = sum([polynomial(x[k], j) * polynomial(x[k], i) for k in range(len(x))])
16+
17+
return S
18+
19+
20+
def get_t_vector(points, n):
21+
x, y = zip(*points)
22+
m = n + 1
23+
t = []
24+
25+
for i in range(m):
26+
sum = 0
27+
for j in range(len(x)):
28+
sum += polynomial(x[j], i) * y[j]
29+
t.append(sum)
30+
31+
return t
32+
33+
34+
def trigonometric_approximate(points, n):
35+
x, y = zip(*points)
36+
37+
S = get_s_matrix(x, n)
38+
t = get_t_vector(points, n)
39+
40+
A = np.linalg.solve(S, t)
41+
42+
def function(x_point):
43+
result = 0
44+
for i in range(0, n+1):
45+
result += polynomial(x_point, i) * A[i]
46+
return result
47+
48+
return function

0 commit comments

Comments
 (0)