Assalamualaikum wr... wb...
Selamat pagi sahabat blogger,,berjmupa lagi dengan saya,,kali ini saya akan berbagi bagaimana cara membuat program metode newton rapshon untuk metode numerick dengan C++..
langsung saja ke intinya:
nah silahkan ikuti subkode dibawah ini:
#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;
}
Semoga bermanfaat selamat mencoba
No comments:
Post a Comment