Search code examples
pythonbuilt-inoperands

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


I am trying to implement a Bulirsch-Stoer Algorithm : I've created a function that contains a simplified algorith of it. But first, I've created a midpoint function : basically, the firs application of this midpoint method uses two integration steps. The number of steps is increased by two in successive integration, each integration being followed by a Richardson extrapolation : the procedure is stopped when two successive solutions differ (in the root-mean-square sense) by less than a prescribed tolerance.

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")

And here is the algorithm :

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)

I am trying to test it on an example :

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")

but I have this kind of error :

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

On this line : e = sqrt(np.sum((r[1] - r_old)**2)/n)

Could someone please help me with this error : I Don't understand its origin at all !!

Thanks a lot !


Solution

  • change line r_old = r[1].copy to r_old = r[1].copy()