![]() |
How to make a double loop to evaluate this triple integral - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: How to make a double loop to evaluate this triple integral (/thread-42079.html) |
How to make a double loop to evaluate this triple integral - Safinazsalem - May-06-2024 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 vanishesSo 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! RE: How to make a double loop to evaluate this triple integral - sakshi009 - Dec-26-2024 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 π. |