-
Notifications
You must be signed in to change notification settings - Fork 0
/
falsePositionMethod.m
63 lines (51 loc) · 1.55 KB
/
falsePositionMethod.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
51
52
53
54
55
56
57
58
59
60
61
62
63
% Author: Diego Coglievina Díaz
% Numerical Methods
% Universidad Anáhuac Querétaro
% 00437641
% this is a bracketed method
% Special case of Newton-raphson method
% It is used when the derivative of a function cannot be computed.
function x = falsePositionMethod(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
c = a - (b-a)*f(a)/(f(b) - f(a));
while n < LOI
if f(a)*f(c) < 0
b = c;
else
a = c;
end
n = n + 1;
c = a - (b-a)*f(a)/(f(b) - f(a));
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-a)*f(a)/(f(b) - f(a));
while epsilon > error
% Implementation of the Method
if f(a)*f(c) < 0
b = c;
else
a = c;
end
c_next = a - (b-a)*f(a)/(f(b) - f(a));
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