denim
denim

Reputation: 49

Python Gaussian Quadrature and the Trapezoid rule Integral error

QUESTION DETAILS

Been trying to do this python. Question details are in the image link

Am getting this error when running the program

Any help is appreciated

   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

Here is the code

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)

Upvotes: 0

Views: 1260

Answers (1)

vencaslac
vencaslac

Reputation: 2855

Your issue is here:

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 does not interpret things like 2(x+1) as 2*(x+1) automatically. You need to specify the multiplication operator * explicitly.

For Python 2() is a function call of a function named 2. But 2 is an int object and it cannot be called like a function

Upvotes: 5

Related Questions