2 added 2 characters in body
source | link

I know this question is quite old, but here's my attempt. It's as fast as MichaelsMichael's, but much simpler:

Bisection[f_, int_, tol_, niter_] := Block[
  {m = tol + 1, prev, ym, yl = f[Last@int]},
  NestWhile[(
     prev = m;
     m = Total@#/2;
     ym = f[m];
     If[ym*yl > 0, yl = ym; {First@#, m}, {m, Last@#}]
     ) &,
   int,  ym != 0 && Abs[m - prev] > tol &, 2, niter]
  ]

Testing it with

func[t_?NumericQ] := 1 + NIntegrate[Sin[x^2] - x, {x, 0, t}];

Bisection[func, {1, 2.`20}, 10^-14, 1000] // Timing
{0.109375, {1.9252809180739163253, 1.9252809180739234307}}

I know this question is quite old, but here's my attempt. It's as fast as Michaels but much simpler

Bisection[f_, int_, tol_, niter_] := Block[
  {m = tol + 1, prev, ym, yl = f[Last@int]},
  NestWhile[(
     prev = m;
     m = Total@#/2;
     ym = f[m];
     If[ym*yl > 0, yl = ym; {First@#, m}, {m, Last@#}]
     ) &,
   int,  ym != 0 && Abs[m - prev] > tol &, 2, niter]
  ]

Testing it with

func[t_?NumericQ] := 1 + NIntegrate[Sin[x^2] - x, {x, 0, t}];

Bisection[func, {1, 2.`20}, 10^-14, 1000] // Timing
{0.109375, {1.9252809180739163253, 1.9252809180739234307}}

I know this question is quite old, but here's my attempt. It's as fast as Michael's, but much simpler:

Bisection[f_, int_, tol_, niter_] := Block[
  {m = tol + 1, prev, ym, yl = f[Last@int]},
  NestWhile[(
     prev = m;
     m = Total@#/2;
     ym = f[m];
     If[ym*yl > 0, yl = ym; {First@#, m}, {m, Last@#}]
     ) &,
   int,  ym != 0 && Abs[m - prev] > tol &, 2, niter]
  ]

Testing it with

func[t_?NumericQ] := 1 + NIntegrate[Sin[x^2] - x, {x, 0, t}];

Bisection[func, {1, 2.`20}, 10^-14, 1000] // Timing
{0.109375, {1.9252809180739163253, 1.9252809180739234307}}
1
source | link

I know this question is quite old, but here's my attempt. It's as fast as Michaels but much simpler

Bisection[f_, int_, tol_, niter_] := Block[
  {m = tol + 1, prev, ym, yl = f[Last@int]},
  NestWhile[(
     prev = m;
     m = Total@#/2;
     ym = f[m];
     If[ym*yl > 0, yl = ym; {First@#, m}, {m, Last@#}]
     ) &,
   int,  ym != 0 && Abs[m - prev] > tol &, 2, niter]
  ]

Testing it with

func[t_?NumericQ] := 1 + NIntegrate[Sin[x^2] - x, {x, 0, t}];

Bisection[func, {1, 2.`20}, 10^-14, 1000] // Timing
{0.109375, {1.9252809180739163253, 1.9252809180739234307}}