I think similar questions have answered here and here. I am also having similar problems with a code that performs the trapezoidal and simpson's 1by3 rule. I have ran the code on a desktop which runs fine, but using a different machine, the code fails. I am sharing the code and the error message.
Code
import numpy as np
exp = 5.33333
ul = 10
ll = -10
n = 50
def f(x): # The function
return np.e**x**2
def trapezoidal(ll, ul, n): #Setting up Trapezoidal rule
traint = (f(ll) + f(ul)) / 2
h = (ul - ll) / 2
for i in range(1,n):
k = ll + i * h
traint = traint + f(k)
traint = h * traint
return traint
def simpson1by3(ll, ul, n): #Setting up Simpson 1by3 rule
simint = (f(ll) + f(ul)) / 2
h = (ul - ll) / 2
for i in range(1,n,2):
simint = simint + (2 * f(ll + i * h)) + (4 * f(ll + ((i + 1) * h)))
simint = simint * h / 3
return simint
res_sim = simpson1by3(ll, ul, n)
res_trap = trapezoidal(ll, ul, n)
e_rel_trap = (res_trap - exp) / exp
e_rel_simp = (res_simp - exp) / exp
f1 = open("trap.txt", "w+")
f2 = open("simp.txt", "w+")
for n in range(1,50):
h = (ul - ll) / 2
res_trap = trapezoidal(ll, ul, n)
res_simp = simpson1by3(ll, ul, n)
f1.write(str(n) + " " + str(res_trap) + '\n')
f2.write(str(n) + " " + str(res_simp) + '\n')
f1.close()
f2.close()
The error message is,
Traceback (most recent call last):
File "simpson1by3+trapezoidal.py", line 30, in <module>
res_sim = simpson1by3(ll, ul, n)
File "simpson1by3+trapezoidal.py", line 26, in simpson1by3
simint = simint + (2 * f(ll + i * h)) + (4 * f(ll + ((i + 1) * h)))
File "simpson1by3+trapezoidal.py", line 11, in f
return np.e**x**2
OverflowError: (34, 'Numerical result out of range')