Kamis, 20 Oktober 2011

Contoh Source Code Listing Program C++ Regula Falsi, Newton-Rhapson, dan Iterasi

Hai Agan agan,,mas bro,,mbak sist yang saya Hormati
and tman2 cwekq yang sangat saya sayangi,,terutama luna,and icha

Kali Ini CandrA7x akan membahas tentang Listing Program C++ untuk menyelesaikan persamaan tak linear
Persamaan yang di dalamnya ada e^x atau ln x,,gak mungkin kan kalau agan faktorkan and agan temukan jawabannya
Nah,,disitulah metode numerik hadir untuk "menebak-nebak" pendekatan jawaban akar dari x nya


Metode yang umum dipakai untuk penyelesaian persamaan tak linear
1. Metode Biseksi
2. Metode Regula Falsi
3. Metode Newton Rapshon
4. Metode Secant
5. Metode Iterasi

Tapi gak gue bahas semua gan
Ilmu gue masih blum nyampek

Langsung saja ke TKP gan

A. Contoh Program C++ Metode Regula Falsi

/* Nama Program : regula-falsi.cpp */
#include iostream.h
#include stdlib.h
#include conio.h
#include stdio.h
#include math.h
#define e 2.718281828
#define error_limit 0.00000000000001 //flexible sesuai kebutuhan
main()
{
FILE *regula_falsi, *regula_x;
double fx1, fx2, fx3,x1, x2, x3,x10, x20, f1f2,x, dx, y1, y2, dy;
int i, j, k;
char lagi, belum;
do
{
clrscr();
j = 0;
regula_falsi = fopen("regula_falsi.txt", "w+");
regula_x = fopen("cross_regula.txt", "w+");
do
{
clrscr();
cout << "Metode Regula-Falsi\n"; cout << "===================\n\n"; //Persamaan utama dan persamaan untuk mencari titik potongnya cout << "f(x) = 2*e^x - x - 3\n\n"; cout << "y1 = 2e^x dan y2 = x+3\n\n"; cout << "Mencari Titik Potong terdekat\n"; cout << "-----------------------------\n\n"; cout << "Berapa Nilai awal x = "; cin >> x;
cout << "\nBerapa Nilai Interval x = "; cin >> dx;
cout << "\nBerapa kali Iterasi = "; cin >> k;
printf("\n\t-------------------------------------------------\n");
printf("\tIterasi ke-\tx\ty1\ty2\tselisih\n");
printf("\t-------------------------------------------------\n\n");
fprintf(regula_x,"\n\t-------------------------------------------------\n");
fprintf(regula_x,"\tIterasi ke-\tx\ty1\ty2\tselisih\n");
fprintf(regula_x,"\t-------------------------------------------------\n\n");
for (i = 0; i < k; i++) { y1 = 2*pow(e,x); y2 = x + 3; dy = y1 - y2; printf("\t\t%d\t%.1f\t%.2f\t%.2f\t%.2f\n", i+1, x, y1, y2, dy); fprintf(regula_x, "\t\t%d\t%.1f\t%.2f\t%.2f\t%.2f\n", i+1, x, y1,y2,dy); x += dx; } printf("\n\t-------------------------------------------------\n\n"); fprintf(regula_x, "\t-------------------------------------------------\n\n"); fclose(regula_x); cout << "\nApakah Titik Potong telah ditemukan ? "; cin >> belum;
}while(belum != 'y');
cout << "\nMencari Titik Potong sebenarnya\n"; cout << "===============================\n\n"; cout << "Masukkan Nilai x1 = "; cin >> x1;
x10 = x1;
fx1 = 2*pow(e,x1) - x1 - 3;
do
{
cout << "\nMasukkan Nilai x2 = "; cin >> x2;
fx2 = 2*pow(e,x2) - x2 - 3;
x20 = x2;
f1f2 = fx1*fx2;
}while(f1f2 >= 0.0);
printf("\n----------------------------------------------------------------------------\n");
printf("Iterasi ke-\tx3\tfx3\t\tfx3 (16 digit) Error Aproksimasi\n");
printf("----------------------------------------------------------------------------\n\n");
fprintf(regula_falsi,"\n----------------------------------------------------------------------------\n");
fprintf(regula_falsi,"Iterasi ke-\tx3\tfx3\t\tfx3 (16 digit) ErrorAproksimasi\n");
fprintf(regula_falsi, "----------------------------------------------------------------------------\n\n");
do
{
j++;
x3 = (x1*fx2 - x2*fx1)/(fx2 - fx1);
fx3 = 2*pow(e,x3) - x3 - 3;
printf("\t%d\t%.3f\t%.6f\t%.16f\n", j, x3, fx3, fabs(fx3));
fprintf(regula_falsi,"\t%d\t%.3f\t%.6f\t%.16f\n", j, x3, fx3,
fabs(fx3));
if(fx1*fx3 < 0) { x2 = x3; fx2 = fx3; } else { x1 = x3; fx1 = fx3; } }while(fabs(fx3) > error_limit);
printf("\n----------------------------------------------------------------------------\n");
fprintf(regula_falsi,"\n----------------------------------------------------------------------------");
printf("\n\nDengan Nilai awal x1 = %.2f dan x2 = %.2f", x10, x20);
printf("\n\nDiperoleh Akar Persamaan x = %.3f", x3);
printf("\n\nDengan Error sebesar = %.16f", fabs(fx3));
fprintf(regula_falsi,"\n\nDengan Nilai awal x1 = %.2f dan x2 = %.2f", x10,x20);
fprintf(regula_falsi,"\n\nDiperoleh Akar Persamaan x = %.3f", x3);
fprintf(regula_falsi,"\n\nDengan Error sebesar = %.16f", fabs(fx3));
fclose(regula_falsi);
cout << "\n\nTekan Enter untuk melanjutkan ........."; getch(); clrscr(); cout << "\n\nCoba lagi dengan x dan y berbeda (y/t) ? "; cin >> lagi;
}while(lagi != 't');
return 0;
}

