正文
增加 10 行删除 10 行
389 行未改动全部展开3903913923933943953963973983994004014024034044054064074082 行未改动411412413414415416417418419420421422423424425426427428429430431432433434435287 行未改动全部展开389 行未改动全部展开使用费拉里方法求解四次方程 ax^4 + bx^3 + cx^2 + dx + e = 0"""# 化为首一形式p, q, r, s = b/a, c/a, d/a, e/a# 消去三次项P = q - 3*p**2/8Q = r - p**3/8 - p*q/2R = s - p*r/4 + p**2*q/16 - 3*p**4/256# 求解辅助三次方程# 这里简化处理,实际需要用卡尔丹公式# ... 具体实现略 ...return rootsdef newton_method(coeffs, x0, tol=1e-10, max_iter=100):"""2 行未改动x0: 初始猜测值"""def poly_eval(x, coeffs):return sum(c * x**(len(coeffs)-1-i) for i, c in enumerate(coeffs))def poly_deriv(x, coeffs):return sum((len(coeffs)-1-i) * c * x**(len(coeffs)-2-i)for i, c in enumerate(coeffs[:-1]))x = x0for _ in range(max_iter):fx = poly_eval(x, coeffs)if abs(fx) < tol:return xfpx = poly_deriv(x, coeffs)if abs(fpx) < tol:breakx = x - fx / fpxreturn x# 使用示例coeffs = [1, -2, -2, 6, -3] # x^4 - 2x^3 - 2x^2 + 6x - 3 = 0287 行未改动全部展开389 行未改动全部展开3903913923933943953963973983994004014024034044054064074082 行未改动411412413414415416417418419420421422423424425426427428429430431432433434435287 行未改动全部展开389 行未改动全部展开使用费拉里方法求解四次方程 ax^4 + bx^3 + cx^2 + dx + e = 0"""# 化为首一形式p, q, r, s = b/a, c/a, d/a, e/a# 消去三次项P = q - 3*p**2/8Q = r - p**3/8 - p*q/2R = s - p*r/4 + p**2*q/16 - 3*p**4/256# 求解辅助三次方程# 这里简化处理,实际需要用卡尔丹公式# ... 具体实现略 ...return rootsdef newton_method(coeffs, x0, tol=1e-10, max_iter=100):"""2 行未改动x0: 初始猜测值"""def poly_eval(x, coeffs):return sum(c * x**(len(coeffs)-1-i) for i, c in enumerate(coeffs))def poly_deriv(x, coeffs):return sum((len(coeffs)-1-i) * c * x**(len(coeffs)-2-i)for i, c in enumerate(coeffs[:-1]))x = x0for _ in range(max_iter):fx = poly_eval(x, coeffs)if abs(fx) < tol:return xfpx = poly_deriv(x, coeffs)if abs(fpx) < tol:breakx = x - fx / fpxreturn x# 使用示例coeffs = [1, -2, -2, 6, -3] # x^4 - 2x^3 - 2x^2 + 6x - 3 = 0287 行未改动全部展开