# /* Contoh Soal Newton-Rhapson Bab II Hal.

16-17 Diktat Metode Komputasi 2004 */
/* 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;

Bank Soal Metode Komputasi – 2006
Copyright @ Mayor Lek Arwin D.W. Sumari, S.T.
24
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");

printf("\nDengan tebakan awal x = %. do { clrscr(). x."\t----------------------------------------------------------\n\n").h> #include <conio. x0).4f\n".h> main() { FILE *newton.3f\t\t%. cout << "Jawaban Soal No. cout << "Metode Newton-Rhapson\n".3f". 4.3f\t%. cout << "y' = 3 + sin(x)\n\n".dif. dif2.d\t%.h> #include <stdio. fprintf(newton. dx. syarat. i."\n\nDiperoleh Akar Persamaan x = %. x0).cpp */ #include <iostream.3f\t\t%. fclose(newton).cos(x)\n\n". x.h> #include <math.3f\t%.3*pow(x. cout << "=====================\n\n". i. y). dif. /* Perhitungan Newton-Rhapson */ i = 1.3f\t%. printf("\t\t%. "w+").h> #include <stdlib. x = x . dif2. fprintf(newton. dif1 = 3 + sin(x). do { /* rumus x(i+1)=x-(y/y') */ y = 4 + 5*pow(x. dif1 = (10*x) . x).8f".cos(x)."\t\t%. fprintf(newton. y = 3*x . cin >> lagi. dif2. Diktat Metode Komputasi 2004 */ /* Nama Program : newton-rhapson_4a. . return 0. }while(lagi != 't'). dif1.pow(x. dif = y/dif1.8f".3). "\t----------------------------------------------------------\n\n").W. cout << "y'' = cos(x)\n\n". y. y).a. 25 /* Penyelesaian Soal Newton-Rhapson Bab II Hal.2). fprintf(newton. printf("\t-----------------------------------------------------------\n\n").\n". Sumari. dif2 = cos(x). char ya.3f\t%. 4."\nDengan tebakan awal x = %. printf("\n\nDiperoleh Akar Persamaan x = %. fprintf(newton. "\tIterasi ke-\tx\ty\tdy\t\ty/dy\n").fprintf(newton. //Persamaan dan turunannya cout << "y = 3*x .3f".a. } Bank Soal Metode Komputasi – 2006 Copyright @ Mayor Lek Arwin D.d\t%. dif).4f\n". dif). //Mencari Nilai x -> 0 cout << "Berapa Nilai x awal = ". fprintf(newton.2) . do { newton = fopen("newton_4a. i++.8f". }while(fabs(y) > error_limit). cout << "\n\nCoba dengan x awal yang berbeda (y/t) ? ". printf("\n\nDengan Error sebesar = %. j. y. 20 No. x. x). lagi. y.8f".txt". int i. cin >> x.T. double x0."\n\nDengan Error sebesar = %. S.

