Integration using trapezium method SciPy problems ('numpy.ndarray' object is not callable)

174 Views Asked by At

I'm trying to create a function that lets you input an expression for y with upper and lower bounds and N number of steps. It takes the expression, should integrate it and then run it through the trapezium method for N steps and spit out the result. Currently there is an error ('numpy.ndarray' object is not callable) in my way. Not sure how to fix it! Here's the code!


import numpy as np
import scipy.integrate as integrate
%matplotlib inline

def trap(f,b,a):
        return 0.5 * (b-a)*(f(a) + f(b))
def multistep(f,method,b,a,N):
    step = (b-a)/N
    lower = np.linspace(b,a,N,endpoint=False)
    upper = np.linspace(b+step,a,N)
    result = 0
    for n in range(N):
        result += method(f,lower[n],upper[n])
    return result

def trapezium_rule_integration_function(f,a,b,N):
    x = np.linspace(b,a,N)
    yf = []
    for p in x:
        yf.append(f(p))
    y = np.array(yf)
    return multistep(y,trap,b,a,N)
y = 1/(1+x**2)
trapezium_rule_integration_function(y,10,0,100)

Thank you in advance for any support available.

Johann Brahms

1

There are 1 best solutions below

1
Robin Thibaut On

By looking quickly, I see that this error stems from the function trapezium_rule_integration_function,

y = np.array(yf)
return multistep(y,trap,b,a,N)

The first argument of multistep should be a function, but you provided an array y, which is not callable by '()'. This array is then passed to multistep, which expects a function f instead.

By the way, you should rewrite y as

y = lambda x: 1/(1+x**2)

Best of luck.