1. Persamaan yang dicari : 9x^4 + 2x^3 + 3x - 1 = 0
nilai awal x1 : 5
nilai awal x2 : 6
nilai error : 0.00001
total iterasi : 15
2. Kode program (bahasa C) :
/*mencari akar persamaan dari persamaan 9x^4 + 2x^3 + 3x - 1 = 0*/
#include<stdio.h>
#include<math.h>
double power(double, int);
double value(double);
double calculate(double, double);
main()
{
double x1, x2, e;
int itr;
printf ("Input Nilai Awal x1 : ");
scanf ("%lf", &x1);
printf ("Input Nilai Awal x2 : ");
scanf ("%lf", &x2);
printf ("Input Nilai Error : ");
scanf ("%lf", &e);
printf ("Input Total Iterasi : ");
scanf ("%i", &itr);
int i=1;
double xt, error=1.0000;
printf("\n");
printf ("+------------+--------+--------+--------+-----------+\n");
printf ("|Iterasi ke- | xl | xb | xt | |error| |\n");
printf ("+------------+--------+--------+--------+-----------+\n");
for (i=1;(i<itr) && (error>e);i++)
{
xt = calculate(x1, x2);
printf ("| %3i |%8.4lf|%8.4lf|%8.4lf|%11.6lf|\n", i, x1, x2, xt, cabs(value(xt)));
error = cabs(value(xt));
x1=x2;
x2=xt;
}
printf ("+------------+--------+--------+--------+-----------+\n");
}
double power(double number, int power)
{
double result = 1.00;
if (power == 0.0)
{
return result;
}
int i;
for (i = 1; i <= power; i++)
{
result *=number;
}
return result;
}
double value(double x)
{
return ((4*(power(x,3))) - (15*(power(x,2))) + 17*x - 6);
}
double calculate(double xl, double xb)
{
return (xb - ((xb-xl)/(value(xb)-value(xl)))*value(xb));
}
nilai awal x1 : 5
nilai awal x2 : 6
nilai error : 0.00001
total iterasi : 15
2. Kode program (bahasa C) :
/*mencari akar persamaan dari persamaan 9x^4 + 2x^3 + 3x - 1 = 0*/
#include<stdio.h>
#include<math.h>
double power(double, int);
double value(double);
double calculate(double, double);
main()
{
double x1, x2, e;
int itr;
printf ("Input Nilai Awal x1 : ");
scanf ("%lf", &x1);
printf ("Input Nilai Awal x2 : ");
scanf ("%lf", &x2);
printf ("Input Nilai Error : ");
scanf ("%lf", &e);
printf ("Input Total Iterasi : ");
scanf ("%i", &itr);
int i=1;
double xt, error=1.0000;
printf("\n");
printf ("+------------+--------+--------+--------+-----------+\n");
printf ("|Iterasi ke- | xl | xb | xt | |error| |\n");
printf ("+------------+--------+--------+--------+-----------+\n");
for (i=1;(i<itr) && (error>e);i++)
{
xt = calculate(x1, x2);
printf ("| %3i |%8.4lf|%8.4lf|%8.4lf|%11.6lf|\n", i, x1, x2, xt, cabs(value(xt)));
error = cabs(value(xt));
x1=x2;
x2=xt;
}
printf ("+------------+--------+--------+--------+-----------+\n");
}
double power(double number, int power)
{
double result = 1.00;
if (power == 0.0)
{
return result;
}
int i;
for (i = 1; i <= power; i++)
{
result *=number;
}
return result;
}
double value(double x)
{
return ((4*(power(x,3))) - (15*(power(x,2))) + 17*x - 6);
}
double calculate(double xl, double xb)
{
return (xb - ((xb-xl)/(value(xb)-value(xl)))*value(xb));
}
3. Hasil keluaran :
4. Hasil akhir :
x = 2
berhenti pada iterasi ke-11, di mana nilai error sudah 0
Nama : Adhia Saputra
NIM : D1042131046
Teknik Informatika UNTAN 2013 (Reguler B)
No comments:
Post a Comment