Jumat, 17 Juni 2011

program eliminasi gaus

/* Contoh soal Metode Eliminasi Gauss untuk penyelesaian Persamaan Linier Simultan (PLS) */
/* Nama Program : eliminasi_gauss.cpp */
/* Untuk penyelesaian Matriks n x n */

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#define Data 10
main()
{
FILE *eliminasi_gauss;
double A[Data][Data], /* Matriks A */
H[Data], /* Matriks H */
x[Data], /* Matriks x */
m, /* Faktor Pengali */
temp;
int i, j, k, n, p, q, r;
char lagi;
do
{
clrscr();
eliminasi_gauss = fopen("eliminasi_gauss.txt", "w+");
cout << "\n\nMetode Eliminasi Gauss Matriks untuk PLS\n";
cout << "========================================\n\n";
/*********************************************************/
/* Pengisian Matriks A (3 x 3) dan H (3 x 1) */
/*********************************************************/
/* Pengisian Data Matriks A (umumnya (3 x 3) */
cout << "\nMasukkan Nilai Matriks A (umumnya (3 x 3))\n";
cout << "------------------------------------------\n";
cout << "\nMasukkan Jumlah Baris ? ";
cin >> p;
cout << "\nMasukkan Jumlah Kolom ? ";
cin >> q;
printf("\n");
/* Matriks A */
for (i = 0; i < p; i++)
{
for (j = 0; j < q; j++)
{
printf("A[%d][%d] = ", i+1, j+1);
cin >> A[i][j];
}
printf("\n");
}
/* Pengisian Data Matriks H (umumnya (3 x 1) */
cout << "\nMasukkan Nilai Matriks H (umumnya (3 x 1))\n";
cout << "------------------------------------------\n";
cout << "\nMasukkan Jumlah Baris ? ";
cin >> r;
printf("\n");

/* Matriks H */
for (n = 0; n < r; n++)
{
printf("H[%d] = ", n+1);
cin >> H[n];
}
/*********************************************************/
/* Mencetak Data Matriks A (3 x 3) dan H (3 x 1) */
/*********************************************************/
/* Cetak Matriks A */
cout << "\n\n\nMatriks A dan H sebelum proses Eliminasi Gauss\n";
cout << "----------------------------------------------";
printf("\n\nA = ");
fprintf(eliminasi_gauss,"\n\nA = ");
for (i = 0; i < p; i++)
{
printf("\n");
fprintf(eliminasi_gauss,"\n");
for (j = 0; j < q; j++)
{
printf("\t%.3f", A[i][j]);
fprintf(eliminasi_gauss,"\t%.3f", A[i][j]);
}
}
/* Cetak Matriks H */
printf("\n\nH = \n");
fprintf(eliminasi_gauss,"\n\nH = \n");
for (n = 0; n < r; n++)
{
printf("\t%.3f\n", H[n]);
fprintf(eliminasi_gauss,"\t%.3f\n", H[n]);
}
/*********************************************************/
/* Eliminasi Gauss Matriks A (3 x 3) dan H (3 x 1) */
/*********************************************************/
/* Proses Eliminasi Gauss */
for (k = 0; k < p-1; k++)
{
for (i = k+1; i < q; i++)
{
m = A[i][k]/A[k][k];
for (j = k; j < q; j++)
{
A[i][j] = A[i][j] - m * A[k][j];
}
H[i] = H[i] - m * H[k];
}
}
/*********************************************************/
/* Cetak Matriks A (3 x 3) dan H (3 x 1) Hasil Eliminasi */
/*********************************************************/
/* Cetak Matriks A hasil Eliminasi Gauss */
cout << "\n\nMatriks A dan H setelah proses Eliminasi Gauss\n";
cout << "----------------------------------------------";
printf("\n\nA = ");
fprintf(eliminasi_gauss,"\n\nA = ");
for (i = 0; i < p; i++)
{
printf("\n");
fprintf(eliminasi_gauss,"\n");

for (j = 0; j < q; j++)
{
printf("\t%.3f", A[i][j]);
fprintf(eliminasi_gauss,"\t%.3f", A[i][j]);
}
}
/* Cetak Matriks H hasil Eliminasi Gauss */
printf("\n\nH = \n");
fprintf(eliminasi_gauss,"\n\nH = \n");
for (n = 0; n < r; n++)
{
printf("\t%.3f\n", H[n]);
fprintf(eliminasi_gauss,"\t%.3f\n", H[n]);
}
/*********************************************************/
/* Matriks x (3 x 1) Hasil Penghitungan */
/*********************************************************/
/* Solusi x[i] */
for(k = p-1; k >= 0; k--)
{
temp = 0.0;
for(j = k+1; j < q; j++)
{
temp = temp + A[k][j] * x[j];
}
x[k] = (H[k] - temp)/A[k][k];
}
/* Cetak Matriks x hasil Perhitungan */
cout << "\n\nSolusi x\n";
cout << "--------";
printf("\n\nx = \n");
fprintf(eliminasi_gauss,"\n\nSolusi x\n");
for (n = r-1; n >= 0; n--)
{
printf("\tx[%d] = %.3f\n", n+1, x[n]);
fprintf(eliminasi_gauss,"\tx[%d] = %.3f\n", n+1, x[n]);
}
fclose(eliminasi_gauss);
cout << "\n\n\nCoba lagi dengan data yang berbeda (y/t) ? ";
cin >> lagi;
}while(lagi != 't');
return 0;
}

Tidak ada komentar:

Posting Komentar