Advertisement
lpuarmy

BSx | Numerik

Mar 12th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4.  
  5. double f(double x)
  6. {
  7.     return (x+exp(x));
  8. }
  9.  
  10. main()
  11. {
  12.     double a, b, e, c, ya, yb, yc,tempc;
  13.     int i=1, n,tempi;
  14.    
  15.     puts("\t\t\tPENYELESAIAN PERSAMAAN NON LINIER");
  16.     puts("\t\t\t\t   menggunakan");
  17.     puts("\t\t\t\t METODE  BISEKSI");
  18.     puts("");
  19.     puts("persamaan fungsi = x+exp(x) ");
  20.     puts("");
  21.  
  22.     printf("Masukkan batas bawah (a)  = ");
  23.     scanf("%lf", &a);
  24.     printf("Masukkan batas atas  (b)  = ");
  25.     scanf("%lf", &b);
  26.     printf("Masukkan toleransi error  = ");
  27.     scanf("%lf", &e);
  28.     printf("Masukkan iterasi maksimum = ");
  29.     scanf("%d", &n);
  30.     puts("");
  31.    
  32.     ya = f(a);
  33.     yb = f(b);
  34.  
  35.     printf("i       a         b         c         ya          yb          yc\n");
  36.     puts("");
  37.  
  38.     while(i <= n)
  39.     {
  40.         if(ya*yb>0)
  41.             break;
  42.         else
  43.         {
  44.             c = (a+b)/2;
  45.             yc = f(c);
  46.  
  47.             printf("%-8d%-10.4g%-10.4g%-10.4g%-12.4g%-12.4g%-10.4g\n", i, a, b, c, ya, yb,yc);
  48.  
  49.             if(yc*ya<0)
  50.             {
  51.                 b = c;
  52.                 yb = yc;
  53.             }
  54.             else
  55.             {
  56.                 a = c;
  57.                 ya = yc;
  58.             }
  59.             if(i == 1)
  60.                 tempc = yc;
  61.             if(fabs(yc) <= fabs(tempc)) {
  62.                 tempi = i;
  63.                 tempc = yc;
  64.             }
  65.  
  66.             if(fabs(yc)<e)
  67.                 break;
  68.             else
  69.                 i++;
  70.         }
  71.     }
  72.     puts("");
  73.  
  74.     printf("Akar persamaan di c = %lf\n", c);
  75.     printf("Dengan nilai yc     = %lf\n", yc);
  76.     printf("Pada iterasi ke     = %d\n", i-1);
  77.     puts("");
  78.     printf("Yang mendekati 0 adalah iterasi ke = %d\nyc = %lf\n", tempi,tempc);
  79.  
  80.     return 0;
  81.     getch();
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement