龙空技术网

用Python进行数值分析 - 插值

刹那不悟 144

前言:

现在咱们对“插值 python”大约比较关注,大家都需要了解一些“插值 python”的相关知识。那么小编同时在网上汇集了一些对于“插值 python””的相关内容,希望大家能喜欢,咱们快快来学习一下吧!

import numpy as npimport scipy as spfrom scipy.interpolate import lagrangefrom scipy.interpolate import BarycentricInterpolatorprint('numpy is of version: ', np.__version__)print('scipy is of version: ', sp.__version__)# 使用scipy自带的拉格朗日插值模块函数求解def lagrangePolynomialDemo1():    xElements = np.array([0, 1, 2])    yElements = np.array([0, 1, 8])    polynomial = lagrange(xElements, yElements)    print('生成的多项式: \n', polynomial)    # 结果表达式: 3*x**2 - 2*x# 使用scipy自带的拉格朗日插值模块函数求解def lagrangePolynomialDemo2():    xElements = np.array([0, 1, 2])    yElements = np.array([1, np.power(np.e, 1), np.power(np.e, 2)])    polynomial = lagrange(xElements, yElements)    print('生成的多项式: \n', polynomial)    # 结果表达式: 1.476*x**2 + 0.242*x + 1# 使用scipy自带的拉格朗日插值模块函数求解def lagrangePolynomialDemo3():    xElements = np.array([0, 2, 3])    yElements = np.array([1, 2, 4])    polynomial = lagrange(xElements, yElements)    print('生成的多项式: \n', polynomial)    # 结果表达式: 0.5*x**2 - 0.5*x + 1# 牛顿差商插值def newtonPolynomialDemo1():    xElements = np.array([0, 2, 3])    yElements = np.array([1, 2, 4])    temp1 = np.zeros(xElements.size)    temp2 = np.zeros(xElements.size)    temp3 = np.zeros(xElements.size)    for i in np.arange(xElements.size):        temp1[i] = yElements[i]    for i in np.arange(1, xElements.size):        temp2[i] = (temp1[i] - temp1[i-1])/(xElements[i] - xElements[i-1])    for i in np.arange(2, xElements.size):        temp3[i] = (temp2[i] - temp2[i-1])/(xElements[i] - xElements[i-2])    result = np.array([temp1[0], temp2[1], temp3[2]])    print(result)    # scipy/numpy没找到对应的牛顿插值模块,算法low了点,但是可以很清楚的看明白具体过程~    # 结果表达式需要根据得到的数组化简一下: 1 + 0.5*(x-0) + 0.5*(x-0)*(x-2) = 0.5*x**2 - 0.5*x + 1def newtonPolynomialDemo2():    xElements = np.array([0, 1, 2, 3])    yElements = np.array([2, 1, 0, -1])    temp1 = np.zeros(xElements.size)    temp2 = np.zeros(xElements.size)    temp3 = np.zeros(xElements.size)    temp4 = np.zeros(xElements.size)    for i in np.arange(xElements.size):        temp1[i] = yElements[i]    for i in np.arange(1, xElements.size):        temp2[i] = (temp1[i] - temp1[i-1])/(xElements[i] - xElements[i-1])    for i in np.arange(2, xElements.size):        temp3[i] = (temp2[i] - temp2[i-1])/(xElements[i] - xElements[i-2])    for i in np.arange(3, xElements.size):        temp4[i] = (temp3[i] - temp3[i-1])/(xElements[i] - xElements[i-3])    result = np.array([temp1[0], temp2[1], temp3[2], temp4[3]])    print(result)    # 结果表达式的化简过程: 2 + (-1)*(x-0) + 0*(x-0)(x-1) + 0*(x-0)(x-1)(x-2) = -x + 2# scipy.interpolate下还有很多其他的插值方法,具体见文档#  BarycentricInterpolatorDemo():    xElements = np.array([0, 1, 2, 3])    yElements = np.array([2, 1, 0, -1])    # 根据传入的x, y构建一个多项式    polynomial = BarycentricInterpolator(xElements, yElements)    # 根据构建的多项式算出来y值    predictValues = polynomial._evaluate(np.array([5, 4, 3, 2, 1]))    # 感觉就是上面的-x + 2表达式算出来的:)    print(predictValues)if __name__ == "__main__":    # lagrangePolynomialDemo1()    # lagrangePolynomialDemo2()    # lagrangePolynomialDemo3()    # newtonPolynomialDemo1()    # newtonPolynomialDemo2()    BarycentricInterpolatorDemo()

标签: #插值 python