python插值函数代码
时间: 2023-07-23 08:31:39 浏览: 189
以下是Python中的插值函数代码示例:
1. 线性插值
```python
from scipy.interpolate import interp1d
# 输入数据
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]
# 创建插值函数对象
f = interp1d(x, y)
# 计算插值结果
x_new = [1.5, 2.5, 3.5, 4.5]
y_new = f(x_new)
print(y_new)
```
2. 二次样条插值
```python
from scipy.interpolate import interp1d
from scipy.interpolate import interp1d
# 输入数据
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]
# 创建插值函数对象
f = interp1d(x, y, kind='quadratic')
# 计算插值结果
x_new = [1.5, 2.5, 3.5, 4.5]
y_new = f(x_new)
print(y_new)
```
3. 三次样条插值
```python
from scipy.interpolate import interp1d
from scipy.interpolate import interp1d
# 输入数据
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]
# 创建插值函数对象
f = interp1d(x, y, kind='cubic')
# 计算插值结果
x_new = [1.5, 2.5, 3.5, 4.5]
y_new = f(x_new)
print(y_new)
```
以上代码使用了SciPy库中的interp1d函数,可以实现线性插值、二次样条插值和三次样条插值。其中,kind参数可以设置为linear、quadratic和cubic,分别对应线性插值、二次样条插值和三次样条插值。
阅读全文