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

Python實現(xiàn)分段線性插值

 更新時間:2018年12月17日 15:16:13   作者:肥宅_Sean  
這篇文章主要為大家詳細(xì)介紹了Python實現(xiàn)分段線性插值,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Python實現(xiàn)分段線性插值的具體代碼,供大家參考,具體內(nèi)容如下

函數(shù):

算法

這個算法不算難。甚至可以說是非常簡陋。但是在代碼實現(xiàn)上卻比之前的稍微麻煩點。主要體現(xià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):
 by = f(begin)
 ey = f(end)
 I = (n - end) / (begin - end) * by + (n - begin) / (end - begin) * ey
 return I


def calnf(x):
 nf = []
 for i in range(len(x) - 1):
  nf.append(cal(x[i], x[i + 1]))
 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)

 nf = calnf(x)
 draw(nf)
 lossCal(nf)

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

相關(guān)文章

最新評論

怀集县| 平顺县| 青阳县| 洛阳市| 柳州市| 佳木斯市| 金沙县| 大理市| 类乌齐县| 阳原县| 莱阳市| 龙海市| 周口市| 喜德县| 千阳县| 卢龙县| 临澧县| 揭西县| 体育| 海伦市| 孟州市| 衡山县| 阿勒泰市| 遂宁市| 梅河口市| 巨鹿县| 息烽县| 突泉县| 云阳县| 仲巴县| 通榆县| 乐陵市| 类乌齐县| 乐业县| 达日县| 阿拉善左旗| 紫阳县| 扎兰屯市| 海南省| 京山县| 含山县|