0

I want to take this triple integral over func(eps,e,p,M):

enter image description here

The integral is over eps, e and p and the code is here:

def Probability(eps,e,p,M):
    prob=eps*e*(e**2-p**2)*function(M)
    return prob

a=0
b=np.inf

def gfun():
    return 0.25

def hfun():
    return 0.5

def qfun(e):
    return -e

def rfun(e):
    return 3*e-1

def n(M):
    n=integrate.tplquad(Probability, 0, np.inf,gfun,hfun,qfun,rfun,args=(M))
    return n

but I get this error:

integrate() argument after * must be an iterable, not float

I don't know what's the problem, and the other question: is the ordering of parameters in function Probability True?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Smtl
  • 49
  • 6
  • 2
    my guess that `args` must be passed as `tuple` like `(M,)`, not as scalar – Azat Ibrakov Sep 14 '18 at 10:16
  • why? what is tuple? what is the difference? I tried that but the new error is "gfun() takes 0 positional arguments but 1 was given" – Smtl Sep 14 '18 at 10:43

1 Answers1

0

As mentioned, the additional arguments have to be in a tuple. Note that (1) is actually the integer 1, a one-tuple is written with a comma (1,). And this tuple is also passed to the boundary functions. This is the reason for the error "gfun() takes 0 positional arguments but 1 was given". The code below is working:

import numpy as np
from scipy.integrate import tplquad

def probability(eps, e, p, M):
    prob = eps * e * (e**2 - p**2) * M
    return prob


def gfun(M):
    return 0.25

def hfun(M):
    return 0.5

def qfun(e, M):
    return -e

def rfun(e, M):
    return 3*e - 1

M = .1
n = tplquad(probability, 0, 12, gfun, hfun, qfun, rfun, args=(M,))

A tuple is almost like a list (see for more detail python: list vs tuple, when to use each?). You could also write:

n = tplquad(probability, 0, 12, gfun, hfun, qfun, rfun, args=[M])

Here the comma is not needed.

xdze2
  • 3,986
  • 2
  • 12
  • 29
  • another question! If I have a complicated function of M in my integrand(f(M)), It takes l long time to interpret the integral. Does it calculate f(M) in every step of integration? should I define f(M) as a 'variable' instead of function for a specific M? – Smtl Sep 16 '18 at 07:15
  • @Sina.t indeed I think it could be better to do the computation of `f(M)` outside the `probabilty()` function, since it is not a function of the integration variables, and then pass the result `f(M)` as an additional argument – xdze2 Sep 17 '18 at 08:16