已知函数表:
表头 | 表头 | 表头 | 表头 |
---|---|---|---|
x | 1.45 | 1.36 | 1.14 |
y | 3.14 | 4.15 | 5.65 |
用拉格朗日插值公式计算x=1.4和y=5.01所对应的近似值。
x = [1.45, 1.36, 1.14]
y = [3.14, 4.15, 5.65]
def L(x0):
return (x0 - x[1])/(x[0] - x[1]) * y[0] \
+ (x0 - x[0])/(x[1] - x[0]) * y[1] \
+ (x0 - x[2])/(x[0] - x[2]) * y[2]
x0 = 1.4
e = abs(L(x0) - 5.01)
while e > 1e-5:
y0 = L(x0)
e = abs(y0 - 5.01)
x0 = x0 - e/y0 * (x[0] - x[1])/(y[0] - y[1])
print(x0)
白菜,加油!
不知道你这个问题是否已经解决, 如果还没有解决的话:def parse_y(x):
if (x == 'no'):
return 0
else:
return 1
data['y'] = data['y'].apply(parse_y)
data['y'] = data['y'].astype(int)