-2
from scipy.integrate import quad
from math import sqrt
f = lambda x, a: a**2 * x # here a is a constant.
F = lambda x, a: quad(f, 0, x, args=(a,))[0]
rho = 5

I need to compute the integral of

1/sqrt(F(rho,a)-F(s,a)), 

s is from 0 (lower limit) to rho (upper limit).

mathfun
  • 101
  • 7
  • Welcome to StackOverflow. This question is missing context or other details: Please improve the question by providing additional context, which ideally includes your thoughts on the problem and any attempts you have made to solve it. This information helps others identify where you have difficulties and helps them write answers appropriate to your experience level. You also need to state exactly what your difficulty is, what you expected, what you got, and any traceback. – Rory Daulton Jul 25 '19 at 18:30

2 Answers2

1

I think your question is missing some information (about a for example) from your previous post How to use `scipy.integrate.quad` to compute integral of a function which depends on the integral of another function You should probably fix that by editing your question text.


Regarding the current issue: Why don't you just define a new function g and then refer to the other function and integrate the same way as in F?

from scipy.integrate import quad
from math import sqrt

f = lambda x, a: a**2 * x
F = lambda x, a: quad(f, 0, x, args=(a,))[0]

rho = 5
g = lambda x, a: 1 / sqrt(F(rho, a) - F(x, a))

I = quad(g, 0, rho, args=(4,))
print(I)

This prints:

(0.5553603672694568, 1.9614421198355103e-11)
finefoot
  • 9,914
  • 7
  • 59
  • 102
  • @ finefoot, thank you for your help. With your help I proceed to a new step in my problem. Could you please help me with this problem too.https://stackoverflow.com/questions/57225636/solving-integral-nonlinear-equations-simultaneously – mathfun Jul 27 '19 at 00:58
0

All integrals can be computed symbolically, no need for quad. F is

def F(x):
    return a**2 * x**2 / 2

and

def g(x):
    1 / sqrt(a**2 / 2 * (rho**2 - s**2))

val = quad(g, 0, rho)

is

pi / sqrt(2) / a

(independent of rho).


Besides, you can simply define a outside of the functions. (I never understood why it's necessary for quad to have an args argument.)

from scipy.integrate import quad
from math import sqrt

a = 3.14

f = lambda x: a**2 * x
F = lambda x: quad(f, 0, x, args=(a,))[0]
rho = 5
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • Could you please give me an idea of how to solve this problem, https://stackoverflow.com/questions/57225636/solving-integral-nonlinear-equations-simultaneously – mathfun Jul 29 '19 at 19:32
  • To the parenthetical comment in the answer, you may want to have `args` if there is an outer optimization problem. For example, I want to optimize `a`, which is a parameter in this integral, to fit to some data. – Matthew Flamm Jul 29 '19 at 19:56
  • @MatthewFlamm You could still just define `a` outside of the function to be integrated. – Nico Schlömer Jul 29 '19 at 19:58
  • I think you need yet another nested function to do it your way. But note that I said 'may want to have', since I think this is a matter of style and taste. – Matthew Flamm Jul 29 '19 at 20:03