Python Is there a Python function that finds an unknown inside an integral?

AI Thread Summary
To find an unknown variable h within an integral, one effective method is to define a function f(h) that represents the difference between the integral and a constant C, then use a root solver from the SciPy library. The integration can be performed using scipy.integrate.quad_vec, which allows for simultaneous evaluation of the function and its derivative. An iterative approach, such as Newton's method, can be implemented to refine the estimate of h until the difference between successive approximations is below a specified tolerance. An initial guess for h is necessary, and the process continues until convergence is achieved. This method provides a systematic way to solve for h in the integral.
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
I have a integral with unknown h. My integral looks like this

1613063957901.png


where C, a, b are constants F(x) and G(x) are two functions. So the only unknows in the integral is h. How can I solve it ? I guess I need to use scipy but I don't know how to implement or use which functions.

Thanks
 
Technology news on Phys.org
One approach would be to define f(h) = the_integral(h) - C, and then use some scipy root solver to find h such that f(h) = 0.
 
Using Newton's method.

Integration is done using scipy.integrate.quad_vec in order to evaluate the function and its derivative at the same time. (We can't do this if we use scipy.optimize.fsolve; we would have to pass the function and its derivative as separate arguments.)

Python:
from scipy.integrate import quad
import numpy as np

# An initial guess for h. Change this to something more suitable.
h0 = 1.0

# Loop terminates if absolute difference between successive approximations 
# is less than this.
tol = 1E-6

h_new = h0
h_old = hnew + 2*tol
while abs(h_old - h_new) >= tol:
   h_old = h_new

   # Compute integral at h_old as well as its derivative with respect to h
   res = quad_vec(
      lambda x : np.array([
         1/np.sqrt(a*G(x) + (h_old**2 - b)*F(x)), 
         -3*np.sqrt(a*G(x) + (h_old**2 - b)*F(x))**(-3) * h_old * F(x)
      ]),
      0, 100
   ) 
   
   # The actual value is res[0]. Other elements may contain error indications
   # which you should check before continuing. See the documentation for details.

   # New guess
   h_new = h_old - (res[0][0] - C)/res[0][1]

# h_new now contains the estimated value of h.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
9
Views
3K
Replies
10
Views
3K
Replies
1
Views
1K
Replies
2
Views
4K
Replies
6
Views
7K
Replies
15
Views
2K
Replies
17
Views
7K
Back
Top