Python 从插值函数中检索值

Python 从插值函数中检索值,python,numpy,scipy,interpolation,Python,Numpy,Scipy,Interpolation,我对python非常陌生,作为一个项目,我决定用python编写Mathematica项目,看看它是如何工作的,因此代码的编写风格尽可能接近Mathematica 我正在努力从插值函数调用值,在一个简单的工作示例中,我想这样做: import numpy as np from scipy.interpolate import interp1d a = np.linspace(1,10,10) b = np.sin(a) inter = interp1d(a,b) # this is wher

我对python非常陌生,作为一个项目,我决定用python编写Mathematica项目,看看它是如何工作的,因此代码的编写风格尽可能接近Mathematica

我正在努力从插值函数调用值,在一个简单的工作示例中,我想这样做:

import numpy as np
from scipy.interpolate import interp1d

a = np.linspace(1,10,10)
b = np.sin(a)
inter = interp1d(a,b)

# this is where i get a value from the interpolated function
s0 = inter(a[0])
print(s0)
这是我的MWE,不起作用:

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
from scipy import integrate
from scipy.interpolate import interp1d

E0 = -0.015
L = 5.5
Ns = 1000


# this solves for where the energy E0 intersects the potential y
def req(E0):
    L=5.5
    r = Symbol('r')
    y = -(2*L**2)/(r**3)+(L**2)/(r**2)-(2)/(r)
    rr = (E0-y)*(r**4)
    rreq = Eq(rr, 0)
    rrt = sorted(solve(rr), key=int)
    return rrt 

# upper and lower limits on r
r1 = req(E0)[1]
r0 = req(E0)[2]

# initialise the arrays
a = np.array([1])
b = np.array([1])

# numerically integrate the function R(r)
for n in range(2, Ns):
    # integrate 
    lwlmt = r0
    uplmt = r0+(n-1)*(r1-r0)/(Ns-1)

    result, error = integrate.quad(lambda ra: -1/((E0-(-(2*L**2)/(ra**3)+(L**2)/(ra**2)-(2)/(ra)))*(ra**4))**(0.5), r0, uplmt)

    a = np.append([uplmt],[[a]])
    b = np.append([result],[[b]])

# chop the 1 from the end
aa = a[:-1]
ba = b[:-1]

# interpolate
inter = interp1d(aa,ba)

# this is the problem
print(inter(110))

# this is what i would ideally like to do,
# get the start and end points however i receive an error
s0 = inter(aa[0])
s1 = inter(aa[len(aa)-1])


plt.plot(aa,inter(aa))
plt.show()
奇怪的是,我的MWE只有在使用整个数组作为参数时才起作用。
inter(aa)
它返回一个插值点列表。 我不明白为什么第一个例子有效,而第二个不有效。两个数组看起来是相同的,但是只有第一个示例实际生成一个输出

编辑:添加返回的错误

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/nick/Documents/python/<ipython-input-4-b36b3f397c2e> in <module>()
     45 # this is what i would ideally like to do,

     46 # get the start and end points

---> 47 s0 = inter(aa[1])
     48 s1 = inter(aa[len(aa)-1])
     49 

/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.pyc in __call__(self, x_new)
    364         # from y_new and insert them where self.axis was in the list of axes.

    365         nx = x_new.ndim
--> 366         ny = y_new.ndim
    367 
    368         # 6. Fill any values that were out of bounds with fill_value.


AttributeError: 'Float' object has no attribute 'ndim'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
/home/nick/Documents/python/in()
45#这是我最理想的选择,
46#了解起点和终点
--->47 s0=内部(aa[1])
48 s1=内部(aa[len(aa)-1])
49
/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.pyc in_u_调用(self,x_新)
364#从y#U新建并将其插入轴列表中self.axis的位置。
365 nx=x_new.ndim
-->366 ny=y_new.ndim
367
368         # 6. 用Fill_值填充超出范围的任何值。
AttributeError:“Float”对象没有属性“ndim”
这是我在val参数
inter([val])
中输入的任何数字的错误,即在
aa
范围内,
req()
的结果是一个symphy对象,而不是一个真正的Python浮点,因此,
uplmt
也是一个symphy对象。numpy的数值例程不知道如何处理这些sympy对象。尽早转换为Python浮点对象

在我的机器上,
r0
r1
的值实际上很复杂,只是有很小的虚部,它们会导致比您显示的更早的错误。不过,转换它们很容易:

# upper and lower limits on r
r1 = complex(req(E0)[1]).real
r0 = complex(req(E0)[2]).real

在我做出更改后,您的脚本将为我执行到完成,尽管我不能保证它会给出您所需的数值结果。

您能否更具体地说明“不起作用”:生成错误的结果、给出错误(什么是回溯)等?谢谢您的回答,这正是我想要的/