Python Forum
How to make a double loop to evaluate this triple integral
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make a double loop to evaluate this triple integral
#1
I have a function f(t1,t2,t3, a,k) where I make a triple integral over t1,t2,t3 and make a loop on the value of the variable "k"

-The integration limits of t1 and t2 are from 0 to a

-The integration limits of t3 is from 0 to infinity

The question is how to make a second loop over the variable a where it tends to zero

Here is the code that I use:


# %%
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.10061*10**-5;ti=-100*H;end=-H;step=H;a=0;

f = lambda t1, t2,t3, k: t1 + t2 +k +  a +t3
X = np.arange(0,50,0.1)

g=float('inf')
#plot(X,f(X))

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

plt.plot(X,F(X))
plt.title("P(k)")
plt.show()
As you glance that in this code I write f(t1,t2,t3,k) and let a=0. But that makes the integral vanishes

So how to adjust this code and write f(t1,t2,t3,a,k) then run a second loop on "a" as a variable like "k" such that "a" tends to zero.


Any help is appreciated!
Reply
#2
To incorporate a second loop over the variable π‘Ž, making it decrease towards zero, the code must be adjusted to dynamically update the value of
π‘Ž during the computation. Below is a step-by-step explanation and solution:

Modify the Function Definition:

Update the function f to accept π‘Ž as an argument, allowing π‘Ž to change during iterations.

Add an Outer Loop for π‘Ž :

Introduce a loop where π‘Ž progressively decreases, such as using a logarithmic or linear scale. The integration will be recalculated for each π‘Ž.

Adjust Integration:

The integration must respect the limits based on the current value of π‘Ž. Modify the integration process accordingly.

Here’s the adjusted code:

import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt
import math

# Define constants
H = 4.10061 * 10 ** -5
g = float('inf')

# Function definition with `a` as an argument
def f(t1, t2, t3, a, k):
    return t1 + t2 + k + a + t3

# X-axis values for `k`
X = np.arange(0, 50, 0.1)

# Function to compute the integral for a given `k` and `a`
def F(k, a):
    y, err = integrate.tplquad(
        f, 
        0, a,  # t1 limits
        lambda x: 0, lambda x: a,  # t2 limits
        lambda x, y: 0, lambda x, y: g,  # t3 limits
        args=(a, k)
    )
    return y / (math.exp(H * a) ** 2)

# Loop over `a` values
a_values = np.logspace(-3, 0, num=50)  # `a` tending to 0
results = []

for a in a_values:
    result = [F(k, a) for k in X]
    results.append(result)

# Plot for the smallest `a` as an example
plt.plot(X, results[-1], label=f'a={a_values[-1]:.3f}')
plt.title("P(k) for Different a")
plt.xlabel("k")
plt.ylabel("P(k)")
plt.legend()
plt.show()
This solution enables integration for dynamically decreasing π‘Ž.
buran write Dec-28-2024, 07:26 AM:
spam content removed
Larz60+ write Dec-26-2024, 03:12 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Tags have been added this time. Please use BBCode tags on future posts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Evaluate Calculations yrstruly 0 1,905 Jun-17-2023, 06:51 PM
Last Post: yrstruly
  Can I evaluate a Chebyshev polynomial using a function player1681 1 2,639 Nov-22-2019, 06:33 AM
Last Post: scidam
  Double 'for' loop and writing in a new columns dataframe marco_ita 0 2,248 Sep-07-2019, 12:44 PM
Last Post: marco_ita
  finding the integral of probability density function Staph 3 4,786 Aug-11-2019, 09:19 AM
Last Post: buran
  Evaluate dataset with logistic regression chisox721 6 4,926 Jun-06-2019, 03:01 PM
Last Post: chisox721
  Hydrogen ground state integral Eduard 2 3,239 Sep-26-2018, 10:19 PM
Last Post: Eduard

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020