使用 SciPy 积分本身涉及积分的函数
Integrating a function which itself involves an integral using SciPy
所以我想做的是计算 y 的积分,然后我想计算 exp(-t* 解)并在 x 上积分。
应该是这样的:
x (exp(-t* B)) 从 0 到 Pi 的积分
B= y(3.0*(sin(x)*sin(y)*sin(TM)+cos(x)*cos(TM))**2.0-1.0)**2.0 从 0 积分到 2Pi
我尝试用 scipy 来做,但是如果没有 x,它不会对 y 进行积分。
到目前为止,这是我的代码:
from numpy import cos, sin, exp
import math
import scipy.integrate as integrate
t=0.0
TM=(54.74/180)*math.pi
def integrand(y,x):
return (3.0*A(y,x)**2.0-1.0)**2.0
def A(y,x):
return sin(x)*sin(y)*sin(TM)+cos(x)*cos(TM)
while t<10:
t+=4
resultbet, err=integrate.nquad(integrand, [(0.0, 2*math.pi)])
result=exp(-t*resultbet)
resultalph, err=integrate.nquad(result, [(0.0, math.pi)])
您想对 y 求积分,然后应用指数函数,然后求积分
超过 x。这不是二重积分,这是根据积分定义的函数的积分。我相应地重写了代码,保留了 A 和被积函数的定义:
def B(x):
return integrate.quad(lambda y: integrand(y,x), 0, 2*math.pi)[0]
while t<10:
t += 4
result = integrate.quad(lambda x: x*exp(-t*B(x)), 0, math.pi)
print(result)
输出:
(0.28030701904213656, 1.0526577138223263e-08)
(0.1972630762340601, 1.3996736645569703e-12)
(0.16081518182419666, 9.188712047715815e-11)
这里,第一个数字是积分值,第二个是误差估计;这就是 integrate.quad returns。 (这就是函数 B 末尾有 [0] 的原因。)
函数 B 将 x 作为参数,returns 对 y 从 0 到 2*pi 进行一维积分的结果。然后在一个循环中,对函数x*exp(-t*B(x))
进行积分
所以我想做的是计算 y 的积分,然后我想计算 exp(-t* 解)并在 x 上积分。
应该是这样的:
x (exp(-t* B)) 从 0 到 Pi 的积分
B= y(3.0*(sin(x)*sin(y)*sin(TM)+cos(x)*cos(TM))**2.0-1.0)**2.0 从 0 积分到 2Pi
我尝试用 scipy 来做,但是如果没有 x,它不会对 y 进行积分。
到目前为止,这是我的代码:
from numpy import cos, sin, exp
import math
import scipy.integrate as integrate
t=0.0
TM=(54.74/180)*math.pi
def integrand(y,x):
return (3.0*A(y,x)**2.0-1.0)**2.0
def A(y,x):
return sin(x)*sin(y)*sin(TM)+cos(x)*cos(TM)
while t<10:
t+=4
resultbet, err=integrate.nquad(integrand, [(0.0, 2*math.pi)])
result=exp(-t*resultbet)
resultalph, err=integrate.nquad(result, [(0.0, math.pi)])
您想对 y 求积分,然后应用指数函数,然后求积分 超过 x。这不是二重积分,这是根据积分定义的函数的积分。我相应地重写了代码,保留了 A 和被积函数的定义:
def B(x):
return integrate.quad(lambda y: integrand(y,x), 0, 2*math.pi)[0]
while t<10:
t += 4
result = integrate.quad(lambda x: x*exp(-t*B(x)), 0, math.pi)
print(result)
输出:
(0.28030701904213656, 1.0526577138223263e-08)
(0.1972630762340601, 1.3996736645569703e-12)
(0.16081518182419666, 9.188712047715815e-11)
这里,第一个数字是积分值,第二个是误差估计;这就是 integrate.quad returns。 (这就是函数 B 末尾有 [0] 的原因。)
函数 B 将 x 作为参数,returns 对 y 从 0 到 2*pi 进行一维积分的结果。然后在一个循环中,对函数x*exp(-t*B(x))
进行积分