Syslab 使用线性插值函数 LinearInterpolation 时出现报错:ERROR: MethodError: no method matching linear_interpolation
技术分享
发布于 2025-07-22 14:53:17
查看 2过去315天
问题现象
using TyMath
using TyPlot
using Interpolations
x = 1:9
y = [19.21 18.15 15.36 18.10 16.89 11.32 7.45 5.24 7.01]
etp = Interpolations.LinearInterpolation(x, y)

解决方法
LinearInterpolation 函数在使用时,输入的插值节点参数应为向量,使用矩阵变量输入会导致输入参数不匹配,造成报错;需要使用 vec 向量将矩阵转为向量再使用线性插值函数。
1.将上述代码修改为以下:
using TyMath
using TyPlot
using Interpolations
x = 1:9
y = [19.21 18.15 15.36 18.10 16.89 11.32 7.45 5.24 7.01]
# etp = Interpolations.LinearInterpolation(x, y)
y = vec(y) #将矩阵 y 转为向量
etp = Interpolations.LinearInterpolation(x, y)
2.可以看到此时运行出正确结果:

所属专栏:Syslab基础平台
产品信息:Syslab科学计算环境