-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbisectionMethod.m
More file actions
57 lines (49 loc) · 1.35 KB
/
bisectionMethod.m
File metadata and controls
57 lines (49 loc) · 1.35 KB
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
51
52
53
54
55
56
57
% Author: Diego Coglievina Díaz
% Numerical Methods
% Universidad Anáhuac Querétaro
% 00437641
% this is a bracketed method
% a and b are the initial values for x
function x = bisectionMethod(a, b, f, LOI, error)
n = 0;
% Error prevention
if isempty(error) && isempty(LOI)
x = "ERROR: MISSING PARAMETERS (LOI || error)";
elseif isempty(error)
% If no error is provided, the function will loop until the LOI is
% reached
while n < LOI
c = (a + b)/2;
if f(a)*f(c) < 0
b = c;
else
a = c;
end
n = n + 1;
end
x = c;
else
% If no LOI is provided, the loop will stop when epsilon is smaller
% than the desiered error
limit = 20;
epsilon = 100;
c = (a + b)/2;
while epsilon > error
% Implementation of the Method
if f(a)*f(c) < 0
b = c;
else
a = c;
end
c_next = (a + b)/2;
epsilon = abs((c_next - c)/c_next)*100;
n = n + 1;
c = c_next;
if n == limit
x = "ERROR: DOES NOT CONVERGE";
break
end
x = c;
end
end
end