积分的计算
1.一重积分的计算
Γ
(
z
)
=
∫
1
2
(
x
+
1
)
d
x
.
\Gamma(z) = \int_1^2\ (x+1)dx\,.
Γ(z)=∫12 (x+1)dx.
#方法一(scipy)
from scipy import integrate
import scipy.integrate
def fun(x):
return x+1
v,err=integrate.quad(fun,1,2)
print(v)
# 方法二(sympy)
import sympy
f=x+1
sympy.integrate(f,1,2)
2.二重积分的计算
Γ
(
z
)
=
∫
1
2
∫
1
x
x
y
d
x
d
y
.
\Gamma(z) = \int_1^2\int_1^x xydxdy\,.
Γ(z)=∫12∫1xxydxdy.
#二重积分的计算
# 方法一:(sympy)
from sympy.abc import x,y,z
import sympy
ff=y*x
ff1=sympy.integrate(ff,(y,1,x))
ff2=sympy.integrate(ff1,(x,1,2))#求多重积分
print(ff2.evalf())
# 方法二(scipy)
from scipy import integrate
def a(x,y):
return x*y
def b(x):
return x
v,err=integrate.dblquad(a,1,2,1,b)
print(v)
3.三重积分的计算
c=1-x-2*y
b=(1-x)/2
Γ
(
z
)
=
∫
0
1
∫
0
b
∫
0
c
2
x
d
x
d
y
.
\Gamma(z) = \int_0^1\int_0^b \int_0^c 2x dxdy\,.
Γ(z)=∫01∫0b∫0c2xdxdy.
#方法一(sympy)
from sympy.abc import x,y,z
import sympy
from scipy import integrate
f1=sympy.integrate(2*x,(z,0,1-x-2*y))
f2=sympy.integrate(f1,(y,0,(1-x)/2))
f3=sympy.integrate(f2,(x,0,1))
print(f3.evalf())
#方法二(scipy)
from scipy import integrate
def f(x,y,z):
return 2*x
def b(x):
return (1-x)/2
def c(x,y):
return 1-x-2*y
v,err=integrate.tplquad(f,0,1,0,b,0,c)
print(v)