Hey, what's going on?

Numerical Methods in Pascal: Polynomial Root Finder

Posted by Syeilendra Pramuditya on April 29, 2014

program polinomial; {Mencari seluruh akar polinom}
{https://syeilendrapramuditya.wordpress.com}
uses crt;

const
  max = 100;
var
  n  : integer;
  a  : array[-1..max] of real;
  b  : array[-1..max] of real;
  iter,i,j,k   : longint;
  x0,x1,t,beda : real;

begin
     clrscr;
     writeln;
     writeln('           Program Polinom');
     writeln('           ===============');
     writeln;
     write('Masukan nilai tebakan awal (misal : 5) : ');
     iter := 0;
     readln(x0);
     write('Masukan orde polinomial : ');readln(n);
     { misal (x-1)*(x+1)*(x+2) = x^3+2*x^2-x-2 }
     writeln('Masukan satu per satu koefisien polinom');
     writeln('dimulai dari suku polinom dengan orde terendah (x pangkat nol)');
     for i := 0 to n do
     begin
       write('masukan koefisien ke ',i,' : ');
       readln(a[i]);
     end;
     writeln;
     repeat
       repeat
           iter := iter +1;
          b[n-1] := a[n];
          t := b[n-1];
          for j := n downto 2 do
          begin
            b[j-2] := b[j-1]*x0+a[j-1];
            t := t*x0+b[j-2];
          end;
          b[-1] := b[0]*x0 + a[0];
          x1 := x0 - b[-1]/t;
          beda := (x1-x0)/x0;
          x0 := x1;
       until (abs(beda)<1e-6)or (iter>1e6);
     n := n-1;
     writeln('nilai x',n+1, ' adalah',x1:10:4 );
     for k := 0 to n do a[k] := b[k];
     until (n<=0);
     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: