如何使用两个数组和引用将带有“interp1”的Matlab函数转换为带有“interp1d”的Python函数

2024-06-02 07:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我像这样使用我的Matlab代码

Fas=[1.4, 1.4, 1.3]
Fvs=[1.5, 1.4, 1.3]

if S<=0.1
Fa=Fas(1);
Fv=Fvs(1);

elseif S<=0.2
Fa=interp1([0.1 0.2],[Fas(1) Fas(2)],S);
Fv=interp1([0.1 0.2],[Fvs(1) Fvs(2)],S);
end

然后我想用interp1d将其转换为Python

所以我就这样写

if S<=0.1:
    Fa=Fas[0]
    Fv=Fvs[0]
elif S<=0.2:
    Fa=interp1.interp1d([0.1, 0.2],[Fas[0], Fas[1]],S)
    Fv=interp1.interp1d([0.1, 0.2],[Fvs[0], Fvs[1]],S)

然后我得到了这样的错误信息


  Traceback (most recent call last):
  File "main.py", line 15, in <module>
    [Spa_design,Fa,Fv,Sds,Sd1,To,Ts,Tl]=spe(site,S,T)
  File "/home/runner/timehistoryspectrumscale/Design_spectrum.py", line 28, in 
  Design_spectrum
    Fa=interp1.interp1d([0.1, 0.2],[Fas[0], Fas[1]],S)
  File "/opt/virtualenvs/python3/lib/python3.8/site- 
  packages/scipy/interpolate/interpolate.py", line 444, in __init__
    raise NotImplementedError("%s is unsupported: Use fitpack "
  NotImplementedError: 0.176 is unsupported: Use fitpack routines for other 
  types.



我使用带(和S=0.176)的插值代码

   from scipy import interpolate as interp1

我是初学者,所以我不知道该模块的确切语法。但是我想找到一种方法,通过interp1d使用两个具有参考值的数组进行插值


Tags: 代码inpyiflinesitefilefa
1条回答
网友
1楼 · 发布于 2024-06-02 07:24:01

scipy.interpolate.interp1d返回一个函数,因此可以使用参数调用该函数

from scipy import interpolate
Fas = np.array([1,2,3])
Fvs = np.array([1,2,3])

S = 0.15

if S<=0.1:
    Fa=Fas[0]
    Fv=Fvs[0]
elif S<=0.2:
    fa = interpolate.interp1d([0.1, 0.2] , [Fas[0], Fas[1]])
    Fa = fa(S)
    fv = interpolate.interp1d([0.1, 0.2] , [Fvs[0], Fvs[1]])
    Fv = fa(S)

我猜你在处理光谱。就在昨天,我写了一个python code用于从地面运动数据推导光谱。也许它会对你有用

相关问题 更多 >