已知sinx的一组x,y对应关系,用拉格朗日插值法估计sin(0.3367)的值.
x | x0.32 | 0.34 | 0.36 |
---|---|---|---|
y | 0.314567 | 0.333487 | 0.352274 |
/
/
class Interpolation: def __init__(self, x, y): self.x = x self.y = y def func(self, X): s = 0 for i in range(len(self.x)): W = 1 w = (X - self.x[i]) for j in range(len(self.x)): W *= (X - (self.x[j])) if i == j: continue else: w *= (self.x[i] - self.x[j]) L = self.y[i] * W / w s += L print(round(s, 8)) while (True): x=input('x:') y=input('y:') X=input('X:') if x=='Q' or y=='Q' or X=='Q': break else: p1 = Interpolation(eval(x),eval(y)) # 前面是x的取值,后面是y的取值; p1.func(eval(X)) # 现在要计算的是 sin(0.3367) 的值
程序还有好多不足的问题需要改进(^o^),