我有以下Python脚本
def application():
from math import exp
v = lambda t: 3*(t**2)*exp(t**3)
n = int(input('n: '))
numerical = trapezoidal(v, 0, 1, n)
# Compare with exact result
V = lambda t: exp(t**3)
exact = V(1) - V(0)
error = exact - numerical
print("n = %d: estimation = %.16f, error: %g"%(n, numerical, error))
def trapezoidal(f,a,b,n):
h = float(b-a)/n #width
result = 0.5*f(a) + 0.5*f(b)
for i in range(1,n):
result += f(a + i*h)
result *= h
return result
名为trapezoidal.py
。我无法理解为什么当我从终端(python trapezoidal.py
)呼叫它时,我什么都没得到。
答案 0 :(得分:0)
您应该定义主要功能
def main():
# your code here
if __name__ == "__main__":
main()