# Contoh Soal Newton /* Newton-rhapson Bab Ii Hal. 16-17

/* 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");

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;

}

Bank Soal Metode Komputasi 2006

Copyright @ Mayor Lek Arwin D.W. Sumari, S.T.

25

/* Penyelesaian Soal Newton-Rhapson Bab II Hal. 20 No. 4.a. Diktat Metode Komputasi

2004 */

/* Nama Program : newton-rhapson_4a.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");

Bank Soal Metode Komputasi 2006

Copyright @ Mayor Lek Arwin D.W. Sumari, S.T.

26

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;

}

Bank Soal Metode Komputasi 2006

Copyright @ Mayor Lek Arwin D.W. Sumari, S.T.

27

/* Contoh Soal Metode Iterasi Bentuk x = g(x) Bab II Hal. 18-19 Diktat Metode

Komputasi 2004 */

/* Nama Program : x_gx.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);

Bank Soal Metode Komputasi 2006

Copyright @ Mayor Lek Arwin D.W. Sumari, S.T.

28

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;

}

Bank Soal Metode Komputasi 2006

Copyright @ Mayor Lek Arwin D.W. Sumari, S.T.

29

/* Penyelesaian Soal Metode Iterasi Bentuk x = g(x)Bab II Hal. 20 No. 5.d. Diktat

Metode Komputasi 2004 */

/* Nama Program : x_gx_5d.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");

Bank Soal Metode Komputasi 2006

Copyright @ Mayor Lek Arwin D.W. Sumari, S.T.

30

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;

### }KONVERSI NILAI ANGKA KE HURUF

Di kesempatan ini saya akan sedikit membahas tentang konversi nilai angka ke huruf.

Si bawah ini adalah source code nya dalam C++ :

#include

#include

using namespace std;

int main(int argc, char *argv[])

{

int nilai_angka;

char huruf;

cout << "===============================" << endl;

cout << " KONVERSI NILAI ANGKA KE HURUF " << endl;

cout << "===============================" << endl;

cout << endl;

cout << " Masukkan Nilai : " ;

cin >> nilai_angka ;

if((nilai_angka > 0) && (nilai_angka <= 20))\

huruf = ‘E';

else if((nilai_angka > 20) && (nilai_angka <= 40))

huruf = ‘D';

else if((nilai_angka > 40) && (nilai_angka <= 60))

huruf = ‘C';

else if((nilai_angka > 60) && (nilai_angka <= 80))

huruf = ‘B';

else if((nilai_angka > 80) && (nilai_angka <= 100))

huruf = ‘A';

else huruf = ‘-_-‘;

cout << " Nilai Hurufnya Adalah = " << huruf << endl;

cout << endl;

system("PAUSE");

return EXIT_SUCCESS; }