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

In summary: Use it as you see fit.In summary, one approach to solving an integral with an unknown h is to define a function f(h) that computes the integral and its derivative, and then use a root solver from scipy to find the value of h that makes f(h) equal to a given constant. This can be done using Newton's method and scipy's quad_vec function for evaluating the integral and its derivative at the same time.
  • #1
Arman777
Insights Author
Gold Member
2,168
193
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
  • #3
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.
 
  • #4
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.
 

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

1. What is an unknown in an integral?

An unknown in an integral refers to a variable that is being integrated over, also known as a variable of integration. This variable is often denoted as x or t and represents the input value of the function being integrated.

2. How do I find an unknown in an integral using Python?

Python has a built-in function called integrate in the scipy library that can be used to find an unknown in an integral. This function takes in the function to be integrated, the limits of integration, and the variable of integration as parameters and returns the result of the integral.

3. Can I use a specific method to find the unknown in an integral?

Yes, the integrate function in Python allows you to specify the method you want to use to find the unknown in the integral. Some commonly used methods include quad, trapz, and simps. These methods differ in their accuracy and speed of computation, so it is important to choose the appropriate method for your specific integral.

4. Is it possible to find the unknown in a multidimensional integral using Python?

Yes, the scipy library also has a function called nquad that can be used to find the unknown in a multidimensional integral. This function takes in the function to be integrated, the limits of integration for each dimension, and the variables of integration as parameters and returns the result of the integral.

5. Are there any limitations to using Python to find an unknown in an integral?

While Python's integrate and nquad functions are powerful tools for finding unknowns in integrals, they may not be able to handle certain types of integrals, such as those with singularities or infinite integrals. In these cases, it may be necessary to use other numerical or analytical methods to find the unknown.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
1
Views
694
  • Programming and Computer Science
Replies
2
Views
2K
Replies
6
Views
738
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top