Pages

Minggu, 27 November 2011

METODE BAGI DUA [TUGAS METNUM] C++

  • C++
  • #include 
    #include 
    #include 
    
    float a,c,b,F_a,F_c,F_b,E;
    int max;
    
    float f(float x)
    {
       return (x*x*x*x*x)-x-1;
    }
    
    void main()
    {
       int it;
       char selang;
       float epsilon;
    
       printf("Batas bawah      = "); scanf("%f",&a);
       printf("Batas atas       = "); scanf("%f",&b);
       printf("Toleransi Error  = "); scanf("%f",&E);
       printf("Iterasi maksimum = "); scanf("%d",&max);
    
       it = 0;
       F_a = f(a);
       F_b = f(b);
       if(F_a * F_b > 0) printf("Nilai F(a) x F(b) > 0\n");
       else
       {
        printf("--------------------------------------------------------------------------\n");
          printf(" r     a       b        c       f(a)      f(b)     f(c)  selang  lebarnya \n");
          printf("--------------------------------------------------------------------------\n");
          do
          {
          it = it + 1;
             c = (a + b) / 2;
             F_c = f(c);
             if(F_a * F_c <= 0) { selang ='A'; }
             else {selang ='B'; }
    
             printf("%2d %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f   C%c    %8.5f \n",
                     it,a,b,c,F_a,F_b,F_c,selang,fabs(a-c));
             epsilon = fabs(a-c);
             if(F_a * F_c <= 0) { b = c; F_b = F_c; }
             else { a = c; F_a = F_c; }
          } while(it <= max && epsilon > E);
    
          if(it <= max)
          {
             printf("\n Hampiran (pendekatan) nilai x = %g\n",c);
             printf(" pada iterasi ke-%d",it);
          }
          else printf("Toleransi tidak terpenuhi, Jumlah Iterasi kurang\n");
       }
       getch();
    }
    
  • HASIL
Print Friendly and PDF

Artikel Terkait:

1 komentar:

the animal mengatakan...

tank mas bro

Posting Komentar

Related Posts Plugin for WordPress, Blogger...