-
Notifications
You must be signed in to change notification settings - Fork 0
/
False_Position.m
43 lines (33 loc) · 917 Bytes
/
False_Position.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
clear all;
f = @(x)(x*x - x - 2);
xl = input('Enter lower limit: ');
xu = input('Enter upper limit: ');
es = input('Enter estimated error: ');
fxl = f(xl);
fxu = f(xu);
if fxl * fxu < 0
fprintf('Intital guesses are right. \n');
xr = xu - (fxu * (xl - xu) / (fxl - fxu));
ea = 10000;
fprintf('xr: '); disp(xr);
fprintf('ea: '); disp(ea);
while ea > es
fxr = f(xr);
if fxl * fxr > 0
xl = xr;
fxl = fxr;
fprintf('xl: '); disp(xr);
else
xu = xr;
fxu = fxr;
fprintf('xu: '); disp(xr);
end
xrprev = xr;
xr = xu - (fxu * (xl - xu) / (fxl - fxu));
ea = abs(((xrprev - xr) / xr) * 100);
fprintf('xr: '); disp(xr);
fprintf('ea: '); disp(ea);
end
else
fprintf('Intital guesses are wrong.');
end