/* Nama Program : regula-falsi_1c.cpp */
#include iostream.h
#include stdlib.h
#include conio.h
#include stdio.h
#include math.h
#define e 2.718281828
#define error_limit 0.000000000000001 //flexible sesuai kebutuhan
main()
{
FILE *regula_falsi, *regula_x;
double fx1, fx2, fx3,x1, x2, x3,x10, x20, f1f2,x, dx, y1, y2, dy;
int i, j, k;
char lagi, belum;
do
{
clrscr();
j = 0;
regula_falsi = fopen("regula_falsi_3c.txt", "w+");
regula_x = fopen("cross_regula_3c.txt", "w+");
do
{
clrscr();
cout << "Metode Regula-Falsi\n"; cout << "Soal No. 3.c.\n"; cout << "===================\n\n"; //Persamaan utama dan persamaan untuk mencari titik potongnya cout << "f(x) = 3*x - cos(x) = 0\n\n"; cout << "y1 = 3*x dan y2 = cos(x)\n\n"; cout << "Mencari Titik Potong terdekat\n"; cout << "-----------------------------\n\n"; cout << "Berapa Nilai awal x = "; cin >> x;
cout << "\nBerapa Nilai Interval x = "; cin >> dx;
cout << "\nMaksimum Iterasi = "; cin >> k;
printf("\n\t-------------------------------------------------\n");
printf("\tIterasi ke-\tx\ty1\ty2\tselisih\n");
printf("\t-------------------------------------------------\n\n");
fprintf(regula_x, "\n\t-------------------------------------------------\n");
fprintf(regula_x, "\tIterasi ke-\tx\ty1\ty2\tselisih\n");
fprintf(regula_x, "\t-------------------------------------------------\n\n");
for (i = 0; i < k; i++) { y1 = 3*x; y2 = cos(x); dy = y1 - y2; printf("\t\t%d\t%.1f\t%.2f\t%.2f\t%.2f\n", i+1, x, y1, y2, dy); fprintf(regula_x, "\t\t%d\t%.1f\t%.2f\t%.2f\t%.2f\n", i+1, x, y1,y2, dy); x += dx; } printf("\n\t-------------------------------------------------\n\n"); fprintf(regula_x, "\t-------------------------------------------------\n\n"); fclose(regula_x); cout << "Apakah Titik Potong telah ditemukan ? "; cin >> belum;
}while(belum != 'y');
cout << "\n\nMencari Titik Potong sebenarnya\n"; cout << "===============================\n\n"; cout << "Masukkan Nilai x1 = "; cin >> x1;
x10 = x1;
fx1 = 3*x1 - cos(x1);
do
{
cout << "\nMasukkan Nilai x2 = "; cin >> x2;
fx2 = 3*x2 - cos(x2);
x20 = x2;
f1f2 = fx1*fx2;
}while(f1f2 >= 0.0);
printf("\n----------------------------------------------------------------------------\n");
printf("Iterasi ke-\tx3\tfx3\t\tfx3 (16 digit) Error Aproksimasi\n");
printf("----------------------------------------------------------------------------\n\n");
fprintf(regula_falsi, "\n----------------------------------------------------------------------------\n");
fprintf(regula_falsi, "Iterasi ke-\tx3\tfx3\t\tfx3 (16 digit) ErrorAproksimasi\n");
fprintf(regula_falsi, "----------------------------------------------------------------------------\n\n");
do
{
j++;
x3 = (x1*fx2 - x2*fx1)/(fx2 - fx1);
fx3 = 3*x3 - cos(x3);
printf("\t%d\t%.3f\t%.6f\t%.16f\n", j, x3, fx3, fx3);
fprintf(regula_falsi,"\t%d\t%.3f\t%.6f\t%.16f\n", j, x3, fx3, fx3);
if(fx1*fx3 < 0) { x2 = x3; fx2 = fx3; } { x1 = x3; fx1 = fx3; } }while(fabs(fx3) > error_limit);
printf("\n----------------------------------------------------------------------------\n");
fprintf(regula_falsi,"\n----------------------------------------------------------------------------");
printf("\n\nDengan Nilai awal x1 = %.2f dan x2 = %.2f", x10, x20);
printf("\n\nDiperoleh Akar Persamaan x = %.3f", x3);
printf("\n\nDengan Error sebesar = %.16f", fabs(fx3));
fprintf(regula_falsi,"\n\nDengan Nilai awal x1 = %.2f dan x2 = %.2f", x10,x20);
fprintf(regula_falsi,"\n\nDiperoleh Akar Persamaan x = %.3f", x3);
fprintf(regula_falsi,"\n\nDengan Error sebesar = %.16f", fabs(fx3));
fclose(regula_falsi);
cout << "\n\nTekan Enter untuk melanjutkan ........."; getch(); clrscr(); cout << "\n\nCoba lagi dengan x dan y berbeda (y/t) ? "; cin >> lagi;
clrscr();
}while(lagi != 't');
return 0;
}

