ariestamirra

Metode Biseksi

Apr 1st, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double N,a,b,e,xr;
  5. int i;
  6.  
  7. double f(double x){
  8.     return exp(-x) - x;
  9. }
  10.  
  11. void title(){
  12.     puts("Praktikum Metode Numerik");
  13.     puts("Metode Biseksi\n");
  14. }
  15.  
  16. void input(){
  17.     fflush(stdin);
  18.     printf("Masukkan batas bawah : ");
  19.     scanf("%lf",&a);
  20.     fflush(stdin);
  21.     printf("Masukkan batas atas : ");
  22.     scanf("%lf",&b);
  23.     fflush(stdin);
  24.     printf("Masukkan toleransi : ");
  25.     scanf("%lf",&e);
  26.  
  27.     fflush(stdin);
  28.     printf("Masukkan jumlah iterasi : ");
  29.     scanf("%lf",&N);
  30. }
  31.  
  32. //Metode Biseksi
  33. void metode(){
  34.  
  35.     if(f(a)*f(b)>0){
  36.         puts("Tidak ada akar");
  37.         return;
  38.     }
  39.     i = 0;
  40.     printf("%-4s\t%-8s\t%-8s\t%-8s\t%-8s\t%-8s\t%-8s\n","i","a","b","xr","f(xr)","f(a)","Keterangan");
  41.     do{
  42.         i++;
  43.         xr = (a + b)/2;
  44.         printf("%-4d\t%-8lf\t%-8lf\t%-8lf\t%-8lf\t%-8lf\t%-8s\n",i,a,b,xr,f(xr),f(a),((f(a)*f(xr))<0)?"Berbeda tanda":"");
  45.  
  46.         if(f(a)*f(xr)<0){
  47.             b=xr;
  48.         }else
  49.             a=xr;
  50.     }while(fabs(b-a)>e && i<N);
  51.  
  52.     printf("Nilai xr = %lg\ndan f(xr) = %lg\n\n",xr,f(xr));
  53.     printf("Nilai error nya : %lg\n",fabs(f(xr)));
  54. }
  55.  
  56. char ulangi(){
  57.     char opt;
  58.     fflush(stdin);
  59.     printf("Ulangi [y/t] ? :");
  60.     scanf("%c",&opt);
  61.     if(opt=='y'||opt=='Y')
  62.         opt=1;
  63.     else
  64.         opt=0;
  65.     return opt;
  66. }
  67.  
  68. int main()
  69. {
  70.     title();
  71.     do{
  72.         input();
  73.         metode();
  74.     }while(ulangi());
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment