Young87

当前位置:首页 >个人收藏

python 求 牛顿插值法中的差商表

# -*- coding: utf-8 -*-
"""
Created on Wed Oct 18 16:36:43 2017

@author: www
"""
#本程序可以实现求出多项式的差商表。从而利用牛顿插值公式解决多项式的插值问题。
import matplotlib.pyplot as plt 
def fun(x,f):
    rows=len(x)        #行数
    cols=len(x)+1      #列数
    temp=[[0 for col in range(cols)] for row in range(rows)]
    
    for i in range(rows):
        temp[i][0]=x[i]
        temp[i][1]=f[i]

    for i in range(1,rows):
        for j in range(2,i+2):
            print(i,j )
            temp[i][j]=(temp[i][j-1]-temp[i-1][j-1])/(temp[i][0]-temp[i-j+1][0])
            
    print("差商表为")
    print(temp)
    
    

#==============================================================================
# x=[1,3,2]
# f=[1,2,-1]
#==============================================================================
x=[0,1,2,4]
f=[3,6,11,51]
plt.plot(x,f)
fun(x,f)
 

 

如果想得到对应的多项式公式,在这个我不知道怎么解决类似2(x+1)+1这种多项式的化简问题。现在想到的一种思路是利用重载运算符不知道可不可以,在这里记录下来,

希望有大神看到了可以评论指点一下,不胜感激。

 

 

 

 

除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog

上一篇: 给新入坑的程序员十条忠告

下一篇: 惊呆!谷歌AI自动编程效率超研发工程师,作为AI工程师的我感到了森森的压力

精华推荐