TyperError:-不支持的操作数类型:'float'和'builtin_function_or_method'

问题描述 投票:1回答:1

我正在尝试实现Bulirsch-Stoer算法:我已经创建了一个包含简化算法的函数。但是首先,我创建了一个midpoint函数:基本上,此中点方法的firs应用程序使用两个集成步骤。在连续积分中,步骤数增加了两个,每个积分后均进行了Richardson推断:当两个连续解之差(在均方根意义上)相差小于规定的公差时,该过程将停止。

def integrate(F,x,y,xStop,tol):
def midpoint(F,x,y,xStop,nSteps):
#Midpoint formulas    
    h = (xStop -x)/nSteps
    y0 = y
    y1 = y0 + h*F(x,y0)

    for i in range(nSteps-1):
        x = x + h
        y2 = y0 + 2.0*h*F(x,y1)
        y0 = y1
        y1 = y2
    return 0.5*(y1 + y0 + h*F(x,y2))
def richardson(r,k):
#Richardson's extrapolation    
    for j in range(k-1,0,-1):
        const = (k/(k - 1.0))**(2.0*(k-j))
        r[j] = (const*r[j+1] - r[j])/(const - 1.0)
    return

kMax = 51
n = len(y)
r = np.zeros((kMax,n))
#Start with two integration steps
nSteps = 2
r[1] = midpoint(F,x,y,xStop,nSteps)
r_old = r[1].copy
#Increase the number of integration by 2
#and refine result by Richardson extrapolation
for k in range(2,kMax):
    nSteps = 2*k
    r[k] = midpoint(F,x,y,xStop,nSteps)
    richardson(r,k)
    #Compute RMS change in solution
    e = sqrt(np.sum((r[1] - r_old)**2)/n)
    #Check for convergence
    if e < tol: return r[1]
    r_old = r[1].copy()
print("Midpoint method did not converge")

这是算法:

def bulStoer(F,x,y,xStop,H,tol = 1.0e-6):
X =[]
Y =[]
X.append(x)
Y.append(y)
while x < xStop:
    H = min(H,xStop -x)
    y = integrate(F,x,y, x + H, tol) #Midpoint method
    x = x + H
    X.append(x)
    Y.append(y)
return np.array(X),np.array(Y)

我正在尝试在一个示例上对其进行测试:

def F(x,y):
F =np.zeros(2)
F[0] = y[1]
F[1] =(-y[1] - y[0]/0.45 + 9.0)/2.0
return F

H = 0.25
xStop = 10.0
x = 0.0
y = np.array([0.0,0.0])
X,Y = bulStoer(F,x,y,xStop,H)
plt.plot(X,Y[:,1],'o-')
plt.xlabel('Time(s)')
pkt.ylabel('Current(A)')
plt.grid(True)
plt.show
input("\nPress return to exit")

但是我有这种错误:

unsupported operand type(s) for -: 'float' and 'builtin_function_or_method'

在此行上:e = sqrt(np.sum((r[1] - r_old)**2)/n)

有人可以帮助我解决这个错误:我根本不了解它的起源!

非常感谢!

python built-in operands
1个回答
0
投票

r_old = r[1].copy线更改为r_old = r[1].copy()


0
投票

我没有注意!非常感谢M.Khoronko的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.