-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbisectionMethod.m
50 lines (48 loc) · 1.74 KB
/
bisectionMethod.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
% =========================================================================
% AUTHOR ..... [Thomas/Alisha]
% UPDATED .... [Jan 27 2018]
%
% Use the bisection method to find a root of a function with initial
% guesses a and b. The root will be computed to within a given tolerance.
%
% INPUT
% f .......... Function handle for the function f(x) being solved
% a .......... Left endpoint of interval
% b .......... Right endpoint of interval
% maxIter .... Maximum number of iterations
% tol ........ Tolerance for the solution
%
% OUTPUT
% c .... A vector containing the iterates of the bisection method with at
% most maxIter elements. Each element of c is the midpoint between
% a and b at each iteration:
% c(i) = (a + b)/2
% The last element of c is such that
% abs(f(c(end))) < tol
% unless c has exactly maxIter elements in which case the
% iteration may not have converged.
%
% ASSUMPTIONS
% 1. f(a)*f(b) < 0
% 2.a<b
% =========================================================================
function c = bisectionMethod(f,a,b,tol,maxIter) % function and its inputs
i = 1; % initialize the iteration count
% initialize c as a vector to store all the iterations
c = zeros(1,maxIter);
while i <= maxIter % beginning of loop
c(i) = (a + b)/2;
% end loop if approximation is within user given input
if abs(f(c(i))) <= tol
break;
end
% condition to set up more precise boundary intervals
if f(a)*f(c(i)) > 0
a = c(i);
else
b = c(i);
% proceed to next iteration in the loop
i = i + 1;
end
end
end