how to run the same integral for different values of constants?
Asked Answered
S

1

0

I want to run an integral for different values of "e" and "m" and put the results in a list.

m =[0.14, 0.14, 0.14, 1.30, 4.50]
e = [2/3, -1/3, -1/3, 2/3, -1/3]

def f(z, r):
return ((e)**2)*(alpha_elm*Nc/2*np.pi**2)*(4)*(Q2)*(z**2)*((1-z)**2)*((scipy.special.k0(r*(z*(1-z)*Q2 + (m**2))))**2)

integrate.nquad(f, [[0, 1],[0, np.inf]])

how can i do that?

Sidney answered 7/10, 2021 at 3:55 Comment(2)
You mean call the function with m[0] and e[0], then m[1] and e[1], etc?Amaya
Yes! i have no clue how to do that... i tried to put "m[n]" and e[n] in the argument and run it from 0 to "len(m)", but it didn't worked...Sidney
A
1

You can define a partially applied version of your function where you set the values for e and m. Then iterate over their ranges of values and compute the specific results:

from functools import partial

def f(m, e, z, r):
    return ((e)**2)*(alpha_elm*Nc/2*np.pi**2)*(4)*(Q2)*(z**2)*((1-z)**2)*((scipy.special.k0(r*(z*(1-z)*Q2 + (m**2))))**2)

results = []
for mm, ee in zip(m, e):
    partial_f = partial(f, mm, ee)
    result = integrate.nquad(partial_f, [[0, 1], [0, np.inf]])
    results.append(result)

I would however strongly suggest to reformat and break down the overly complex definition of your function f.

Amaya answered 7/10, 2021 at 4:14 Comment(1)
Extremely useful answer.Skyscraper

© 2022 - 2024 — McMap. All rights reserved.