Senin, 30 Juni 2014

Mencari Invers Matriks dengan Metode Dekomposisi Adomian

Algoritma Program Pascal untuk Metode Dekomposisi Adomian pada Sistem Persamaan Linier
program Adomian_2x2;
uses wincrt;
var
   i, j, k, l, n              : integer;
   r                          : real;
   A, P, Q, Pinvers, PinversQ : array[1..2,1..2] of real;
   Ainvers                    : array[0..1000,1..2,1..2] of real;
   sigma, hasil               : array[1..2,1..2] of real;

Begin
     write('Masukkan n: ');readln(n);
     writeln('Masukkan matriks A');
     for i:= 1 to 2 do
     begin
          for j:= 1 to 2 do
          begin
               write('a',i,',',j,' = ');readln(A[i,j]);
          end;
     end;
     clrscr;

     writeln('Matriks A');
     for i:= 1 to 2 do
     begin
          for j:= 1 to 2 do
          begin
               write(' ',A[i,j]:0:4);
          end;
          writeln;
     end;

     {Pencarian P dan Q}
     if A[1,1]*A[2,2] > A[1,2]*A[2,1] then
     begin
          P[1,1]:= A[1,1];
          P[2,2]:= A[2,2];
          Q[1,2]:= A[1,2];
          Q[2,1]:= A[2,1];
     end;
     if A[1,1]*A[2,2] < A[1,2]*A[2,1] then
     begin
          P[1,2]:= A[1,2];
          P[2,1]:= A[2,1];
          Q[1,1]:= A[1,1];
          Q[2,2]:= A[2,2];
     end;

     {Cetak P dan Q}
     writeln('Matriks P');
     for i:= 1 to 2 do
     begin
          for j:= 1 to 2 do
          begin
               write(' ',P[i,j]:0:4);
          end;
          writeln;
     end;
     writeln('Matriks Q');
     for i:= 1 to 2 do
     begin
          for j:= 1 to 2 do
          begin
               write(' ',Q[i,j]:0:4);
          end;
          writeln;
     end;

     {Hitung P invers}
     for i:= 1 to 2 do
     begin
          for j:= 1 to 2 do
          begin
               if i = j then
               begin
                    if i = 1 then k:= 2;
                    if i = 2 then k:= 1;
                    Pinvers[i,i]:= (1/(P[1,1]*P[2,2] - P[1,2]*P[2,1]))*P[k,k];
               end;

               if i <> j then
               begin
                    Pinvers[i,j]:= (-1)*(1/(P[1,1]*P[2,2] - P[1,2]*P[2,1]))*P[i,j];
               end;
          end;
     end;

     {Cetak P invers}
     writeln('Matriks P invers');
     for i:= 1 to 2 do
     begin
          for j:= 1 to 2 do
          begin
               write(' ',Pinvers[i,j]:0:4);
          end;
          writeln;
     end;

     {Hitung P invers Q}
     for i:= 1 to 2 do
     begin
          for j:= 1 to 2 do
          begin
               if i = j then
               begin
                    if i > 1 then k:= 1;
                    if i < 2 then k:= 2;
                    PinversQ[i,i]:= Pinvers[i,i]*Q[i,i] + Pinvers[i,k]*Q[k,i];
               end;

               if i <> j then
               begin
                    PinversQ[i,j]:= Pinvers[i,i]*Q[i,j] + Pinvers[i,j]*Q[j,j];
               end;
          end;
     end;

     {Cetak P invers Q}
     writeln('Matriks P invers Q');
     for i:= 1 to 2 do
     begin
          for j:= 1 to 2 do
          begin
               write(' ',PinversQ[i,j]:0:4);
          end;
          writeln;
     end;
     readln;

     {Pencarian A invers}
     {A 0 invers}
     for i:= 1 to 2 do
     begin
          for j:= 1 to 2 do
          begin
               Ainvers[0,i,j]:= Pinvers[i,j];
          end;
     end;

     {A invers 1-selesai}
     for l:= 1 to n-1 do
     begin
          for i:= 1 to 2 do
          begin
               for j:= 1 to 2 do
               begin
                    if i = j then
                    begin
                         if i > 1 then k:= 1;
                         if i < 2 then k:= 2;
                         Ainvers[l,i,i]:= (-1)*(PinversQ[i,i]*Ainvers[l-1,i,i] + PinversQ[i,k]*Ainvers[l-1,k,i]);
                    end;

                    if i <> j then
                    begin
                         Ainvers[l,i,j]:= (-1)*(PinversQ[i,i]*Ainvers[l-1,i,j] + PinversQ[i,j]*Ainvers[l-1,j,j]);
                    end;
               end;
          end;
     end;

     {Cetak A invers 0 - selesai}
     for l:= 0 to n-1 do
     begin
          writeln('Matirks A',l,' invers adalah');
          for i:= 1 to 2 do
          begin
               for j:= 1 to 2 do
               begin
                    write(' ',Ainvers[l,i,j]:0:4);
               end;
               writeln;
          end;
     end;

     {Hitung sigma}
     for l:= 0 to n-1 do
     begin
          for i:= 1 to 2 do
          begin
               for j:= 1 to 2 do
               begin
                    sigma[i,j]:= sigma[i,j]+Ainvers[l,i,j];
               end;
          end;
     end;

     {Cetak sigma}
     writeln('Matriks Sigma adalah');
     for i:= 1 to 2 do
     begin
          for j:= 1 to 2 do
          begin
               write(' ',sigma[i,j]:0:4);
          end;
          writeln;
     end;

     {Hitung hasil akhir}
     for i:= 1 to 2 do
     begin
          for j:= 1 to 2 do
          begin
               if i = j then
               begin
                    if i > 1 then k:= 1;
                    if i < 2 then k:= 2;
                    hasil[i,i]:= sigma[i,i]*A[i,i] + sigma[i,k]*A[k,i];
               end;

               if i <> j then
               begin
                    hasil[i,j]:= sigma[i,i]*A[i,j] + sigma[i,j]*A[j,j];
               end;
          end;
     end;

     {Cetak hasil akhir}
     writeln('Matriks hasil akhir adalah');
     for i:= 1 to 2 do
     begin
          for j:= 1 to 2 do
          begin
               write(' ',hasil[i,j]:0:4);
          end;
          writeln;
     end;
End.


Preview Program:

Tidak ada komentar:

Posting Komentar

Related Posts Plugin for WordPress, Blogger...