我需要在win7中用python 3.2解决嵌入了另一个积分方程的积分方程。
有2个积分方程。
代码在这里:
import numpy as np
from scipy.optimize.minpack import fsolve
from numpy import exp
from scipy.integrate.quadpack import quad
import matplotlib.pyplot as plt
import sympy as syp
lb = 0
def integrand2(x, a):
print("integrand2 called")
return x**(a-1) * exp(-x)
def integrand1(x, b, c):
print("integrand1 called")
integral , err = quad(integrand2, lb/b, syp.oo , args=(1+c))
return c/(b*integral)
def intergralFunc1(b,c):
integral,err = quad(integrand1, 0, 10, args=(b,c))
print("integral is ", integral, " and err is ", err)
print("b is ", b, " and c is ", c)
return 10 - integral
def findGuess():
vfunc = np.vectorize(intergralFunc1)
f1 = np.linspace(0.01, 10,10)
f2 = np.linspace(0.01, 10,10)
result = vfunc(f1, f2)
plt.plot(f1, result)
plt.xlabel('f1')
plt.subplot(211)
plt.plot(f2, result)
plt.xlabel('f2')
plt.subplot(212)
plt.show()
def solveFunction():
sol= fsolve(intergralFunc1, 5, 5, full_output=True)
return sol
if __name__ == '__main__':
findGuess()
sol = solveFunction()
print("sol is ", sol)
print("verification: \n")
print("f(b,c) is ", intergralFunc1(sol[0],5))
我得到的结果毫无意义。
integral is nan and err is nan
b is [ 5.] and c is 5
f(b,c) is nan
任何帮助将不胜感激!!!
答案 0 :(得分:1)
由于某种原因,integrand1
返回numpy.ndarray
,预计会返回浮点数。
同样在我的机器numpy
报告
y:289: UserWarning: Extremely bad integrand behavior occurs at some points of the
integration interval.
这意味着您的问题在数值上非常不稳定。因此,可以预期无意义的结果。
答案 1 :(得分:0)
一个问题是使用sympy.oo
表示无穷大。
In [36]: quad(integrand2, 0, syp.oo, args=(1+5,))
(nan, nan)
In [37]: quad(integrand2, 0, np.inf, args=(1+5,))
(120.0, 2.2281810652e-07)
In [38]: quad(integrand2, 0, float(syp.oo), args=(1+5,))
(120.0, 2.2281810652e-07)
这不是一个浮点数:
In [39]: type(syp.oo)
sympy.core.numbers.Infinity
不确定为什么它不能按预期工作。