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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

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