>>> from biseccio import succ_biseccio ---- inici >>> def f1(x): ... return 2*x - 6 >>> eps = 1e-3 >>> c = succ_biseccio(f1, 2.9, 3.15, eps) Valors arrodonits dels extrems dels intervals. >>> [(round(e, 3), round(d, 3)) for e, d in c] [(2.9, 3.15), (2.9, 3.025), (2.962, 3.025), (2.994, 3.025), (2.994, 3.009), (2.994, 3.002), (2.998, 3.002), (3.0, 3.002), (3.0, 3.001)] Només el darrer interval té una longitud més petita que eps. >>> [abs(e - d) < eps for e, d in c] == [False]*8 + [True] True El punt mig del darrer interval s'acosta prou al zero de la funció en l'interval. >>> e, d = c[-1] >>> f1((e + d)/2) < eps True ---- fi >>> c = succ_biseccio(f1, 0, 5, eps) >>> len(c) 14 >>> [abs(e - d) < eps for e, d in c] == [False]*13 + [True] True >>> e, d = c[-1] >>> round(e, 4) 2.9999 >>> round(d, 4) 3.0005 >>> f1((e + d)/2) < eps True >>> eps = 1e-5 >>> c = succ_biseccio(f1, 0, 5, eps) >>> len(c) 20 >>> [abs(e - d) < eps for e, d in c] == [False]*19 + [True] True >>> e, d = c[-1] >>> round(e, 6) 2.999992 >>> round(d, 6) 3.000002 >>> f1((e + d)/2) < eps True >>> def f2(x): ... return (x - 2)*(x + 1) >>> eps = 1e-3 >>> c = succ_biseccio(f2, 1.9, 2.3, eps) >>> [(round(e, 3), round(d, 3)) for e, d in c] [(1.9, 2.3), (1.9, 2.1), (2.0, 2.1), (2.0, 2.05), (2.0, 2.025), (2.0, 2.012), (2.0, 2.006), (2.0, 2.003), (2.0, 2.002), (2.0, 2.001)] >>> len(c) 10 >>> [abs(e - d) < eps for e, d in c] == [False]*9 + [True] True >>> e, d = c[-1] >>> f2((e + d)/2) < eps*10 True >>> c = succ_biseccio(f2, 0, 3, eps) >>> len(c) 13 >>> [abs(e - d) < eps for e, d in c] == [False]*12 + [True] True >>> e, d = c[-1] >>> round(e, 4) 1.9995 >>> round(d, 4) 2.0002 >>> f2((e + d)/2) < eps True >>> eps = 1e-5 >>> c = succ_biseccio(f2, 0, 5, eps) >>> len(c) 20 >>> [abs(e - d) < eps for e, d in c] == [False]*19 + [True] True >>> e, d = c[-1] >>> round(e, 6) 1.999998 >>> round(d, 6) 2.000008 >>> f2((e + d)/2) < eps True >>> eps = 1e-3 >>> c = succ_biseccio(f2, -2, -0.5, eps) >>> [(round(e, 4), round(d, 4)) for e, d in c] [(-2, -0.5), (-1.25, -0.5), (-1.25, -0.875), (-1.0625, -0.875), (-1.0625, -0.9688), (-1.0156, -0.9688), (-1.0156, -0.9922), (-1.0039, -0.9922), (-1.0039, -0.998), (-1.001, -0.998), (-1.001, -0.9995), (-1.0002, -0.9995)] >>> len(c) 12 >>> [abs(e - d) < eps for e, d in c] == [False]*11 + [True] True >>> e, d = c[-1] >>> f2((e + d)/2) < eps True >>> c = succ_biseccio(f2, -5, 0, eps) >>> len(c) 14 >>> [abs(e - d) < eps for e, d in c] == [False]*13 + [True] True >>> e, d = c[-1] >>> round(e, 4) -1.0004 >>> round(d, 4) -0.9998 >>> f2((e + d)/2) < eps True >>> eps = 1e-5 >>> c = succ_biseccio(f2, -4, 1, eps) >>> len(c) 20 >>> [abs(e - d) < eps for e, d in c] == [False]*19 + [True] True >>> e, d = c[-1] >>> round(e, 6) -1.000008 >>> round(d, 6) -0.999998 >>> f2((e + d)/2) < eps True