Advertisement
pendekar_langit

tabulasi

Nov 26th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package tabulasi;
  8. import java.util.Scanner;
  9. import java.math.*;
  10. /**
  11.  *
  12.  * @author surya
  13.  */
  14. public class Tabulasi {
  15. private static float x1,x2,x,xa,xb,h,y,y1,y2,ya,yb;
  16. private static int i,j,k;
  17. private static String tampung,x1k,x2k;
  18.     /**
  19.      * @param args the command line arguments
  20.      */
  21.  
  22.     public static void main(String[] args) {
  23.         // TODO code application logic here
  24.         Scanner datain = new Scanner (System.in);
  25.         do{
  26.         System.out.print("Masukkan batas atas = ");
  27.         x1 = datain.nextInt();
  28.         y1 = x1*x1*x1-7*x1+1;
  29.         System.out.print("Masukkan batas bawah = ");
  30.         x2 = datain.nextInt();
  31.         y2 = x2*x2*x2-7*x2+1;
  32.         }while ((y1*y2)>=0);
  33.         k=0;
  34.         do {
  35.             k= k+1;
  36.             if(x1>x2){
  37.                 xa=x1;
  38.                 xb=x2;
  39.             }else{
  40.                 xa=x2;
  41.                 xb=x1;
  42.             }
  43.             h=(xa-xb)/10;
  44.             i=0;
  45.             do{
  46.                 i=i+1;
  47.                 x=xb+h*i;
  48.                 ya=x*x*x+7*x-1;
  49.                 yb=(x-h)*(x-h)*(x-h)-7*(x-h)+1;
  50.             }while ((ya*yb)>0);
  51.             x1=x;
  52.             x2=x-h;
  53.             tampung="Tabulasi ke-"+k+"\n";
  54.             tampung+="----------------------------------------"+"\n";
  55.             tampung+="n         x          f(x)       error   "+"\n";
  56.             tampung+="----------------------------------------"+"\n";
  57.             while (j<=9){
  58.                x = xb + h * (j - 1);
  59.                y = x * x * x - 7 * x + 1;
  60.                tampung+=""+j+" :: "+x+"   ::  "+y+"  ::  "+Math.abs(y)+"  ::  "+"\n";
  61.                j++;
  62.             }
  63.             j=10;
  64.             while(j<=11){
  65.                x = xb + h * (j - 1);
  66.                y = x * x * x - 7 * x + 1;
  67.                tampung+=""+j+" :: "+x+"  ::  "+y+"  ::  "+Math.abs(y)+"  ::  "+"\n";
  68.                j++;
  69.             }
  70.             tampung+="-------------------------------------------";
  71.         } while (Math.abs((y))<10e-8);
  72.         tampung+="Akar pendekatan = "+x+"\n";
  73.         tampung+="Error = "+Math.abs(y)+"\n";
  74.         System.out.print(tampung);
  75.     }
  76.    
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement