You are on page 1of 3

#include <iostream>

#include <math.h>
using namespace std;
/*Kode Ditulis Oleh Bagus Suwandi
blog = http://suwandibagus.wordpress.com
*/
const float e = 2.7182818;
float fungsi(float);
int main(){
cout.setf(ios::fixed, ios::floatfield);
float batBawah, batAtas, pembH, xBaHasil, xAtHasil, pembHHasil;
int pemb, ySemNeg, yKepSementara;
cout << ">>>>>>>>> ********* PROGRAM PENYELESAIAN PERSAMAAN LINIER *********
>>>>>>>>>" << endl;
cout << ">> MENGGUNAKAN METODE TABEL
>>" << endl;
cout << "___________________________________________________________________
__________" << endl;
cout << "- Fungsi yang akan dihitung adalah x-e^(x)=0" << endl;
cout << "===================================================================
==========" << endl;
cout << "" << endl;
cout << "Masukkan batas bawah (X bawah) = ";
cin >> batBawah;
cout << "Masukkan batas atas (X atas) = ";
cin >> batAtas;
cout << "Masukkan jumlah pembagian (N) = ";
cin >> pemb;
float x[pemb], y[pemb];
pembH = (batAtas-batBawah)/pemb;
cout << "" << endl;
cout << "Diperoleh : " << endl;
cout << "" << endl;
cout << "---------------------------" << endl;
cout << "Nilai X \t" << "Nilai F(x)" << endl;
cout << "---------------------------" << endl;
for(int i=0 ; i<=pemb ; i++){
x[i] = batBawah + (i*pembH);
y[i] = fungsi(x[i]);
cout.precision(2);
cout << x[i] << "\t\t";
cout.precision(5);
cout << y[i] << endl;
}
cout << "---------------------------" << endl;
cout << "" << endl;
cout << "Keputusan Penyelesaian" <<endl;
cout << "" << endl;
cout << "---------------------------" << endl;
cout << "Nilai X \t" << "Nilai F(x)" << endl;
cout << "---------------------------" << endl;
for(int j=0 ; j<pemb ; j++){
if(y[j] == 0){
cout << x[j] << "\t\t";
cout << y[j] << endl;
} else if(y[j] < 0){
ySemNeg = j;
}
}
xBaHasil = x[ySemNeg];
xAtHasil = x[ySemNeg+1];
cout.precision(2);
cout << x[ySemNeg] << "\t\t";
cout.precision(5);
cout << y[ySemNeg] << endl;
cout << "" << endl;
cout << "Keputusan Penyelesaian Lebih Spesifik" <<endl;
cout << "" << endl;
cout << "---------------------------" << endl;
cout << "Nilai X \t" << "Nilai F(x)" << endl;
cout << "---------------------------" << endl;
float xHasil[pemb], yHasil[pemb];
pembHHasil = (xAtHasil - xBaHasil)/pemb;
for(int k=0 ; k<=pemb ; k++){
xHasil[k] = xBaHasil + (k*pembHHasil);
yHasil[k] = fungsi(xHasil[k]);
}
float xKep, yKep;
for(int l=0 ; l<pemb ; l++){
if(yHasil[l] == 0){
yKep = yHasil[l];
xKep = xHasil[l];
} else if(yHasil[l] < 0){
yKepSementara = l;
float a,b;
a = yHasil[yKepSementara];
b = yHasil[yKepSementara + 1];
yKep = (((a*(-1)) < b) ? a : b);
}
if(yHasil[l] == yKep){
xKep = xHasil[l];
}
}
cout.precision(2);
cout << xKep << "\t\t";
cout.precision(5);
cout << yKep << endl;
cout << "---------------------------" << endl;
return 0;
}
float fungsi(float x){
float exp;
exp = pow(e,x);
return x+exp;
}

You might also like