本文实例为大家分享了python实现三次样条插值的具体代码,供大家参考,具体内容如下
函数:
算法分析
三次样条插值。就是在分段插值的一种情况。
要求:
- 在每个分段区间上是三次多项式(这就是三次样条中的三次的来源)
- 在整个区间(开区间)上二阶导数连续(当然啦,这里主要是强调在节点上的连续)
- 加上边界条件。边界条件只需要给出两个方程。构建一个方程组,就可以解出所有的参数。
这里话,根据第一类样条作为边界。(就是知道两端节点的导数数值,然后来做三次样条插值)
但是这里也分为两种情况,分别是这个数值是随便给的一个数,还是说根据函数的在对应点上数值给出。
情况一:两边导数数值给出
这里假设数值均为1。即 f′(x0)=f′(xn)=f′(xn)=1的情况。
情况一图像
情况一代码
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
import numpy as np from sympy import * import matplotlib.pyplot as plt def f(x): return 1 / ( 1 + x * * 2 ) def cal(begin, end, i): by = f(begin) ey = f(end) i = ms[i] * ((end - n) * * 3 ) / 6 + ms[i + 1 ] * ((n - begin) * * 3 ) / 6 + (by - ms[i] / 6 ) * (end - n) + ( ey - ms[i + 1 ] / 6 ) * (n - begin) return i def ff(x): # f[x0, x1, ..., xk] ans = 0 for i in range ( len (x)): temp = 1 for j in range ( len (x)): if i ! = j: temp * = (x[i] - x[j]) ans + = f(x[i]) / temp return ans def calm(): lam = [ 1 ] + [ 1 / 2 ] * 9 miu = [ 1 / 2 ] * 9 + [ 1 ] # y = 1 / (1 + n ** 2) # df = diff(y, n) x = np.array( range ( 11 )) - 5 # ds = [6 * (ff(x[0:2]) - df.subs(n, x[0]))] ds = [ 6 * (ff(x[ 0 : 2 ]) - 1 )] for i in range ( 9 ): ds.append( 6 * ff(x[i: i + 3 ])) # ds.append(6 * (df.subs(n, x[10]) - ff(x[-2:]))) ds.append( 6 * ( 1 - ff(x[ - 2 :]))) mat = np.eye( 11 , 11 ) * 2 for i in range ( 11 ): if i = = 0 : mat[i][ 1 ] = lam[i] elif i = = 10 : mat[i][ 9 ] = miu[i - 1 ] else : mat[i][i - 1 ] = miu[i - 1 ] mat[i][i + 1 ] = lam[i] ds = np.mat(ds) mat = np.mat(mat) ms = ds * mat.i return ms.tolist()[ 0 ] def calnf(x): nf = [] for i in range ( len (x) - 1 ): nf.append(cal(x[i], x[i + 1 ], i)) return nf def calf(f, x): y = [] for i in x: y.append(f.subs(n, i)) return y def nfsub(x, nf): tempx = np.array( range ( 11 )) - 5 dx = [] for i in range ( 10 ): labelx = [] for j in range ( len (x)): if x[j] > = tempx[i] and x[j] < tempx[i + 1 ]: labelx.append(x[j]) elif i = = 9 and x[j] > = tempx[i] and x[j] < = tempx[i + 1 ]: labelx.append(x[j]) dx = dx + calf(nf[i], labelx) return np.array(dx) def draw(nf): plt.rcparams[ 'font.sans-serif' ] = [ 'simhei' ] plt.rcparams[ 'axes.unicode_minus' ] = false x = np.linspace( - 5 , 5 , 101 ) y = f(x) ly = nfsub(x, nf) plt.plot(x, y, label = '原函数' ) plt.plot(x, ly, label = '三次样条插值函数' ) plt.xlabel( 'x' ) plt.ylabel( 'y' ) plt.legend() plt.savefig( '1.png' ) plt.show() def losscal(nf): x = np.linspace( - 5 , 5 , 101 ) y = f(x) ly = nfsub(x, nf) ly = np.array(ly) temp = ly - y temp = abs (temp) print (temp.mean()) if __name__ = = '__main__' : x = np.array( range ( 11 )) - 5 y = f(x) n, m = symbols( 'n m' ) init_printing(use_unicode = true) ms = calm() nf = calnf(x) draw(nf) losscal(nf) |
情况二:两边导数数值由函数本身算出
这里假设数值均为1。即 f′(xi)=s′(xi)(i=0,n)f′(xi)=s′(xi)(i=0,n)的情况。
情况二图像
情况二代码
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
import numpy as np from sympy import * import matplotlib.pyplot as plt def f(x): return 1 / ( 1 + x * * 2 ) def cal(begin, end, i): by = f(begin) ey = f(end) i = ms[i] * ((end - n) * * 3 ) / 6 + ms[i + 1 ] * ((n - begin) * * 3 ) / 6 + (by - ms[i] / 6 ) * (end - n) + ( ey - ms[i + 1 ] / 6 ) * (n - begin) return i def ff(x): # f[x0, x1, ..., xk] ans = 0 for i in range ( len (x)): temp = 1 for j in range ( len (x)): if i ! = j: temp * = (x[i] - x[j]) ans + = f(x[i]) / temp return ans def calm(): lam = [ 1 ] + [ 1 / 2 ] * 9 miu = [ 1 / 2 ] * 9 + [ 1 ] y = 1 / ( 1 + n * * 2 ) df = diff(y, n) x = np.array( range ( 11 )) - 5 ds = [ 6 * (ff(x[ 0 : 2 ]) - df.subs(n, x[ 0 ]))] # ds = [6 * (ff(x[0:2]) - 1)] for i in range ( 9 ): ds.append( 6 * ff(x[i: i + 3 ])) ds.append( 6 * (df.subs(n, x[ 10 ]) - ff(x[ - 2 :]))) # ds.append(6 * (1 - ff(x[-2:]))) mat = np.eye( 11 , 11 ) * 2 for i in range ( 11 ): if i = = 0 : mat[i][ 1 ] = lam[i] elif i = = 10 : mat[i][ 9 ] = miu[i - 1 ] else : mat[i][i - 1 ] = miu[i - 1 ] mat[i][i + 1 ] = lam[i] ds = np.mat(ds) mat = np.mat(mat) ms = ds * mat.i return ms.tolist()[ 0 ] def calnf(x): nf = [] for i in range ( len (x) - 1 ): nf.append(cal(x[i], x[i + 1 ], i)) return nf def calf(f, x): y = [] for i in x: y.append(f.subs(n, i)) return y def nfsub(x, nf): tempx = np.array( range ( 11 )) - 5 dx = [] for i in range ( 10 ): labelx = [] for j in range ( len (x)): if x[j] > = tempx[i] and x[j] < tempx[i + 1 ]: labelx.append(x[j]) elif i = = 9 and x[j] > = tempx[i] and x[j] < = tempx[i + 1 ]: labelx.append(x[j]) dx = dx + calf(nf[i], labelx) return np.array(dx) def draw(nf): plt.rcparams[ 'font.sans-serif' ] = [ 'simhei' ] plt.rcparams[ 'axes.unicode_minus' ] = false x = np.linspace( - 5 , 5 , 101 ) y = f(x) ly = nfsub(x, nf) plt.plot(x, y, label = '原函数' ) plt.plot(x, ly, label = '三次样条插值函数' ) plt.xlabel( 'x' ) plt.ylabel( 'y' ) plt.legend() plt.savefig( '1.png' ) plt.show() def losscal(nf): x = np.linspace( - 5 , 5 , 101 ) y = f(x) ly = nfsub(x, nf) ly = np.array(ly) temp = ly - y temp = abs (temp) print (temp.mean()) if __name__ = = '__main__' : x = np.array( range ( 11 )) - 5 y = f(x) n, m = symbols( 'n m' ) init_printing(use_unicode = true) ms = calm() nf = calnf(x) draw(nf) losscal(nf) |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/a19990412/article/details/80574057