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

Click For Summary
SUMMARY

This discussion focuses on solving an integral with an unknown variable h using Python's SciPy library. The recommended approach involves defining a function f(h) that represents the difference between the integral and a constant C, and then utilizing SciPy's root solver to find the value of h that satisfies f(h) = 0. The integration is performed using scipy.integrate.quad_vec to simultaneously evaluate the function and its derivative, enhancing the efficiency of the solution process.

PREREQUISITES
  • Understanding of integrals and their properties
  • Familiarity with Python programming
  • Knowledge of SciPy library functions, specifically scipy.integrate.quad_vec and scipy.optimize
  • Basic concepts of numerical methods, particularly Newton's method
NEXT STEPS
  • Explore the documentation for scipy.integrate.quad_vec to understand its usage and parameters
  • Learn about root-finding algorithms in SciPy, focusing on scipy.optimize.fsolve
  • Study Newton's method in detail to grasp its application in numerical solutions
  • Investigate error handling and validation techniques for numerical integration results
USEFUL FOR

Mathematicians, data scientists, and software developers who need to solve integrals with unknown variables using Python, particularly those utilizing the SciPy library for numerical computations.

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.
 
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 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 17 ·
Replies
17
Views
7K