-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapproximation01.py
More file actions
65 lines (46 loc) · 1007 Bytes
/
approximation01.py
File metadata and controls
65 lines (46 loc) · 1007 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
def funct(t, y):
return 1 + y/t
def approx_f(x, w_i, x_i, n):
result = 0
tmp_l = 1
for i in range(0, n+1):
tmp_l = 1
for j in range(0, n+1):
if i == j:
continue
tmp_l *= (x - x_i[j])
tmp_l /= (x_i[i] - x_i[j])
result += w_i[i]*tmp_l
return result
a = 1
b = 2
y_0 = 2
h = 0.25
n = 0
tmp_t = a
w_i = np.array([y_0])
x_i = np.array([a])
pl_x = np.array([a])
pl_y = np.array([y_0])
while(tmp_t < b):
w_i = np.append(w_i, w_i[n] + funct(tmp_t, w_i[n]))
n+=1
tmp_t+=h
x_i = np.append(x_i, tmp_t)
"""
for i in range(0, 11):
pl_x = np.append(pl_x, a + (b-a)*i/100)
pl_y = np.append(pl_y, approx_f(a+(b-a)*i/100, w_i, x_i, n))
"""
lag = 0.01
x = np.arange(1.0, 2.0, lag)
y = approx_f(x, w_i, x_i, n)
fig = plt.figure()
plt.plot(x, y)
plt.grid(True)
plt.show()
print(w_i)
print(x_i)