python實現(xiàn)三次樣條插值
本文實例為大家分享了python實現(xiàn)三次樣條插值的具體代碼,供大家參考,具體內(nèi)容如下
函數(shù):

算法分析
三次樣條插值。就是在分段插值的一種情況。
要求:
- 在每個分段區(qū)間上是三次多項式(這就是三次樣條中的三次的來源)
- 在整個區(qū)間(開區(qū)間)上二階導數(shù)連續(xù)(當然啦,這里主要是強調(diào)在節(jié)點上的連續(xù))
- 加上邊界條件。邊界條件只需要給出兩個方程。構(gòu)建一個方程組,就可以解出所有的參數(shù)。
這里話,根據(jù)第一類樣條作為邊界。(就是知道兩端節(jié)點的導數(shù)數(shù)值,然后來做三次樣條插值)
但是這里也分為兩種情況,分別是這個數(shù)值是隨便給的一個數(shù),還是說根據(jù)函數(shù)的在對應點上數(shù)值給出。
情況一:兩邊導數(shù)數(shù)值給出
這里假設數(shù)值均為1。即 f′(x0)=f′(xn)=f′(xn)=1的情況。
情況一圖像

情況一代碼
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='原函數(shù)')
plt.plot(x, Ly, label='三次樣條插值函數(shù)')
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)
情況二:兩邊導數(shù)數(shù)值由函數(shù)本身算出
這里假設數(shù)值均為1。即 f′(xi)=S′(xi)(i=0,n)f′(xi)=S′(xi)(i=0,n)的情況。
情況二圖像

情況二代碼
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='原函數(shù)')
plt.plot(x, Ly, label='三次樣條插值函數(shù)')
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)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Django數(shù)據(jù)模型中on_delete使用詳解
這篇文章主要介紹了Django數(shù)據(jù)模型中on_delete使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11

