-
Notifications
You must be signed in to change notification settings - Fork 4
/
interpol.py
55 lines (42 loc) · 1.2 KB
/
interpol.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
# interpol.py: 多項式補間
import numpy as np
import scipy.linalg as sclinalg
import scipy.interpolate as scipl
# 大本の関数
def org_func(x):
return np.sin(x)
# 補間点数
str_n = input('補間点数を入力 n = ')
n = int(str_n)
# 補間点
x = np.zeros(n)
y = np.zeros(n)
print('x.size = ', x.size)
for i in range(0, n):
print('補間点xを入力 x[' + str(i) + '] = ')
str_x = input()
x[i] = float(str_x)
y[i] = org_func(x[i])
print('x = ', x)
print('y = ', y)
# バンデルモンド行列生成
v_mat = np.zeros((n, n))
for i in range(0, n):
for j in range(0, n):
v_mat[i, j] = x[i] ** j
print('V = \n', v_mat)
# new_y := V^(^1) * y
v_coef = sclinalg.inv(v_mat) @ y
print('v_coef = ', np.flip(v_coef))
# ラグランジュ補間
l_poly = scipl.lagrange(x, y)
print('l_coef = ', l_poly.coef)
# Kroph補間
k_poly = scipl.KroghInterpolator(x, y)
print('k(x) = ', k_poly(x))
print('k\'(x) = ', k_poly.derivative(x, der=1))
print('k\'\'(x) = ', k_poly.derivative(x, der=2))
# -------------------------------------
# Copyright (c) 2021 Tomonori Kouya
# All rights reserved.
# -------------------------------------