Reputation: 27
Is it possible to apply fsolve method for an integral with the unknown in the upper limit and in the integrand??? I am ussing quad method for integration in python.
Thanks in advance!!!
Upvotes: 1
Views: 2721
Reputation: 114921
Sure.
Suppose you want to find x such that the integral over t from t=0 to t=x of t*(1-x*t) is 0. You can do this by defining two functions. integrand(t, x)
will evaluate t*(1-x*t), and func(x)
will integrate integrand
using quad
, with x
as both the upper limit of the integration, and as the extra argument of the integrand. Here's a demo:
import numpy as np
from scipy.integrate import quad
from scipy.optimize import fsolve
def integrand(t, x):
return t*(1 - x*t)
def func(x):
y, err = quad(integrand, 0, x, args=(x,))
return y
# Use 1.0 as the initial guess. Note that a bad initial guess
# might generate a warning and return the degenerate solution at x=0.
sol = fsolve(func, 1.0)
print "Solution: ", sol[0]
# The exact solution that we want is sqrt(3/2)
print "Exact solution:", np.sqrt(1.5)
Output:
Solution: 1.22474487139
Exact solution: 1.22474487139
Upvotes: 1