Python实现牛顿插值

# 求差商
def Difference(x , y):
    # Python二维数组初始化
    temp = [[0 for col in range(len(x))] for row in range(len(y))]
    for i in range(len(y)):
        temp[i][0] = y[i]
    result = [y[0]]
    # 差商表
    for col in range(1,len(y)):
        for row in range(col,len(y)):
            # 这里的col既代表列数,也代表几阶差商
            degree = col
            temp[row][col] = (temp[row-1][col-1]-temp[row][col-1])/(x[row-degree]-x[row])
            if row == col:
                result.append(temp[row][col])
    return result

# 牛顿插值
def Newton (result, x, y, In):
    temp = [1]
    t = 1
    sum = temp[0]*result[0]
    for i in range(1, len(result)):
        for j in range(i):
            t *= (In - x[j])
        temp.append(t)
        sum += temp[i] * result[i]
        # 这里t要重置为1
        t = 1
    return sum


x = [0.40,0.55,0.65,0.80,0.90]
y = [0.41075,0.57815,0.69675,0.88811,1.02652]
# 求各阶差商
result = Difference(x, y)
In = float(input())
# 求牛顿插值结果
print(format(Newton(result,x,y,In), '.3f'))








  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值