You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
import numpy as np
# 给定数据
t_values = np . array ( [ 0 , 10 , 15 , 20 , 22.5 , 30 ] )
v_values = np . array ( [ 0 , 227.04 , 362.78 , 517.35 , 602.97 , 901.67 ] )
# 计算Newton插值法所需的差商
def divided_diff ( t_values , v_values ) :
# 用给定的数值初始化差商数组
differences = v_values . copy ( )
# 遍历每个数据点,计算差商
for i in range ( 1 , len ( t_values ) ) :
for j in range ( len ( t_values ) - 1 , i - 1 , - 1 ) :
differences [ j ] = ( differences [ j ] - differences [ j - 1 ] ) / ( t_values [ j ] - t_values [ j - i ] )
return differences
# 使用Newton插值法寻找 t=16 时的速度
def newton_interpolation ( t_values , v_values , target_t ) :
differences = divided_diff ( t_values , v_values )
p = differences [ 0 ]
for i in range ( 1 , len ( t_values ) ) :
term = differences [ i ]
for j in range ( i ) :
term * = ( target_t - t_values [ j ] )
p + = term
return p
t_target = 16
velocity_at_16 = newton_interpolation ( t_values , v_values , t_target )
print ( f " 使用Newton插值法, t=16 秒时的速度为: { velocity_at_16 : .2f } m/s " )