- Back to Home »
- PASCAL , TUGAS KULIAH »
- Program Sederhana Menghitung Iterasi Jacobi Dan Dekomposisi Crout
Oleh : Muhammad Irobby Yusuf
Assalamualaikum,
kali ini saya akan sharing tugas kuliah metode numerik membuat program untuk menghitung iterasi jacobi dan dekomposisi crout :)
coba ketikan script pascal dibawah ini :
program jacobi_crout;
uses crt;
type
mat55=array [0..5,0..5] of real;
var
iterasi:boolean;
a,l,u: mat55;
b,x,y:array [0..5] of real;
k21,k31,k41:real;
k32,k42,k43:real;
a11,a12,a13,a14:real;
a21,a22,a23,a24:real;
a31,a32,a33,a34:real;
a41,a42,a43,a44:real;
b1,b2,b3,b4 : real;
x1,x2,x3,x4:array[0..20] of real;
k,i,j,n:integer;
E1,E2,E3,E4:array[0..20]of real;
menu:integer;
procedure inisialisasi;//pemberian nilai awal dalam program
begin
iterasi:=false;
n:=4;
//inisialisasi dalam array
a[0][0]:=1;a[0][1]:=2;a[0][2]:=3;a[0][3]:=-1;
a[1][0]:=2;a[1][1]:=5;a[1][2]:=4;a[1][3]:=8;
a[2][0]:=4;a[2][1]:=2;a[2][2]:=2;a[2][3]:=1;
a[3][0]:=6;a[3][1]:=4;a[3][2]:=-1;a[3][3]:=-2;
b[0]:=10;
b[1]:=8;
b[2]:=-2;
b[3]:=4;
//inisialisasi matriks
a11:=1; a12:=2; a13:=3; a14:=-1;
a21:=2; a22:=5; a23:=4; a24:=8;
a31:=4; a32:=2; a33:=2; a34:=1;
a41:=6; a42:=4; a43:=-1;a44:=-2;
b1:=10;
b2:=8;
b3:=-2;
b4:=4;
k:=0;
x1[k]:=0;x2[k]:=0;x3[k]:=0;x4[k]:=0;
end;
procedure inisialisasi_E;//nilai awal untuk E iterasi ke-0 sampai 1
begin
E1[0]:=1;E1[1]:=1;
E2[0]:=1;E2[1]:=1;
E3[0]:=1;E3[1]:=1;
E4[0]:=1;E4[1]:=1;
end;
procedure profil; //data pribadi
begin
gotoxy(47,4);write('NIM : 10112291');
gotoxy(47,5);write('Nama : Muhammad Irobby Yusuf');
gotoxy(47,6);write('Kelas : Metnum 14');
end;
procedure kotak; //garis kotak untuk profil
begin
for i:=1 to n+1 do
begin
gotoxy(45,2+i);write('|');
end;
for i:=1 to n+1 do
begin
gotoxy(79,2+i);write('|');
end;
for i:=1 to n+29 do
begin
gotoxy(45+i,3);write('-');
end;
for i:=1 to n+29 do
begin
gotoxy(45+i,7);write('-');
end;
end;
Procedure matrikB(x2:integer); //nilai matrik B
begin
for i:=0 to n-1 do
begin
gotoxy(x2,10+2*i);write(b[i]:0:0);
end;
end;
//Procedure garis Matriks A dalam soal
procedure MA; //garis matrik B
begin
gotoxy(2,3);write(' A =');
for i:=1 to n+9 do
begin
gotoxy(8,2+i);write('|');
end;
for i:=1 to n+9 do
begin
gotoxy(27,2+i);write('|');
end;
end;
//Procedure garis Matriks B dalam soal
procedure MB; //garis matrik B
begin
gotoxy(30,3);write(' B =');
for i:=1 to n+9 do
begin
gotoxy(35,2+i);write('|');
end;
for i:=1 to n+9 do
begin
gotoxy(41,2+i);write('|');
end;
end;
procedure garis1 (x1:integer);
begin
for i:=0 to n-1 do
begin
gotoxy(x1,3+i);write('|');
end;
end;
procedure garis2 (x1:integer);
begin
for i:=0 to n-1 do
begin
gotoxy(x1,10+i);write('|');
end;
end;
procedure garisY (x1:integer); //garis untuk nilai Y
begin
for i:=0 to n-1 do
begin
gotoxy(x1,20+i);write('|');
end;
end;
procedure garisv (x1:integer);
begin
for i:=0 to n+2 do
begin
gotoxy(x1,10+i);write('|');
end;
end;
procedure soal; //soal tugas
begin
gotoxy(1,1);write(' Soal : ');
for i:=0 to n-1 do
begin
for j:=0 to n-1 do
begin
gotoxy(11+4*j,3+4*i);write(a[i][j]:0:0);
end;
writeln;
end;
for i:=0 to n-1 do
begin
gotoxy(38,3+4*i);write(b[i]:0:0);
end;
writeln;
writeln;
writeln(' Membuat Program untuk Menentukan Solusi SPL diatas');
writeln(' dengan menggunakan Metode Crout dan iterasi Metode Jacobi');
end;
procedure tampilan;
begin
kotak;
profil;
MA;
MB;
soal;
end;
procedure gauss; //menentukan U dalam metode gaus naif untuk membandingkan saja
begin
writeln (' Eliminasi Gaus untuk menentukan U (sebagai pembanding saja)');
gotoxy(2,4);write('U=');
garis1(5);
k21 := -(a21/a11);
k31 := -(a31/a11);
k41 := -(a41/a11);
for i:=0 to n-1 do
begin
//elimanasi pertama
a[1][i]:= (k21*a[0][i])+a[1][i];
a[2][i]:= (k31*a[0][i])+a[2][i];
a[3][i]:= (k41*a[0][i])+a[3][i];
gotoxy(7+4*i,3);write(a[0][i]:0:0);
gotoxy(7+4*i,4);write(a[1][i]:0:0);
gotoxy(7+4*i,5);write(a[2][i]:0:0);
gotoxy(7+4*i,6);write(a[3][i]:0:0);
end;
garis1(22);
gotoxy(24,5);write('~~');
garis1(27);
k32:= -(a[2][1]/a[1][1]);
k42:= -(a[3][1]/a[1][1]);
for i:=0 to n-1 do
begin
//eliminasi pertama
a[2][i]:= (k32*a[1][i])+a[2][i];
a[3][i]:= (k42*a[1][i])+a[3][i];
gotoxy(29+4*i,3);write(a[0][i]:0:0);
gotoxy(29+4*i,4);write(a[1][i]:0:0);
gotoxy(29+4*i,5);write(a[2][i]:0:0);
gotoxy(29+4*i,6);write(a[3][i]:0:0);
end;
garis1(44);
gotoxy(46,5);write('~~');
garis1(49);
k43:= -(a[3][2]/a[2][2]);
for i:=0 to n-1 do
begin
//eliminasi ke-3
a[3][i]:= (k43*a[2][i])+a[3][i];
a[3][2]:= 0;//nilainya -0, sekian sehingga dibulatkan menjadi 0
gotoxy(51+4*i,3);write(a[0][i]:0:0);
gotoxy(51+4*i,4);write(a[1][i]:0:0);
gotoxy(51+4*i,5);write(a[2][i]:0:0);
gotoxy(51+4*i,6);write(a[3][i]:0:0);
gotoxy(63,6);write(a[3][3]:2:4);//di replace karena Tampilan matrik sebelumnya tidak angka dibelakang koma
end ;
garis1(71);
end;
//================================METODE CROUT=======================================
procedure D_crout;
begin
//===========JAWABAN PROGRAM===============
gotoxy(2,8);write('DEKOMPOSISI LU DENGAN METODE CROUT');
inisialisasi;
//menentukan U dan L dengan metode crout
u[0][0]:=a11; u[0][1]:=a12; u[0][2]:=a13; u[0][3]:=a14; //iterasi 1
l[1][0]:=a21/a11; l[2][0]:=a31/a11; l[3][0]:=a41/a11; //iterasi 2
u[1][1]:=a22-(l[1][0]*a12); u[1][2]:=a23-(l[1][0]*a13); u[1][3]:=a24-(l[1][0]*a14); //iterasi 3
l[2][1]:=(a32-(l[2][0]*a12))/u[1][1]; l[3][1]:=(a42-(l[3][0]*a12))/u[1][1]; //iterasi 4
u[2][2]:=a33-((l[2][0]*a13)+(l[2][1]*u[1][2])); u[2][3]:=a34-((l[2][0]*a14)+(l[2][1]*u[1][3])); //iterasi 5
l[3][2]:=(a43-((l[3][0]*a13)+(l[3][1]*u[1][2])))/u[2][2]; //iterasi 6
u[3][3]:=a44 - (l[3][0]*a14)-(l[3][1]*u[1][3])-(l[3][2]*u[2][3]); //itearsi z
//===============MATRIKS U================
gotoxy(2,10);write('U=');
garis2(5);
for i:=0 to n-1 do
begin
gotoxy(7+4*i,10);write(u[0][i]:0:0);
gotoxy(7+4*i,11);write(u[1][i]:0:0);
gotoxy(7+4*i,12);write(u[2][i]:0:0);
gotoxy(7+4*i,13);write(u[3][i]:0:0);
gotoxy(19,13);write(u[3][3]:2:4);// nilai U44 di replace karena hasilnya desimal sedang matrik tidak disetting desimal
end;
garis2(27);
//================MATRIKS L================
gotoxy(29,10);write('L=');
garisv(32);
l[0][0]:=1; l[1][1]:=1; l[2][2]:=1; l[3][3]:=1;
for i:=0 to n-1 do
begin
gotoxy(34,10+2*i);write(l[i][0]:0:0);
gotoxy(38,10+2*i);write(l[i][1]:0:0);
gotoxy(42,10+2*i);write(l[i][2]:0:0);
gotoxy(42,16);write(l[i][2]:2:4); //di replace karena matrik tidak menentukan angka dibelakang koma sedang hasilnya desimal
gotoxy(49,10+2*i);write(l[i][3]:0:0);
end;
garisv(51);
gotoxy(2,15);write('Nilai U dengan Gaus dan');
gotoxy(2,16);write('metode Crout bernilai sama');
//=================MATRIKS B===============
gotoxy(53,10);write('B=');
garisv(56);
matrikB(58);
garisv(61);
//Ly = b
gotoxy(2,18);write('dari persamaan Ly = b maka : dari persamaan Ux=y maka :');
gotoxy(2,20);write('Y=');
garisY(5);
//mencari nilai y
y[0]:= b[0]/l[0][0]; //mencari nilai y1 -> y1=b1/L11
y[1]:= b[1] - l[1][0]*y[0]; //mencari nilai y2 -> y2=b2-L21*y1
y[2]:= b[2] - l[2][0]*y[0] - l[2][1]*y[1] ; //mencari nilai y3 -> y3=b3-L31*y1 - L32*y2
y[3]:= b[3] - l[3][0]*y[0] - l[3][1]*y[1] - l[3][2]*y[2]; //mencari nilai y4 -> y4=b4-L41*y1 - L42*y2 - L43*y3
//output nilai y
gotoxy(7,20);write(y[0]:2:4);
gotoxy(7,21);write(y[1]:2:4);
gotoxy(7,22);write(y[2]:2:4);
gotoxy(7,23);write(y[3]:2:4);
garisy(16);
//Ux = y
//mencari nilai x
gotoxy(42,20);write('x=');
garisY(46);
x[3]:= y[3]/u[3][3]; //x4 -> y4/u44
x[2]:= (y[2] - u[2][3]*x[3])/u[2][2]; //x3 ->(y3 - u34*x4)/u33
x[1]:= (y[1] - u[1][2]*x[2] - u[1][3]*x[3])/u[1][1]; //x2 ->(y2 - u23*x3 - u24*x4)/u22
x[0]:= (y[0] - u[0][1]*x[1] - u[0][2]*x[2] - u[0][3]*x[3])/u[0][0]; //x1 ->(y1 - u12*x2 - u13*x3 - u14*x4)/u11
//output nilai x
gotoxy(48,20);write(x[0]:2:4);
gotoxy(48,21);write(x[1]:2:4);
gotoxy(48,22);write(x[2]:2:4);
gotoxy(48,23);write(x[3]:2:4);
garisy(56);
end;
//=================================METODE JACOBI===================================
procedure header; //header soal jacobi
begin
writeln('toleransi galat relatif hampiran 10^(-6) atau E<0.000006');
writeln();
writeln('diambil tebakan awal x=[0 0 0 0]T');
writeln();
writeln(' Metode Iterasi Jacobi ');
writeln('===============================================================================');
writeln('i x1 E1 x2 E2 x3 E3 x4 E4 ');
writeln('===============================================================================');
end;
procedure M_Jacobi;
begin
inisialisasi;
gotoxy(1,2);header;
repeat
begin
gotoxy(1,k+11);write(k);//nilai iterasi
//menentukan nilai iterasi x1,x2,x3,x4
x1[k+1]:=(b1 - a12*x2[k] - a13*x3[k] - a14*x4[k])/a11;
x2[k+1]:=(b2 - a21*x1[k] - a23*x3[k] - a24*x4[k])/a22;
x3[k+1]:=(b3 - a31*x1[k] - a32*x2[k] - a34*x4[k])/a33;
x4[k+1]:=(b4 - a41*x1[k] - a42*x2[k] - a43*x3[k])/a44;
//outputkan nilai
gotoxy(4,k+11);write(x1[k]:2:4);
gotoxy(23,k+11);write(x2[k]:2:4);
gotoxy(44,k+11);write(x3[k]:2:4);
gotoxy(63,k+11);write(x4[k]:2:4);
if k>1 then
begin
//menentukan nilai E1,E2,E3,E4
E1[k]:=abs((x1[k]-x1[k-1])/x1[k]);
E2[k]:=abs((x2[k]-x2[k-1])/x2[k]);
E3[k]:=abs((x3[k]-x3[k-1])/x3[k]);
E4[k]:=abs((x4[k]-x4[k-1])/x4[k]);
//outputkan nilai
gotoxy(14,k+11);write(E1[k]:2:4);
gotoxy(34,k+11);write(E2[k]:2:4);
gotoxy(54,k+11);write(E3[k]:2:4);
gotoxy(73,k+11);write(E4[k]:2:4);
end;
k:=k+1;
end; //iterasi E dimulai dari iterasi ke 3
inisialisasi_E; //karena dari iterasi ke-0 sampai 1 E bernilai 0, maka ditentukan inisalisasi awal
if (E1[k-1]<0.000006) or (E2[k-1]<0.000006) or (E3[k-1]<0.000006) or (E4[k-1]<0.000006) then
begin
iterasi:=true;
end;
until iterasi;
writeln;
writeln;
writeln('===============================================================================');
gotoxy(2,17);write('tekan Enter untuk melanjutkan ! . .');
end;
//========================MENU====================
procedure menu_pilihan(var menu:integer);
begin
repeat
gotoxy(3,19);write('Menu Pilihan : ');
gotoxy(3,20);write('1. Metode Crout ');
gotoxy(3,21);write('2. Metode Jacobi ');
gotoxy(3,22);write('0. Keluar ');
gotoxy(3,23);write('pilihan : ');readln(menu);
until menu in[0,1,2];
end;
//program utama
begin
repeat
inisialisasi;
clrscr;
tampilan; //tampilan awal program
menu_pilihan(menu);
if (menu=1) then
begin
clrscr;
gauss; //metode eliminasi gauss --> hanya sebagai pembanding nilai matriks U saja
D_crout; //metode Dekomposisi metode Crout
readln;
end;
if (menu=2) then
begin
clrscr;
M_jacobi; //metode iterasi jacobi
readln;
end;
until (menu=0);
end.
dan setelah di compile akan menampilkan menu pilihan dan soal seperti di bawah ini :
jika user memilih menu no 1 maka akan menampilkan hasil perhitungan menggunakan dekomposisi Crout seperti dibawah ini :
dan jika memilih menu no 2 maka program akan menampilkan perhitungan menggunakan iterasi jacobi seperti gambar di bawah ini :
dan jika memilih menu no 0 maka program akan keluar.
wallahualam bishawab.
semoga bermanfaat :)