Selasa, 03 Juni 2014

Dekomposisi Segitiga pada Metode Numerik


Algoritma program Pascal Dekomposisi Segitiga
program Dekomposisi_Segitiga_The_Queen_of_Science;
uses wincrt;
var
   i, j, k, n    : integer;
   s             : real;
   a, l, u       : array[1..30,1..30] of real;
   c, x, y       : array[1..30] of real;
label 1, 2, 3;

Begin
1:
  {Input}
  write('Orde matriks anda(maksimal 30x30): ');readln(n);
  writeln('Masukkan elemen-elemen matriks.');
  for i:= 1 to n do
  begin
       for j:= 1 to n do
       begin
            write('a',i,'',j,' = ');readln(a[i,j]);
       end;

       write('Masukkan ruas kanan c',i,' = ');readln(c[i]);
  end;
  clrscr;


  {Pembentukan L awal dan U awal}
  for i:= 1 to n do
  begin
       for j:= 1 to n do
       begin
            if j < i then u[i,j]:= 0;
            if j > i then l[i,j]:= 0;
            if j = i then l[i,j]:= 1;
       end;
  end;

  {Proses A = LU}
  for i:= 1 to n do
  begin
       for j:= 1 to n do
       begin
            s:= 0;
            if j <= i then
            begin
                 for k:= 1 to n do
                 begin
                      s:= s + u[k,i]*l[j,k];
                 end;
                 u[j,i]:= (a[j,i] - s)/l[j,j];
            end
            else
                begin
                     for k:= 1 to n do
                     begin
                          s:= s + u[k,i]*l[j,k];
                     end;
                     l[j,i]:= (a[j,i] - s)/u[i,i];
                end;
       end;
  end;

  {Cetak L dan U}
  write('            L');
  writeln('                           U');
  for i:= 1 to n do
  begin
       write('|');
       for j:= 1 to n do
       begin
            write(' ',l[i,j]:0:4);
       end;
       write(' |');

       write('     |');
       for j:= 1 to n do
       begin
            write(' ',u[i,j]:0:4);
       end;
       writeln(' |');
  end;

  {Proses LY = C}
  for i:= 1 to n do
  begin
       y[i]:= c[i];
       for j:= 1 to n do
       begin
            if j <> i then y[i]:= y[i] - l[i,j]*y[j];
       end;
  end;

  {Cetak Y}
  writeln;
  write('            Y = |');
  for i:= 1 to n do
  begin
       write(' ',y[i]:0:4);
  end;
  writeln(' |');

  {Proses UX = Y}
  for i:= n downto 1 do
  begin
       s:= 0;
       for j:= 1 to n do
       begin
            s:= s + u[i,j]*x[j];
       end;
       x[i]:= (y[i] - s)/u[i,i];
  end;

  {Cetak X}
  writeln;
  write('            X = |');
  for i:= 1 to n do
  begin
       write(' ',x[i]:0:4);
  end;
  writeln(' |');writeln;

  {Kesimpulan}
  writeln('Jadi, solusi dari sistem persamaan linier');
  writeln;
  for i:= 1 to n do
  begin
       for j:= 1 to n do
       begin
            if j = 1 then write('  ',a[i,j]:0:4,'x',j)
            else
                write(' + ',a[i,j]:0:4,'x',j);
       end;
       writeln(' = ',c[i]:0:4);
  end;

  writeln;writeln('adalah');
  for i:= 1 to n do
  begin
       writeln('      x',i,' = ',x[i]:0:4);
  end;
End.


Preview program:

Download program: klik disini

Tidak ada komentar:

Posting Komentar

Related Posts Plugin for WordPress, Blogger...