What is wrong with my implementation of numerical integration?
Question:
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')
Answers:
This mainly due to the limitation of your local machine.
You see, python allocates only some amount of space for integers and floats
Its usually 4 for int
and 8 for float
.
in you code, you have set the data type as float with the f
You computer cannot handle such calculations that is taking 16 bytes of memory,
If you are sure that your calculations or input would not have any decimals in it,
you can change the data type to integers.. for the inputs, If it still doesnt work, consider also changing the answers’s data type/
Hope it is helpfull
Seems your integration interval is wrongly set. If I didn’t remembered wrongly the interval shall be like (b-a)/2n, i.e., ul-ll/n
or ul-ll/2n
depends on your setting but not ul-ll/2
. Setting the interval to ul-ll/2
makes your actual integral upper limit too large, and for a function like e**x**2, it overflows out of max Pythonic float range (around 1.8e308 == 2^1024 == FP256 MAX on my machine)
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')
This mainly due to the limitation of your local machine.
You see, python allocates only some amount of space for integers and floats
Its usually 4 for int
and 8 for float
.
in you code, you have set the data type as float with the f
You computer cannot handle such calculations that is taking 16 bytes of memory,
If you are sure that your calculations or input would not have any decimals in it,
you can change the data type to integers.. for the inputs, If it still doesnt work, consider also changing the answers’s data type/
Hope it is helpfull
Seems your integration interval is wrongly set. If I didn’t remembered wrongly the interval shall be like (b-a)/2n, i.e., ul-ll/n
or ul-ll/2n
depends on your setting but not ul-ll/2
. Setting the interval to ul-ll/2
makes your actual integral upper limit too large, and for a function like e**x**2, it overflows out of max Pythonic float range (around 1.8e308 == 2^1024 == FP256 MAX on my machine)