def trapz(funct, args, a, b):
N = 100 #number of steps
step = (b-a)/N #step size
y = [] #initialize a list of values of y
for i in range(1,N-1) #loop through values of x while omitting the first and last points
x = a + (step * i) #each subsequent x value will be increased by the step size
y.append(funct(x,args)) # call the desired function and pass it the required arguments and x value
mid = math.fsum(y) #sum the values of y
area = step * ((funct(b,args) - funct(a,args))/2 + mid) #find the area under the curve with the trapezoid
#rule for numerical itnegration
return area #return the value of area back to the calling function
评论列表
文章目录