欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

分段埃尔米特插值Python实现并检查误差

程序员文章站 2022-07-05 17:18:32
...

函数

y=11+x2

图像

分段埃尔米特插值Python实现并检查误差
可以看到,这里已经几乎没有任何差距了。。

代码

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)
    Y = 1 / (1 + n ** 2)
    df = diff(Y)
    dfb = df.subs(n, begin)
    dfe = df.subs(n, end)

    oldFrac = ((n - end) / (begin - end))
    newFrac = ((n - begin) / (end - begin))

    I = (oldFrac ** 2) * (1 + 2 * newFrac) * by + (newFrac ** 2) * (1 + 2 * oldFrac) * ey + (oldFrac ** 2) * (
                n - begin) * dfb + (newFrac ** 2) * (n - end) * dfe
    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='分段Hermite插值函数')
    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)