B. Contoh Program C++ Metode Newton Rapshon

/* Nama Program : newton-rhapson.cpp */
#include iostream.h
#include stdlib.h
#include conio.h
#include stdio.h
#include math.h
#define error_limit 1E-13 /* flexible sesuai kebutuhan */
main()
{
FILE *newton;
int i, j;
double x, x0, xuji, dx, y,
dif1, dif2, dif,
syarat;
char lagi;
do
{
clrscr();
newton = fopen("newton.txt", "w+");
cout << "Metode Newton-Rhapson\n"; cout << "=====================\n\n"; /* Persamaan dan turunannya */ cout << "y = 4 + 5*x^2 - x^3\n\n"; cout << "y' = 10*x - 3*x^2\n\n"; cout << "y'' = 10 - 6x\n\n"; /* Mencari Nilai x -> 0 */
cout << "\n\nMencari Nilai x -> 0";
cout << "\n\nBerapa Nilai x ? "; cin >> xuji;
cout << "\nBerapa Nilai Interval x ? "; cin >> dx;
cout << "\nMaksimum Iterasi ? "; cin >> j;
printf("\n\t------------------------------------\n");
printf("\tIterasi ke-\tx\ty\n");
printf("\t------------------------------------\n\n");
fprintf(newton,"\n\t------------------------------------\n");
fprintf(newton,"\tIterasi ke-\tx\ty\n");
fprintf(newton,"\t------------------------------------\n\n");
for(i = 1; i <= j; i++) { y = 4 + 5*pow(xuji,2) - pow(xuji,3); printf("\t\t%.d\t%.3f\t%.3f\n", i, xuji, y); fprintf(newton,"\t\t%.d\t%.3f\t%.3f\n", i, xuji, y); xuji += dx; } printf("\t------------------------------------\n\n"); fprintf(newton,"\t------------------------------------\n\n"); do { /* uji nilai x */ cout << "\n\nUji Nilai x yang dipilih"; cout << "\n\nBerapa Nilai x -> 0 ? ";
cin >> x;
y = 4 + 5*pow(x,2) - pow(x,3);
dif1 = 10*x - 3*pow(x,2);
dif2 = 10 - 6*x;
/* Syarat agar x awal terpenuhi */
syarat = abs((y*dif2)/(dif1*dif1));
}while(syarat >= 1.00);
x0 = x;
printf("\n\t-----------------------------------------------------------\n");
printf("\tIterasi ke-\tx\ty\tdy\t\ty/dy\n");
printf("\t-----------------------------------------------------------\n\n");
fprintf(newton, "\n\t-----------------------------------------------------------\n");
fprintf(newton, "\tIterasi ke-\tx\ty\tdy\t\ty/dy\n");
fprintf(newton, "\t-----------------------------------------------------------\n\n");
/* Perhitungan Newton-Rhapson */
i = 1;
do
{
/* rumus x(i+1)=x-(y/y') */
y = 4 + 5*pow(x,2) - pow(x,3);
dif1 = (10*x) - 3*pow(x,2);
dif = y/dif1;
x = x - dif;
printf("\t\t%.d\t%.3f\t%.3f\t%.3f\t\t%.4f\n", i, x, y, dif2, dif);
fprintf(newton,"\t\t%.d\t%.3f\t%.3f\t%.3f\t\t%.4f\n", i, x, y, dif2,
dif);
i++;
}while(fabs(y) > error_limit);
printf("\t-----------------------------------------------------------\n\n");
fprintf(newton,"\t-----------------------------------------------------------\n\n");
printf("\nDengan tebakan awal x = %.3f", x0);
printf("\n\nDiperoleh Akar Persamaan x = %.8f", x);
printf("\n\nDengan Error sebesar = %.8f", y);
fprintf(newton,"\nDengan tebakan awal x = %.3f", x0);
fprintf(newton,"\n\nDiperoleh Akar Persamaan x = %.8f", x);
fprintf(newton,"\n\nDengan Error sebesar = %.8f", y);
fclose(newton);
cout << "\n\nCoba dengan x awal yang berbeda (y/t) ? "; cin >> lagi;
}while(lagi != 't');
return 0;
}


