Jumat, 24 Juni 2011

Program C++ untuk menyelesaikan Sistem Persamaan Linier


Good friends ... more and more difficult course not. Now we learn about the system of linear equations and meimplementasikannya into C + + language. For that friends can get it by looking at the algorithm and the program now. Do not miss ... okeee ... lets try this ..! Hehehehe ..

Algoritma :
Procedure gauss(input a : matriks; b : vector; output x : vector)
Deklarasi
i, j, k               : integer
temp, s           : real

Deskripsi
for i <= 1 to a.baris – 1 do
for k <= i + 1 to a.baris do
temp <= a.elemen[k,i] / a.elemen[i,j]
for j <= i + 1 to a.baris do
a.elemen[k] ß a.elemen[k,j] – temp*a.elemen[i,j]
endfor
b.elemen[k] <= b.elemen[k] – temp*b.elemen[i]
a.elemen[k,i] <= 0.0
endfor
x.baris <= a.baris
for i<= a.baris downto 1 do
s <= b.elemen[i]
for j <= i + 1 to a.baris do
s <= s – a.elemen[i,j]*x.elemen[j]
x.elemen[i] <= s/a.elemen[i,i]
endfor
endfor

Berikut kode programnya :
#include <iostream.h>
#include <conio.h>
#include "matriks.h"
#include "vector.h"
#define maks 25
#define false 0
#define true 1

class SPL{
friend ostream& operator << (ostream&, SPL&);
friend istream& operator >> (istream&, SPL&);

public :
SPL();
void gauss();
void cetak();
private :
Matriks koef;
Vektor konstanta;
int banyak;
Vektor X;
};
SPL::SPL(){
  Banyak = 3;
}

ostream& operator << (ostream& out, SPL& A){
Vektor hasil;
A.gauss();
A.cetak();
return out;
}

istream& operator >> (istream& in, SPL& A){
cout<<"Jumlah persamaan : ";
in>>A.banyak;
A.koef.beri_nilaiBaris(A.banyak);
A.koef.beri_nilaiKolom(A.banyak);
in>>A.koef;
A.konstanta.beri_nilaiBanyak(A.banyak);
in>>A.konstanta;
return in;
}

void SPL::gauss(){
int i,j,k;
float temp,s;
error = false;
for (i=0; i<banyak – 1; i++)
for (k=i+1; k < banyak; k++){
if ((koef.A[i][j] == 0.0)){
error = true;
return;
}
temp = koef.A[k][i]/koef.A[i][i];
for (j=i+1; j<banyak; j++)
  koef.A[k][j]=koef.A[k][j]-temp*koef.A[i][j];
konstanta.elemen[k]=konstanta.elemen[k]-temp*konstanta.elemen[i];
koef.A[k][i]=0.0;
}
X.banyak = banyak;
for (i=banyak-1; i>=0; i++){
s = konstanta.elemen[i];
for (j=i+1; j<banyak; j++)
s -= koef.A[i][j]*konstanta.elemen[j];
if ((koef.A[i][i]==0.0)){
error = true;
return;
}
X.elemen[i]=s/koef.A[i][i];
}
}

void SPL::cetak(){
int I;
if ((error))
{ cout<<"Persamaan tidak dapat diselesaikan.\n";
}
else{ cout<<"\n";
cout<<"Penyelesaian SPL : \n";
cout<<X;
}
}

void main(){
SPL X;
Cout<<X;
getch();
}


explanation:
The above program is a program used to solve a system of linear equations which in this case using a matrix. To complete the system of linear equations that add up the second row by (-1) x row first, then add the third row by (-1) x row first and then add the third row with (-2) x the second row. Then substituted and obtained results.

1 komentar:

reyy mengatakan...

you forgot to put the matriks and vector headers

Posting Komentar

Template by:

Free Blog Templates