For E=0.46732451
and t=1.07589765
I am trying to solve for the upper limit of the integral t= \\int_{0}^{z} 1/sqrt(2*(0.46732451-z**2)), I plotted this function and it looks like this .
Around t=1
it kind of asymptotes.
I have the following code
import numpy as np
from scipy import integrate
from scipy.optimize import fsolve
def fg(z_up,t,E):
def h(z,E):
return 1/(np.sqrt(2*(E-z**2)))
b, err = integrate.quad(h, 0, z_up,args=(E))
return b-t
x0 = 0.1
print fsolve(fg, x0, args=(1.07589765, 0.46732451))[0]
But this code just outputs the guess value, no matter what I put, so I am guessing it has something to do with the fact that the curve asymptotes there. I should note that this code works for other values of t
which are away from the asymptotic region.
Can anyone help me resolve this?
Thanks
EDIT After playing around for a while, I solved the problem, but it's kind of a patchwork, it only works for similar problems not in general (or does it?)
I made the following changes: the maximum value that z
can attain is sqrt(0.46732451)
, so I set x0=0.5*np.sqrt(0.46732451)
and set factor
anywhere between 0.1
to 1
, and out pops the correct answer. I don't have an explanation for this, perhaps someone who is an expert in this matter can help?
You should use bisect
instead, as it handles nan
without problems:
print bisect(fg, 0.4, 0.7, args=(1.07589765, 0.46732451))
Here 0.4 and 0.7 are taken as an example but you can generalize this for almost any diverging integral by using 0
and let's say 1e12
as the limits.
However, I'm not sure I understand what you really want to do... if you want to find the limit at which the integral diverges, cf. your
I am trying to solve for the upper limit of the integral
then it's simply for z_up -> \\sqrt{E} \\approx 0,683611374
... So to find the (approximate) numerical value of the integral you just have to decrease z_up
from that value until quad
stops giving a nan
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.