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

算法分析
三次樣條插值。就是在分段插值的一種情況。
要求:
- 在每個(gè)分段區(qū)間上是三次多項(xiàng)式(這就是三次樣條中的三次的來源)
- 在整個(gè)區(qū)間(開區(qū)間)上二階導(dǎo)數(shù)連續(xù)(當(dāng)然啦,這里主要是強(qiáng)調(diào)在節(jié)點(diǎn)上的連續(xù))
- 加上邊界條件。邊界條件只需要給出兩個(gè)方程。構(gòu)建一個(gè)方程組,就可以解出所有的參數(shù)。
這里話,根據(jù)第一類樣條作為邊界。(就是知道兩端節(jié)點(diǎn)的導(dǎo)數(shù)數(shù)值,然后來做三次樣條插值)
但是這里也分為兩種情況,分別是這個(gè)數(shù)值是隨便給的一個(gè)數(shù),還是說根據(jù)函數(shù)的在對應(yīng)點(diǎn)上數(shù)值給出。
情況一:兩邊導(dǎo)數(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)
情況二:兩邊導(dǎo)數(shù)數(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Python中執(zhí)行和調(diào)用JavaScript的多種方法小結(jié)
JavaScript(JS)是一種常用的腳本語言,通常用于網(wǎng)頁開發(fā),但有時(shí)也需要在Python中執(zhí)行或調(diào)用JavaScript代碼,本文將詳細(xì)介紹Python中執(zhí)行和調(diào)用JavaScript的多種方法,每種方法都將附有示例代碼,方便理解如何在Python中與JavaScript進(jìn)行互動(dòng),需要的朋友可以參考下2023-11-11
使用Pyinstaller轉(zhuǎn)換.py文件為.exe可執(zhí)行程序過程詳解
這篇文章主要介紹了使用Pyinstaller轉(zhuǎn)換.py文件為.exe可執(zhí)行程序過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
pybaobabdt庫基于python的決策樹隨機(jī)森林可視化工具使用
這篇文章主要為大家介紹了pybaobabdt庫基于python的決策樹隨機(jī)森林可視化工具使用探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-02-02
基于Python實(shí)現(xiàn)通過微信搜索功能查看誰把你刪除了
這篇文章主要介紹了基于Python實(shí)現(xiàn)微信搜索查看誰把你刪除了的相關(guān)資料,需要的朋友可以參考下2016-01-01
Pandas數(shù)據(jù)集的合并與連接merge()方法
Pandas數(shù)據(jù)集的合并與連接(merge())是數(shù)據(jù)處理過程中常用的操作之一,在使用Pandas進(jìn)行數(shù)據(jù)集合并時(shí),可以使用merge()函數(shù)將兩個(gè)或多個(gè)數(shù)據(jù)集按照指定的列進(jìn)行合并,本文就來介紹一下,感興趣的可以了解一下2023-11-11
Django import export實(shí)現(xiàn)數(shù)據(jù)庫導(dǎo)入導(dǎo)出方式
這篇文章主要介紹了Django import export實(shí)現(xiàn)數(shù)據(jù)庫導(dǎo)入導(dǎo)出方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

