trapz.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:IntroPython2016a 作者: UWPCE-PythonCert 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号