"\t------------------------------------\n\n"). y. x0). "\t------------------------------------\n\n"). y).cos(x). x). printf("\n\nDiperoleh Akar Persamaan x = %.3f". Bank Soal Metode Komputasi – 2006 Copyright @ Mayor Lek Arwin D.3f\n".3f\t%. y. printf("\t\t%. fprintf(newton. printf("\tIterasi ke-\tx\ty\tdy\t\ty/dy\n"). cin >> lagi. cin >> x."\t\t%. x.3f". cout << "\nMaksimum Iterasi = ". i++) { //rumus x(i+1)=x-(y/y') y = 3*x .W. S. fprintf(newton. x0). dif1. x."\t----------------------------------------------------------\n\n"). }while(lagi != 't'). printf("\t-----------------------------------------------------------\n\n"). y).8f". i++) { y = 3*x . cin >> dx. x.cos(x). printf("\nDengan tebakan awal x = %. i.3f\t%. return 0.4f\n".d\t%. } printf("\t-----------------------------------------------------------\n\n"). cin >> j. printf("\n\t-----------------------------------------------------------\n"). cout << "\nBerapa Nilai Interval x = ".//Syarat agar x awal terpenuhi syarat = abs((y*dif2)/(dif1*dif1)).d\t%. fprintf(newton. cout << "\n\nCoba dengan x awal yang berbeda (y/t) ? ". i <= j. y). dif1 = 3 + sin(x).3f\t%.4f\n". dif).dif. cout << "\nMaksimum Iterasi = ". i. dif). "\t----------------------------------------------------------\n\n"). cout << "\nNilai " << x << " menghasilkan " << syarat.3f\t%. x). fprintf(newton. fprintf(newton. cout << "\nBerapa Nilai x (y -> 0) = ". "\tIterasi ke-\tx\ty\tdy\t\ty/dy\n").8f".3f\t%. fprintf(newton. x0 = x. printf("\n\t------------------------------------\n"). fprintf(newton.T. fprintf(newton. printf("\t------------------------------------\n\n"). } printf("\t------------------------------------\n\n")."\t\t%. y). printf("\t\t%. printf("\n\nDengan Error sebesar = %. x += dx. fprintf(newton.3f".3f\t%. cin >> j. fprintf(newton. for(i = 1. fprintf(newton.3f\t\t%.3f\n". printf("\tIterasi ke-\tx\ty\n"). dif1. "\tIterasi ke-\tx\ty\n"). }while(ya != 'y'). x = x . cout << "Pilih Nilai x tersebut untuk perhitungan berikutnya\n\n". ."\nDengan tebakan awal x = %."\n\nDiperoleh Akar Persamaan x = %. i. dif = y/dif1. Sumari.3f\t\t%. "\n\t------------------------------------\n").d\t%. "\n\t---------------------------------------------------------\n"). //Perhitungan Newton-Rhapson cout << "Perhatikan Nilai x yang menghasilkan fungsi y paling mendekati 0\n". fprintf(newton. i <= j. i. cout << "\n\nApakah x memenuhi syarat ? ". fclose(newton). x. 26 for(i = 1. cin >> ya. fprintf(newton.3f"."\n\nDengan Error sebesar = %.d\t%.

fprintf(x_gx.9*pow(xi. S. . xi.".3f". printf("\t\t%d\t%. xi. Bank Soal Metode Komputasi – 2006 Copyright @ Mayor Lek Arwin D. printf("\nx = %. getch().." diperoleh\n"). x0..W.. printf("\nAproksimasi Akar Persamaan adalah x = %. 27 /* Contoh Soal Metode Iterasi Bentuk x = g(x) Bab II Hal.6 = 0\n\n".. char lagi.10f\n".. cout << "Masukkan Nilai x Asumsi = ". fprintf(x_gx. Sumari. }while(fabs(fx) > error_limit). "\t--------------------------------------------\n\n"). //Persamaan. g1x = (-pow(x.. Sumari. fabs(fx)). g1x. do { clrscr().h> #include <math. fprintf(x_gx. i+1.h> #include <conio. cout << "\nTekan Enter . fx = pow(xi.cpp */ #include <iostream. cout << "=======================\n\n".00000001 main() { FILE *x_gx. xi. "w+"). x0). printf("\nError Hasil Aproksimasi adalah f(x) = %. 28 x = xi.2)/6)+ x.T.8f\t%. fx). } while(abs(g1x) >= 1)..2)/2 + 0. S. "\n\t--------------------------------------------\n").3) .6. printf("\t--------------------------------------------\n\n"). cout << "g(x) = -(x^3/18) + x^2/2 + 1/3\n\n". fprintf(x_gx. do { clrscr().h> #define error_limit 0.3f". "\tIterasi ke-\tx = g(x)\tf(x)\n"). konversinya dan turunannya cout << "f(x) = x^3 . cin >> x. 18-19 Diktat Metode Komputasi 2004 */ /* Nama Program : x_gx. x0). i = 0.14f\n". x).9*x^2 + 18x . cout << "g'(x) = -(x^2/6) + x\n\n". double fx. do { /* rumus xi = g(x) = -(x^3)/18 + x^2/2 +(1/3) */ xi = -pow(x.W.6f\n\n".h> #include <stdlib.} Bank Soal Metode Komputasi – 2006 Copyright @ Mayor Lek Arwin D. cout << "Bila berulang berarti Nilai g'(x) > 1\n\n".2) + 18*xi . fx).T.txt". printf("\nDengan tebakan awal x = %. int i. printf("\n\t--------------------------------------------\n"). x_gx = fopen("x_gx.3f\tg'(x) = %.3)/18 + pow(x. cout << "Metode Iterasi x = g(x)\n". fprintf(x_gx. "\n\t--------------------------------------------\n\n"). x. x0 = x."\t\t%d\t%. fprintf(x_gx.8f\n".. i+1. fprintf(x_gx. printf("\tIterasi ke-\tx = g(x)\tf(x)\n"). abs(g1x)). i++."\nDengan tebakan awal x = %.8f\t%.3333333333. printf("\n\t--------------------------------------------\n\n"). printf(" diperoleh\n").8f\n".h> #include <stdio. x.

fprintf(x_gx."\nAproksimasi Akar Persamaan adalah x = %. Bank Soal Metode Komputasi – 2006 Copyright @ Mayor Lek Arwin D. }while(lagi != 't'). cout << "Masukkan Nilai x Asumsi = "."\nError Hasil Aproksimasi adalah f(x) = %. xi.h> #include <conio.. x). Diktat Metode Komputasi 2004 */ /* Nama Program : x_gx_5d. x0 = x. fprintf(x_gx. cout << "Bila berulang berarti Nilai g'(x) > 1\n\n". cin >> j.x) . fx = pow(e. cout << "Jawaban Soal No.. "w+").h> #include <stdio.3*x. fprintf(x_gx. fprintf(x_gx. cin >> lagi.W. cout << "\n\nBerapa Kali Iterasi (min. i < j. xi. S. x = xi. xi. "\n\t--------------------------------------------\n"). printf("\t\t%d\t%.. cout << "g(x) = e^x/3\n\n".6f\t%. Sumari.h> #include <stdlib. getch()...d."\t\t%d\t%. printf("\t--------------------------------------------\n\n"). g1x = pow(e.h> #include <math. 20) = ". cout << "g'(x) = e^x/3\n\n". clrscr().3*x = 0\n\n".x)/3.10f\n". } while(abs(g1x) >= 1). . fx). do { clrscr(). "\tIterasi ke-\tx = g(x)\tf(x)\n"). konversinya dan turunannya cout << "f(x) = e^x . 20 No. //Persamaan.3f\tg'(x) = %.cpp */ #include <iostream. x0. \n".". char lagi. Sumari. int i. S. return 0. fabs(g1x)).x)/3. printf("\nx = %..h> #define e 2. x. fx).8f\n".fprintf(x_gx.d. 30 for(i = 0. cout << "Metode Iterasi x = g(x)\n". cout << "=======================\n\n". fabs(fx)).8f\n". cin >> x. "\t--------------------------------------------\n\n"). do { clrscr(). } Bank Soal Metode Komputasi – 2006 Copyright @ Mayor Lek Arwin D. j. printf("\tIterasi ke-\tx = g(x)\tf(x)\n"). x_gx = fopen("x_gx_5d. double fx.6f\n\n". 5. 5. 29 /* Penyelesaian Soal Metode Iterasi Bentuk x = g(x)Bab II Hal.6f\t%. i+1.718281828 main() { FILE *x_gx. x. cout << "\nTekan Enter . fclose(x_gx).txt".T. cout << "\n\nCoba lagi dengan x awal yang berbeda (y/t) ? ".. i++) { //rumus xi = g(x) = e^x/3 xi = pow(e.10f\n". g1x.. fprintf(x_gx.W.T. i+1. printf("\n\t--------------------------------------------\n").

cout << " Masukkan Nilai : " .} printf("\n\t--------------------------------------------\n\n")."\nAproksimasi Akar Persamaan adalah x = %. printf("\nAproksimasi Akar Persamaan adalah x = %. cout << "===============================" << endl. fprintf(x_gx. printf(" diperoleh\n"). char huruf.6f\n".8f\n". return 0. int main(int argc. fabs(fx)).3f". }while(lagi != 'n'). fprintf(x_gx. cout << endl."\nDengan tebakan awal x = %. x). if((nilai_angka > 0) && (nilai_angka <= 20))\ huruf = 'E'. } KONVERSI NILAI ANGKA KE HURUF Maret 2011 Di kesempatan ini saya akan sedikit membahas tentang konversi nilai angka ke huruf. cout << "===============================" << endl. Si bawah ini adalah source code nya dalam C++ : #include #include using namespace std. cin >> nilai_angka . fabs(fx)). "\n\t--------------------------------------------\n\n"). .3f". x0). printf("\nDengan tebakan awal x = %. x). else if((nilai_angka > 20) && (nilai_angka <= 40)) huruf = 'D'. fclose(x_gx). cin >> lagi. fprintf(x_gx. char *argv[]) { int nilai_angka.6f\n". cout << "\n\nCoba lagi dengan x awal yang berbeda ? ". x0).8f\n". cout << " KONVERSI NILAI ANGKA KE HURUF " << endl. fprintf(x_gx. fprintf(x_gx."\nError Hasil Aproksimasi adalah f(x) = %. printf("\nError Hasil Aproksimasi adalah f(x) = %." diperoleh\n").

system("PAUSE"). else huruf = '-_-'. else if((nilai_angka > 80) && (nilai_angka <= 100)) huruf = 'A'. cout << endl. cout << " Nilai Hurufnya Adalah = " << huruf << endl.else if((nilai_angka > 40) && (nilai_angka <= 60)) huruf = 'C'. else if((nilai_angka > 60) && (nilai_angka <= 80)) huruf = 'B'. return EXIT_SUCCESS. } .