Python Forum
How to run this triple integration in python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to run this triple integration in python (/thread-42161.html)



How to run this triple integration in python - Safinazsalem - May-20-2024

When I run the following triple integral in python, it gives me an error:

 IntegrationWarning: The occurrence of roundoff error is detected, which prevents the requested tolerance from being achieved
Here is the code, it is to plot a triple integral f(t1, t2, m, k) against a variable k.


import numpy as np
import scipy.special
from scipy import integrate
from scipy.special import kn
import matplotlib.pyplot as plt

import math
import time, sys

H = 4
ti = -H
a = 0.1

f = lambda t1, t2, m, k: (k**3 * m**3) * (1/(H * t1)) * (1/(H * t2)) * (2/k)**2

X = np.arange(0, 50, 0.1)
g = float('inf')

def F(x):
    res = np.zeros_like(x)
    for i, val in enumerate(x):
        y, err = integrate.tplquad(f, ti, a, lambda x: ti, lambda x: a,  lambda x, y: 0, lambda x, y: g, args=(val,))
        res[i] = y
    return res

plt.plot(X, F(X))
plt.title("P(k), H=2.5")
plt.xlabel("k")
plt.ylabel("P")
plt.savefig('P(k).png')
plt.show()
How can I run the code without IntegrationWarning?


RE: How to run this triple integration in python - Axel_Erfurt - May-20-2024

There are more errors

Output:
/tmp/tmp.py:16: RuntimeWarning: divide by zero encountered in scalar divide f = lambda t1, t2, m, k: (k**3 * m**3) * (1/(H * t1)) * (1/(H * t2)) * (2/k)**2 /tmp/tmp.py:16: RuntimeWarning: invalid value encountered in scalar multiply f = lambda t1, t2, m, k: (k**3 * m**3) * (1/(H * t1)) * (1/(H * t2)) * (2/k)**2 /usr/lib/python3/dist-packages/scipy/integrate/_quadpack_py.py:1225: IntegrationWarning: The occurrence of roundoff error is detected, which prevents the requested tolerance from being achieved. The error may be underestimated. quad_r = quad(f, low, high, args=args, full_output=self.full_output,