-
Notifications
You must be signed in to change notification settings - Fork 0
/
bisection_method.m
35 lines (27 loc) · 1.17 KB
/
bisection_method.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
function quadratic_bisection()
a = input('Enter coefficient a: ');
b = input('Enter coefficient b: ');
c = input('Enter coefficient c: ');
left_endpoint = input('Enter left endpoint of the interval: ');
right_endpoint = input('Enter right endpoint of the interval: ');
tolerance = input('Enter tolerance level (e.g., 1e-6): ');
max_iterations = input('Enter maximum number of iterations: ');
if sign(polyval([a, b, c], left_endpoint)) * sign(polyval([a, b, c], right_endpoint)) > 0
fprintf('The chosen interval does not contain a root.\n');
return;
end
for iteration = 1:max_iterations
midpoint = (left_endpoint + right_endpoint) / 2;
f_midpoint = polyval([a, b, c], midpoint);
if abs(f_midpoint) < tolerance
fprintf('Root found: x = %f\n', midpoint);
return;
end
if sign(f_midpoint) * sign(polyval([a, b, c], left_endpoint)) < 0
right_endpoint = midpoint;
else
left_endpoint = midpoint;
end
end
fprintf('Bisection method did not converge within the maximum number of iterations.\n');
end