曾经尝试做这个python。问题详情在图片链接中
运行程序时出现此错误
感谢您的帮助
File "C:/Users/User/Desktop/Python/tapez.py", line 9, in exactIntegral
integral = ((4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +
TypeError: 'int' object is not callable
这是代码
import math
import numpy
import scipy.integrate.quadrature as quad
def fun(x):
return x*numpy.sin(x)
def exactIntegral(a, b):
integral = (((9)+4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +
(2(math.exp( 0.15 * b)))))+ (((9)+4(math.cos(0.4*a)** 2)) *
((5(math.exp( -0.5 * a))) + (2(math.exp( 0.15 * a)))))
return integral
a = 0.0
b = 8.0
exact = exactIntegral(a, b)
estimate = quad(fun,a,b)
print("Gaussian Quadrature: ", exact)
# Trapazoid Rule
n = 100
h = (b-a)/(n-1)
x = numpy.linspace(a,b,num=n)
area = 0
for i in range(n-1):
area = area + h*(fun(x[i]) + fun(x[i+1]))/2.0
print("Trapazoid rule: ", area)
答案 0 :(得分:4)
您的问题在这里:
integral = (((9)+4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +
(2(math.exp( 0.15 * b)))))+ (((9)+4(math.cos(0.4*a)** 2)) *
((5(math.exp( -0.5 * a))) + (2(math.exp( 0.15 * a))))
Python不会自动将2(x+1)
之类的内容解释为2*(x+1)
。您需要明确指定乘法运算符*
。
对于Python,2()
是名为2
的函数的函数调用。但是2
是一个int对象,不能像函数一样调用