Skip to content

Instantly share code, notes, and snippets.

@ti-nspire
Last active March 31, 2018 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ti-nspire/577dbbfba7f982f0087e42bcf048adc8 to your computer and use it in GitHub Desktop.
Save ti-nspire/577dbbfba7f982f0087e42bcf048adc8 to your computer and use it in GitHub Desktop.
近似多項式との誤差の極値を求める
from scipy.signal import argrelmax
import numpy as np
def epsilon(f, coeffList):
a, b = -1, 1 # 区間
dim = len(coeffList)
f = f
g = lambda x : sum([coeffList[n] * x**n for n in np.arange(dim)])
ε = lambda f, g : f - g
# x 軸を細かく分割して、
xList = np.linspace(a, b, 2**16+1)
# 各 x における誤差 ε を計算して、
εList = ε(f(xList), g(xList))
# 誤差 ε が極値となるところの x のインデックスを求めて、
peaksIndex = argrelmax(np.abs(εList))
# 区間の両端も含めて誤差 ε が極値となるところの x をリスト化する。
x0List = np.concatenate(([a], xList[peaksIndex], [b]))
# 区間の両端も含めて誤差 ε の極値をリスト化する。
εa = εList[0]
εb = εList[-1]
ε0List = np.concatenate(([εa], εList[peaksIndex], [εb]))
return x0List, ε0List
########
# test #
########
if __name__ == "__main__":
f = lambda x : np.sin(np.pi * x / 2)
# チェビシェフ補間で求めた近似多項式の係数が下のとおりであるとする。
# この cList から、誤差 ε が極値を取るときの x とそのときの極値とを求める。
cList = np.array([0.0, 1.57065736, -0.0, -0.64345777, 0.0, 0.07293465])
x0List, ε0List = epsilon(f, cList)
print(x0List)
print(ε0List)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment