python: fsolve with unknown inside the upper limit of an integral

人走茶凉 提交于 2019-12-06 15:01:48

问题


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!!!


回答1:


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


来源:https://stackoverflow.com/questions/27382952/python-fsolve-with-unknown-inside-the-upper-limit-of-an-integral

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!