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

Click For Summary

Discussion Overview

The discussion revolves around finding an unknown variable, h, within an integral using Python, specifically through the use of the SciPy library. Participants explore various methods and functions that could be employed to solve this problem, including numerical approaches and integration techniques.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant presents an integral with an unknown h and seeks guidance on how to solve it using SciPy.
  • Another participant suggests a link to a Stack Overflow post that may provide relevant insights or solutions.
  • A proposed approach involves defining a function f(h) that represents the difference between the integral and a constant C, and using a SciPy root solver to find h such that f(h) equals zero.
  • A further suggestion includes using Newton's method for solving the equation, highlighting the use of scipy.integrate.quad_vec to evaluate the integral and its derivative simultaneously.
  • A code snippet is provided that outlines an iterative method to estimate h, including considerations for initial guesses and convergence criteria.

Areas of Agreement / Disagreement

Participants present various methods and approaches without reaching a consensus on a single solution. Multiple viewpoints and techniques are discussed, indicating that the problem remains open to interpretation and exploration.

Contextual Notes

The discussion does not clarify specific assumptions regarding the integral or the functions involved, nor does it resolve potential limitations in the proposed methods or the applicability of the suggested code.

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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
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