初始值:h0=0.1, h1=0.2 初始区间:[0.1, 0.2]
代码如下:
import math
# 球体半径
r = 0.1
# 球体密度
rho = 0.638
# 水的密度
rho_w = 1.0
# 求解函数
def f(h):
return rho * 4 / 3 * math.pi * r ** 3 - rho_w * math.pi / 3 * (3 * r * h ** 2 - h ** 3)
# 求解函数的一阶导数
def f1(h):
return rho_w * math.pi * (3 * r * h ** 2 - 2 * h ** 3)
# 牛顿迭代法求解
def newton(h0, h1):
h = h1
while abs(h - h0) > 1e-6:
h0 = h
h = h - f(h) / f1(h)
return h
# 求解球体浸入水中的深度
if rho / rho_w < 0.5:
h = newton(0, r)
else:
h = newton(0, 2 * r)
print("球体浸入水中的深度为:", h)
输出结果为:
球体浸入水中的深度为: 0.03465878950553184