저는 파이썬에 매우 익숙하며, 프로젝트로서 파이썬에서 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)
으로 사용하면 보간 된 점의 목록을 반환합니다. 첫 번째 예제가 작동하는 이유는 알 수 없지만 두 번째 예제는 작동하지 않습니다. 두 배열은 동일하게 보이지만 첫 번째 예제 만 실제로 출력을 생성합니다.
편집 : 오류를 추가하면이 그 aa
의 범위에, 나는 발 인수 inter([val])
에 넣어 숫자에 대한 오류입니다
---------------------------------------------------------------------------
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'
를 반환했습니다.
'작동하지 않습니다'에 대해 좀 더 구체적으로 표현할 수 있습니까? 잘못된 결과를 산출하고 오류를 표시합니다 (추적 표시는 무엇입니까?)? –