-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbisection.m
35 lines (35 loc) · 1.03 KB
/
bisection.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
clear all;
#clf;
printf("bisection method of root finding\n");
a = input('input a :');
b = input('input b :');
tol = input('input tolerance :');
loops = 0;
itr = [];%stores the iterates
fileid = fopen('b_itr.txt' , 'w');
fprintf(fileid,'%8s %13s %14s %14s\n','Iteration','a', 'b', 'r');
fprintf(fileid, '%7d %16.8f %16.8f %16.8f\n', loops, a, b, (a+b)/2);
##this 'if' is to check whether the inputs are already the solutions
if func((a+b)/2) ~= 0
#while the width of the iterates is more than tolerance
while abs(a - b) > tol
if func(a) * func((a+b)/2) < 0
b = (a+b)/2;
elseif func(b) * func((a+b)/2) < 0
a = (a+b)/2;
end
r = (a + b) / 2;
itr = [itr;r];
loops = loops + 1;
fprintf(fileid, '%7d %16.8f %16.8f %16.8f\n', loops, a, b, r);
end
end
fclose(fileid);
j = [1:1:loops];
plot(j,itr, "b-");
title("Bisection method");
xlabel('Iteration no.');
ylabel('iterate value');
xlim([1,loops]);
ylim([min(itr), max(itr)]);
print -dpdf Iteration-no-vs-points_fix_b.pdf