nah ini buat yang persamaannya kita tentuin sendiri dengan banyak variabel yang tidak diketahui n.

program metnum_gaus_seidel_input;
uses crt;
const n=3;

var
   a    : array [1..n,1..n] of real;
   b,x  : array [1..n] of real;
   i,j,imax,lamda,sentinel,iter   : integer;
   es,ea,dummy,sum,old            : real;
   klm,brs                        : integer;
   pil                            : char;

begin
  clrscr;
  writeln('program mencari akar sejati dg Metode Gaus Seidel');
  writeln('masukan nilai untuk a[i,j] dan b[i]');
  writeln('a11 x1 + a12 x2 + a13 x3 + . . . . + a1j xn  = b1 ');
  writeln('a21 x1 + a22 x2 + a23 x3 + . . . . + a2j xn  = b2 ');
  writeln(' .   .    .  .     .   .              .   .     . ');
  writeln(' .   .    .  .     .   .              .   .     . ');
  writeln('ai1 x1 + ai2 x2 + ai3 x3 + . . . . + aij xn  = bn ');
  write('berapa banyak varibael yg tidak diketahui : ');readln(n);
  brs:=6;
  for i:=1 to n do
  begin
    klm:=1;
    for j:=1 to n do
    begin
      gotoxy(klm,brs);
      write('a[',i,',',j,'] : ');readln(a[i,j]);
      inc(klm,15);
    end;
    gotoxy(klm,brs);
    write('nilai b[',i,'] : ');readln(b[i]);
    inc(brs);
  end;

  write('nilai stopping criteria ');readln(es);
  write('masukan tebakan awal [y/n] ');readln(pil);
  if (pil='y') or (pil='Y') then
  begin
    for i:=1 to n do
      begin
        write('x[',i,'] : ');readln(x[i]);
      end;
    end
  else
    for i:=1 to n do
       x[i]:=0;


  for i:=1 to n do
  begin
    dummy:=a[i,i];
    for j:=1 to n do
      a[i,j] := a[i,j]/dummy;
    b[i]:=b[i]/dummy;
  end;

  for i:=1 to n do
  begin
    sum:=b[i];
    for j:=1 to n do
      if i<>j then
        sum:=sum-a[i,j]*x[j];
    x[i]:=sum;
  end;

  iter:=0;
  repeat
    inc(iter);
    sentinel:=1;
    for i:=1 to n do
     begin
      old:=x[i];
      sum:=b[i];

      for j:=1 to n do
        if i<>j then
          sum:=sum-a[i,j]*x[j];
      x[i]:=sum;
      if (sentinel=1) and (x[i]<>0) then
       begin
         ea:=abs((x[i]-old)/x[i]);
         if (ea>es) then
           sentinel:=0;
       end;
     end;
  until(sentinel=1);

  for i:=1 to n do
     writeln('nilai x[',i,'] : ',x[i]:8:5);
  writeln('nilai ea      : ',es:8:5);
  writeln('nilai iterasi : ',iter);

  readln;
end.

kembali ke halaman sebelumnya

kembali ke halaman Metode Numerik

Home