BISECTION METHOD
program bisection_method;
var
x1,x2,x3:real;
acc:integer;
function f(t:real):real;
begin
f:=t*t*t-t-1;
{the required function goes here. in this case it's
f(x)= x^3 - x - 1 }
end;
begin
writeln('Bisection method');
writeln('enter the 1st start value: ');
readln(x1);
writeln('enter the 2nd start value: ');
readln(x2);
if (f(x1)*f(x2)>0) or (f(x1)=f(x2)) then
writeln('Invalid starting points')
else
begin
writeln('to how many decimal places? ');
readln(acc);
if f(x1)=0 then begin x2:=x1;x3:=x1;end;
if f(x2)=0 then begin x1:=x2;x3:=x2;end;
while abs(f(x1)-f(x2))>1/exp(acc*(ln(10))) do
begin
x3:=(x1+x2)/2;
if f(x3)*f(x2)>=0 then x2:=x3;
if f(x3)*f(x1)>=0 then x1:=x3;
end;
writeln('The answer is: ',x3:5:acc+1);
end;
readln;
end.
Output Bisection method
Bisection method
enter the 1st start value:
0.4
enter the 2nd start value:
to how many decimal places?
The answer is: 1.3247