python:积分未知的上限内含未知的fsolve [英] python: fsolve with unknown inside the upper limit of an integral
问题描述
是否可以将fsolve方法应用于上限和被积分数未知的积分???我正在使用Quad方法在python中进行集成.
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.
提前谢谢!
推荐答案
可以.
假设您要查找x,使得t *(1-x * t)的从t = 0到t = x的t上的积分为0.您可以通过定义两个函数来做到这一点. integrand(t, x)
将计算t *(1-x * t),func(x)
将使用quad
对integrand
进行积分,其中x
既是积分的上限,又是被积分数的额外参数.这是一个演示:
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)
输出:
Solution: 1.22474487139
Exact solution: 1.22474487139
这篇关于python:积分未知的上限内含未知的fsolve的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!