最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)三次樣條插值

 更新時(shí)間:2018年12月17日 14:17:41   作者:肥宅_Sean  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)三次樣條插值,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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ù)的在對(duì)應(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)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

兴义市| 图木舒克市| 丁青县| 阿拉善左旗| 喀什市| 普定县| 普定县| 清徐县| 大田县| 石棉县| 六盘水市| 汝阳县| 无极县| 崇仁县| 盐津县| 平安县| 安福县| 余姚市| 十堰市| 固始县| 万盛区| 离岛区| 姚安县| 定安县| 灵山县| 汾阳市| 彭州市| 双牌县| 隆安县| 佛山市| 丹江口市| 长沙县| 维西| 绥江县| 黄浦区| 汶上县| 洛扎县| 阿拉善盟| 田东县| 嘉荫县| 紫金县|