/* Nama Program : newton-rhapson_2.cpp */
#include iostream.h
#include stdlib.h
#include conio.h
#include stdio.h
#include math.h
main()
{
FILE *newton;
int i, j;
double x0, x, dx, y,
dif1, dif2, dif,
syarat;
char ya, lagi;
do
{
newton = fopen("newton_4a.txt", "w+");
do
{
clrscr();
cout << "Metode Newton-Rhapson\n"; cout << "Jawaban Soal No. 4.a.\n"; cout << "=====================\n\n"; //Persamaan dan turunannya cout << "y = 3*x - cos(x)\n\n"; cout << "y' = 3 + sin(x)\n\n"; cout << "y'' = cos(x)\n\n"; //Mencari Nilai x -> 0
cout << "Berapa Nilai x awal = "; cin >> x;
y = 3*x - cos(x);
dif1 = 3 + sin(x);
dif2 = cos(x);
//Syarat agar x awal terpenuhi
syarat = abs((y*dif2)/(dif1*dif1));
cout << "\nNilai " << x << " menghasilkan " << syarat; cout << "\n\nApakah x memenuhi syarat ? "; cin >> ya;
}while(ya != 'y');
x0 = x;
cout << "\nBerapa Nilai Interval x = "; cin >> dx;
cout << "\nMaksimum Iterasi = "; cin >> j;
printf("\n\t------------------------------------\n");
printf("\tIterasi ke-\tx\ty\n");
printf("\t------------------------------------\n\n");
fprintf(newton, "\n\t------------------------------------\n");
fprintf(newton, "\tIterasi ke-\tx\ty\n");
fprintf(newton, "\t------------------------------------\n\n");
for(i = 1; i <= j; i++) { y = 3*x - cos(x); printf("\t\t%.d\t%.3f\t%.3f\n", i, x, y); fprintf(newton,"\t\t%.d\t%.3f\t%.3f\n", i, x, y); x += dx; } printf("\t------------------------------------\n\n"); fprintf(newton,"\t------------------------------------\n\n"); //Perhitungan Newton-Rhapson cout << "Perhatikan Nilai x yang menghasilkan fungsi y paling mendekati 0\n"; cout << "Pilih Nilai x tersebut untuk perhitungan berikutnya\n\n"; cout << "\nBerapa Nilai x (y -> 0) = ";
cin >> x;
cout << "\nMaksimum Iterasi = "; cin >> j;
printf("\n\t-----------------------------------------------------------\n");
printf("\tIterasi ke-\tx\ty\tdy\t\ty/dy\n");
printf("\t-----------------------------------------------------------\n\n");
fprintf(newton, "\n\t----------------------------------------------------------\n");
fprintf(newton, "\tIterasi ke-\tx\ty\tdy\t\ty/dy\n");
fprintf(newton, "\t-----------------------------------------------------------\n\n");
for(i = 1; i <= j; i++) { //rumus x(i+1)=x-(y/y') y = 3*x - cos(x); dif1 = 3 + sin(x); dif = y/dif1; x = x - dif; printf("\t\t%.d\t%.3f\t%.3f\t%.3f\t\t%.4f\n", i, x, y, dif1, dif); fprintf(newton,"\t\t%.d\t%.3f\t%.3f\t%.3f\t\t%.4f\n", i, x, y, dif1, dif); } printf("\t-----------------------------------------------------------\n\n"); fprintf(newton,"\t-----------------------------------------------------------\n\n"); printf("\nDengan tebakan awal x = %.3f", x0); printf("\n\nDiperoleh Akar Persamaan x = %.3f", x); printf("\n\nDengan Error sebesar = %.8f", y); fprintf(newton,"\nDengan tebakan awal x = %.3f", x0); fprintf(newton,"\n\nDiperoleh Akar Persamaan x = %.3f", x); fprintf(newton,"\n\nDengan Error sebesar = %.8f", y); fclose(newton); cout << "\n\nCoba dengan x awal yang berbeda (y/t) ? "; cin >> lagi;
}while(lagi != 't');
return 0;

C. Contoh Program C++ Metode Iterasi

/* Nama Program : Iterasi Bentuk x = g(x).cpp */
#include iostream.h
#include stdlib.h
#include conio.h
#include stdio.h
#include math.h
#define error_limit 0.00000001
main()
{
FILE *x_gx;
int i;
double fx, g1x, x, xi, x0;
char lagi;
do
{
clrscr();
x_gx = fopen("x_gx.txt", "w+");
do
{
clrscr();
cout << "Metode Iterasi x = g(x)\n"; cout << "=======================\n\n"; //Persamaan, konversinya dan turunannya cout << "f(x) = x^3 - 9*x^2 + 18x - 6 = 0\n\n"; cout << "g(x) = -(x^3/18) + x^2/2 + 1/3\n\n"; cout << "g'(x) = -(x^2/6) + x\n\n"; cout << "Bila berulang berarti Nilai g'(x) > 1\n\n";
cout << "Masukkan Nilai x Asumsi = "; cin >> x;
g1x = (-pow(x,2)/6)+ x;
printf("\nx = %.3f\tg'(x) = %.6f\n\n", x, abs(g1x));
cout << "\nTekan Enter ........."; getch(); } while(abs(g1x) >= 1);
x0 = x;
i = 0;
printf("\n\t--------------------------------------------\n");
printf("\tIterasi ke-\tx = g(x)\tf(x)\n");
printf("\t--------------------------------------------\n\n");
fprintf(x_gx, "\n\t--------------------------------------------\n");
fprintf(x_gx, "\tIterasi ke-\tx = g(x)\tf(x)\n");
fprintf(x_gx, "\t--------------------------------------------\n\n");
do
{
/* rumus xi = g(x) = -(x^3)/18 + x^2/2 +(1/3) */
xi = -pow(x,3)/18 + pow(x,2)/2 + 0.3333333333;
fx = pow(xi,3) - 9*pow(xi,2) + 18*xi - 6;
printf("\t\t%d\t%.8f\t%.8f\n", i+1, xi, fx);
fprintf(x_gx,"\t\t%d\t%.8f\t%.8f\n", i+1, xi, fx);
x = xi;
i++;
}while(fabs(fx) > error_limit);
printf("\n\t--------------------------------------------\n\n");
fprintf(x_gx, "\n\t--------------------------------------------\n\n");
printf("\nDengan tebakan awal x = %.3f", x0);
printf(" diperoleh\n");
printf("\nAproksimasi Akar Persamaan adalah x = %.10f\n", x);
printf("\nError Hasil Aproksimasi adalah f(x) = %.14f\n", fabs(fx));
fprintf(x_gx,"\nDengan tebakan awal x = %.3f", x0);
fprintf(x_gx," diperoleh\n");
fprintf(x_gx,"\nAproksimasi Akar Persamaan adalah x = %.10f\n", x);
fprintf(x_gx,"\nError Hasil Aproksimasi adalah f(x) = %.10f\n", fabs(fx));
fclose(x_gx);
cout << "\n\nCoba lagi dengan x awal yang berbeda (y/t) ? "; cin >> lagi;
}while(lagi != 't');
return 0;
}



/* Nama Program : Iterasi_2.cpp */
#include iostream.h
#include stdlib.h
#include conio.h
#include stdio.h
#include math.h
#define e 2.718281828
main()
{
FILE *x_gx;
int i, j;
double fx, g1x, x, xi, x0;
char lagi;
do
{
clrscr();
x_gx = fopen("x_gx_5d.txt", "w+");
do
{
clrscr();
cout << "Metode Iterasi x = g(x)\n"; cout << "Jawaban Soal No. 5.d. \n"; cout << "=======================\n\n"; //Persamaan, konversinya dan turunannya cout << "f(x) = e^x - 3*x = 0\n\n"; cout << "g(x) = e^x/3\n\n"; cout << "g'(x) = e^x/3\n\n"; cout << "Bila berulang berarti Nilai g'(x) > 1\n\n";
cout << "Masukkan Nilai x Asumsi = "; cin >> x;
g1x = pow(e,x)/3;
printf("\nx = %.3f\tg'(x) = %.6f\n\n", x, fabs(g1x));
cout << "\nTekan Enter ........."; getch(); } while(abs(g1x) >= 1);
x0 = x;
cout << "\n\nBerapa Kali Iterasi (min. 20) = "; cin >> j;
clrscr();
printf("\n\t--------------------------------------------\n");
printf("\tIterasi ke-\tx = g(x)\tf(x)\n");
printf("\t--------------------------------------------\n\n");
fprintf(x_gx, "\n\t--------------------------------------------\n");
fprintf(x_gx, "\tIterasi ke-\tx = g(x)\tf(x)\n");
fprintf(x_gx, "\t--------------------------------------------\n\n");

for(i = 0; i < j; i++) { //rumus xi = g(x) = e^x/3 xi = pow(e,x)/3; fx = pow(e,x) - 3*x; printf("\t\t%d\t%.6f\t%.8f\n", i+1, xi, fx); fprintf(x_gx,"\t\t%d\t%.6f\t%.8f\n", i+1, xi, fx); x = xi; } printf("\n\t--------------------------------------------\n\n"); fprintf(x_gx, "\n\t--------------------------------------------\n\n"); printf("\nDengan tebakan awal x = %.3f", x0); printf(" diperoleh\n"); printf("\nAproksimasi Akar Persamaan adalah x = %.6f\n", x); printf("\nError Hasil Aproksimasi adalah f(x) = %.8f\n", fabs(fx)); fprintf(x_gx,"\nDengan tebakan awal x = %.3f", x0); fprintf(x_gx," diperoleh\n"); fprintf(x_gx,"\nAproksimasi Akar Persamaan adalah x = %.6f\n", x); fprintf(x_gx,"\nError Hasil Aproksimasi adalah f(x) = %.8f\n", fabs(fx)); fclose(x_gx); cout << "\n\nCoba lagi dengan x awal yang berbeda ? "; cin >> lagi;
}while(lagi != 'n');
return 0;
}


Uda gue coba dan Alhamdulillah ya,,bisa di running
Yang penting bukan masalah bisa atau tidaknya program tersebut di running
Tp adalah mengerti tidaknya agan atas campuran dari bahasa C dan C++
Dan terlebih lagi,,mengerti algoritma program tersebut

Wookeeey gan
Bantu Sundul gan