同步操作将从 编程语言算法集/Python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import mathfrom collections.abc import Callabledef intersection(function: Callable[[float], float], x0: float, x1: float) -> float:"""function is the f we want to find its rootx0 and x1 are two random starting points>>> intersection(lambda x: x ** 3 - 1, -5, 5)0.9999999999954654>>> intersection(lambda x: x ** 3 - 1, 5, 5)Traceback (most recent call last):...ZeroDivisionError: float division by zero, could not find root>>> intersection(lambda x: x ** 3 - 1, 100, 200)1.0000000000003888>>> intersection(lambda x: x ** 2 - 4 * x + 3, 0, 2)0.9999999998088019>>> intersection(lambda x: x ** 2 - 4 * x + 3, 2, 4)2.9999999998088023>>> intersection(lambda x: x ** 2 - 4 * x + 3, 4, 1000)3.0000000001786042>>> intersection(math.sin, -math.pi, math.pi)0.0>>> intersection(math.cos, -math.pi, math.pi)Traceback (most recent call last):...ZeroDivisionError: float division by zero, could not find root"""x_n: float = x0x_n1: float = x1while True:if x_n == x_n1 or function(x_n1) == function(x_n):raise ZeroDivisionError("float division by zero, could not find root")x_n2: float = x_n1 - (function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n)))if abs(x_n2 - x_n1) < 10**-5:return x_n2x_n = x_n1x_n1 = x_n2def f(x: float) -> float:return math.pow(x, 3) - (2 * x) - 5if __name__ == "__main__":print(intersection(f, 3, 3.5))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。