Mencari akar
menggunakan metode biseksi (di bagi dua)
Listing Program :
#include
<stdio.h>
#include
<conio.h>
#include
<math.h>
float
f(float x)
{
float d;
d=(x*x*x)+(2*x)+1;
return d;
}
main()
{
float
x1,x2,xr,error,tol;
int i=1;
printf("\n\t\t\t\t
METODE BISECTION\n");
printf("\t\t\t\t
METODE BAGI DUA \n");
printf("\t\t\t\t
****************\n\n");
printf("\t\t\t\t
x^3+2x+1=0\n\n");
printf("\tMasukkan
batas atas persamaan(x1): ");scanf("%f",&x1);
printf("\tMasukkan
batas bawah persamaan (x2): ");scanf("%f",&x2);
printf("\tMasukkan
nilai toleransi: ");scanf("%f",&tol);
printf("\tTidak
ada akar di antara kedua batas persamaan\n");
{
printf("\n
\ti \tx1 \tx2 \txr \tf(x1) \tf(x2) \tf(xr) \terror \n");
do
{
xr=(x1+x2)/2;
error=fabs(x2-x1);
printf("\t%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n",i,x1,x2,xr,f(x1),f(x2),f(xr),error);
if(f(x1)*f(x2)<tol)
x2=xr;
else
x1=xr;
i++;
}
while(error>tol);
printf("\n \tnilai xr terakhir :
%.3f\n",xr);
printf("\tBanyaknya iterasi : %d
\n",i-1);
printf("\n\t\t\t\t\t\t\tTERIMA
KASIH");
printf("\n\t\t\t\t\t\t\t*************");
printf("\n\t\t\t\t\t\t\tKurniawan
Priyananto");
}
getch();
return 0;
}
Tampilan Program :
Tidak ada komentar:
Posting Komentar