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

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

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

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

函數(shù):

算法

這個(gè)算法不算難。甚至可以說是非常簡陋。但是在代碼實(shí)現(xiàn)上卻比之前的稍微麻煩點(diǎ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)文章

最新評論

炉霍县| 洛川县| 集安市| 黄平县| 寿阳县| 平泉县| 伊春市| 镇远县| 南丰县| 阿图什市| 米泉市| 商洛市| 襄垣县| 抚松县| 思茅市| 嵩明县| 稻城县| 莆田市| 隆安县| 莒南县| 大邑县| 余庆县| 乐昌市| 陇西县| 手游| 平顶山市| 登封市| 博野县| 衡南县| 江华| 电白县| 曲沃县| 密山市| 花垣县| 汉中市| 湖口县| 嘉峪关市| 南城县| 武义县| 长宁区| 五家渠市|