TyperError : unsupported operand type(s) for -: 'float' and 'builtin_function_or_method'
TyperError : unsupported operand type(s) for -: 'float' and 'builtin_function_or_method'
我正在尝试实现 Bulirsch-Stoer 算法:我创建了一个包含它的简化算法的函数。但首先,我创建了一个 midpoint
函数:基本上,这个中点方法的第一个应用程序使用两个积分步骤。在连续积分中,步数增加了两个,每次积分后都进行理查森外推:当两个连续解的差异(在均方根意义上)小于规定的公差时,该过程停止。
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)
有人可以帮我解决这个错误吗:我根本不明白它的起源!!
非常感谢!
将行 r_old = r[1].copy
更改为 r_old = r[1].copy()
我正在尝试实现 Bulirsch-Stoer 算法:我创建了一个包含它的简化算法的函数。但首先,我创建了一个 midpoint
函数:基本上,这个中点方法的第一个应用程序使用两个积分步骤。在连续积分中,步数增加了两个,每次积分后都进行理查森外推:当两个连续解的差异(在均方根意义上)小于规定的公差时,该过程停止。
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)
有人可以帮我解决这个错误吗:我根本不明白它的起源!!
非常感谢!
将行 r_old = r[1].copy
更改为 r_old = r[1].copy()