Skip to content
The Chat Archive

Side-by-side comparison

Both conversations start with the same prompt. Responses were generated in different, uncontrolled contexts. This is not a benchmark.
First prompt

一个实心球体浮在水面上,根据阿基米德原理,球体的浮力和球体排出水的重量相等。令Vs = (4/3)πr^3为球的体积,Vω为球体局部浸入水中时排出水的体积。在静态平衡的状态下,球的重量和水的浮力相等, ρgVs = ρwgVw 或 sVs = Vw 其中是ρ球体密度,g是重力加速度,ρw是水的密度,s = ρ/ρw是球体材料的比重,它决定球体沉入水面的深度。 当s<0.5时球体有高度为h部分的体积在水下,当s>0.5时球体有高度为h部分的体积在水上 球体高度为h部分的体积为Vh=(π/3)(3rh^2-h^3).假设球体半径为r=10cm, 密度为ρ=0.638,求球体浸入水中的质量为多少? (请自行取合适的初始值和初始区间,选用一种非线性方程求根算法求解。) 给出python代码

gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

使用牛顿迭代法求解非线性方程

def f(h): r = 0.1 rho = 0.638 rho_w = 1.0 s = rho / rho_w Vh = (1.0 / 3) * (3 * r * h ** 2 - h ** 3) Vs = 4 / 3 * 3.14 * r ** 3 Vw = Vs - Vh return rho * g * Vs - rho_w * g * Vw

def f_derivative(h): r = 0.1 rho = 0.638 rho_w = 1.0 s = rho / rho_w return -g * (4 * 3.14 * r ** 3 * (2 * r - h) - 3 * (3 * r * h ** 2 - h ** 3)) * rho

g = 9.8 # 重力加速度 h = 0.05 # 初始值 epsilon = 1e-8 # 精度

while True: h_next = h - f(h) / f_derivative(h) if abs(h_next - h) < epsilon: break h = h_next

r = 0.1 rho = 0.638 Vs = 4 / 3 * 3.14 * r ** 3 Vh = (1.0 / 3) * (3 * r * h ** 2 - h ** 3) Vw = Vs - Vh m = Vw * 1000 # 水的密度为1000kg/m^3

print("球体浸入水中的质量为:{:.3f} kg".format(m))

gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

使用牛顿迭代法求解方程f(x)=0的根

def newton(func, derivative, x0, tol=1e-6, maxiter=100): """ :param func: 原函数 f(x) :param derivative: 导函数 f'(x) :param x0: 迭代初始值 :param tol: 迭代精度 :param maxiter: 最大迭代次数 :return: 迭代结果 x """ x = x0 for i in range(maxiter): f = func(x) if abs(f) < tol: return x df = derivative(x) x = x - f / df return x

定义球体高度方程

def f(h): return s * (4 / 3) * pi * r ** 3 - (pi / 3) * (3 * r * h ** 2 - h ** 3)

定义球体高度方程的导函数

def df(h): return -2 * pi * r * (r - h) * s

r = 0.1 # 球体半径,单位:m rho = 0.638 # 球体密度,单位:kg/m^3 rho_w = 1000 # 水的密度,单位:kg/m^3 s = rho / rho_w # 球体材料的比重 V_s = (4 / 3) * pi * r ** 3 # 球体体积

当球体部分浸入水中

if s < 0.5: V_h = newton(f, df, 0.05) # 使用牛顿迭代法求解球体浸入水中的高度 V_w = V_s - pi * r ** 2 * V_h # 计算球体浸入水中排出的水的体积

当球体部分浮在水面上

else: h = 2 * r # 设定初始值 V_h = V_s - (4 / 3) * pi * r ** 3 + pi * r ** 2 * h - (pi / 3) * (3 * r * h ** 2 - h ** 3) # 计算球体高度为h部分的体积 V_w = pi * r ** 2 * h # 计算球体浮在水面上排出的水的体积

m = V_w * rho_w # 计算球体浸入水中的质量 print("球体浸入水中的质量为 %f kg" % m)

Sign in to record which response you prefer.