Python實現(xiàn)分段線性插值
更新時間:2018年12月17日 15:16:13 作者:肥宅_Sean
這篇文章主要為大家詳細介紹了Python實現(xiàn)分段線性插值,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Python實現(xiàn)分段線性插值的具體代碼,供大家參考,具體內容如下
函數:
算法
這個算法不算難。甚至可以說是非常簡陋。但是在代碼實現(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='原函數') plt.plot(x, Ly, label='分段線性插值函數') 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)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
django queryset 去重 .distinct()說明
這篇文章主要介紹了django queryset 去重 .distinct()說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05python Selenium等待元素出現(xiàn)的具體方法
在本篇文章里小編給大家分享的是一篇關于python Selenium等待元素出現(xiàn)的具體方法,以后需要的朋友們可以學習參考下。2021-08-08Python?jieba庫文本處理詞性標注和關鍵詞提取進行文本情感分析
這篇文章主要為大家介紹了Python使用中文文本處理利器jieba庫中的詞性標注和關鍵詞提取功能進行文本情感分析實例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12在Python反編譯中批量pyc轉?py的實現(xiàn)代碼
這篇文章主要介紹了在Python反編譯中批量pyc轉?py的實現(xiàn)代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02