使用R语言(积分)计算三平面切割的球体部分的体积

4

我想计算一个球的一部分的体积(V),这个球是由三个平面(x=0,y=0和z=1.5)与球体相交而成的。我正在使用R语言编程,以下是我的代码。我尝试了两种不同的方法,使用笛卡尔坐标和极坐标。但它们都得出了负数。

##  Compute the Volume between 3 planes x=0, y=0 and z=1.5 and a sphere
library("pracma", lib.loc="~/R/win-library/3.1")
f <- function(x, y)  (sqrt(4 -x^2 - y^2) - 1.5 ) # here the function(x,y)  is subtracted with -1.5 which represents the plane z=1.5 
xmin <- 0
xmax <- 2
ymin <- 0 
ymax <- function(x) (sqrt(4 - x^2))
I <- integral2(f, xmin, xmax, ymin, ymax)
I$Q   # Integral over sector is not so exact

# Exact Volume from AutoCAD V=0.3600



##  Volume of the sphere: use polar coordinates
f0 <- function(x, y) (sqrt(4 - x^2 - y^2)-1.5)  # for x^2 + y^2 <= 4 the f(x,y) means z changes between zmin=1 and zmax= sqrt(4-x^2-y^2)
fp <- function(t, r) r * f0(r*cos(t), r*sin(t))
quad2d(fp, 0, pi/2, 0, 2, n = 101)  # -0.523597

正确答案是 V= 0.3600 。请问有人可以给我提示吗?
谢谢。
2个回答

5

您的X-Y积分区域覆盖了f(x,y)-1.5为负数和正数的区域。您的球体与线z=1.5的交点是半径为sqrt(7/4)(使用勾股定理)的圆,因此请适当调整您的限制:

library(pracma)
f <- function(x, y)  (sqrt(4 -x^2 - y^2) - 1.5 ) # here the function(x,y)  is subtracted with -1.5 which represents the plane z=1.5 
xmin <- 0
xmax <- sqrt(7/4)
ymin <- 0 
ymax <- function(x) (sqrt(7/4 - x^2))
I <- integral2(f, xmin, xmax, ymin, ymax)
I$Q   # Integral over sector is not so exact
# [1] 0.3599741

非常接近您的期望。

那非常有帮助。但是当我想计算三个平面 x=1,y=1,z=1 和半径为2的同心球之间的体积时,我更改了限制条件(x从1到sqrt(3),y从1到sqrt(3-x^2))。它给出了错误的结果(V=0.0330)。正确的结果是V=0.0152。 - Mohamad Reza Salehi Sadaghiani
不,不是这样的。至少如果我理解你的整合域的话,正确的数值应该是0.26113536...!将上限y修正为sqrt(4-x^2)--应该是这样的--然后integral2将返回0.251140。您可以尝试通过设置“reltol”参数来提高精度,但我认为使用极坐标会得到更好的结果。 - Hans W.
我使用了AutoCAD,并且在三个平面x=1,y=1和z=1以及一个球体x^2+y^2+z^2=4之间的确切体积为V=0.0152。 - Mohamad Reza Salehi Sadaghiani

0

我已经解决了半径为2的球体以及球壳与三个平面x=1,y=1和z=1之间的相交体积。

##  Compute the Volume between 3 planes x=1.0, y=1.0 and z=1.0 and a sphere
library(pracma)
f <- function(x, y)  (sqrt(4 -x^2 - y^2) - 1 ) # here the function(x,y) is  subtracted with -1.5 which represents the plane z=1.5 
xmin <- 1
xmax <- sqrt(2)
ymin <- 1 
ymax <- function(x) (sqrt(3 - x^2))
I <- integral2(f, xmin, xmax, ymin, ymax)
I$Q   # 
# [1] 0.01520549
# Exact Volume from AutoCAD: 0.0152

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接