编制python整数计算程序

2024-03-28 11:50:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python程序来计算一个函数在一个点上的积分。但这根本不有效,因为我想同时计算更多的x值,而不仅仅是一个值。有人知道吗?你知道吗

from scitools.std import *
def trapezoidal(f,a,x,n):
    h = (x-a)/float(n)
    I = 0.5*f(a)
    for i in iseq(1, n-1):
        I += f(a+i*h)
    I += 0.5*f(x)
    I *= h
    return I

class Integral:
    def __init__(self, f, a, n=100):
        self.f, self.a, self.n = f,a,n
    def __call__(self,x):
        return trapezoidal(self.f,self.a,x,self.n)

Tags: 函数infromimportself程序forreturn