2012-11-02 22 views
4

我正在努力使用scipy.integrate,我用tplquad,但我怎樣才能使用integrate來獲得(截斷)球的體積?謝謝如何使用scipy.integrate獲取截斷球體的體積?

import scipy 
from scipy.integrate import quad, dblquad, tplquad 
from math import* 
from numpy import * 

R = 0.025235 #radius 
theta0 = acos(0.023895) #the angle from the edge of truncated plane to the center of 
sphere 

def f_1(phi,theta,r): 
    return r**2*sin(theta)*phi**0 
Volume = tplquad(f_1, 0.0,R, lambda y: theta0, lambda y: pi, lambda y,z: 0.0,lambda 
y,z: 2*pi) 

print Volume 
+2

odeint用於解決微分方程,不積分。我真的不明白,爲什麼你想在這裏使用它。此外,可以通過分析整合最後兩個維度來簡化積分,使得您只剩下一個積分。 –

回答

2

要截斷角度,使用球座標系統很方便。假設定義taken from Arkansas TUradius (r)theta (t)phi (p)爲: enter image description here

然後,您可以截斷設置限制:r1r2t1t2p1p2

import scipy 
from scipy.integrate import quad, dblquad, tplquad 
from numpy import * 
# limits for radius 
r1 = 0. 
r2 = 1. 
# limits for theta 
t1 = 0 
t2 = 2*pi 
# limits for phi 
p1 = 0 
p2 = pi 

def diff_volume(p,t,r): 
    return r**2*sin(p) 

volume = tplquad(diff_volume, r1, r2, lambda r: t1, lambda r: t2, 
             lambda r,t: p1, lambda r,t: p2)[0] 

乘坐飛機截斷它是方便使用笛卡爾座標系(x,y,z),其中x**2+y**2+z**2=R**2see mathworld)。在這裏,我截斷球體的一半來證明:

from `x1=-R` to `x2=R`<br> 
from `y1=0` to `y2=(R**2-x**2)**0.5`<br> 
from `z1=-(R**2-x**2-y**2)**0.5` to `z2=(R**2-x**2-y**2)**0.5`<br> 
(an useful example using lambdas): 

R= 2. 
# limits for x 
x1 = -R 
x2 = R 

def diff_volume(z,y,x): 
    return 1. 

volume = tplquad(diff_volume, x1, x2, 
       lambda x: 0., lambda x: (R**2-x**2)**0.5, 
       lambda x,y: -(R**2-x**2-y**2)**0.5, 
       lambda x,y: (R**2-x**2-y**2)**0.5)[0]