Hey, what's going on?

Numerical Methods in Pascal: Root Finding by Hybrid Bisection-NR

Posted by Syeilendra Pramuditya on April 25, 2014

Program Hybrid; {Bisection - Newton Rhapson Combined}
{https://syeilendrapramuditya.wordpress.com}
uses crt;
Function f(x:real):real;
begin
f := sqr(x) - 9;
end;

Function df(x:real):real;
begin
df:=2*x;
end;

Procedure BacaInput(var a,b:real);
begin
clrscr;
repeat
write('Masukan interval evaluasi : ');readln(a,b);
until(f(a)*f(b) < 0);
end;

Procedure Bisection(var a,b,newrootB:real);
begin
NewRootB := (a+b)/2;
IF (f(a)*f(newrootB) < 0) then
b:=newrootB
else
a := newrootB;
end;

Procedure NewtonRhapson(var b:real;var newrootNR:real);
begin
newrootNR := b - f(b)/df(b);
end;

Procedure TulisOutput(newrootB:real);
begin
writeln('Akar : ',newrootB:10:2);
end;

var a,b,c,d,newrootNR,newrootB : real;
    iterNR,iterBi,iter:integer;

Begin
iterNR := 0;iterBi:=0;
BacaInput(a,b);
c:=a;d:=b;
Repeat
begin
NewtonRhapson(b,newrootNR);
IF (newrootNR >= c) and (newrootNR <= d) then
  begin
  inc(iterNR);
  b := newrootNR;
  NewtonRhapson(b,newrootNR);writeln('Newton Rhapson');
  end
else
  begin
   inc(iterBi);
   writeln('Bisection');
   Bisection(a,b,NewrootB);
   end;
end;
Until (abs(f(newrootNR)) < 1e-6) OR (abs(f(newrootB)) < 1e-6)  OR (iter = 10000);
writeln;
if abs(f(newrootNR)) < abs(f(newrootB)) then
writeln('Akar = ',newrootNR:10:2)
else
writeln('Akar = ',newrootB:10:2);
writeln;
writeln('Iterasi Bisection      : ',iterBi);
writeln('Iterasi Newton Rhapson : ',iterNR);
writeln('Iterasi Total          : ',iterBi + iterNR);
readln;
end.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: