定义球体半径、密度、水密度和重力加速度
r = 0.1 rho = 0.638 rho_w = 1000 g = 9.8
定义函数计算V_h和V_omega
def calculate_V_h(h): return (1/3)3.14(3rh2-h3)
def calculate_V_omega(h): return (4/3)3.14r**3 - calculate_V_h(h)
定义牛顿迭代法求解球体浸入水中的深度
def newton_method(f, df, x0): eps = 1e-8 x = x0 while abs(f(x)) > eps: x = x - f(x)/df(x) return x
定义方程
def equation(h): return rhogcalculate_V_h(h) - rho_wgcalculate_V_omega(h)
定义方程的导数
def derivative_equation(h): return rhog(3rh2 - 3/2*h3) - rho_wg(4/3)3.14r**3
调用牛顿迭代法求解
result = newton_method(equation, derivative_equation, 0.01)
print("球体浸入水中的深度为:",result,"m")