bisection

[r, iter] = bisection(f,a,b,tol,maxit) uses the bisection method to find a root for the function f in the interval [a,b].
Returns the root (r) and the number of iterations to find it (iter).
Options Description Values Default values
tol Tolerance on the function value positive integer 1e-6
maxit Maximum number of iterations positive integer 100

Examples

>> f = @(x) cos(x)
a = 0
b = 3

[f:@(x) cos(x)]
[a:0]
[b:3]

>> [r, i] = bisection(f,a,b) 

[r:1.5708]
[i:20]

>> op = struct('show', false)
overlay({plot(a:0.1:b, f(a:0.1:b),op), scatter(r,0,op)})

[op:1x1 struct]
[ans:1x1 struct] 0.157000s

See also

fminsearch | find